260 likes | 648 Views
Programming in OpenGL. Ryan Holmes CSE 570 February 12 th , 2003. Overview. What does OpenGL include? What doesn’t OpenGL include? How do you get started with OpenGL? Tips and Tricks Resources Questions and Answers. OpenGL Is. A low-level 3D graphics API
E N D
Programming in OpenGL Ryan Holmes CSE 570 February 12th, 2003
Overview • What does OpenGL include? • What doesn’t OpenGL include? • How do you get started with OpenGL? • Tips and Tricks • Resources • Questions and Answers
OpenGL Is • A low-level 3D graphics API • A separate piece of code you access through an API • An interface to hardware • Primitive-based • A state machine
A low-level 3D graphics API • Separate code • Opengl32.dll on Windows • Vendors package their own version of this library with the graphics card • Windows 2000 supports a software-only version of OpenGL 1.1 out of the box
A low-level 3D graphics API • An interface to hardware • The library knows how to interface with the card drivers to get the hardware to handle the graphics. • Anything not done in hardware is done in software
A low-level 3D graphics API • Primitive-based • Objects consist of points, line-segments, and polygons • OpenGL is not aware of any connections between primitives • Exception • The GLU libraries include quadric and NURBS “objects” that encapsulate primitives for you
A state machine • Functions are global and change the state of the OpenGL environment • State can be pushed onto stacks and popped back off • OpenGL properties remain as you set them until you set them again
OpenGL Is Not • A modeling language • Compiled directly into your code • 3D object-oriented
Getting Started - Syntax • OpenGL core functions are prefixed with gl • OpenGL utility functions are prefixed with glu • OpenGL typedef defined types are prefixed with GL • OpenGL constants are all caps and prefixed with GL_
Getting Started - Example ::glBegin(GL_LINE_LOOP); ::glVertex3d(0, 0, 0); ::glVertex3d(x, 0, 0); ::glVertex3d(x, y, 0); ::glVertex3d(0, y, 0); ::glEnd(); ::glBegin(GL_LINE_LOOP); ::glVertex3d(0, 0, z); ::glVertex3d(x, 0, z); ::glVertex3d(x, y, z); ::glVertex3d(0, y, z); ::glEnd(); ::glBegin(GL_LINES); ::glVertex3d(0, 0, 0); ::glVertex3d(0, 0, z); ::glVertex3d(x, 0, 0); ::glVertex3d(x, 0, z); ::glVertex3d(x, y, 0); ::glVertex3d(x, y, z); ::glVertex3d(0, y, 0); ::glVertex3d(0, y, z); ::glEnd();
Getting Started - Interface • OpenGL has no direct interface functionality • Mouse, keyboard and window management handled by separate interface • Windows wgl (“wiggle”) functions
Environment Choice • MFC • Strong interface support (Dialogs, menus, mouse handling) • Steep learning curve • GLUT • Portable • Easier learning curve
MFC Program Organization • Document/View architecture • Document – Encapsulate Data • View – Display Data from Document • OnDraw() • Called every time the window needs to be displayed. Indirectly called by you with the Invalidate() call.
GLUT Program Organization • Callback architecture – Register functions to be called when events happen. • glutCreateWindow() – Setup • glutDisplayFunc() – Registers your “OnDraw” equivalent • glutMainLoop() – Handles redraw decisions
Creating Primitives • glBegin(type) and glEnd() • All primitives begin as vertices • GL_POINTS GL_POLYGON • GL_LINES GL_LINE_STRIP • GL_LINE_LOOP GL_QUADS • GL_QUAD_STRIP GL_TRIANGLES • GL_TRIANGLE_FAN • GL_TRIANGLE_STRIP
Vertex Data • x,y,z coordinates • Color (If lighting is disabled) • Materials (If lighting is enabled) • Normal (If lighting is enabled) • Other data (e.g. texture coordinates) • Vertex data is part of the current state!
Shading • OpenGL supports flat shading (GL_FLAT) and smooth (GL_SMOOTH) (Gouraud) shading only – glShadeModel(type) • Shading is vertex-centric, not face-centric • Lighting disabled – glColor • Lighting enabled – glMaterial and normals must be specified properly
Flat Shading Sequence // Set material with glMaterial glNormal3d(0, 0, 1.0); glBegin(GL_TRIANGLES); glVertex3d(0,0,0); glVertex3d(2, 0, 0); glVertex3d(1, 1, 0); glEnd();
Smooth Shading Sequence // Set material with glMaterial glBegin(GL_TRIANGLES); glNormal3d(0, 0, 1.0); glVertex3d(0,0,0); glNormal3d(0, 0, 1.0); glVertex3d(2, 0, 0); glNormal3d(0, 0, 1.0); glVertex3d(1, 1, 0); glEnd();
Typical Rendering Sequence • Clear the frame buffer and depth buffer – glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) • Specify all primitives in the scene • glBegin(type) • glVertex()… • glEnd() • Swap the buffers – windowing system specific
Tips and Tricks I • Plan first • Break things into pieces • Put only what is necessary into OnDraw • Choose your “default” state and initialize it during program start-up. Make sure that every function returns the state to that default before it exits
Tips and Tricks II • Read the Red Book • Appendices contain a wealth of speed-up and optimization information • Learn the debugging tools. When a graphics program doesn’t display anything, it can be difficult to figure out what’s wrong
Fundamental Principle of Graphics Programming Graphics programming is 30% understanding what data you need and 70% keeping track of where that data is and updating it. Design your data structures accordingly.
Resources I • http://www.eas.asu.edu/~cse470/ • Project Zero walk-through and skeleton code. • Links page • The “Red Book” http://biology.ncsa.uiuc.edu/library/SGI_bookshelves/SGIindex/SGI_Developer_OpenGL_PG.html
Resources II • GLUT - http://www.xmission.com/~nate/glut.html • GLUT Documentation - http://www.opengl.org/developers/documentation/glut/ • .NET Framework -http://csgl.sourceforge.net/