Presentation is loading. Please wait.

Presentation is loading. Please wait.

C / C++ Graphics Programming with OpenGL & OpenSceneGraph

Similar presentations


Presentation on theme: "C / C++ Graphics Programming with OpenGL & OpenSceneGraph"— Presentation transcript:

1 C / C++ Graphics Programming with OpenGL & OpenSceneGraph
Erik Brisson 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 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 C/C++ Graphics Programming IS&T Spring 2011

4 Human view of physical world
We will encounter waves in many contexts in nature. Energy and matter closely related (deep physics) Frequency and amplitude Waves hit objects and reflected/bounced in complex ways C/C++ Graphics Programming IS&T Spring 2011

5 C/C++ Graphics Programming IS&T Spring 2011
Human view of image We will encounter waves in many contexts in nature. Energy and matter closely related (deep physics) Frequency and amplitude Waves hit objects and reflected/bounced in complex ways C/C++ Graphics Programming IS&T Spring 2011

6 Computer graphics paradigm
When can change when it passes through materials, e.g. colored filters Plane = flat sheet/surface C/C++ Graphics Programming IS&T Spring 2011

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

8 C/C++ Graphics Programming IS&T Spring 2011
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 C/C++ Graphics Programming IS&T Spring 2011
Resulting image How we see in 3D Multiple depth cues: Lighting/shading, shadows, reflections, movement Stereopsis from two eyes (binocular vision) is fundamental. Principles of stereopsis know to Euclid (~300 BC) Demo with Rubik’s cube C/C++ Graphics Programming IS&T Spring 2011

11 Model geometry – two cuboids
How we see in 3D Multiple depth cues: Lighting/shading, shadows, reflections, movement Stereopsis from two eyes (binocular vision) is fundamental. Principles of stereopsis know to Euclid (~300 BC) Demo with Rubik’s cube C/C++ Graphics Programming IS&T Spring 2011

12 Model geometry - approximation
How we see in 3D Multiple depth cues: Lighting/shading, shadows, reflections, movement Stereopsis from two eyes (binocular vision) is fundamental. Principles of stereopsis know to Euclid (~300 BC) Demo with Rubik’s cube C/C++ Graphics Programming IS&T Spring 2011

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

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

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

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

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 ) C/C++ Graphics Programming IS&T Spring 2011

18 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 18

19 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 19

20 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 blue, GLdouble alpha ) C/C++ Graphics Programming IS&T Spring 2011 20

21 C/C++ Graphics Programming IS&T Spring 2011
Try this yourself - % cp ex_green_tri.c play.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 21

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

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

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 24

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 int nframe = 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) / ; xoffset = sin((double)(M_PI*time)); glPushMatrix(); glTranslatef(xoffset, 0.0, 0.0); drawstuff(); glPopMatrix(); glutPostRedisplay(); } C/C++ Graphics Programming IS&T Spring 2011 27

28 Rotation using OpenGL transform
void mydraw(void) [ex_rotate.c] { static float time = 0.0; float xoffset; time = glutGet(GLUT_ELAPSED_TIME) / ; 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

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

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

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

35 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

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

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

38 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

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

42 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

43 BPC: Art and Computation – Spring 2007
Back to lighting We will encounter waves in many contexts in nature. Energy and matter closely related (deep physics) Frequency and amplitude Waves hit objects and reflected/bounced in complex ways 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 BPC: Art and Computation – Spring 2007
Unit shading vectors We will encounter waves in many contexts in nature. Energy and matter closely related (deep physics) Frequency and amplitude Waves hit objects and reflected/bounced in complex ways 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 BPC: Art and Computation – Spring 2007
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
How we see in 3D Multiple depth cues: Lighting/shading, shadows, reflections, movement Stereopsis from two eyes (binocular vision) is fundamental. Principles of stereopsis know to Euclid (~300 BC) Demo with Rubik’s cube BPC: Art and Computation – Spring 2007

49 BPC: Art and Computation – Spring 2007
Texture map full + How we see in 3D Multiple depth cues: Lighting/shading, shadows, reflections, movement Stereopsis from two eyes (binocular vision) is fundamental. Principles of stereopsis know to Euclid (~300 BC) Demo with Rubik’s cube BPC: Art and Computation – Spring 2007

50 OpenGL Helpful Materials
Examples from this tutorial Examples from opengl.org Many books on OpenGL C/C++ Graphics Programming IS&T Spring 2011

51 C/C++ Graphics Programming IS&T Spring 2011
OpenSceneGraph Open source OpenGL based Similar to SGI Performer Many utility functions Notably, 3-D file readers C/C++ Graphics Programming IS&T Spring 2011

52 Get 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: ) C/C++ Graphics Programming IS&T Spring 2011

53 Setting 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

54 C/C++ Graphics Programming IS&T Spring 2011
First 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

55 C/C++ Graphics Programming IS&T Spring 2011
A 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

56 Example, 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

57 Example, 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

58 C/C++ Graphics Programming IS&T Spring 2011
Your 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

59 A bit more about scaling & transforms
C/C++ Graphics Programming IS&T Spring 2011

60 Solving this scaling problem
ex_vec_arrow.cpp make_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

61 C/C++ Graphics Programming IS&T Spring 2011
Exercise % 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

62 C/C++ Graphics Programming IS&T Spring 2011
Exercise % 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

63 C/C++ Graphics Programming IS&T Spring 2011
“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

64 C/C++ Graphics Programming IS&T Spring 2011
Exercise 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

65 OpenSceneGraph resources
Examples katana: /usr/local/OpenSceneGraph/examples Books OpenSceneGraph Quick Start Guide OpenSceneGraph Reference Guides New book OpenSceneGraph 3.0: Beginner's Guide C/C++ Graphics Programming IS&T Spring 2011

66 C/C++ Graphics Programming IS&T Spring 2011
SCV 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 help Web-based tutorials Consultation by appointment Erik Brisson C/C++ Graphics Programming IS&T Spring 2011


Download ppt "C / C++ Graphics Programming with OpenGL & OpenSceneGraph"

Similar presentations


Ads by Google