Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.

Similar presentations


Presentation on theme: "OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when."— Presentation transcript:

1 OpenGL CS418 Computer Graphics John C. Hart

2 OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when event occurs Example: Declare a function myMouse to respond to mouse click Register it: Tell OpenGL to call it when mouse clicked Code? glutMouseFunc(myMouse);

3 GL Utility Toolkit (GLUT) OpenGL is window system independent Concerned only with drawing No window management functions (create, resize, etc) Very portable GLUT: Minimal window management: fast prototyping Interfaces with different windowing systems Allows easy porting between windowing systems

4 Program Structure Configure and open window (GLUT) Initialize OpenGL state Register input callback functions (GLUT) Render Resize Input: keyboard, mouse, etc My initialization Set background color, clear color, drawing color, point size, establish coordinate system, etc. glutMainLoop( ) Waits here infinitely till action is selected

5 GLUT: Opening a window GLUT used to open window glutInit(&argc, argv); initializes glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); sets display mode (e.g. single buffer with RGB) glutInitWindowSize(640,480); sets window size (WxH) glutInitPosition(100,150); sets upper left corner of window glutCreateWindow(“my first attempt”); open window with title “my first attempt”

6 OpenGL Skeleton void main(int argc, char** argv){ // First initialize toolkit, set display mode and create window glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow(“my first attempt”); // … then register callback functions, // … do my initialization //.. wait in glutMainLoop for events }

7 GLUT Callback Functions Register all events your program will react to Event occurs => system generates callback Callback: routine system calls when event occurs No registered callback = no action

8 GLUT Callback Functions GLUT Callback functions in skeleton glutDisplayFunc(myDisplay): window contents need to be redrawn glutReshapeFunc(myReshape): called when window is reshaped glutMouseFunc(myMouse): called when mouse button is pressed glutKeyboardFunc(mykeyboard): called when keyboard is pressed or released glutMainLoop( ): program draws initial picture and enters infinite loop till event

9 Example: Rendering Callback Do all your drawing in the display function Called initially and when picture changes (e.g.resize) First, register callback in main( ) function glutDisplayFunc( display ); Then, implement display function void display( void ) { // put drawing stuff here ……. glBegin( GL_LINES ); glVertex3fv( v[0] ); glVertex3fv( v[1] ); …………… glEnd(); }

10 OpenGL Skeleton void main(int argc, char** argv){ // First initialize toolkit, set display mode and create window glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow(“my first attempt”); // … now register callback functions glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); myInit( ); glutMainLoop( ); }

11 OpenGL Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: Runs on everything, including cell phones (OpenGL/ES) Alternatives: Microsoft’s Direct3D – limited to MSWindows Sun’s Java3D – slower, implemented on top of OpenGL

12 APP GLUT Library Layers OpenGL = GL + GLU –Basic low-level GL routines implemented using OS graphics routines –Timesaving higher-level GLU routines implemented using GL routines GLUT opens and manages OpenGL windows and adds helper functions OpenGL Extensions provide direct device-dependent access to hardware GL GLU OS Driver HW EXT

13 Vertex Pipeline Homogeneous Divide Model Coords Model Xform World Coords Viewing Xform Still Clip Coords Clipping Window Coordinates Window Coordinates Window to Viewport Window to Viewport Coordinates Viewport Coordinates Clip Coords Viewing Coords Perspective Distortion

14 Viewport Coordinates Physical per-pixel integer coordinates Also called screen or device coordinates glViewport(x,y,w,h) x,y – lower left pixel w – width h – height Sometimes (0,0) is in the upper left corner (e.g. for mouse input) (0,0)(HRES-1,0) (HRES-1, VRES-1) (0,VRES-1)

15 Window Coordinates Logical, mathematical floating-point coordinates glOrtho(l,r,b,t,n,f) left, right, bottom, top near, far: limits depth gluOrtho2D(l,r,b,t) calls glOrtho(l,r,b,t,-1,1) To use per-pixel coordinates, call: gluOrtho2D(-.5,-.5,w-.5,h-.5); (-1,-1)(1,-1) (1,1) (-1,1)

16 OpenGL State OpenGL commands change its internal state variables which control how it turns geometric models into pictures E.g. the window-to-viewport transformation glViewport(x,y,w,h) glOrtho(l,r,b,t,n,f) Can query this state: GLfloat buf[4]; glGetFloatv(GL_VIEWPORT,buf); x = buf[0]; y = buf[1]; w = buf[2]; h = buf[3]; (l,b)(r,b) (r,t)(l,t) (x,y)(x+w-1,y) (x+w-1,y+h-1) (x,y+h-1) W2V OpenGL State: x,y,w,h,l,r,b,t,n,f,…

17 OpenGL Commands gl[u]Fubar[234][dfis][v] [u]: GLU v. GL command [234]: dimension [dfisb]: data type –d: GLdouble (double) –f: GLfloat (float) –i: GLint (int) –s: string [v]: vector (reference v. value) Examples: glVertex3f(-0.5, 3.14159, 2); glVertex2i(200, 350); GLdouble point[4]; glVertex4dv(point); Examples: glVertex3f(-0.5, 3.14159, 2); glVertex2i(200, 350); GLdouble point[4]; glVertex4dv(point);

18 Points glBegin(GL_POINTS); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

19 Lines glBegin(GL_LINES); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

20 Line Strip glBegin(GL_LINE_STRIP); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

21 Line Loop glBegin(GL_LINE_LOOP); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

22 Polygon glBegin(GL_POLYGON); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1) OpenGL only supports convex polygons (and really only triangles)

23 Quads glBegin(GL_QUADS); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

24 Quads glBegin(GL_QUADS); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glEnd(); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.);

25 Quads glBegin(GL_QUADS); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glEnd(); (-1,1) (-1,-1)(1,-1) (1,1) (-1,1) Lines should never pass through a vertex.

26 Triangles glBegin(GL_TRIANGLES); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

27 Triangles glBegin(GL_TRIANGLES); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.6,1.); glVertex2f(-.2,.6); glVertex2f(.6,1.); glVertex2f(-.2,.6); glVertex2f(.2,.6); glVertex2f(.6,1.); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); … glEnd(); (-1,-1)(1,-1) (1,1) (-1,1) Lines should never pass through a vertex.

