Download presentation
Presentation is loading. Please wait.
Published byJoseph Ball Modified over 8 years ago
1
OpenGL Programming Guide Chapter 2 Korea Univ. Graphics Labs. Ji Jun Yong Korea Univ. Graphics Labs. Ji Jun Yong
2
Contents Clear the window to an arbitrary color Clear the window to an arbitrary color Force any pending drawing to complete Force any pending drawing to complete Draw with any geometric primitive Draw with any geometric primitive Turn states on and off and query state variables Turn states on and off and query state variables Control the display of geometric primitives Control the display of geometric primitives Specify normal vectors Specify normal vectors Clear the window to an arbitrary color Clear the window to an arbitrary color Force any pending drawing to complete Force any pending drawing to complete Draw with any geometric primitive Draw with any geometric primitive Turn states on and off and query state variables Turn states on and off and query state variables Control the display of geometric primitives Control the display of geometric primitives Specify normal vectors Specify normal vectors
3
Clear the window to an arbitrary color (1/3) Why we use clear window command? Why we use clear window command? much efficient than a general-purpose drawing command Difficult to figure out an appropriate size and location for a window-clearing rectangle For clearing other buffers conveniently Why we use clear window command? Why we use clear window command? much efficient than a general-purpose drawing command Difficult to figure out an appropriate size and location for a window-clearing rectangle For clearing other buffers conveniently
4
Clear the window to an arbitrary color (2/3) void glClearColor (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); void glClear (GLbitfield mask); BufferName Color buffer GL_COLOR_BUFFER_BIT Depth buffer GL_DEPTH_BUFFER_BIT Accumulation buffer GL_ACCUM_BUFFER_BIT Stencil buffer GL_STENCIL_BUFFER_BIT red, green, blue, alpha values are clamped to the range [0, 1] glClearDepth(), glClearIndex(), glClearAccum()
5
Clear the window to an arbitrary color (3/3) Ex1) glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Advice1) OpenGL keeps track of the current clearing color as a state variable, So you set the clearing color once. Advice2) OpenGL allows you to specify multiple buffers if hardware supports, And some graphics hardware allows set of buffers to be cleared simultaneously glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // much faster and, glClear(GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT); Ex1) glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Advice1) OpenGL keeps track of the current clearing color as a state variable, So you set the clearing color once. Advice2) OpenGL allows you to specify multiple buffers if hardware supports, And some graphics hardware allows set of buffers to be cleared simultaneously glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // much faster and, glClear(GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
6
Specifying a Color void glColor3f (Glclampf red, Glclampf green, Glclampf blue); Advice1) OpenGL keeps track of the current color as a state variable, So you set the clearing color once. Advice2) With OpenGL, the description of the shape of an object being drawn is independent of the description of its color Ex1) glColor3f(1.0, 0.0, 0.0); //set red Draw_Object(A); //drawing A as red Draw_Object(B);//drawing B as red glColor3f(0.0, 1.0, 0.0); //set green glColor3f(0.0, 0.0, 1.0); //set blue Draw_Objecy(C); //drawing C as blue Advice1) OpenGL keeps track of the current color as a state variable, So you set the clearing color once. Advice2) With OpenGL, the description of the shape of an object being drawn is independent of the description of its color Ex1) glColor3f(1.0, 0.0, 0.0); //set red Draw_Object(A); //drawing A as red Draw_Object(B);//drawing B as red glColor3f(0.0, 1.0, 0.0); //set green glColor3f(0.0, 0.0, 1.0); //set blue Draw_Objecy(C); //drawing C as blue
7
Forcing Completion of Drawing void glFlush(void); Forces previously issued OpenGL command to begin execution Forces the client to send the network packet even though it might not be full Doesn’t wait for drawing to complete A few commands like double buffering automatically flush pending commands void glFinish(void); Forces previously issued OpenGL command to complete This command doesn’t return until all effects from previous commands are fully realized
8
Draw with any geometric primitive (1/3) void glVertex{234}{sifd}[v](TYPE coords); Effective only between glBegin() and glEnd() pair void glBegin(Glenum mode); void glEnd(void); A few valid commands between glBegin() and glEnd()
9
Draw with any geometric primitive (2/3) ValueMeaning GL_POINTS Individual points GL_LINES Pairs of vertices interpreted as individual line segments GL_LINE_STRIP Series of connected line segments GL_LINE_LOOP Same as above, with a segment added between last and first vertices GL_TRIANGLES Triples of vertices interpreted as triangles GL_TRIANGLE_STRIP Linked strip of triangles GL_TRIANGLE_FAN Linked fan of triangles GL_QUADS Quadruples of vertices interpreted as four- sided polygons GL_QUAD_STRIP Linked strip of quadrilaterals GL_POLYGON Boundary of a simple, convex polygon
10
Draw with any geometric primitive (3/3) Ex1) glBegin(GL_POINTS); glColor3f(0.0, 1.0, 0.0);//green glColor3f(1.0, 0.0, 0.0);//red glVertex(…); glColor3f(1.0, 1.0, 0.0);//yellow glColor3f(0.0, 0.0, 1.0);//blue glVertex(…); glEnd(); Advice1) At the moment glVertex*() is called, OpenGL assigns the resulting vertex the current color, texture coordinates, normal vector information, and so on Ex1) glBegin(GL_POINTS); glColor3f(0.0, 1.0, 0.0);//green glColor3f(1.0, 0.0, 0.0);//red glVertex(…); glColor3f(1.0, 1.0, 0.0);//yellow glColor3f(0.0, 0.0, 1.0);//blue glVertex(…); glEnd(); Advice1) At the moment glVertex*() is called, OpenGL assigns the resulting vertex the current color, texture coordinates, normal vector information, and so on
11
Turn states on and off and query state variables (1/2) By default, most of these states are initially inactive By default, most of these states are initially inactive void glEnable(GLenum cap); void glDisable(Glenum cap); GLboolean glIsEnabled(GLenum capability); There are more than 40 enumerated values Returns GL_TRUE or GL_FALSE, depending on whether or not the queried capability is currently activated
12
Turn states on and off and query state variables (2/2) void glGetBooleanv(GLenum pname,Glboolean *params); void glGetIntegerv(GLenum pname,Glint *params); void glGetFloatv(GLenum pname,Glfloat *params); void glGetDoublev(GLenum pname,Gldouble *params); void glGetPointerv(GLenum pname,Glvoid *params); To find out what values are set for many states Ex1) glGetFloatv(GL_CURRENT_COLOR, params); Ex1) glGetFloatv(GL_CURRENT_COLOR, params);
13
Control the display of geometric primitives (1/5) void glPointSize(GLfloat size); Set the width in pixels, size must be greater than 0.0 and by default is 1.0 void glLineWidth(GLfloat size); Set the width in pixels, size must be greater than 0.0 and by default is 1.0
14
Control the display of geometric primitives (2/5) void glLineStipple(GLint factor, GLushort pattern); The pattern argument is a 16-bit series of 0s and 1s The pattern can be strechted out by using factor Ex1) glLineStipple(1, 0x3F07); // 0011111100000111 glEnable(GL_LINE_STIPLLE); Ex1) glLineStipple(1, 0x3F07); // 0011111100000111 glEnable(GL_LINE_STIPLLE); void glPolygonStipple(const GLubyte * mask); The pattern argument is a 32x32 bitmap Ex1) glPolygonStipple(xxx); glEnable(GL_POLYGON_STIPLLE); Ex1) glPolygonStipple(xxx); glEnable(GL_POLYGON_STIPLLE);
15
Control the display of geometric primitives (3/5) void glPolygonMode(GLenum face, GLenum mode); face parameter can be GL_FRONT_AND_BACK, GL_FRONT, or GL_BACK mode parameter can be GL_POINT, GL_LINE, or GL_FILL Ex1) you can have the front faces filled and back faces outlined glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_LINE); Ex1) you can have the front faces filled and back faces outlined glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_LINE);
16
Control the display of geometric primitives (4/5) void glFrontFace(GLenum mode); Controls how front facing polygons are determined. By default, mode is GL_CCW (counter-clock), the other parameter is GL_CW (clock). (in window coordinates) void glCullFace(GLenum mode); Indicates which polygons should be discarded (culled) mode parameter can be GL_FRONT_AND_BACK, GL_FRONT, or GL_BACK Culling must be enables using glEnable(GL_CULL_FACE)
17
Control the display of geometric primitives (5/5) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_POLYGON); glEdgeFlag(GL_TRUE); glVertex3fv(V0); glEdeFlag(GL_FALSE); glVertex3fv(V1); glEdgeFlag(GL_TRUE); glVertex3fv(V2); glEnd(); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_POLYGON); glEdgeFlag(GL_TRUE); glVertex3fv(V0); glEdeFlag(GL_FALSE); glVertex3fv(V1); glEdgeFlag(GL_TRUE); glVertex3fv(V2); glEnd(); V2 V1 V0
18
Specify normal vectors You can’t assign normals anywhere other than at the vertices By convention, the normal is the one that points to the outside of the surface being modeled Before lighting, normal vector should be normalized Normal vectors remain normalized as long as your model transformations include only rotation and translation OpenGL command for automatic normalizing glEnable(GL_NORMALIZE) // performing full-fledfed normalization glEnable(GL_RESCALE_NORMAL) // only in case when you supply normal vector You can’t assign normals anywhere other than at the vertices By convention, the normal is the one that points to the outside of the surface being modeled Before lighting, normal vector should be normalized Normal vectors remain normalized as long as your model transformations include only rotation and translation OpenGL command for automatic normalizing glEnable(GL_NORMALIZE) // performing full-fledfed normalization glEnable(GL_RESCALE_NORMAL) // only in case when you supply normal vector void glNormal3{bsidf}(TYPE nx, TYPE ny, TYPE nz); void glNormal3{bsidf}v(const TYPE *v);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.