Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL E-Manual:

Similar presentations


Presentation on theme: "OpenGL E-Manual:"— Presentation transcript:

1 OpenGL E-Manual: http://fly.cc.fer.hr/~unreal/theredbook/

2 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL What it is: –Software interface to graphics hardware –about 120 C-callable routines for 3D graphics –Hardware independent What it is not: –Not a windowing system (no window creation) –Not a UI system (no keyboard and mouse routines) –Not a 3D modeling system (Open Inventor, VRML, Java3D)

3 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Functionality Simple geometric objects (e.g. lines, polygons, rectangles, etc.) Transformations, viewing, clipping Hidden line & hidden surface removal Color, lighting, texture Bitmaps, fonts, and images Immediate- & Retained- mode graphics

4 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Convention Functions: –prefix gl + capital first letter (e.g. glClearColor) Constants: –prefix GL + all capitals (e.g. GL_COLOR_BUFER_BIT)

5 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Convention Many variations of the same functions –glColor[2,3,4][b,s,i,f,d,ub,us,ui](v) [2,3,4]: dimension [b,s,i,f,d,ub,us,ui]: data type (v): optional pointer (vector) representation Example: glColor3i(1, 0, 0) or glColor3f(1.0, 1.0, 1.0) or GLfloat color_array[] = {1.0, 1.0, 1.0}; glColor3fv(color_array)

6 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Basic Concepts OpenGL as a state machine Graphics primitives going through a “pipeline” of rendering operations OpenGL controls the state of the pipeline with many state variables (fg & bg colors, line thickness, texture pattern, eyes, lights, surface material, etc.) Binary state: glEnable & glDisable Query: glGet[Boolean,Integer,Float,Double]

7 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Rendering Pipeline viewing transform graphics primitives modeling transform clipping shading & texture projection images in Internal buffer viewport transform images on screen Transform matrix Eye, lookat, headup Parallel or Persepctive volume Material, lights, surface color Viewport location

8 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Points, Lines, Polygons Specified by a set of vertices –glVertex[2,3,4][s,i,f,d](v) (TYPE coords) Polygons: –simple, convex, no holes Grouped together by glBegin() & glEnd() glBegin(GL_POLYGON) glVertex3f( …) glEnd

9 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Primitives There is debate, similar to CISC vs. RISC, over how many primitives to support OpenGL takes the middle ground Primitives specified by vertex calls (glVertex*) bracketed by glBegin(type) and glEnd() Line-based primitives: Line segments (GL_LINES), Polylines (GL_LINE_STRIP)

10 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Polygons Polygons are objects with a line loop border and an interior Polygons must be simple (no crossing edges), convex (the line segment between two interior points does not leave the polygon) and flat (planar) Triangles always obey these properties and are often better supported in hardware

11 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Polygon Types Polygons (GL_POLYGON) successive vertices define line segments, last vertex connects to first Triangles and Quadrilaterals (GL_TRIANGLES, GL_QUADS) successive groups of 3 or 4 interpreted as triangles or quads Strips and Fans (GL_TRIANGLE_STRIP, GL_QUAD_STRIP, GL_TRIANGLE_FAN) joined triangles or quads that share vertices

12 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Other Primitives GL_TRIANGLE_STRIP 012, 213, 234, 435, … GL_QUAD_STRIP 0132, 2354, 4576, …

13 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Points, Lines, Polygons Details Points: –size: glPointSize(GLfloat size) Lines: –width: glLineWidth(GLfloat width) –stippled lines: glLineStipple(GLint factor, GLushort pattern) glEnable(GL_LINE_STIPPLE)

14 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Points, Lines, Polygons Details glPolygonMode(face, mode) –face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK –mode: GL_POINT, GL_LINE, GL_FILL –default: both front and back as filled

15 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Points, Lines, Polygons Details face culling –glEnable(GL_CULL_FACE) –glCullFace(mode) mode: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK outside: back-facing polygon not visible inside: front-facing polygon not visible

16 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Geometric Transform Step 1: Modeling transform –A global “world” coordinate system where one constructs and manipulates models glTranslate[f,d](x,y,z) glRotate[f,d](angle,x,y,z) glScale[f,d](x,y,z)

17 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Geometric Transform Step 2: Viewing transform –Select the eye pos, look-at dir, head-up dir, and view volume

18 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Geometric Transform Step 3: Clipping –Remove primitives that are not in the view volume

