state management and drawing geometry objects (opengl book ch 2)
DESCRIPTION
State Management and Drawing Geometry Objects (OpenGL Book Ch 2). Objective. Clear the window to an arbitrary color Force any pending drawing to complete Draw with any geometric primitive Turn states on and off and query state variables Control the display of those primitives - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/1.jpg)
State Management and Drawing Geometry Objects
(OpenGL Book Ch 2)
![Page 2: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/2.jpg)
Objective
• Clear the window to an arbitrary color
• Force any pending drawing to complete
• Draw with any geometric primitive
• Turn states on and off and query state variables
• Control the display of those primitives
• Specify normal vectors
• Use vertex arrays
• Save and restore state variables
![Page 3: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/3.jpg)
Clearing the Window
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
// or run more slowly with,
// glClear(GL_COLOR_BUFFER_BIT);
// glClear(GL_DEPTH_BUFFER_BIT);
![Page 4: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/4.jpg)
Specifying a Color
• Pseudocodeset_current_color(red);
draw_object(A);
draw_object(B);
set_current_color(green); // wasted
set_current_color(blue);
draw_object(C);
• glColor3f(1.0, 0.0, 0.0);
![Page 5: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/5.jpg)
Forcing Completion of Drawing
• glFlush();– Forces previously issued OpenGL commands to b
egin execution.
• glFinish();– Forces all previously issued OpenGL command s t
o complete. This command doesn’t return until all effects from previous commands are fully realized.
![Page 6: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/6.jpg)
What are Points, Lines, and Polygon?
• Points– represented by a vertex
• Lines– refer to line segments
• Polygons– must be simple, convex, and planar
• Rectangles– glRectf(x1, y1, x2, y2);– glRectfv(*pt1, *pt2);
![Page 7: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/7.jpg)
Curves and Curved Surface
Approximating Curves
![Page 8: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/8.jpg)
Specifying Vertices
glVertex2s(2, 3);
glVertex3d(0.0, 0.0, 3.14);
glVertex4f(2.4, 1.0, -2.2, 2.0);
GLdouble v[3] = {1.0, 9.0, 8.0};
glVertex3dv(v);
![Page 9: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/9.jpg)
Drawing Geometric Primitives
glBegin(GL_POLYGON);
glVertex2f(0.0, 0.0);
glVertex2f(4.0, 3.0);
glVertex2f(6.0, 1.5);
glVertex2f(4.0, 0.0);
glEnd();
![Page 10: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/10.jpg)
OpenGL Geometric Primitives
• GL_POINTS• GL_LINES• GL_LINE_STRIP• GL_LINE_LOOP• GL_TRIANGLES• GL_TRIANGLE_STRIP• GL_TRIANGLE_FAN• GL_QUADS• GL_QUAD_STRIP• GL_POLYGON
![Page 11: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/11.jpg)
Geometric Primitive Types
![Page 12: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/12.jpg)
Valid Commands between glBegin(), glEnd()
• glVertex*()• glColor*(), glIndex*()• glNormal*()• glTexCoord*()• glEdgeFlag*()• glMaterial*()• glArrayElement()• glEvalCoord*(), glEvalPoint*()• glCallList(), glCallLists()• and any C or C++ codes
![Page 13: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/13.jpg)
Basic State Management
• glEnable(GL_DEPTH_TEST);• glDisable(GL_FOG)• if (glIsEnabled(GL_FOG)) ...• glGetBooleanv();• glGetIntegerv();• glGetFloatv();• glGetDoublev(GL_CURRENT_COLOR,x);• glGetPointerv();
![Page 14: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/14.jpg)
Point and Line Details
glPointSize(2.0);
glLineWidth(2.0);
glLineStipple(1,0xAAAA);
glLineStipple(2,0xAAAA);
glEnable(GL_LINE_STIPPLE);
![Page 15: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/15.jpg)
Stippled Lines
![Page 16: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/16.jpg)
Polygon Details
• Drawing polygons as points, outlines, or solids
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_LINE);
glPolygonMode(GL_FRONT_AND_BACK, G
L_POINT);
![Page 17: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/17.jpg)
Reversing and Culling Polygon Faces
glFrontFace(GL_CCW);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glCullFace(GL_FRONT);
glCullFace(GL_FRONT_AND_BACK);
![Page 18: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/18.jpg)
Stippling Polygons
glEnable(GL_POLYGON_STIPPLE);
// Define stipple pattern fly here...
glPolygonStipple(fly);
![Page 19: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/19.jpg)
Marking Polygon Boundary Edges
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_POLYGON);
glEdgeFlag(GL_TRUE);
glVertex3fv(V0);
glEdgeFlag(GL_FALSE);
glVertex3fv(V1);
glEdgeFlag(GL_TRUE);
glVertex3fv(V2);
glEnd();
![Page 20: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/20.jpg)
Normal Vectors
glBegin (GL_POLYGON);
glNormal3fv(n0);
glVertex3fv(v0);
glNormal3fv(n1);
glVertex3fv(v1); glVertex3fv(v2);
glEnd();
• Provide Unit Normals! glEnable(GL_NORMALIZE) can be expensive
![Page 21: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/21.jpg)
Example: Drawing a unit cubeStatic GLfloat vdata[8][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0},{1.0,1.0,1.0},{0.0,1.0,1.0}}; //global!!Static GLint allIndx[6][4]= { {4,5,6,7},{1,2,6,5},{0,1,5,4}, {0,3,2,1},{0,4,5,4},{2,3,7,6}};for (i=0; i<6; i++){
glBegin(GL_QUADS);glVertex3fv(&vdata[allIndx[i][0]][0]);glVertex3fv(&vdata[allIndx[i][1]][0]);glVertex3fv(&vdata[allIndx[i][2]][0]);glVertex3fv(&vdata[allIndx[i][3]][0]);
glEnd();}
0
7
3 2
1
5
6
4
X
Y
Z
![Page 22: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/22.jpg)
Vertex Arrays
• To reduce the number of function calls
– Six sides; eight shared vertices
Step 1: Enabling arrays
Step 2: Specifying data for the arrays
Step 3: Dereferencing and rendering
![Page 23: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/23.jpg)
Step 1: Enabling Arrays
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);
glEnableClientState(GL_EDGE_FLAG_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
![Page 24: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/24.jpg)
Step 2: Specifying Data for the Arrays
• glVertexPointer(size, type, stride, pointer)
• glColorPointer(size, type, stride, pointer)
• glIndexPointer(type, stride, pointer)
• glNormalPointer(type, stride, pointer)
• glTexCoordPointer(size, type, stride, pointer)
• glEdgeFlagPointer(stride, pointer)
![Page 25: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/25.jpg)
Step 2: Specifying Data for the Arrays
• glVertexPointer(size, type, stride, ptr)
static GLfloat v[] = {
0.0,0.0,0.0, 1.0,0.0,0.0, 1.0,1.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0, 1.0,0.0,1.0, 1.0,1.0,1.0), 0.0,1.0,1.0 };
glVertexPointer(3, GL_FLOAT, 0, v);
![Page 26: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/26.jpg)
Stride
static GLfloat intertwined [ ] = {
1.0, 0.2, 1.0, 100.0, 100.0, 0.0,
/* ………………………………*/
0.2, 0.2, 1.0, 200.0, 100.0, 0.0 };
glColorPointer(3, GL_FLOAT, 6*sizeof(GLfloat), intertwined);
glVertexPointer(3, GL_FLOAT, 6*sizeof(GLfloat), &intertwined[3]);
![Page 27: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/27.jpg)
Step 3: Dereferencing and Rendering
Alternative 1:dereferencing a single array element
glVertexPointer(3, GL_FLOAT, 0, v);glBegin(GL_QUADS);
glArrayElement(4);glArrayElement(5);glArrayElement(6);glArrayElement(7);…
glEnd();
0
7
3 2
1
5
6
4
X
Y
Z
![Page 28: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/28.jpg)
Dereferencing a List of Array Elements
glVertexPointer(3, GL_FLOAT, 0, v);Static GLint allIndx[24]={4,5,6,7, 1,2,6,5, 0,1,5,4,
0,3,2,1, 0,4,5,4, 2,3,7,6};
Alternative 2:glBegin(GL_QUADS); for(int i = 0; i < 24; i++)
glArrayElement(allIndx[i]);glEnd();
Alternative 3: Better still …glDrawElements(GL_QUADS,24,GL_UNSIGNED_INT,allIndx);
![Page 29: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/29.jpg)
Hints for Building Polygonal Models of Surfaces
• Keep polygon orientations consistent.– all clockwise or all counterclockwise
• Watch out for non-triangular polygons.
• Trade-off between speed and quality.
• Avoid T- intersections
• There are more… Read the book.
![Page 30: State Management and Drawing Geometry Objects (OpenGL Book Ch 2)](https://reader036.vdocuments.us/reader036/viewer/2022081506/5681308e550346895d966ab2/html5/thumbnails/30.jpg)
Next …
Vectors, Matrices and Homogeneous coordinate system
Transformations