Beruflich Dokumente
Kultur Dokumente
using OpenGL
Vinod Pathangay
Ph. D. Scholar
Visualization and Perception Lab
IIT Madras
Why OpenGL?
• Device independence
• Platform independence
– SGI Irix, Linux, Windows
• Abstractions (GL, GLU, GLUT)
• Open source
• Other APIs
– OpenInventor (SGI)
– DirectX (Microsoft), Java3D (Sun)
Features in OpenGL
• 3D Transformations
– Rotations, scaling, translation, perspective
• Colour
• Lighting
– Flat shading, Gouraud shading, Phong shading
• Rendering
– Texture mapping
• Modeling
– non-uniform rational B-spline (NURB) curves,
surfaces
• Others
– atmospheric fog, alpha blending, motion blur
OpenGL Operation
Display Lists
Per-vertex ops
Commands Evaluator & primitive Rasterization Per-fragment
assembly ops
Texture
Pixel ops
memory
Frame buffer
What is to be drawn?
How it is to be drawn?
From OpenGL reference manual “Blue book”
OpenGL Operation
Can accumulate some commands in
a display list for processing at a later
time (Batch mode). Or can proceed
Display Lists
immediately through the pipeline
Per-vertex ops
Commands Evaluator & primitive Rasterization Per-fragment
assembly ops
Texture
Pixel ops
memory
Frame buffer
Per-vertex ops
Commands Evaluator & primitive Rasterization Per-fragment
assembly ops
Texture
Pixel ops
memory
Frame buffer
Per-vertex ops
Commands Evaluator & primitive Rasterization Per-fragment
assembly ops
Texture
Pixel ops
memory
Frame buffer
Per-vertex ops
Commands Evaluator & primitive Rasterization Per-fragment
assembly ops
Texture
Pixel ops
memory
Frame buffer
Per-vertex ops
Commands Evaluator & primitive Rasterization Per-fragment
assembly ops
Texture
Pixel ops
memory
Frame buffer
Per-vertex ops
Commands Evaluator & primitive Rasterization Per-fragment
assembly ops
Texture
Pixel ops
memory
Frame buffer
• Viewing – perspective/orthographic
GLU • Image scaling, polygon tessellation
• Sphere, cylinders, quadratic surfaces
GL
• Primitives - points, line, polygons
• Shading and Colour
• Translation, rotation, scaling
• Viewing, Clipping, Texture
• Hidden surface removal
Structure of a GLUT Program
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Interactive rotating cube");
glutDisplayFunc(display); // display callback
glutKeyboardFunc(myKeyHandler); // keyborad callback
glutMouseFunc(myMouseClickHandler); // mouse callback
glutMotionFunc(myMouseMotionHandler); // mouse move callback
init();
glutMainLoop();
}
void display() {...}
void myKeyHandler( unsigned char key, int x, int y) {...}
void myMouseClickHandler( int button, int state, int x, int y ) {...}
void myMouseMotionHandler( int x, int y) {...}
Viewing in 2D
void init(void) {
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0f,0.0f,1.0f);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(
0.0, // left
screenWidth, // right
0.0, // bottom
screenHeight); // top
}
Drawing in 2D
GL_LINES
glBegin(GL_POINTS); GL_LINE_STRIP
glVertex2d(x1, y1); GL_LINE_LOOP
glVertex2d(x2, y2); GL_POLYGON
.
.
.
glVertex2d(xn, yn);
glEnd();
Demo – 2D Curves
Demo – 2D Polygon Drawing
Assigning Colours
v1,v2,…, vN VM P clip VP
modelview projection perspective viewport
matrix matrix division matrix
3D Viewing – ModelView Matrix
world clip normalized device
coordinates coordinates coordinates window
coordinates
v1,v2,…, vN VM P clip VP
modelview projection perspective viewport
matrix matrix division matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// viewing transform
gluLookAt(eyeX, eyeY, eyeZ,
lookAtX, lookAtY, lookAtZ,
upX, upY, upZ);
// model transform
glTranslatef(delX, delY, delZ);
glRotatef(angle, i, j, k);
glScalef(multX,multY, multZ);
3D Viewing – Projection Matrix
world clip normalized device
coordinates coordinates coordinates window
coordinates
v1,v2,…, vN VM P clip VP
modelview projection perspective viewport
matrix matrix division matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// perspective transform
gluPerspective( viewAngle, aspectRatio,nearZ,farZ );
// other commands for setting projection matrix
glFrustum(left, right, top, bottom);
glOrtho(left, right, top, bottom);
gluOrtho2D(left, right, top, bottom);
Demo – 3D Wireframe (Gaussian)
Demo – 3D Wireframe (Gabor)
Lighting up the 3D World
Ambient light Diffuse light
(source at infinity) (from a point source)
Y Y
Z Z
X X
GLfloat light0_colour] = {1, 1.0, 1, 1.0};
GLfloat light0_position[] = {0.0, 1.0, 0.0, 0.0};