Computer Graphics (Fall 2008) COMS 4160, Lecture 9: OpenGL 1

Slides:



Advertisements
Similar presentations
Computer Graphics - Graphics Programming -
Advertisements

©Zachary Wartell, UNCC9/28/ :30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights.
Chapter 2: Graphics Programming
Computer Graphics CSCE 441
Pemrograman OpenGL Dasar
© 2004, Tom Duff and George Ledin Jr1 Lectures OpenGL Introduction By Tom Duff Pixar Animation Studios Emeryville, California and George Ledin Jr Sonoma.
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
What is OpenGL? Low level 2D and 3D Graphics Library Competitor to Direct3D (the rendering part of DirectX) Used in: CAD, virtual reality, scientific.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
Draw a Simple Object. Example 1/4 #include “ glut.h ” void display(); void reshape(GLsizei w, GLsizei h); void main(int argc, char** argv){ glutInit(&argc,
OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
OpenGL and Projections
Computer Graphics (Fall 2003) COMS 4160, Lecture 6: OpenGL 2 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics (Fall 2005) COMS 4160, Lecture 10: OpenGL 1
CENG477 Introduction to Computer Graphics Introduction to OpenGL, GLUT and GLUI.
Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki.
Chapter 03: Graphics Primitives Course web page: Chapter #3.
Reference1. [OpenGL course slides by Rasmus Stenholt]
Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003.
Computer Graphics Bing-Yu Chen National Taiwan University.
1 GLUT Callback functions Event-driven: Programs that use windows  Input/Output  Wait until an event happens and then execute some pre-defined functions.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
Lecture 3 OpenGL.
Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 6: OpenGL 1
Graphics Systems and OpenGL. Business of Generating Images Images are made up of pixels.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
Modeling with OpenGL Practice with OpenGL transformations.
Introduction to OpenGL  OpenGL is a graphics API  Software library  Layer between programmer and graphics hardware (and software)  OpenGL can fit in.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
Draw a Simple Object. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall.
Doç. Dr. Cemil Öz SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz.
Graphics: Conceptual Model
CSCE 441: Computer Graphics
Introduction to OpenGL Programming
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
Lecture 2 Review OpenGL Libraries Graphics Overview Rendering Pipeline OpenGL command structure.
Chap 2 Write a Simple OpenGL Program. Preparing 1/2 environment : Microsoft Visual C 、 Microsoft Visual C++.Net Also need : GLUT
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 2: Complete Programs
The User Interface Lecture 2 Mon, Aug 27, 2007.
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Starting to draw dealing with Windows which libraries? clipping
גרפיקה ממוחשבת: מבוא ל-OpenGL
Rendering Pipeline, OpenGL/GLUT
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Starting to draw dealing with Windows which libraries? clipping
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Computer Graphics (Fall 2008) COMS 4160, Lecture 9: OpenGL 1

To Do  Start thinking (now) about HW 3. Milestones are due soon.

Course Outline  3D Graphics Pipeline Rendering (Creating, shading images from geometry, lighting, materials) Modeling (Creating 3D Geometry)

Course Outline  3D Graphics Pipeline Rendering (Creating, shading images from geometry, lighting, materials) Modeling (Creating 3D Geometry) Unit 1: Transformations Weeks 1,2. Ass 1 due Sep 25 Unit 2: Spline Curves Weeks 3,4. Ass 2 due Oct 7 Unit 3: OpenGL Weeks 5-7. Ass 3 due Nov 11 Midterm on units 1-3: Oct 20

Demo: Surreal (HW 3)

Methodology for Lecture  This unit different from others in course  Other units stress mathematical understanding  This stresses implementation details and programming  I am going to show (maybe write) actual code  Same code (with comments) available online to help you understand how to implement basic concepts  I hope the online code helps you understand HW 3 better  ASK QUESTIONS if confused!!  Simple demo 4160-opengl\opengl1\opengl1-orig.exe4160-opengl\opengl1\opengl1-orig.exe  This lecture deals with very basic OpenGL setup. Next 2 lectures will likely be more interesting

Outline  Basic idea about OpenGL  Basic setup and buffers  Matrix modes  Window system interaction and callbacks  Drawing basic OpenGL primitives Best source for OpenGL is the redbook. Of course, this is more a reference manual than a textbook, and you are better off implementing rather reading end to end. Though if you do have time, the book is actually quite readable

