CSC 461: Lecture 6CSC 461: Lecture 6 11
CSC461 Lecture 6:
2D Programming in OpenGL
Objectives:Objectives:
Fundamental OpenGL primitivesFundamental OpenGL primitives AttributesAttributes ViewportViewport
CSC 461: Lecture 6CSC 461: Lecture 6 22
OpenGL PrimitivesOpenGL Primitives
GL_QUAD_STRIPGL_QUAD_STRIP
GL_POLYGONGL_POLYGON
GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP GL_TRIANGLE_FANGL_TRIANGLE_FAN
GL_POINTSGL_POINTS
GL_LINESGL_LINES
GL_LINE_LOOPGL_LINE_LOOP
GL_LINE_STRIPGL_LINE_STRIP
GL_TRIANGLESGL_TRIANGLES
CSC 461: Lecture 6CSC 461: Lecture 6 33
Polygon IssuesPolygon IssuesOpenGL will only display polygons correctly that areOpenGL will only display polygons correctly that are
SimpleSimple: edges cannot cross: edges cannot crossConvexConvex: All points on line segment between two : All points on line segment between two
points in a polygon are also in the polygonpoints in a polygon are also in the polygonFlatFlat: all vertices are in the same plane: all vertices are in the same plane
User program must check if above trueUser program must check if above trueTriangles satisfy all conditionsTriangles satisfy all conditions
nonsimple polygon nonconvex polygon
CSC 461: Lecture 6CSC 461: Lecture 6 44
Drawing a SphereDrawing a Sphere
Sphere description:Sphere description:X(X(θθ,,ФФ)=sin)=sinθθcoscosФФy(y(θθ,,ФФ)=cos)=cosθθcoscosФФz(z(θθ,,ФФ)=sin)=sinФФ
ApproximationApproximation A set of polygonsA set of polygons Efficiently usingEfficiently using
• Quad stripsQuad strips• Triangle stripsTriangle strips
CSC 461: Lecture 6CSC 461: Lecture 6 55
Drawing a Sphere Drawing a Sphere (Cont.)(Cont.)
Fix Fix θθ and change and change ФФ get circles of get circles of constant longitudeconstant longitude
Fix Fix ФФ and change and change θθ get circles of get circles of constant latitudeconstant latitude
Generate points at fixed increments of Generate points at fixed increments of θθ and and ФФ quadrilaterals quadrilaterals
Color corresponds to increments of 20 Color corresponds to increments of 20 degrees in degrees in θθ and 20 degrees in and 20 degrees in ФФ
CSC 461: Lecture 6CSC 461: Lecture 6 66
Source code: Drawing quadrilateralsSource code: Drawing quadrilateralsc=M_PI/180;c=M_PI/180;for (phi=-80.0; phi<=80.0; phi+=20.0) {for (phi=-80.0; phi<=80.0; phi+=20.0) {
glBegin(GL_QUAD_STRIP);glBegin(GL_QUAD_STRIP);for (theta=-180.0; theta<=180.0; theta+=20.0) {for (theta=-180.0; theta<=180.0; theta+=20.0) {
x = sin(c*theta)*cos(c.phi);x = sin(c*theta)*cos(c.phi);y = cos(c*theta)*cos(c*phi);y = cos(c*theta)*cos(c*phi);z = sin(c*theta);z = sin(c*theta);glVertex3d(x,y,x);glVertex3d(x,y,x);
x = sin(c*theta)*cos(c*(phi+20.0));x = sin(c*theta)*cos(c*(phi+20.0));y = cos(c*theta)*cos(c*(phi+20.0));y = cos(c*theta)*cos(c*(phi+20.0));z = sin(c*(phi+20.0));z = sin(c*(phi+20.0));glVertex3d(x,y,z);glVertex3d(x,y,z);
}}glEnd();glEnd();
CSC 461: Lecture 6CSC 461: Lecture 6 77
Drawing Drawing polespoles
c=M_PI/180.0;c=M_PI/180.0;glBegin(GL_TRANGLE_FAN);glBegin(GL_TRANGLE_FAN);
glVertex3d(x,y,z);glVertex3d(x,y,z);z=sin(c*80.0);z=sin(c*80.0);for (theta=-180.0; theta<=180.0; theta+=20) {for (theta=-180.0; theta<=180.0; theta+=20) {
x = sin(c*theta)*cos(c.80.0);x = sin(c*theta)*cos(c.80.0);y = cos(c*theta)*cos(c*80.0);y = cos(c*theta)*cos(c*80.0);glVertex3d(x,y,z);glVertex3d(x,y,z);
}}
glEnd()glEnd()X=y=0;X=y=0;z=-1;z=-1;glBegin(GL_TRIANGLE_FAN);glBegin(GL_TRIANGLE_FAN);
glVertex3d(x,y,z);glVertex3d(x,y,z);z=-sin(c*80.0);z=-sin(c*80.0);for (theta=-180.0; theta<=180.0; theta+=20) {for (theta=-180.0; theta<=180.0; theta+=20) {
x = sin(c*theta)*cos(c.80.0);x = sin(c*theta)*cos(c.80.0);y = cos(c*theta)*cos(c*80.0);y = cos(c*theta)*cos(c*80.0);glVertex3d(x,y,z);glVertex3d(x,y,z);
}}
glEnd();glEnd();
CSC 461: Lecture 6CSC 461: Lecture 6 88
AttributesAttributes
Attributes are part of the OpenGL and determine Attributes are part of the OpenGL and determine the appearance of objectsthe appearance of objects
Color (points, lines, polygons)Color (points, lines, polygons) Size and width (points, lines)Size and width (points, lines) Stipple pattern (lines, polygons)Stipple pattern (lines, polygons) Polygon modePolygon mode
• Display as filled: solid color or stipple patternDisplay as filled: solid color or stipple pattern• Display edgesDisplay edges
CSC 461: Lecture 6CSC 461: Lecture 6 99
RGB colorRGB colorEach color component stored separately in Each color component stored separately in the frame bufferthe frame buffer
Usually 8 bits per component in bufferUsually 8 bits per component in bufferNote in Note in glColor3fglColor3f the color values range the color values range from 0.0 (none) to 1.0 (all), while in from 0.0 (none) to 1.0 (all), while in glColor3ubglColor3ub the values range from 0 to 255 the values range from 0 to 255
CSC 461: Lecture 6CSC 461: Lecture 6 1010
Indexed ColorIndexed ColorColors are indices into tables of RGB valuesColors are indices into tables of RGB valuesSelecting color from tableSelecting color from table
glIndexi(glIndexi(elementelement))Setting color into table: Setting color into table:
glutSetColor(int color, GLfloat r, GLfloat b, GLfloat g)glutSetColor(int color, GLfloat r, GLfloat b, GLfloat g)Requires less memoryRequires less memory
indices usually 8 bitsindices usually 8 bits not as important nownot as important now
• Memory inexpensiveMemory inexpensive• Need more colors for shadingNeed more colors for shading
CSC 461: Lecture 6CSC 461: Lecture 6 1111
Color and StateColor and StateThe color as set by The color as set by glColorglColor becomes part of becomes part of
the state and will be used until changedthe state and will be used until changedColors and other attributes are not part of the Colors and other attributes are not part of the
object but are assigned when the object is object but are assigned when the object is renderedrendered
We can create conceptual We can create conceptual vertexvertex colors colors by code by code such assuch as
glColor(…)glColor(…) glVertex(…)glVertex(…) glColor(…)glColor(…) glVertex(…)glVertex(…)
CSC 461: Lecture 6CSC 461: Lecture 6 1212
Smooth ColorSmooth ColorDefault is Default is smoothsmooth shading shading
OpenGL interpolates vertex colors across visible OpenGL interpolates vertex colors across visible polygonspolygons
Alternative is Alternative is flat shadingflat shadingColor of first vertex Color of first vertex
determines fill colordetermines fill colorCodeCode
glShadeModel(GL_SMOOTH)glShadeModel(GL_SMOOTH) glShadeModel(GL_FLAT)glShadeModel(GL_FLAT)
CSC 461: Lecture 6CSC 461: Lecture 6 1313
ViewportsViewportsDo not have use the entire window for the Do not have use the entire window for the image: image: glViewport(x,y,w,h)glViewport(x,y,w,h)
Values in pixels (screen coordinates)Values in pixels (screen coordinates)
Top Related