130 likes | 145 Views
CSC461 Lecture 6: 2D Programming in OpenGL. Objectives: Fundamental OpenGL primitives Attributes Viewport. GL_POINTS. GL_POLYGON. GL_LINE_STRIP. GL_LINES. GL_LINE_LOOP. GL_TRIANGLES. GL_QUAD_STRIP. GL_TRIANGLE_FAN. GL_TRIANGLE_STRIP. OpenGL Primitives. Polygon Issues.
E N D
CSC461 Lecture 6:2D Programming in OpenGL Objectives: • Fundamental OpenGL primitives • Attributes • Viewport CSC 461: Lecture 6
GL_POINTS GL_POLYGON GL_LINE_STRIP GL_LINES GL_LINE_LOOP GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_FAN GL_TRIANGLE_STRIP OpenGL Primitives CSC 461: Lecture 6
Polygon Issues • OpenGL will only display polygons correctly that are • Simple: edges cannot cross • 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 • User program must check if above true • Triangles satisfy all conditions nonconvex polygon nonsimple polygon CSC 461: Lecture 6
Drawing a Sphere • Sphere description: X(θ,Ф)=sinθcosФ y(θ,Ф)=cosθcosФ z(θ,Ф)=sinФ • Approximation • A set of polygons • Efficiently using • Quad strips • Triangle strips CSC 461: Lecture 6
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 Ф CSC 461: Lecture 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(); CSC 461: Lecture 6
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(); CSC 461: Lecture 6
Attributes • Attributes are part of the OpenGL and determine the appearance of objects • Color (points, lines, polygons) • Size and width (points, lines) • Stipple pattern (lines, polygons) • Polygon mode • Display as filled: solid color or stipple pattern • Display edges CSC 461: Lecture 6
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 CSC 461: Lecture 6
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 • not as important now • Memory inexpensive • Need more colors for shading CSC 461: Lecture 6
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 • We can create conceptual vertex colors by code such as glColor(…) glVertex(…) glColor(…) glVertex(…) CSC 461: Lecture 6
Smooth Color • Default is smooth shading • OpenGL interpolates vertex colors across visible polygons • Alternative is flat shading • Color of first vertex determines fill color • Code • glShadeModel(GL_SMOOTH) • glShadeModel(GL_FLAT) CSC 461: Lecture 6
Viewports • Do not have use the entire window for the image: glViewport(x,y,w,h) • Values in pixels (screen coordinates) CSC 461: Lecture 6