UNM CS 433 - Programming with OpenGL Part 2- Complete Programs

Unformatted text preview:

1Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Programming with OpenGLPart 2: Complete ProgramsEd AngelProfessor of Computer Science,Electrical and ComputerEngineering, and Media ArtsUniversity of New Mexico2Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Objectives• Refine the first program- Alter the default values- Introduce a standard program structure• Simple viewing- Two-dimensional viewing as a special case ofthree-dimensional viewing• Fundamental OpenGL primitives• Attributes3Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Program Structure• Most OpenGL programs have a similarstructure that consists of the following functions- main():• defines the callback functions• opens one or more windows with the required properties• enters event loop (last executable statement)- init(): sets the state variables• Viewing• Attributes- callbacks• Display function• Input and window functions4Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005simple.c revisited• In this version, we shall see the sameoutput but we have defined all therelevant state values through functioncalls using the default values• In particular, we set- Colors- Viewing conditions- Window properties5Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005main.c#include <GL/glut.h>int main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500); glutInitWindowPosition(0,0);glutCreateWindow("simple");glutDisplayFunc(mydisplay);init();glutMainLoop();}includes gl.hdefine window propertiesset OpenGL stateenter event loopdisplay callback6Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005GLUT functions•glutInit allows application to get command linearguments and initializes system•gluInitDisplayMode requests properties for thewindow (the rendering context)- RGB color- Single buffering- Properties logically ORed together•glutWindowSize in pixels•glutWindowPosition from top-left corner of display•glutCreateWindow create window with title “simple”•glutDisplayFunc display callback•glutMainLoop enter infinite event loop7Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005init.cvoid init(){glClearColor (0.0, 0.0, 0.0, 1.0);glColor3f(1.0, 1.0, 1.0);glMatrixMode (GL_PROJECTION);glLoadIdentity ();glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);}black clear coloropaque windowfill/draw with whiteviewing volume8Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Coordinate Systems• The units in glVertex are determined by theapplication and are called object or problemcoordinates• The viewing specifications are also in objectcoordinates and it is the size of the viewingvolume that determines what will appear in theimage• Internally, OpenGL will convert to camera (eye)coordinates and later to screen coordinates• OpenGL also uses some internal representationsthat usually are not visible to the application9Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005OpenGL Camera• OpenGL places a camera at the origin inobject space pointing in the negative zdirection• The default viewing volume is a box centered at the origin with a side of length 210Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Orthographic Viewingz=0z=0In the default orthographic view, points are projected forward along the z axis onto theplane z=011Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Transformations and Viewing• In OpenGL, projection is carried out by aprojection matrix (transformation)• There is only one set of transformationfunctions so we must set the matrix mode first glMatrixMode (GL_PROJECTION)• Transformation functions are incremental sowe start with an identity matrix and alter it with aprojection matrix that gives the view volume glLoadIdentity();glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);12Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Two- and three-dimensional viewing• In glOrtho(left, right, bottom, top,near, far) the near and far distances aremeasured from the camera• Two-dimensional vertex commands place all verticesin the plane z=0• If the application is in two dimensions, we can usethe function gluOrtho2D(left, right,bottom,top)• In two dimensions, the view or clipping volumebecomes a clipping window13Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005mydisplay.cvoid mydisplay(){glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_POLYGON);glVertex2f(-0.5, -0.5);glVertex2f(-0.5, 0.5);glVertex2f(0.5, 0.5);glVertex2f(0.5, -0.5);glEnd();glFlush();}14Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005OpenGL PrimitivesGL_QUAD_STRIPGL_QUAD_STRIPGL_POLYGONGL_POLYGONGL_TRIANGLE_STRIPGL_TRIANGLE_STRIPGL_TRIANGLE_FANGL_TRIANGLE_FANGL_POINTSGL_POINTSGL_LINESGL_LINESGL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIPGL_TRIANGLESGL_TRIANGLES15Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Polygon Issues• OpenGL will only display polygons correctly that are- Simple: edges cannot cross- Convex: All points on line segment between two points in apolygon are also in the polygon- Flat: all vertices are in the same plane• User program can check if above true- OpenGL will produce output if these conditions are violatedbut it may not be what is desired• Triangles satisfy all conditionsnonsimple polygonnonconvex polygon16Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Attributes• Attributes are part of the OpenGL stateand 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• Display vertices17Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005RGB color• Each color component is stored separately inthe frame buffer• Usually 8 bits per component in buffer• Note in glColor3f the color values range from0.0 (none) to 1.0 (all), whereas in glColor3ubthe values range from 0 to 25518Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Indexed Color• Colors are indices into tables of RGB values• Requires less memory- indices usually 8 bits- not as important now• Memory inexpensive• Need more colors for shading19Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Color and State• The color as set by glColor becomes part ofthe state and will be used until


View Full Document

UNM CS 433 - Programming with OpenGL Part 2- Complete Programs

Download Programming with OpenGL Part 2- Complete Programs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming with OpenGL Part 2- Complete Programs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming with OpenGL Part 2- Complete Programs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?