19 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Geometric Transform Step 4: Projection –Map from 3D into 2D

20 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Geometric Transform Step 5: Viewport transform –Map 2D images onto screen

21 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Transform in OpenGL OpenGL uses stacks to maintain transformation matrices (MODELVIEW stack is the most important) You can load, push and pop the stack The current transform is applied to all graphics primitive until it is changed

22 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 General Transform Commands Specify current matrix –glMatrixMode(GLenum mode) GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE Initialize current matrix –glLoadIdentity(void) –glLoadMatrix[f,d](const TYPE *m)

23 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 General Transform Commands Concatenate current matrix –glMultMatrix(const TYPE *m) –C = CM Caveat: OpenGL matrices are stored in column major Best use utility functions glTranslate, glRotate, glScale

24 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Step 1: Modeling Transform glTranslate[f,d](x,y,z) glRotate[f,d](angle,x,y,z) glScale[f,d](x,y,z)  Order is important

25 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Step 1: Modeling Transform glMatrixMode(GL_MOD ELVIEW); glLoadIdentity(); glMultiMatrixf(T); glMultiMatrixf(R); draw_the_object(v); v’ = ITRv glMatrixMode(GL_MOD ELVIEW); glLoadIdentity(); glMultiMatrixf(R); glMultiMatrixf(T); draw_the_object(v); v’ = IRTv

26 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Modeling Transform (global) Usually, think there is a global “world” coordinate system where –all objects are defined –rotation, translation, scaling of objects in the world system –order is reverse (backward)

27 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Two Different Views As a global system object moves but coordinates stay the same apply in the reverse order As a local system object moves and coordinates move with it applied in the forward order

28 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 glLoadIdentity(); glMultiMatrixf(T); glMultiMatrixf(R); draw_the_object(v); Global view Roate object Then translate Local view Translate object Then rotate

29 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 glLoadIdentity(); glMultiMatrixf(R); glMultiMatrixf(T); draw_the_object(v);  Global view  Translate object  Then rotate  Local view  Rotate object  Then translate

30 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 glLoadIdentity(); glRotate(90,0,0,1) glTranslate(1,0,0); glRotate(45,0,0,1) glTranslate(1,0,0); draw_the_object(v); (0,1) (0,0)(1,0) (0,0)

31 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 glLoadIdentity(); glRotate(90,0,0,1) glTranslate(0,1,0); glScale(2,0.5,0) glTranslate(1,0,0); draw_the_object(v); Caveats: scale in local view may distort coordinate systems!! (-1,1) (1,1) (-1,1) (0,-0.5) (-1,-1) (1,0)(0.5,0)(-0.5,0) (-1,-0.5) A movement of 2 instead of 1!

32 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 More on Rotation

33 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學

34 Hierarchical Transform  Used very frequently for building complex objects in a modular manner  Subroutine calls  Be able to push and pop transform matrices as needed  OpenGL provides two stacks  at least 32 4x4 model view matrices  at least 2 4x4 projection matrices  glLoadMatrix(), glMultMatrix(), glLoadIdentity() affect top-most (current) one

35 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Hierarchical Transform  glPushMatrix(void)  topmost matrix is copied (top and second-from-top)  glPopMatrix(void)  topmost is gone (second-from-top becomes top)  Very important  For OpenGL beginner  Transformation ordering  Transformation grouping

36 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Hierarchical Transform glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(T); draw_ car_body(); glPushMatrix(); glTranslatef(T1); draw_wheel(); glPopMatrix(); glPushMatrix(); glTranslatef(T2); draw_wheel(); glPopMatrix();

37 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Hierarchical Transform

38 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Viewing Transform  Default: eyes at origin, looking along -Z  Important parameters: –where is the observer (camera)? origin of the viewing system – What is the look-at direction? -z direction – What is the head-up direction? y direction

39 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Viewing Transform gluLookAt (GLdouble eyex, eyey, eyez, GLdouble centerx, centery, centerz, GLdouble upx, upy, upz)

40 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Viewing Transform  eye and center: local w(z) direction  up and local w(z): local v(y) direction  local v(y) and w(z) directions: local u(x) direction

41 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Viewing Transform - gluLookAt  Occur after modeling transform  Usually involves only translation + rotation (no scaling)  Best done by gluLookAt function  Can also be done using glTranslate + glRotate (need to think about moving the camera instead of object in opposite way)

