1 / 26

Programming in OpenGL

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

biana
Download Presentation

Programming in OpenGL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in OpenGL Ryan Holmes CSE 570 February 12th, 2003

  2. Overview • What does OpenGL include? • What doesn’t OpenGL include? • How do you get started with OpenGL? • Tips and Tricks • Resources • Questions and Answers

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. OpenGL Is Not • A modeling language • Compiled directly into your code • 3D object-oriented

  9. 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_

  10. 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();

  11. Getting Started - Interface • OpenGL has no direct interface functionality • Mouse, keyboard and window management handled by separate interface • Windows wgl (“wiggle”) functions

  12. Environment Choice • MFC • Strong interface support (Dialogs, menus, mouse handling) • Steep learning curve • GLUT • Portable • Easier learning curve

  13. 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.

  14. GLUT Program Organization • Callback architecture – Register functions to be called when events happen. • glutCreateWindow() – Setup • glutDisplayFunc() – Registers your “OnDraw” equivalent • glutMainLoop() – Handles redraw decisions

  15. 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

  16. 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!

  17. 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

  18. 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();

  19. 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();

  20. 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

  21. 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

  22. 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

  23. 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.

  24. 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

  25. 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/

  26. Questions and Answers

More Related