Date post: | 15-Jan-2016 |
Category: |
Documents |
View: | 252 times |
Download: | 1 times |
IS&T Tutorial: Graphics Prog - OpenGL and OSG
C / C++ Graphics Programming withC / C++ Graphics Programming with OpenGL & OpenSceneGraph OpenGL & OpenSceneGraph
Erik Brisson
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
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
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Human view of physical worldHuman view of physical world
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Human view of imageHuman view of image
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Computer graphics paradigmComputer graphics paradigm
IS&T Tutorial: Graphics Prog - OpenGL and OSG
C.G. paradigm with lightC.G. paradigm with light
IS&T Tutorial: Graphics Prog - OpenGL and OSG
C.G. basic calculationC.G. basic calculation
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Result is a digital imageResult is a digital image
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Resulting imageResulting image
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Model geometry – two cuboidsModel geometry – two cuboids
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Model geometry - Model geometry - approximationapproximation
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Polygonal modelsPolygonal models
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Overview: lights, cameras, …Overview: lights, cameras, …
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Coordinate SystemsCoordinate Systems
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Camera View (Frustum)Camera View (Frustum)
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 )
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();}
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);
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 )
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
IS&T Tutorial: Graphics Prog - OpenGL and OSG
OpenGL – primitivesOpenGL – primitives
IS&T Tutorial: Graphics Prog - OpenGL and OSG
OpenGL – primitive exampleOpenGL – primitive example
Let’s look at ex_quad.c
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;
}
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() !
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++;
}
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();
}
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();
}
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)
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)
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
IS&T Tutorial: Graphics Prog - OpenGL and OSG
CompareCompare
Rotate firstRotate firstThen translateThen translate
Translate firstTranslate firstThen rotateThen rotate
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Rotations in 3DRotations in 3D
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)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Camera location setupCamera location setupgluLookat(xeye, yeye, zeye, xat, yat, zat, xup, yup, zup)
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Camera perspective setupCamera perspective setup
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Full view / transform pipeline
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.
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Other transformations - scalingOther transformations - scaling
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Multiple transformations
Example in ex_multi_transform.c
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
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
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)
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
IS&T Tutorial: Graphics Prog - OpenGL and OSG
ShadingShadingambient
diffuse
specular
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Shading at vertices, Shading at vertices, interpolationinterpolation
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Flat shading vs Gouraud Flat shading vs Gouraud shadingshading
IS&T Tutorial: Graphics Prog - OpenGL and OSG
C.G. example - texture mapC.G. example - texture map
IS&T Tutorial: Graphics Prog - OpenGL and OSG
Texture map fullTexture map full
++
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
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
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 )
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
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();
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);
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);
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);
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
IS&T Tutorial: Graphics Prog - OpenGL and OSG
A bit more about scaling & transformsA bit more about scaling & transforms
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);
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
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
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);
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
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
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])