42 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Viewing Transform - the hard way  Use gluLookAt if possible  Think in an object-centered way (forward)  Camera is at the origin pointing along -z  Rotate and translate objects to expose the right view

43 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 glMatrixMode glLoadIdentity glRotateZ(-roll) glRotateX(-pitch) glRotateY(-heading) glTranslate(-px,-py,-pz) // Other modeling transform glBegin draw_car(); glEnd (px,py,pz)

44 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Shading and Texturing  A BIG topic in graphics  For photo realistic rendering  Two aspects: geometry (location and orientation) and appearance (color, shading, texture)  Here we concentrate on geometry only

45 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Perspective Projection  glMatrixMode(GL_PROJECTION);  glLoadIdentity();  glFustrum(GLDouble left, right, bottom, top, near, far);

46 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Perspective Projection  gluPerspective(GLdouble fovy, aspect, near, far) -- for symmetric view volume

47 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Perspective Projection

48 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Example

49 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Parallel (Orthographical) Projection  No perspective foreshortening – sizes and angles can be measured and compared  Useful for engineering drawing – top, front, side views

50 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Parallel (Othographic) Projection  glOrtho(GLdouble left, right, bottom, top, near, far)

51 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Clipping  Get rid of the things that are not seen  To do things efficiently require some mathematical twiddling

52 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Viewport Transform  glViewport(GLint x, y, GLsizei width, height);  The internal buffer is mapped to the rectangle specified by (x,y) lower left corner of size width and height

53 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Viewport Transform  Multiple buffers can be mapped to a single window (if they have different viewports)  Distortion may occur if viewport does not have the right aspect ratio gluPerspective(fovy, 1.0, near, far) glViewport(0,0,400,400) gluPerspective(fovy, 1.0, near, far) glViewport(0,0,400,200)

54 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Other OpenGL Basics  Create a drawing buffer (not a screen window)  Clear buffer  Draw to buffer  Link buffer to screen window display  Interaction (expose, resize, mouse, keyboard input, etc).

55 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Buffers  Rectangular arrays of pixels  Color buffers: front-left, front-right, back-left, back-right  At least one, Color indexed or RGBA  Stereoscopic systems have left and right  Doubled buffered systems have front and back

56 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Buffers  Color Indexed Buffer

57 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Buffers  RGBA Buffer

58 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Other OpenGL Buffers Depth buffers: – for determining hidden surface effects –Z buffer Stencil buffers: – acts like a cardboard stencil (“windshield effect”) –15 bits Z buffer + 1 bit stencil buffer or –24 bits Z buffer + 8 bit stencil buffer Accumulation buffers: – for accumulating multiple images into one (e.g. for anti-aliasing, motion blur)

59 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Buffer Operations Draw –glDrawBuffer(GLenum mode) enabled for writing or clearing GL_FRONT, GL_BACK, GL_RIGHT, GL_LEFT GL_FRONT_RIGHT, GL_FRONT_LEFT GL_BACK_RIGHT, GL_BACK_LEFT GL_AUXI, GL_FRONT_AND_BACK, GL_NONE

60 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Buffer Operations Clear –glClear[Color,Index,Depth,Stencil,Accum] glClearColor(0.0,0.0,0.0,0.0); glClearDepth(1.0); Set clear color, depth values – glClear(GLbitfield mask) GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT GL_STENCIL_BUFFER_BIT GL_ACCUM_BUFFER_BIT

61 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL Misc. Functions Forced completion – glFlush(void) asynchronous – glFinish(void) synchronous – One of them should be called at the end of each frame

62 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Color FLAT vs. SMOOTH glShadeModel(GL_FLAT); glShadeModel(GL_SMOOTH) Color Specification (affects subsequent primitives) glBegin(GL_TRIANGLES); glColor3f(1.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0); glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, 1.0); glEnd();

63 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Depth (z-buffer) For Hidden Surface Removal glutInitDisplayMode(GLUT_DEPTH | ….); glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT | ….);

64 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Shading Need gradient of color to realistically represent 3-D objects Simulate the interaction between light sources and surfaces Recursive interaction of light between surfaces embodied in an integral rendering equation Unsolvable in practice so use approximations Consider rays from light sources that eventually strike the viewing plane

