Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 461: Lecture 6 1 CSC461 Lecture 6: 2D Programming in OpenGL Objectives:  Fundamental OpenGL primitives  Attributes  Viewport.

Similar presentations


Presentation on theme: "CSC 461: Lecture 6 1 CSC461 Lecture 6: 2D Programming in OpenGL Objectives:  Fundamental OpenGL primitives  Attributes  Viewport."— Presentation transcript:

1 CSC 461: Lecture 6 1 CSC461 Lecture 6: 2D Programming in OpenGL Objectives:  Fundamental OpenGL primitives  Attributes  Viewport

2 CSC 461: Lecture 6 2 OpenGL Primitives GL_QUAD_STRIP GL_POLYGON GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_POINTS GL_LINES GL_LINE_LOOP GL_LINE_STRIP GL_TRIANGLES

3 CSC 461: Lecture 6 3 Polygon Issues  OpenGL will only display polygons correctly that are Simple: edges cannot cross Simple: edges cannot cross Convex: All points on line segment between two points in a polygon are also in the polygon Convex: All points on line segment between two points in a polygon are also in the polygon Flat: all vertices are in the same plane Flat: all vertices are in the same plane  User program must check if above true  Triangles satisfy all conditions nonsimple polygon nonconvex polygon

4 CSC 461: Lecture 6 4 Drawing a Sphere  Sphere description: X(θ,Ф)=sinθcosФ y(θ,Ф)=cosθcosФ z(θ,Ф)=sinФ  Approximation A set of polygons A set of polygons Efficiently using Efficiently using Quad stripsQuad strips Triangle stripsTriangle strips

5 CSC 461: Lecture 6 5 Drawing a Sphere (Cont.)  Fix θ and change Ф  get circles of constant longitude  Fix Ф and change θ  get circles of constant latitude  Generate points at fixed increments of θ and Ф  quadrilaterals  Color corresponds to increments of 20 degrees in θ and 20 degrees in Ф

6 CSC 461: Lecture 6 6 Source code: Drawing quadrilaterals c=M_PI/180; for (phi=-80.0; phi<=80.0; phi+=20.0) { glBegin(GL_QUAD_STRIP); for (theta=-180.0; theta<=180.0; theta+=20.0) { x = sin(c*theta)*cos(c.phi); y = cos(c*theta)*cos(c*phi); z = sin(c*theta); glVertex3d(x,y,x); x = sin(c*theta)*cos(c*(phi+20.0)); y = cos(c*theta)*cos(c*(phi+20.0)); z = sin(c*(phi+20.0)); glVertex3d(x,y,z);}glEnd();

7 CSC 461: Lecture 6 7 Drawing poles c=M_PI/180.0;glBegin(GL_TRANGLE_FAN);glVertex3d(x,y,z);z=sin(c*80.0); for (theta=-180.0; theta<=180.0; theta+=20) { x = sin(c*theta)*cos(c.80.0); y = cos(c*theta)*cos(c*80.0); glVertex3d(x,y,z);}glEnd()X=y=0;z=-1;glBegin(GL_TRIANGLE_FAN);glVertex3d(x,y,z);z=-sin(c*80.0); for (theta=-180.0; theta<=180.0; theta+=20) { x = sin(c*theta)*cos(c.80.0); y = cos(c*theta)*cos(c*80.0); glVertex3d(x,y,z);}glEnd();

8 CSC 461: Lecture 6 8 Attributes  Attributes are part of the OpenGL and determine the 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 mode Polygon mode Display as filled: solid color or stipple patternDisplay as filled: solid color or stipple pattern Display edgesDisplay edges

9 CSC 461: Lecture 6 9 RGB color  Each color component stored separately in the frame buffer  Usually 8 bits per component in buffer  Note in glColor3f the color values range from 0.0 (none) to 1.0 (all), while in glColor3ub the values range from 0 to 255

10 CSC 461: Lecture 6 10 Indexed Color  Colors are indices into tables of RGB values  Selecting color from table glIndexi(element)  Setting color into table: glutSetColor(int color, GLfloat r, GLfloat b, GLfloat g)  Requires less memory indices usually 8 bits indices usually 8 bits not as important now not as important now Memory inexpensiveMemory inexpensive Need more colors for shadingNeed more colors for shading

11 CSC 461: Lecture 6 11 Color and State  The color as set by glColor becomes part of the state and will be used until changed Colors and other attributes are not part of the object but are assigned when the object is rendered Colors and other attributes are not part of the object but are assigned when the object is rendered  We can create conceptual vertex colors by code such as glColor(…) glColor(…) glVertex(…) glVertex(…) glColor(…) glColor(…) glVertex(…) glVertex(…)

12 CSC 461: Lecture 6 12 Smooth Color  Default is smooth shading OpenGL interpolates vertex colors across visible polygons OpenGL interpolates vertex colors across visible polygons  Alternative is flat shading Color of first vertex Color of first vertex determines fill color  Code glShadeModel(GL_SMOOTH) glShadeModel(GL_SMOOTH) glShadeModel(GL_FLAT) glShadeModel(GL_FLAT)

13 CSC 461: Lecture 6 13 Viewports  Do not have use the entire window for the image: glViewport(x,y,w,h)  Values in pixels (screen coordinates)


Download ppt "CSC 461: Lecture 6 1 CSC461 Lecture 6: 2D Programming in OpenGL Objectives:  Fundamental OpenGL primitives  Attributes  Viewport."

Similar presentations


Ads by Google