28 Triangle Strip glBegin(GL_TRIANGLE_STRIP); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

29 Triangle Strip glBegin(GL_TRIANGLE_STRIP); glVertex2f(-.6,1.); glVertex2f(.6,1.); glVertex2f(-.2,.6); glVertex2f(.2,.6); glVertex2f(-.2,-.6); glVertex2f(.2,-.6); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glEnd(); … (-1,-1)(1,-1) (1,1) (-1,1) First two vertices prime the pump, then every new vertex creates a triangle connecting it to the previous two vertices

30 Triangle Fan glBegin(GL_TRIANGLE_FAN); glVertex2f(-.2,.6); glVertex2f(-.6,.6); glVertex2f(-.6,1.); glVertex2f(.6,1.); glVertex2f(.2,.6); glVertex2f(.2,-.6); glVertex2f(-.2,-.6); glEnd(); … (-1,-1)(1,-1) (1,1) (-1,1) First two vertices prime the pump, then every new vertex creates a triangle connecting it to the previous vertex and the first vertex

31 Assigning Color glColor3f(0,0,1); glBegin(GL_POLYGON); glVertex2f(-1,1); glVertex2f(-1,-1); glVertex2f(1,-1); glEnd(); glColor3f(1,0,0); glBegin(GL_POLYGON); glVertex2f(-1,1); glVertex2f(-1,-1); glVertex2f(1,-1); glEnd(); glColor3f(0,0,0); glBegin(GL_LINE_LOOP); glVertex2f(-1,1); glVertex2f(-1,-1); glVertex2f(1,-1); glEnd(); glBegin(GL_POLYGON); glColor3f(0,1,0); glVertex2f(-1,1); glColor3f(0,0,1); glVertex2f(-1,-1); glColor3f(1,0,0); glVertex2f(1,-1); glEnd();

32 Indexed Face Set Popular file format –VRML, Wavefront “.obj”, etc. Ordered list of vertices –Prefaced by “v” (Wavefront) –Spatial coordinates x,y,z –Index given by order List of polygons –Prefaced by “f” (Wavefront) –Ordered list of vertex indices –Length = # of sides –Orientation given by order v x 1 y 1 z 1 v x 2 y 2 z 2 v x 3 y 3 z 3 v x 4 y 4 z 4 f 1 2 3 f 2 4 3 (x1,y1,z1)(x1,y1,z1) (x2,y2,z2)(x2,y2,z2) (x3,y3,z3)(x3,y3,z3) (x4,y4,z4)(x4,y4,z4)

33 Other Attributes Vertex normals –Prefixed w/ “vn” (Wavefront) –Contains x,y,z of normal –Not necessarily unit length –Not necessarily in vertex order –Indexed as with vertices Texture coordinates –Prefixed with “vt” (Wavefront) –Not necessarily in vertex order –Contains u,v surface parameters Faces –Uses “/” to separate indices –Vertex “/” normal “/” texture –Normal and texture optional –Can eliminate normal with “//” v x 0 y 0 z 0 v x 1 y 1 z 1 v x 2 y 2 z 2 vn a 0 b 0 c 0 vn a 1 b 1 c 1 vn a 2 b 2 c 2 vt u 0 v 0 vt u 1 v 1 vt u 2 v 2 (x 0,y 0,z 0 ) (a 0,b 0,c 0 ) (u 0,v 0 ) (x1,y1,z1)(a1,b1,c1)(u1,v1)(x1,y1,z1)(a1,b1,c1)(u1,v1) (x2,y2,z2)(a2,b2,c2)(u2,v2)(x2,y2,z2)(a2,b2,c2)(u2,v2) f 0/0/0 1/1/1 2/2/2f 0/0/0 1/0/1 2/0/2


Download ppt "OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when."

Similar presentations


Ads by Google