65 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Light and Matter The reflectance, absorption and transmission properties of a surface interact with light to determine appearance Surface Types: – Specular (shiny) - reflected light is scattered in a narrow range of angles. Perfectly specular (mirror) reflect in a single direction – Diffuse (matt) - reflected light is scattered in all directions. Perfectly diffuse appears the same to all viewers – Translucent - allows light to penetrate, be refracted and emergeelsewhere. Examples: glass, water

66 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Light Sources Each source has an RGB intensity (luminance) Types: –Ambient - uniform lighting scattered in all directions and identical throughout the scene –Point Sources - emit light in all directions from a point. Intensity often scaled inversely with distance. Create un- realistically stark shadows –Spotlights - emit light in a limited range of angles(a cone). Intensity tails off from the cone centre to its edge –Distant Light Sources - replace location of light source with a constant direction for efficiency

67 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Lighting and shading Phong model, Gouraud shading Three steps: – Enable lighting – Specify the lights – Specify the materials Advanced user: – local/infinite viewpoint – two sided lighting

68 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Simple lighting Enable lighting: glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); Specify light sources parameters: glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0,GL_POSITION, light_pos); glLightfv(GL_LIGHT0,GL_DIFFUSE, light_dif); Plus global ambient light glLightModelfv(GL_LIGHT_MODEL_AMBIENT, l_ambient);

69 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Light Sources in OpenGL GLfloat light0_pos[] = {1.0, 2.0, 3.0, 1.0}; GLfloat light1_dir[] = {1.0, 2.0, 3.0, 0.0}; GLfloat lambient[] = {1.0, 0.0, 0.0, 1.0}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); glLightfv(GL_LIGHT0, GL_AMBIENT, lambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, ldiffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, lspecular); Light0 is a point light source (w = 1) Light1 is a distant light source (w = 0) ldiffuse and lspecular are similar 4 element arrays to lambient Must enable lighting and the particular light sources (0-7) Lighting setup: glLightfv(source, paramter, pointer_to_array) glLightf(source, parameter, value) Variations allow attenuation, one or two sided lighting and spotlight settings

70 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Normal Vectors Define normal vectors for each vertex of every polygons These normal vectors determine the orientation of the polygon relative to the light sources. glBegin(GL_TRIANGLES); glNormal3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, 0.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(1.0, 0.0, 0.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(0.0, 1.0, 0.0); glEnd();

71 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Specifying materials One function call for each property: glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_amb ); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE, mat_diff); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_sp ec) glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,100.0); glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,mat_em i);

72 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Materials in OpenGL GLfloat mambient[] = {0.2, 0.2, 0.2, 1.0}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mdiffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mspecular); glMaterialfv(GL_FRONT, GL_SHININESS, 100.0); mdiffuse and mspecular are similar 4 element arrays to lambient Material setup: glMaterialfv(face, type, pointer_to_array) glMaterialf(face, type, value) Variations allow light emmiting surfaces Before each glVertex3f it is necessary to set glNormal3f to ensure correct lighting

73 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學

74 Texture quadratic=gluNewQuadric();// Create A Pointer To The Quadric gluQuadricOrientation(quadratic,GLU_OUTSIDE); gluQuadricNormals(quadratic, GLU_SMOOTH);// Create Smooth Normals gluQuadricTexture(quadratic, GL_TRUE);// Create Texture CoordsgluQuadricDrawStyle(quadratic,GLU_FILL); glBindTexture(GL_TEXTURE_2D,texture.texID); gluSphere(quadratic,15.0f,15,15); void glBindTexture( GLenum target, GLuint texture ) target specifies the target. Must be either GL_TEXTURE_1D or GL_TEXTURE_2D. texture specifies the name of a texture. void gluSphere(GLUquadricObj *qobj, GLdouble radius, GLint slices, GLint stacks) qobj specifies the quadrics object (created with gluNewQuadric).gluNewQuadric radius specifies the radius of the sphere. slices specifies the number of subdivisions around the z axis (similar to lines of longitude). stacks specifies the number of subdivisions along the z axis (similar to lines of latitude).

75 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 TGALoader #include "Texture.h“ int LoadTextures() { int Status = FALSE; if(LoadTGA(&texture, “xxx.tga)) { Status = TRUE; glGenTextures(1, &texture.texID); glBindTexture(GL_TEXTURE_2D, texture.texID); glTexImage2D(GL_TEXTURE_2D, 0, 3, texture.width, texture.height,0,GL_RGB, GL_UNSIGNED_BYTE, texture.imageData); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); if (texture[i].imageData) free(texture[i].imageData); } return Status; }

