is&t tutorial: graphics prog - opengl and osg c / c++ graphics programming with opengl &...
Post on 15-Jan-2016
283 views
TRANSCRIPT
![Page 1: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/1.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
C / C++ Graphics Programming withC / C++ Graphics Programming with OpenGL & OpenSceneGraph OpenGL & OpenSceneGraph
Erik Brisson
![Page 2: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/2.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/3.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/4.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Human view of physical worldHuman view of physical world
![Page 5: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/5.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Human view of imageHuman view of image
![Page 6: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/6.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Computer graphics paradigmComputer graphics paradigm
![Page 7: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/7.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
C.G. paradigm with lightC.G. paradigm with light
![Page 8: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/8.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
C.G. basic calculationC.G. basic calculation
![Page 9: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/9.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Result is a digital imageResult is a digital image
![Page 10: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/10.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Resulting imageResulting image
![Page 11: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/11.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Model geometry – two cuboidsModel geometry – two cuboids
![Page 12: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/12.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Model geometry - Model geometry - approximationapproximation
![Page 13: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/13.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Polygonal modelsPolygonal models
![Page 14: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/14.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Overview: lights, cameras, …Overview: lights, cameras, …
![Page 15: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/15.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Coordinate SystemsCoordinate Systems
![Page 16: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/16.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Camera View (Frustum)Camera View (Frustum)
![Page 17: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/17.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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_Ope
nGL/ogltut )
![Page 18: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/18.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/19.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/20.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/21.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/22.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
OpenGL – primitivesOpenGL – primitives
![Page 23: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/23.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
OpenGL – primitive exampleOpenGL – primitive example
Let’s look at ex_quad.c
![Page 24: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/24.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/25.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/26.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/27.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/28.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/29.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/30.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Composition of transformationsComposition of transformations
Rotate 45 degrees CCW Translate (2, 0) Rotate 45 degrees CCW Translate (2, 0)
![Page 31: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/31.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Composition of transformationsComposition of transformations
Translate (2, 0) Rotate 45 degrees CCW Translate (2, 0) Rotate 45 degrees CCW
![Page 32: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/32.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
CompareCompare
Rotate firstRotate firstThen translateThen translate
Translate firstTranslate firstThen rotateThen rotate
![Page 33: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/33.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Rotations in 3DRotations in 3D
![Page 34: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/34.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/35.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Camera location setupCamera location setupgluLookat(xeye, yeye, zeye, xat, yat, zat, xup, yup, zup)
![Page 36: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/36.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Camera perspective setupCamera perspective setup
![Page 37: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/37.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Full view / transform pipeline
![Page 38: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/38.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/39.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Other transformations - scalingOther transformations - scaling
![Page 40: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/40.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Multiple transformations
Example in ex_multi_transform.c
![Page 41: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/41.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/42.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/43.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/44.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Unit shading vectorsUnit shading vectors
n = surface normal vectoru_light = unit vector to light pointu_eye = unit vector to view point
![Page 45: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/45.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
ShadingShadingambient
diffuse
specular
![Page 46: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/46.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Shading at vertices, Shading at vertices, interpolationinterpolation
![Page 47: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/47.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Flat shading vs Gouraud Flat shading vs Gouraud shadingshading
![Page 48: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/48.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
C.G. example - texture mapC.G. example - texture map
![Page 49: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/49.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Texture map fullTexture map full
++
![Page 50: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/50.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/51.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
OpenSceneGraphOpenSceneGraph
Open source OpenGL based Similar to SGI Performer Many utility functions
– Notably, 3-D file readers
![Page 52: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/52.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/53.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/54.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/55.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/56.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/57.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/58.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/59.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
A bit more about scaling & transformsA bit more about scaling & transforms
![Page 60: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/60.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/61.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/62.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/63.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
““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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/64.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
ExerciseExercise
Modify the last example, so that you can animate the PositionAttitudeTransform “arrow” and see what happens …
Solution is left to you
![Page 65: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/65.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
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: IS&T Tutorial: Graphics Prog - OpenGL and OSG C / C++ Graphics Programming with OpenGL & OpenSceneGraph Erik Brisson ebrisson@bu.edu](https://reader035.vdocuments.mx/reader035/viewer/2022062217/56649d555503460f94a33250/html5/thumbnails/66.jpg)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
SCV relatedSCV related
Please fill out an online evaluation of this tutorial:
scv.bu.edu/survey/tutorial_evaluation.html
System help
[email protected], [email protected] Web-based tutorials
www.bu.edu/tech/research/tutorials Consultation by appointment
Erik Brisson ([email protected])