OpenGL Summer2012
OpenGL Summer2012
OpenGL Summer2012
OpenGL
Log on to katana
% cp –r /scratch/ogltut/ogl .
% cd ogl
void display(void);
void init(void);
/* display is called by the glut main loop once for every animated frame */
void display( ){
}
Steps to edit, compile and run the
program
• Edit the source file in the editor, save it and exit
• >make file_name
• >file_name
• For step0.c:
• >make step0
• >step0
Step1.c
/* Camera position */
/* By the default, the camera is situated at the origin, points down the negative
z-axis, and has an upper vector (0,1,0)*/
gluLookAt(0.,0.,5.,0.,0.,0.,0.,1.,0.);
}
Step1.c
/* display is called by the glut main loop once for every animated frame */
void display( ){
}
GLUT primitives
• void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks);
• void glutWireSphere(GLdouble radius, GLint slices, GLint stacks);
GLUT primitives
• glutSolidSphere glutWireSphere
• glutSolidCube glutWireCube
• glutSolidCone glutWireCone
• glutSolidTorus glutWireTorus
• glutSolidDodecahedron glutWireDodecahedron
• glutSolidOctahedron glutWireOctahedron
• glutSolidTetrahedron glutWireTetrahedron
• glutSolidIcosahedron glutWireIcosahedron
• glutSolidTeapot glutWireTeapot
• More info:
http://www.opengl.org/documentation/specs/glut/spec3/node80.html
Colors: RGBA vs. Color-Index
Color mode
Color-Index
RGBA mode
Mode
Step1.c
mydraw();
glutSwapBuffers();
}
Step1.c
void mydraw() {
glColor3f( 1.0, 0.0, 0.0); /* red color */
glutSolidTeapot(.5); /* draw teapot */
}
Open GL transformation
Step1.c
Modeling
Positioning the Model
Transformation
Viewport transformation
• indicates the shape of the available screen area into which the
scene is mapped
• Since viewport specifies the region the image occupies on the
computer screen, you can think of the viewport transformation
as defining the size and location of the final processed
photograph - for example, whether the photograph should be
enlarged or shrunk.
• if the window changes size, the viewport needs to change
accordingly
void glViewport( int x,
int y,
int width,
int height);
Step1.c
Viewport transformation
Projection
Perspective vs. Orthographic
Projection transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//perspective projection
glFrustum(left, right, bottom, top, near, far);
Or
//orthographic projection
glOrtho (left, right, bottom, top, near, far);
Step1.c
Perspective Transformation
//perspective projection
void glFrustum(double left,
double right,
double bottom,
double top,
double near,
double far);
Step1.c
Perspective Transformation
Four sides of the frustum, its top, and its base correspond to the
six clipping planes of the viewing volume.
Perspective Transformation
//perspective projection
void gluPerspective( double fovy,
double aspect,
double near,
double far);
Step1.c
Orthographic Transformation
//orthographic projection
void glOrtho( double left,
double right,
double bottom,
double top,
double near,
double far);
Step1.c
Modelview Matrix
//perspective projection
void gluLookAt(double eyeX, double eyeY, double eyeZ,
double centerX, double centerY, double centerZ,
double upX, double upY, double upZ);
Step1.c
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION); /* Set up the projection matrix */
glLoadIdentity();
// left,right,bottom,top,near,far
glFrustum(-1.0, 1.0, -1.0, 1.0, 1., 10.0); // perspective view
// glOrtho (-1.0, 1.0, -1.0, 1.0, 1., 10.0); // orthographic view
// gluPerspective(45.0f, 1., 1., 10.); // perspective view
}
Step1.c
glutIdleFunc() callback routine for idle state, usually used for animation
}
Step2.c
. . .
}
Step2.c
double aspect;
glViewport(0,0,width,height); /* Reset the viewport */
aspect = (double)width / (double)height; /* compute aspect ratio*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //reset projection matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0., 0., 5., 0., 0., 0., 0., 1., 0.);
}
step2.c
Geometric Primitives
Polygon
Points Lines
s
Stippling Normals
step3.c
OpenGL Primitives
glBegin(GL_LINES);
glEnd();
http://www.opengl.org/sdk/docs/man/xhtml/glBegin.xml
step3.c
Define a box
void boxDef( float length, float height, float width)
{
glBegin(GL_QUADS);
/* you can color each side or even each vertex in different color */
OpenGL Transformations
Vertex Data
ModelView
Matrix
Projection
Matrix
Object
Coordinates
Perspective
Eye Division
Coordinates
Viewport
Clip Transformation
Coordinates
Device
Coordinates
Window
Coordinates
step3.c
Draw Geometry
step3.c
http://www.opengl.org/sdk/docs/man/xhtml/glRotate.xml
step3.c
glBegin(GL_TRIANGLES);
glVertex3fv(v0);
glVertex3fv(v1);
glVertex3fv(v2);
glEnd();
glEndList();
...
// draw the display list
glCallList(index);
...
// delete it if it is not used any more
glDeleteLists(index, 1);
step4.c
Lighting
Ambient Light has no source, considered
to be everywhere.
Ambient, Diffuse
Diffuse Light Ambient & Diffuse & Specular
shines upon an object indirectly
Light(s) Position
Light
http://www.opengl.org/sdk/docs/man/xhtml/glLight.xml
step5.c
Material Properties
Ambient default = (0.2, 0.2, 0.2, 1.0)
Diffuse In real life diffuse and ambient colors are set to the same value default = (0.8, 0.8, 0.8, 1.0)
Shininess controls the size and brightness of the highlight, value range (0. to 128.) default =0.0
• GLfloat low_shininess [] = {5.}; // the higher value the smaller and brighter (more focused) the highlight
• glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
Emission emissive color of material (usually to simulate a light source), default = (0.0, 0.0, 0.0, 1.0)
• Light:
o set diffuse to the color you want the light to be
o set specular equal to diffuse
o set ambient to 1/4 of diffuse.
• Material:
o set diffuse to the color you want the material to be
o set specular to a gray (white is brightest reflection, black is no reflection)
o set ambient to 1/4 of diffuse
step5.c
Enable Lighting
• /* Enable a single OpenGL light. */
• glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
• glLightfv(GL_LIGHT0, GL_POSITION, light_position);
• glEnable(GL_LIGHT0);
• glEnable(GL_LIGHTING);
1) move the platform down the screen, so it would move along the
y=-4 level
2) make ball "bounce" from the wall, left and right walls and the
platform
3) if the ball misses the platform, it should "fall" beneath the
"floor" and a new ball should appear from the "ceiling".
OpenGL Helpful Materials
Online documentation
• OpenGL: http://www.opengl.org
• GLUT: http://www.freeglut.org
• Reference: http://www.glprogramming.com/blue/
Examples:
• From OpenGL.org (examples and tutorials): http://www.opengl.org/code
Books:
• “Red book”: OpenGL Programming Guide. Woo, Neider, Davis, Shreiner. ISBN 0-201-60458-2.
• “Blue book”: OpenGL Reference Manual. Shreiner. ISBN 0-201-65765-1
Thank you! Final Notes:
• 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
Katia Oleinik([email protected])