1 / 66

C / C++ Graphics Programming with OpenGL & OpenSceneGraph

C / C++ Graphics Programming with OpenGL & OpenSceneGraph. Erik Brisson ebrisson@bu.edu. C / C++ Graphics Programming. OpenGL Program from scratch Access to all graphics card features Available on virtually all platforms OpenSceneGraph Higher level, built on OpenGL

kitra
Télécharger la présentation

C / C++ Graphics Programming with OpenGL & OpenSceneGraph

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. C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu C/C++ Graphics Programming IS&T Spring 2011

  2. C / C++ Graphics Programming • OpenGL • Program from scratch • Access to all graphics card features • Available on virtually all platforms • OpenSceneGraph • Higher level, built on OpenGL • Program using scene graph paradigm • Lots of utility functions C/C++ Graphics Programming IS&T Spring 2011

  3. Tutorial Overview • C.G. Paradigm – models, lighting, transforms, etc. • OpenGL • Overview of OpenGL • Window/events (freeglut) • Hands-on • OpenSceneGraph • Overview • Hands-on C/C++ Graphics Programming IS&T Spring 2011

  4. Human view of physical world C/C++ Graphics Programming IS&T Spring 2011

  5. Human view of image C/C++ Graphics Programming IS&T Spring 2011

  6. Computer graphics paradigm C/C++ Graphics Programming IS&T Spring 2011

  7. C.G. paradigm with light C/C++ Graphics Programming IS&T Spring 2011

  8. C.G. basic calculation C/C++ Graphics Programming IS&T Spring 2011

  9. Result is a digital image C/C++ Graphics Programming IS&T Spring 2011

  10. Resulting image C/C++ Graphics Programming IS&T Spring 2011

  11. Model geometry – two cuboids C/C++ Graphics Programming IS&T Spring 2011

  12. Model geometry - approximation C/C++ Graphics Programming IS&T Spring 2011

  13. Polygonal models C/C++ Graphics Programming IS&T Spring 2011

  14. Overview: lights, cameras, … C/C++ Graphics Programming IS&T Spring 2011

  15. Coordinate Systems C/C++ Graphics Programming IS&T Spring 2011

  16. Camera View (Frustum) C/C++ Graphics Programming IS&T Spring 2011

  17. Get examples; build one, build all Log on to katana % cp –r /scratch/ogltut . % cd ogltut % make ex_green_tri % ./ex_green_tri % make all (note that after tutorial, examples will be available via the web, but not in the location above. Go to http://scv.bu.edu/documentation/presentations/intro_to_OpenGL/ogltut) C/C++ Graphics Programming IS&T Spring 2011

  18. Example In ex_green_tri.c void mydraw(void) { glColor3f( 0.0, 1.0, 0.0); glBegin(GL_POLYGON); glVertex3f( 0.0, 0.0, -5.0); glVertex3f( 1.0, 0.0, -5.0); glVertex3f( 1.0, 2.0, -5.0); glEnd(); } C/C++ Graphics Programming IS&T Spring 2011

  19. RGB color space glColor3f( 0.0, 1.0, 0.0); gives green Colors as RGB (red, green, blue) as RGBA (red, green, blue, alpha) Light is additive, examples white = (1.0, 1.0, 1.0); yellow = (1.0, 1.0, 0.0); C/C++ Graphics Programming IS&T Spring 2011

  20. OpenGL functions % man glColor void glColor3d( GLdouble red, GLdouble green, GLdouble blue ) void glColor3s( GLshort red, GLshort green, GLshort blue ) void glColor3ub( GLubyte red, GLubyte green, GLubyte blue ) void glColor4d( GLdouble red, GLdouble green, GLdouble blue, GLdouble alpha ) C/C++ Graphics Programming IS&T Spring 2011

  21. Try this yourself - % cpex_green_tri.cplay.c • Make the triangle magenta (purple) • solution: soln_magenta_tri.c • (B) Then make it into a rectangle • solution: soln_magenta_rect.c • (C) Then make the vertices different colors • solution: soln_multicolor_rect.c C/C++ Graphics Programming IS&T Spring 2011

  22. OpenGL – primitives C/C++ Graphics Programming IS&T Spring 2011

  23. OpenGL – primitive example Let’s look at ex_quad.c C/C++ Graphics Programming IS&T Spring 2011

  24. Stepping back: the main loop int main(int argc, char **argv) { int i; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("ex_quad"); glutDisplayFunc(display); init(); glutMainLoop(); return 0; } C/C++ Graphics Programming IS&T Spring 2011

  25. GLUT - The OpenGL Utility Toolkit • Actually, we are using freeglut, a completely OpenSourced alternative to the OpenGL Utility Toolkit (GLUT • Allows creation and management of windows containing OpenGL contexts on a wide range of platforms • Provides event management: read mouse, keyboard and joystick functions. • An 'idle' routine and timers • A simple, cascading pop-up menu facility • Utility routines to generate various solid and wire frame objects • Support for bitmap and stroke fonts • Miscellaneous window management functions • Freeglut does not require glutMainLoop() ! C/C++ Graphics Programming IS&T Spring 2011

  26. Example of changes in display() • Run ex_quad_motion void mydraw(void) { static intnframe = 0; float xoffset; xoffset = sin((double)nframe/240.0); drawstuff(xoffset); nframe++; } C/C++ Graphics Programming IS&T Spring 2011

  27. Better way: OpenGL transformations void mydraw(void) [ex_translate.c] { static float time = 0.0; float xoffset; time = glutGet(GLUT_ELAPSED_TIME) / 1000.0; xoffset = sin((double)(M_PI*time)); glPushMatrix(); glTranslatef(xoffset, 0.0, 0.0); drawstuff(); glPopMatrix(); glutPostRedisplay(); } C/C++ Graphics Programming IS&T Spring 2011

  28. Rotation using OpenGL transform void mydraw(void) [ex_rotate.c] { static float time = 0.0; float xoffset; time = glutGet(GLUT_ELAPSED_TIME) / 1000.0; xoffset = sin((double)(M_PI*time)); glPushMatrix(); glTranslatef(xoffset, 0.0, 0.0); drawstuff(); glPopMatrix(); glutPostRedisplay(); } C/C++ Graphics Programming IS&T Spring 2011

  29. Translation and rotation in 2D (B) Rotate by 45 degrees Translate by (7.0, 4.0) (A) Translate by (5.0, 1.0) C/C++ Graphics Programming IS&T Spring 2011

  30. Composition of transformations Rotate 45 degrees CCW Translate (2, 0) C/C++ Graphics Programming IS&T Spring 2011

  31. Composition of transformations Translate (2, 0) Rotate 45 degrees CCW C/C++ Graphics Programming IS&T Spring 2011

  32. Compare Rotate first Then translate Translate first Then rotate C/C++ Graphics Programming IS&T Spring 2011

  33. Rotations in 3D BPC: Art and Computation – Spring 2007

  34. Translations in 3D Sphere translated by (-3.0, 2.0, -4.5) Blue cube translated by (2.0, 0.0, -4.0) rotated 60 deg around y-axis Purple cube translated by (0.0, 0.0, 0.0) BPC: Art and Computation – Spring 2007

  35. Camera location setup gluLookat(xeye, yeye, zeye, xat, yat, zat, xup, yup, zup) BPC: Art and Computation – Spring 2007

  36. Camera perspective setup BPC: Art and Computation – Spring 2007

  37. Full view / transform pipeline C/C++ Graphics Programming IS&T Spring 2011

  38. Your turn • Modify ex_lookat.c so that you can so the sphere through the hole in the torus. • Now change it so that “z” is up. C/C++ Graphics Programming IS&T Spring 2011

  39. Other transformations - scaling C/C++ Graphics Programming IS&T Spring 2011

  40. Multiple transformations • Example in ex_multi_transform.c C/C++ Graphics Programming IS&T Spring 2011

  41. Transform Exercise • Modify ex_multi_transform.c so that there are bouncing tetrahedra on all four corners of the platform. • Solution: soln_four_bounce.c C/C++ Graphics Programming IS&T Spring 2011

  42. Transform Challenge • Modify ex_rotate.c so strips rotate about the vertical axis going through x=2.0 and z = 0.0 • Solution: soln_rotate_challenge.c C/C++ Graphics Programming IS&T Spring 2011

  43. Back to lighting V_eye = (4, 1, 10) - (5, 2, 3) = (-1, -1, 7) V_light = (3, 7, 2) – (5, 2, 3) = (-2, 5, -1) V_eye = P_eye - P V_light = P_light - P BPC: Art and Computation – Spring 2007

  44. Unit shading vectors n = surface normal vector u_light = unit vector to light point u_eye = unit vector to view point BPC: Art and Computation – Spring 2007

  45. Shading ambient diffuse specular BPC: Art and Computation – Spring 2007

  46. Shading at vertices, interpolation BPC: Art and Computation – Spring 2007

  47. Flat shading vs Gouraud shading BPC: Art and Computation – Spring 2007

  48. C.G. example - texture map BPC: Art and Computation – Spring 2007

  49. Texture map full +  BPC: Art and Computation – Spring 2007

  50. OpenGL Helpful Materials • http://www.opengl.org • http://www.freeglut.org • Examples from this tutorial • http://scv.bu.edu/documentation/presentations/intro_to_OpenGL/ogltut • Examples from opengl.org • http://scv.bu.edu/documentation/presentations/intro_to_OpenGL/opengl.org_examples/ • Many books on OpenGL C/C++ Graphics Programming IS&T Spring 2011

More Related