C / C++ Graphics Programming with OpenGL & OpenSceneGraph

66
C/C++ Graphics Programming IS&T Spring 2011 C / C++ Graphics C / C++ Graphics Programming with Programming with OpenGL & OpenGL & OpenSceneGraph OpenSceneGraph Erik Brisson [email protected]

description

C / C++ Graphics Programming with OpenGL & OpenSceneGraph. Erik Brisson [email protected]. 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 - PowerPoint PPT Presentation

Transcript of C / C++ Graphics Programming with OpenGL & OpenSceneGraph

Page 1: 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]

Page 2: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 3: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 4: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Human view of physical worldHuman view of physical world

Page 5: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Human view of imageHuman view of image

Page 6: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Computer graphics paradigmComputer graphics paradigm

Page 7: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

C.G. paradigm with lightC.G. paradigm with light

Page 8: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

C.G. basic calculationC.G. basic calculation

Page 9: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Result is a digital imageResult is a digital image

Page 10: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Resulting imageResulting image

Page 11: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Model geometry – two cuboidsModel geometry – two cuboids

Page 12: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Model geometry - Model geometry - approximationapproximation

Page 13: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Polygonal modelsPolygonal models

Page 14: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Overview: lights, cameras, …Overview: lights, cameras, …

Page 15: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Coordinate SystemsCoordinate Systems

Page 16: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Camera View (Frustum)Camera View (Frustum)

Page 17: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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 )

Page 18: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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();}

Page 19: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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);

Page 20: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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 )

Page 21: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 22: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

OpenGL – primitivesOpenGL – primitives

Page 23: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

OpenGL – primitive exampleOpenGL – primitive example

Let’s look at ex_quad.c

Page 24: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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;}

Page 25: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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() !

Page 26: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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++;}

Page 27: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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();}

Page 28: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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();}

Page 29: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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)

Page 30: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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)

Page 31: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 32: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

CompareCompare

Rotate firstRotate firstThen translateThen translate

Translate firstTranslate firstThen rotateThen rotate

Page 33: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

BPC: Art and Computation – Spring 2007 33

Rotations in 3DRotations in 3D

Page 34: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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)

Page 35: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

BPC: Art and Computation – Spring 2007 35

Camera location setupCamera location setupgluLookat(xeye, yeye, zeye, xat, yat, zat, xup, yup, zup)

Page 36: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

BPC: Art and Computation – Spring 2007 36

Camera perspective setupCamera perspective setup

Page 37: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Full view / transform pipeline

Page 38: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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.

Page 39: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Other transformations - scalingOther transformations - scaling

Page 40: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

Multiple transformations Example in ex_multi_transform.c

Page 41: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 42: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 43: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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)

Page 44: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 45: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

BPC: Art and Computation – Spring 2007 45

ShadingShadingambient

diffuse

specular

Page 46: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

BPC: Art and Computation – Spring 2007 46

Shading at vertices, Shading at vertices, interpolationinterpolation

Page 47: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

BPC: Art and Computation – Spring 2007 47

Flat shading vs Gouraud Flat shading vs Gouraud shadingshading

Page 48: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

BPC: Art and Computation – Spring 2007 48

C.G. example - texture mapC.G. example - texture map

Page 49: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

BPC: Art and Computation – Spring 2007 49

Texture map fullTexture map full

++

Page 50: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 51: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 52: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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 )

Page 53: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 54: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 55: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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);

Page 56: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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);

Page 57: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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);

Page 58: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 59: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

C/C++ Graphics Programming IS&T Spring 2011

A bit more about scaling & transformsA bit more about scaling & transforms

Page 60: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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);

Page 61: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 62: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 63: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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);

Page 64: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 65: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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

Page 66: C / C++ Graphics  Programming with      OpenGL &  OpenSceneGraph

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])