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
01 / 66C / C++ Graphics Programming with OpenGL & OpenSceneGraph
1 / 17
Télécharger la présentation
C / C++ Graphics Programming with OpenGL & OpenSceneGraph
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
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
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
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
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
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
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
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
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