670 likes | 1.1k Views
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
E N D
C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu C/C++ Graphics Programming IS&T Spring 2011
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
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
Human view of physical world C/C++ Graphics Programming IS&T Spring 2011
Human view of image C/C++ Graphics Programming IS&T Spring 2011
Computer graphics paradigm C/C++ Graphics Programming IS&T Spring 2011
C.G. paradigm with light C/C++ Graphics Programming IS&T Spring 2011
C.G. basic calculation C/C++ Graphics Programming IS&T Spring 2011
Result is a digital image C/C++ Graphics Programming IS&T Spring 2011
Resulting image C/C++ Graphics Programming IS&T Spring 2011
Model geometry – two cuboids C/C++ Graphics Programming IS&T Spring 2011
Model geometry - approximation C/C++ Graphics Programming IS&T Spring 2011
Polygonal models C/C++ Graphics Programming IS&T Spring 2011
Overview: lights, cameras, … C/C++ Graphics Programming IS&T Spring 2011
Coordinate Systems C/C++ Graphics Programming IS&T Spring 2011
Camera View (Frustum) C/C++ Graphics Programming IS&T Spring 2011
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
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
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
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
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
OpenGL – primitives C/C++ Graphics Programming IS&T Spring 2011
OpenGL – primitive example Let’s look at ex_quad.c C/C++ Graphics Programming IS&T Spring 2011
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
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
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
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
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
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
Composition of transformations Rotate 45 degrees CCW Translate (2, 0) C/C++ Graphics Programming IS&T Spring 2011
Composition of transformations Translate (2, 0) Rotate 45 degrees CCW C/C++ Graphics Programming IS&T Spring 2011
Compare Rotate first Then translate Translate first Then rotate C/C++ Graphics Programming IS&T Spring 2011
Rotations in 3D BPC: Art and Computation – Spring 2007
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
Camera location setup gluLookat(xeye, yeye, zeye, xat, yat, zat, xup, yup, zup) BPC: Art and Computation – Spring 2007
Camera perspective setup BPC: Art and Computation – Spring 2007
Full view / transform pipeline C/C++ Graphics Programming IS&T Spring 2011
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
Other transformations - scaling C/C++ Graphics Programming IS&T Spring 2011
Multiple transformations • Example in ex_multi_transform.c C/C++ Graphics Programming IS&T Spring 2011
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
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
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
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
Shading ambient diffuse specular BPC: Art and Computation – Spring 2007
Shading at vertices, interpolation BPC: Art and Computation – Spring 2007
Flat shading vs Gouraud shading BPC: Art and Computation – Spring 2007
C.G. example - texture map BPC: Art and Computation – Spring 2007
Texture map full + BPC: Art and Computation – Spring 2007
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