76 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Helper Libraries Remember that OpenGL does not do – windowing, GUI, and modeling A real application will need all the above At least two choices – GLUT (GL Utility Library) or aux (obsolete) simple windowing, GUI and models

77 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL-Related Libraries GLU (prefix glu-) – utility library GLUT (prefix glut-) –windowing, input, simple objects (OpenGL1.1 and later, replacing aux- )

78 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 OpenGL-Related Libraries Open Inventor – objects + methods of interaction – creating + editing 3D scenes – data format exchange

79 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 GL Utility Library (glut) Convenient and easy-to-use For – specifying the display mode – creating window (size and location) – handling window and input events – convenient objects Replaced aux library after version 1.1

80 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 GL Utility Library (glut) glutInit(int argc, char **argv) – initialize glut, process command line arguments such as -geometry, -display, etc. glutInitDisplayMode(unsigned int mode) – Mode for later glutCreateWindow() call – GLUT_RGBA or GLUT_INDEX – GLUT_SINGLE or GLUT_DOUBLE – Associated GLUT_DEPTH, GLUT_STENCIL, GLUT_ACCUM buffers – default: RGBA & SINGLE

81 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 GL Utility Library (glut) glutInitWindowPosition(int x, int y) glutInitWindowSize(int width, int height) – window location and size glutCreateWindow(char *name) – after Init, Displaymode, Position, and Size calls – will not appear until glutMainLoop

82 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 GL Utility Library (glut) glutDisplayFunc(void (*func) (void)) – display function for initial display, de-iconfy and expose glutReshapeFunc(void (*function) (width, height)) – called when window is resized or moved – default glViewport(0,0,width,height)

83 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 GL Utility Library (glut) glutKeyboardFunc(void *(func) (unsinged int key, int x, int y) glutMouseFunc(void *(func) (int button, int state, int x, int y)) – button: GLUT_{LEFT,MIDDLE,RIGHT}_BUTTON mode: GLUT_UP, GLUT_DOWN glutMotionFunc(void *(func) (int x, int y)) – mouse pointer move while one or more mouse buttons is pressed glutIdleFunc(void *(func) (int x, int y))

84 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 GL Utility Library (glut) glut{Wire,Solid}Sphere() glut{Wire,Solid}Cube() glut{Wire,Solid}Box() glut{Wire,Solid}Torus() glut{Wire,Solid}Cylinder() glut{Wire,Solid}Cone() glut{Wire,Solid}Teapot() glut{Wire,Solid}(Octahedron,Tetrahedron, Dodecahedron,Icosahedron) centered at the origin

85 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 GL Utility Library (glut) glutMainLoop (void) – GLUT main loop, never returns

86 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 GLUT Example #include

87 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } void display(void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); /* clear the matrix */ /* viewing transformation */ gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef (1.0, 2.0, 1.0); /* modeling transformation*/ glutWireCube (1.0); glFlush (); }

88 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glMatrixMode (GL_MODELVIEW); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; }

89 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE |GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }

90 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Selection Allow a user of your application to pick an object drawn on the screen. Once you're in selection mode the contents of the frame buffer don't change until you exit selection mode. When you exit, OpenGL returns a list of the primitives that would have intersected the viewing volume. The viewing volume is defined by the current modelview and projection matrices. Each primitive that intersects the viewing volume causes a selection hit. The list of primitives is actually returned as an array of integers called the hit records.

91 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Basic steps 1.Specify the array to be used for the returned hit records with glSelectBuffer(). 2.Enter selection mode by specifying GL_SELECT with glRenderMode(). 3.Initialize the name stack using glInitNames() and glPushName(). 4.Define the viewing volume you want to use for selection. Usually, this is different from the viewing volume you used to draw the scene originally, so you probably want to save and then restore the current transformation state with glPushMatrix() and glPopMatrix(). 5.Alternately issue primitive drawing commands to manipulate the name stack so that each primitive of interest has an appropriate name assigned. glLoadName() 6.Exit selection mode by specifying GL_RENDER with glRenderMode() and process the returned selection data (the hit records).