Introduction to OpenGL  OpenGL is a graphics API  Software library  Layer between programmer and graphics hardware (and software)  OpenGL can fit in many places  Between application and graphics system  Between higher level API and graphics system

Programmer’s View Application Graphics PackageApplication OpenGL Application Programming Interface Hardware and software Output Device Input Device

OpenGL Rendering Pipeline Geometry Primitive Operations Pixel Operations Scan Conversion Texture Memory Fragment Operations Framebuffer Vertices Images Many operations controlled by state (projection matrix, transformation matrix, color etc.) OpenGL is a large state machine

OpenGL Rendering Pipeline Geometry Primitive Operations Pixel Operations Scan Conversion Texture Memory Fragment Operations Framebuffer Vertices Images Traditional Approach: Fixed function pipeline (state machine) New Development (2003-): Programmable pipeline Programmable in Modern GPUs Programmable in Modern GPUs

GPUs and Programmability  Since 2003, can write vertex/pixel shaders  Fixed function pipeline special type of shader  Like writing C programs (see back of OpenGL book)  Performance >> CPU (even used for non-graphics)

GPUs and Programmability  Since 2003, can write vertex/pixel shaders  Fixed function pipeline special type of shader  Like writing C programs (see back of OpenGL book)  Performance >> CPU (even used for non-graphics)  But parallel paradigm  All pixels/vertices operate in parallel  Severe performance overheads for control flow, loops (limitations beginning to be relaxed in modern releases)  Not directly covered in COMS 4160  But you can make use of in assignments for extra credit

Why OpenGL?  Fast  Simple  Window system independent  Supports some high-end graphics features  Geometric and pixel processing  Standard, available on many platforms

Outline  Basic idea about OpenGL  Basic setup and buffers  Matrix modes  Window system interaction and callbacks  Drawing basic OpenGL primitives

Buffers and Window Interactions  Buffers: Color (front, back, left, right), depth (z), accumulation, stencil. When you draw, you write to some buffer (most simply, front and depth)  No window system interactions (for portability)  But can use GLUT (or Motif, GLX, Tcl/Tk)  Callbacks to implement mouse, keyboard interaction

