C / C++ Graphics Programming with OpenGL & OpenSceneGraph
description
Transcript of C / C++ Graphics Programming with OpenGL & OpenSceneGraph
C/C++ Graphics Programming IS&T Spring 2011
C / C++ Graphics Programming withC / C++ Graphics Programming with OpenGL & OpenSceneGraph OpenGL & OpenSceneGraph
Erik [email protected]
C/C++ Graphics Programming IS&T Spring 2011
C / C++ Graphics ProgrammingC / 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 OverviewTutorial 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 worldHuman view of physical world
C/C++ Graphics Programming IS&T Spring 2011
Human view of imageHuman view of image
C/C++ Graphics Programming IS&T Spring 2011
Computer graphics paradigmComputer graphics paradigm
C/C++ Graphics Programming IS&T Spring 2011
C.G. paradigm with lightC.G. paradigm with light
C/C++ Graphics Programming IS&T Spring 2011
C.G. basic calculationC.G. basic calculation
C/C++ Graphics Programming IS&T Spring 2011
Result is a digital imageResult is a digital image
C/C++ Graphics Programming IS&T Spring 2011
Resulting imageResulting image
C/C++ Graphics Programming IS&T Spring 2011
Model geometry – two cuboidsModel geometry – two cuboids
C/C++ Graphics Programming IS&T Spring 2011
Model geometry - Model geometry - approximationapproximation
C/C++ Graphics Programming IS&T Spring 2011
Polygonal modelsPolygonal models
C/C++ Graphics Programming IS&T Spring 2011
Overview: lights, cameras, …Overview: lights, cameras, …
C/C++ Graphics Programming IS&T Spring 2011
Coordinate SystemsCoordinate Systems
C/C++ Graphics Programming IS&T Spring 2011
Camera View (Frustum)Camera View (Frustum)
C/C++ Graphics Programming IS&T Spring 2011
Get examples; build one, build Get examples; build one, build allallLog 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
ExampleExample
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 spaceRGB color space
glColor3f( 0.0, 1.0, 0.0); gives green
Colorsas RGB (red, green, blue)as RGBA (red, green, blue, alpha)
Light is additive, exampleswhite = (1.0, 1.0, 1.0);yellow = (1.0, 1.0, 0.0);
C/C++ Graphics Programming IS&T Spring 2011
OpenGL functionsOpenGL 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 -Try this yourself -
% cp ex_green_tri.c play.c
(A)Make the triangle magenta (purple)solution: soln_magenta_tri.c
(B) Then make it into a rectanglesolution: soln_magenta_rect.c
(C) Then make the vertices different colorssolution: soln_multicolor_rect.c
C/C++ Graphics Programming IS&T Spring 2011
OpenGL – primitivesOpenGL – primitives
C/C++ Graphics Programming IS&T Spring 2011
OpenGL – primitive exampleOpenGL – primitive example
Let’s look at ex_quad.c
C/C++ Graphics Programming IS&T Spring 2011
Stepping back: the main loopStepping 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 int nframe = 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 2DTranslation and rotation in 2D
(B) Rotate (B) Rotate by 45 degreesby 45 degrees TranslateTranslate by (7.0, 4.0)by (7.0, 4.0)
(A) Translate(A) Translate by (5.0, 1.0)by (5.0, 1.0)
C/C++ Graphics Programming IS&T Spring 2011
Composition of transformationsComposition of transformations
Rotate 45 degrees CCW Translate (2, 0) Rotate 45 degrees CCW Translate (2, 0)
C/C++ Graphics Programming IS&T Spring 2011
Composition of transformationsComposition of transformations
Translate (2, 0) Rotate 45 degrees CCW Translate (2, 0) Rotate 45 degrees CCW
C/C++ Graphics Programming IS&T Spring 2011
CompareCompare
Rotate firstRotate firstThen translateThen translate
Translate firstTranslate firstThen rotateThen rotate
BPC: Art and Computation – Spring 2007 33
Rotations in 3DRotations in 3D
BPC: Art and Computation – Spring 2007 34
Translations in 3DTranslations in 3DSphereSphere translated bytranslated by (-3.0, 2.0, -4.5)(-3.0, 2.0, -4.5) Blue cubeBlue cube translated bytranslated by (2.0, 0.0, -4.0)(2.0, 0.0, -4.0) rotated 60 degrotated 60 deg around y-axisaround y-axis
Purple cubePurple cube translated bytranslated by (0.0, 0.0, 0.0)(0.0, 0.0, 0.0)
BPC: Art and Computation – Spring 2007 35
Camera location setupCamera location setupgluLookat(xeye, yeye, zeye, xat, yat, zat, xup, yup, zup)
BPC: Art and Computation – Spring 2007 36
Camera perspective setupCamera perspective setup
C/C++ Graphics Programming IS&T Spring 2011
Full view / transform pipeline
C/C++ Graphics Programming IS&T Spring 2011
Your turnYour 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 - scalingOther 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
BPC: Art and Computation – Spring 2007 43
Back to lightingBack to lighting
V_eye = P_eye - PV_light = P_light - P
V_eye = (4, 1, 10) - (5, 2, 3) = (-1, -1, 7)V_light = (3, 7, 2) – (5, 2, 3) = (-2, 5, -1)
BPC: Art and Computation – Spring 2007 44
Unit shading vectorsUnit shading vectors
n = surface normal vectoru_light = unit vector to light pointu_eye = unit vector to view point
BPC: Art and Computation – Spring 2007 45
ShadingShadingambient
diffuse
specular
BPC: Art and Computation – Spring 2007 46
Shading at vertices, Shading at vertices, interpolationinterpolation
BPC: Art and Computation – Spring 2007 47
Flat shading vs Gouraud Flat shading vs Gouraud shadingshading
BPC: Art and Computation – Spring 2007 48
C.G. example - texture mapC.G. example - texture map
BPC: Art and Computation – Spring 2007 49
Texture map fullTexture map full
++
C/C++ Graphics Programming IS&T Spring 2011
OpenGL Helpful MaterialsOpenGL 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
OpenSceneGraphOpenSceneGraph
Open source OpenGL based Similar to SGI Performer Many utility functions
– Notably, 3-D file readers
C/C++ Graphics Programming IS&T Spring 2011
Get examples; build allGet examples; build all
Log on to katana% cp –r /scratch/osgtut .% cd osgtut% make all
(note that after tutorial, examples will be available via the web, but not in the location above. On the web: http://scv.bu.edu/documentation/presentations/intro_to_OSG/osgtut )
C/C++ Graphics Programming IS&T Spring 2011
Setting environment variablesSetting environment variables
Instead of using “run_osg” can do this:
% setenv OSG_NOTIFY_LEVEL FATAL% setenv LD_LIBRARY_PATH \ /usr/local/OpenSceneGraph/lib % ex_cone
C/C++ Graphics Programming IS&T Spring 2011
First osg exampleFirst osg example Try running: % run_osg ex_simple_viewer cow.obj ex_simple_viewer.cpp // load the nodes from the commandline arguments. osg::Node* model = osgDB::readNodeFile(argv[1]);
// initialize the viewer and set the scene to render osgViewer::Viewer viewer; viewer.setSceneData(model); viewer.setCameraManipulator(new osgGA::TrackballManipulator());
// normal viewer usage. return viewer.run();
C/C++ Graphics Programming IS&T Spring 2011
A few improvementsA few improvements ex_simple_viewer_better.cpp // tilt the scene to give better default eye position rootnode->setMatrix(osg::Matrix::rotate (osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
// run optimization over the scene graph osgUtil::Optimizer optimzer; optimzer.optimize(rootnode);
C/C++ Graphics Programming IS&T Spring 2011
Example, making geometryExample, making geometry ex_cone.cpp // Create a vector to represent the "center of the cone" Vec3 vcen(xcen, ycen, zcen); Cone* cone = new Cone(vcen, radius, height);
// Create a drawable object based on the cone ShapeDrawable *drawable = new ShapeDrawable(cone); drawable->setColor(Vec4(color[0], color[1], color[2], color[3])); Geode* geode = new Geode(); geode->addDrawable(drawable);
C/C++ Graphics Programming IS&T Spring 2011
Example, combining geometryExample, combining geometry ex_arrow.cpp
MatrixTransform* arrow = new MatrixTransform;arrow->setMatrix(Matrix::scale(1.0, 1.0, 1.0));arrow->addChild(cone);arrow->addChild(cylinder);
C/C++ Graphics Programming IS&T Spring 2011
Your turnYour turn % cp ex_arrow.c play_long_arrow.cpp Modify to make the arrow twice as long % make play_long_arrow Solution: soln_long_arrow.cpp
C/C++ Graphics Programming IS&T Spring 2011
A bit more about scaling & transformsA bit more about scaling & transforms
C/C++ Graphics Programming IS&T Spring 2011
Solving this scaling problemSolving this scaling problem ex_vec_arrow.cppmake_vec_arrow(shaft_radius, total_length, r, g, b)
cone_radius = 2*shaft_radius; cone_height = cone_radius; shaft_length = total_length - cone_height; cylinder = make_cylinder(0.0, 0.0, shaft_length/2.0, shaft_radius, shaft_length, r, g, b, 1.0); cone = make_cone(0.0, 0.0, shaft_length + cone_height/4.0, cone_radius, cone_height, r, g, b, 1.0); vec_arrow = new Group; vec_arrow->addChild(cylinder); vec_arrow->addChild(cone);
C/C++ Graphics Programming IS&T Spring 2011
ExerciseExercise % cp ex_vec_arrow.c play_axes.cpp Modify play_axes.cpp to display three unit-length arrows
at the origin – First is RED point in the +X direction– Second is GREEN point in the +Y direction– Third is BLUE point in the +Z direction
% make play_axes.cpp Solution: soln_axes.cpp
C/C++ Graphics Programming IS&T Spring 2011
ExerciseExercise % cp ex_animated_arrow.c play_two_animated_arrows.cpp Modify play_two_animated_arrows.cpp so that the cyan
(blue-green) arrow is tangent to (pointing in) the direction of motion
% make play_two_animated_arrows.cpp Solution: soln_two_animated_arrows.cpp
C/C++ Graphics Programming IS&T Spring 2011
““Cloning”Cloning” ex_twin_arrows.cpp
transform1 = new MatrixTransform(Matrix::translate(2, 2, 0));transform1->addChild(arrow);transform2 = new MatrixTransform(Matrix::translate(-2, -2, 0));transform2->addChild(arrow);rootnode->addChild(transform1);rootnode->addChild(transform2);
C/C++ Graphics Programming IS&T Spring 2011
ExerciseExercise Modify the last example, so that you can animate the
PositionAttitudeTransform “arrow” and see what happens … Solution is left to you
C/C++ Graphics Programming IS&T Spring 2011
OpenSceneGraph resourcesOpenSceneGraph resources http://www.openscenegraph.org
– http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs/
Examples– katana: /usr/local/OpenSceneGraph/examples– http://scv.bu.edu/documentation/presentations/intro_to_OSG/
osgtut Books
– OpenSceneGraph Quick Start Guide– OpenSceneGraph Reference Guides
New book– OpenSceneGraph 3.0: Beginner's Guide
C/C++ Graphics Programming IS&T Spring 2011
SCV relatedSCV related Please fill out an online evaluation of this tutorial:
scv.bu.edu/survey/spring11tut_survey.html- or a paper one (I have copies)
System [email protected], [email protected]
Web-based tutorials www.bu.edu/tech/research/tutorials
Consultation by appointmentErik Brisson ([email protected])