92 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Creating the Name Stack GLuint selectBuf[BUFSIZE]; GLint hits; glSelectBuffer(BUFSIZE, selectBuf) (void) glRenderMode(GL_SELECT); glInitNames(); glPushName(-1); glPushMatrix(); /* save the current transformation state */ /* create your desired viewing volume here */ glLoadName(1); drawSomeObject(); glLoadName(2); drawAnotherObject(); glLoadName(3); drawYetAnotherObject(); drawJustOneMoreObject(); glPopMatrix (); glFulsh(); Hits=glRenderMode(GL_RENDER);

93 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Creating the Name Stack Calls to glPushName(), glPopName(), and glLoadName() are ignored if you're not in selection mode. It simplifies your code to use these calls throughout your drawing code, and then use the same drawing code for both selection and normal rendering modes.

94 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 The Hit Record Each hit record consists of four items, in order: 1.The number of names on the name stack when the hit occurred. 2.Both the minimum and maximum window-coordinate z values of all vertices of the primitives that intersected the viewing volume since the last recorded hit. These two values, which lie in the range [0,1], are each multiplied by 2 32 -1 and rounded to the nearest unsigned integer. 3.The contents of the name stack at the time of the hit, with the bottommost element first.

95 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Picking As an extension of the selection Using the special picking matrix by calling gluPickMatrix(x,y,width,height,viewport[]) Objects that are drawn near the cursor cause selection

96 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 #include int board[3][3]; /* amount of color for each square */ /* Clear color value for every square on the board */ void myinit(void) { int i, j; for (i = 0; i < 3; i++) for (j = 0; j < 3; j ++) board[i][j] = 0; glClearColor (0.0, 0.0, 0.0, 0.0); } void drawSquares(GLenum mode) { GLuint i, j; for (i = 0; i < 3; i++) { if (mode == GL_SELECT) glLoadName (i); for (j = 0; j < 3; j ++) { if (mode == GL_SELECT) glPushName (j); glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0, (GLfloat) board[i][j]/3.0); glRecti (i, j, i+1, j+1);// 3D coordinate if (mode == GL_SELECT) glPopName (); }

97 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 void processHits (GLint hits, GLuint buffer[]) { unsigned int i, j; GLuint ii, jj, names, *ptr; printf ("hits = %d\n", hits); ptr = (GLuint *) buffer; for (i = 0; i < hits; i++) { /* for each hit */ names = *ptr; printf (" number of names for this hit = %d\n", names); ptr++; printf (" z1 is %u;", *ptr); ptr++; printf (" z2 is %u\n", *ptr); ptr++; printf (" names are "); for (j = 0; j < names; j++) { /* for each name */ printf ("%d ", *ptr); if (j == 0) /* set row and column */ ii = *ptr; else if (j == 1) jj = *ptr; ptr++; } printf ("\n"); board[ii][jj] = (board[ii][jj] + 1) % 3; }

98 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 #define BUFSIZE 512 void pickSquares(int button, int state, int x, int y) { GLuint selectBuf[BUFSIZE]; GLint hits; GLint viewport[4]; if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return; glGetIntegerv (GL_VIEWPORT, viewport); glSelectBuffer (BUFSIZE, selectBuf); (void) glRenderMode (GL_SELECT); glInitNames(); glPushName(-1); glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity (); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); gluOrtho2D (0.0, 3.0, 0.0, 3.0); drawSquares (GL_SELECT); glPopMatrix (); glFlush (); hits = glRenderMode (GL_RENDER); processHits (hits, selectBuf); }

99 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 void display(void) { glClear(GL_COLOR_BUFFER_BIT); drawSquares (GL_RENDER); glFlush(); } void myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D (0.0, 3.0, 0.0, 3.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA); glutInitWindowPosition (100, 100); glutInitWindowSize (100, 100]); glutCreateWindow(argv[0]); myinit (); glutMouseFunc (pickSquares); glutReshapeFunc (myReshape); glutMainLoop(display); }

100 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Picking with Multiple Names and a Hierarchical Model Multiple names can also be used to choose parts of a hierarchical object in a scene. If you were rendering an assembly line of automobiles, you might want the user to move the mouse to pick the third bolt on the left front tire of the third car in line. A different name can be used to identify each level of hierarchy: which car, which tire, and finally which bolt.

101 CVVR Lab.@ National Dong Hwa Univ. 國立東華大學 Possible name-stack return values 2 z1z2 2 1 //Car 2, wheel 1 1 z1z2 3 //Car 3 body 3 z1z2 1 1 0 //Bolt 0 on wheel 1 on car 1


Download ppt "OpenGL E-Manual:"

Similar presentations


Ads by Google