Basic setup code (you will likely copy) int main(int argc, char** argv) { glutInit(&argc, argv); // Requests the type of buffers (Single, RGB). // Think about what buffers you would need... glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow ("Simple Demo"); init (); // Always initialize first // Now, we define callbacks and functions for various tasks. glutDisplayFunc(display); glutReshapeFunc(reshape) ; glutKeyboardFunc(keyboard); glutMouseFunc(mouse) ; glutMotionFunc(mousedrag) ; glutMainLoop(); // Start the main code return 0; /* ANSI C requires main to return int. */ }

Outline  Basic idea about OpenGL  Basic setup and buffers  Matrix modes  Window system interaction and callbacks  Drawing basic OpenGL primitives

 Viewing consists of two parts  Object positioning: model view transformation matrix  View projection: projection transformation matrix  OpenGL supports both perspective and orthographic viewing transformations  OpenGL’s camera is always at the origin, pointing in the –z direction  Transformations move objects relative to the camera  Matrices right-multiply top of stack. (Last transform in code is first actually applied) Viewing in OpenGL

Basic initialization code #include int mouseoldx, mouseoldy ; // For mouse motion GLdouble eyeloc = 2.0 ; // Where to look from; initially 0 -2, 2 void init (void) { /* select clearing color */ glClearColor (0.0, 0.0, 0.0, 0.0); /* initialize viewing values */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Think about this. Why is the up vector not normalized? glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(0,-eyeloc,eyeloc,0,0,0,0,1,1) ; }

Outline  Basic idea about OpenGL  Basic setup and buffers  Matrix modes  Window system interaction and callbacks  Drawing basic OpenGL primitives

Window System Interaction  Not part of OpenGL  Toolkits (GLUT) available  Callback functions for events  Keyboard, Mouse, etc.  Open, initialize, resize window  Similar to other systems (X, Java, etc.)  Our main func included glutDisplayFunc(display); glutReshapeFunc(reshape) ; glutKeyboardFunc(keyboard); glutMouseFunc(mouse) ; glutMotionFunc(mousedrag) ;

Basic window interaction code /* Defines what to do when various keys are pressed */ void keyboard (unsigned char key, int x, int y) { switch (key) { case 27: // Escape to quit exit(0) ; break ; default: break ; } /* Reshapes the window appropriately */ void reshape(int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30.0, (GLdouble)w/(GLdouble)h, 1.0, 10.0) ; }

Mouse motion (demo 4160-opengl1\opengl1-orig.exe ) 4160-opengl1\opengl1-orig.exe 4160-opengl1\opengl1-orig.exe /* Defines a Mouse callback to zoom in and out */ /* This is done by modifying gluLookAt */ /* The actual motion is in mousedrag */ /* mouse simply sets state for mousedrag */ void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON) { if (state == GLUT_UP) { // Do Nothing ; } else if (state == GLUT_DOWN) { mouseoldx = x ; mouseoldy = y ; // so we can move wrt x, y } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { // Reset gluLookAt eyeloc = 2.0 ; glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(0,-eyeloc,eyeloc,0,0,0,0,1,1) ; glutPostRedisplay() ; }

Mouse drag (demo 4160-opengl1\opengl1-orig.exe ) 4160-opengl1\opengl1-orig.exe 4160-opengl1\opengl1-orig.exe void mousedrag(int x, int y) { int yloc = y - mouseoldy ; // We will use the y coord to zoom in/out eyeloc += 0.005*yloc ; // Where do we look from if (eyeloc < 0) eyeloc = 0.0 ; mouseoldy = y ; /* Set the eye location */ glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(0,-eyeloc,eyeloc,0,0,0,0,1,1) ; glutPostRedisplay() ; }

Outline  Basic idea about OpenGL  Basic setup and buffers  Matrix modes  Window system interaction and callbacks  Drawing basic OpenGL primitives

OpenGL Primitives Points Lines Polygon Triangle Quad Quad Strip Triangle StripTriangle Fan

GLUT 3D Primitives Cube Sphere Teapot And others…

Drawing idea  Enclose vertices between glBegin() … glEnd() pair  Can include normal C code and attributes like the colors of points, but not other OpenGL commands  Inside are commands like glVertex3f, glColor3f  Attributes must be set before the vertex  Assembly line model (pass vertices, transform, clip, shade)  Client-Server model (client generates vertices, server draws) even if on same machine  glFlush() forces client to send network packet  glFinish() waits for ack, sparingly use synchronization

Geometry  Points (GL_POINTS) Stored in Homogeneous coordinates  Line segments (GL_LINES)  Polygons  Simple, convex (take your chances with concave)  Tessellate, GLU for complex shapes  Rectangles: glRect  Special cases (strips, loops, triangles, fans, quads)  More complex primitives (GLUT): Sphere, teapot, cube,…

Specifying Geometry glBegin(GL_POLYGON) ; // Chapter 2 but I do Counter Clock W  glVertex2f (4.0, 0.0) ;  glVertex2f (6.0, 1.5) ;  glVertex2f (4.0, 3.0) ;  glVertex2f (0.0, 3.0) ;  glVertex2f (0.0, 0.0) ;  // glColor, glIndex, glNormal, glTexCoord, … (pp 47)  // glMaterial, glArrayElement, glEvalCoord, … (pp 48)  // Other GL commands invalid between begin and end  // Can write normal C code… glEnd() ; (0,0) (4,0) (6,1.5) (4,3) (0,3)

Drawing in Display Routine void display(void) { glClear (GL_COLOR_BUFFER_BIT); // draw polygon (square) of unit length centered at the origin // This code draws each vertex in a different color. // The hardware will blend between them. // This is a useful debugging trick. I make sure each vertex // appears exactly where I expect it to appear. glBegin(GL_POLYGON); glColor3f (1.0, 0.0, 0.0); glVertex3f (0.5, 0.5, 0.0); glColor3f (0.0, 1.0, 0.0); glVertex3f (-0.5, 0.5, 0.0); glColor3f (0.0, 0.0, 1.0); glVertex3f (-0.5, -0.5, 0.0); glColor3f (1.0, 1.0, 1.0); glVertex3f (0.5, -0.5, 0.0); glEnd(); glFlush () ; } (-.5, -.5) BLUE (.5, -.5) WHITE (.5,.5) RED (-.5,.5) GREEN

Demo (change colors)