OpenGL

advertisement
OPENGL
OpenGL
OpenGL resources:
 www.opengl.org
 The Red Book, www.glprogramming.com/red/
 Manual pages, www.opengl.org/sdk/docs/man3/
OpenGL



OpenGL core library
OpenGL Utility (GLU)
OpenGL Utility Toolkit (GLUT)
Header files:
 #include <GL/gl.h>
 #include <GL/glu.h>
 #include <GL/glut.h>
#include <GL/glut.h>
// (or others, depending on the system in use)
void init (void) {
glClearColor (1.0, 1.0, 1.0, 0.0);
OpenGL
// Set display-window color to white.
glMatrixMode (GL_PROJECTION);
// Set projection parameters.
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void lineSegment (void) {
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0);
glBegin (GL_LINES);
glVertex2i (180, 15);
glVertex2i (10, 145);
glEnd ( );
glFlush ( );
// Clear display window.
// Set line segment color to blue.
// Specify line-segment geometry.
// Process all OpenGL routines as quickly as possible.
}
void main (int argc, char** argv) {
glutInit (&argc, argv);
// Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
// Set display mode.
glutInitWindowPosition (50, 100);
// Set top-left display-window position.
glutInitWindowSize (400, 300);
// Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.
init ( );
glutDisplayFunc (lineSegment);
glutMainLoop ( );
}
// Execute initialization procedure.
// Send graphics to display window.
// Display everything and wait.
OpenGL
Point Functions
•
glVertex*( );
* : 2, 3, 4
i (integer)
s (short)
f (float)
d (double)
Ex:
Ex:
glBegin(GL_POINTS);
glVertex2i(50, 100);
glEnd();
int p1[ ]={50, 100};
glBegin(GL_POINTS);
glVertex2iv(p1);
glEnd();
OpenGL
Line Functions
•
•
•
GL_LINES
GL_LINE_STRIP
GL_LINE_LOOP
Ex:
glBegin(GL_LINES);
glVertex2iv(p1);
glVertex2iv(p2);
glEnd();
OpenGL
glBegin(GL_LINES);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glEnd();
GL_LINES
GL_LINE_STRIP
p3
p3
p1
p2
p5
p4
p1
p2
p4
GL_LINE_LOOP
p3
p5
p1
p2
p4
OpenGL
glBegin(GL_POINTS);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glEnd();
GL_POINTS
p3
p5
p2
glPointSize(size);
size: 1,2,…
Ex:
glPointSize(2.0);
// 2x2 pixels
p1
p4
OpenGL
Polygon Functions
glRect*(x1, y1, x2, y2);
•
•
•
•
•
i (integer)
s (short)
f (float)
d (double)
v (vector)
Ex:
Ex:
glRecti(200, 100, 50, 250);
(50, 250)
(200, 100)
int v1[ ]={200, 100};
int v2[ ]={50, 250};
glRectiv(v1, v2);
OpenGL
Ex:
glBegin (GL_POLYGON);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glEnd();
p6
p5
p1
p4
p2
p3
GL_POLYGON
OpenGL
Ex:
glBegin (GL_TRIANGLES);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glEnd();
p6
p5
p1
p4
p2
p3
GL_TRIANGLES
OpenGL
Ex:
p6
glBegin (GL_TRIANGLES);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p6);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glEnd();
p5
p1
p4
p2
p3
GL_TRIANGLES
p6
p6
p5
p1
p4
p2
p3
GL_TRIANGLE_STRIP
p5
p1
p4
p2
p3
GL_TRIANGLE_FAN
OpenGL
Ex:
glBegin (GL_QUADS);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glVertex2iv(p7);
glVertex2iv(p8);
glEnd();
p1
p8
p4
p3
p5
p6
p7
p2
GL_QUADS
GL_QUAD_STRIP
OpenGL
RGB colors
Red
Green
Blue
Color
0
0
0
Black
0
0
1
Blue
0
1
0
Green
0
1
1
Cyan
1
0
0
Red
1
0
1
Magenta
1
1
0
Yellow
1
1
1
White
OpenGL
Color Functions
glClearColor(r, g, b, a);
0.0 ≤ r, g, b, a ≤ 1.0
specifies the color for color buffers
glClear(GL_COLOR_BUFFER_BIT);
applies the clear color to the color buffers
Ex:
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor(GL_COLOR_BUFFER_BIT);
// White
OpenGL
Color Functions
glColor3f(r, g, b);
0.0 ≤ r, g, b ≤ 1.0
Ex:
glColor3f(1.0, 0.0, 0.0);
// Red
glColor3ub(r, g, b); 0 ≤ r, g, b ≤ 255
Ex:
glColor3ub(255, 0, 0); // Red
OpenGL
Reshape Function
glutReshapeFunc(func);
Ex:
glutReshapeFunc(my_reshape_func);
OpenGL – Transformations
Matrix Operations
glMatrixMode routine:
•
•
•
•
Projection mode (GL_PROJECTION)
Modelview mode (GL_MODELVIEW)
Texture mode
Color mode
OpenGL – Transformations
Modelview Mode
glMatrixMode(GL_MODELVIEW);
designates 4x4 modelview matrix as the current matrix.
glLoadIdentity();
assigns the identity matrix to the current matrix.
OpenGL – Transformations
Translate
glTranslatef (tx, ty, tz);
Scale
glScalef (sx, sy, sz);
Rotate
glRotatef (theta, vx, vy, vz);
OpenGL
Modelview Matrix
I
glColor3f (0.0, 0.0, 1.0);
glRecti(0,0,80,40);
glFlush ( );
// blue
OpenGL
Modelview Matrix
I
I.T
glColor3f (0.0, 0.0, 1.0);
glRecti(0,0,80,40);
glFlush ( );
glColor3f (1.0, 0.0, 0.0);
glTranslatef(10.0, 10.0, 0.0);
glRecti(0,0,80,40);
glFlush ( );
// blue
// red
OpenGL
Modelview Matrix
I
I.T
I.T.S
glColor3f (0.0, 0.0, 1.0);
glRecti(0,0,80,40);
glFlush ( );
glColor3f (1.0, 0.0, 0.0);
glTranslatef(10.0, 10.0, 0.0);
glRecti(0,0,80,40);
glFlush ( );
glColor3f (1.0, 0.0, 0.0);
glScalef(2.0, 2.0, 0.0);
glRecti(0,0,80,40);
glFlush ( );
// blue
// red
// red
OpenGL
Modelview Matrix
I
I.T
I.T.S
I.T.S.R
glColor3f (0.0, 0.0, 1.0);
glRecti(0,0,80,40);
glFlush ( );
glColor3f (1.0, 0.0, 0.0);
glTranslatef(10.0, 10.0, 0.0);
glRecti(0,0,80,40);
glFlush ( );
glColor3f (1.0, 0.0, 0.0);
glScalef(2.0, 2.0, 0.0);
glRecti(0,0,80,40);
glFlush ( );
// blue
// red
// red
glColor3f (0.0, 1.0, 0.0);
// green
glRotatef(20.0, 0.0, 0.0, 1.0);
glRecti(0,0,80,40);
glFlush ( );
OpenGL – Transformations
Matrix Stack
glPushMatrix ();
glPopMatrix ();
OpenGL
glColor3f (0.0, 0.0, 1.0);
glRecti(0,0,80,40);
glFlush ( );
I
I.T
I.T.S
I.T.R
I.T
I.T
glColor3f (1.0, 0.0, 0.0);
glTranslatef(10.0, 10.0, 0.0);
glRecti(0,0,80,40);
glPushMatrix();
glFlush ( );
glColor3f (1.0, 0.0, 0.0);
glScalef(2.0, 2.0, 0.0);
glRecti(0,0,80,40);
glFlush ( );
// blue
// red
// red
glColor3f (0.0, 1.0, 0.0);
// green
glPopMatrix();
glRotatef(20.0, 0.0, 0.0, 1.0);
glRecti(0,0,80,40);
glFlush ( );
OpenGL – 2D Viewing
Clipping Window
gluOrtho2D (xwmin, xwmax, ywmin, ywmax);
float or double
Viewport Function
glViewport (xvmin, yvmin, vpWidth, vpHeight);
(xwmax, ywmax)
Window
vpHeight
Viewport
vpWidth
(xvmin, yvmin)
(xwmin, ywmin)
integer
OpenGL – 2D Viewing
Create Display Window
•
•
•
•
•
glutInit (&argc, &argv)
glutInitWindowPosition (xTopLeft, yTopLeft)
glutInitWindowSize (width, height)
windowID=glutCreateWindow (“title”)
glutDestroyWindow (windowID)
Display Window Mode
•
glutInitDisplayMode (mode)
Ex:
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
OpenGL – 2D Viewing
Display Functions
•
glutDisplayFunction (dFunc)
dFunc (callback function) describes what is to be displayed in the current
window
•
glutPostRedisplay ()
Indicates that the contents of the current display window should be renewed
•
glutIdleFunc (iFunc)
iFunc is executed when there are no other events for the system to process
•
glutMainLoop ()
OpenGL – 3D Viewing
Projection

glMatrixMode (GL_PROJECTION)
Orthogonal Projection

gluOrtho (xwmin, xwmax, ywmin, ywmax, dnear, dfar)
Perspective Projection

gluPerspective (theta, aspect, dnear, dfar)
•
•
•

theta: field-of-view angle (00 – 1800)
aspect: aspect ratio of the clipping window (width/height)
dnear, dfar: positions of the near and far planes (must have positive values)
glFrustum (xwmin, xwmax, ywmin, ywmax, dnear, dfar)

if xwmin = -xwmax and ywmin = -ywmax then symmetric view volume
OpenGL – 3D Viewing

glMatrixMode (GL_MODELVIEW)

gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz)
Designates the origin of the viewing reference frame as,
• the world coordinate position P0=(x0, y0, z0)
• the reference position Pref=(xref, yref, zref) and
• the viewup vector V=(Vx, Vy, Vz)
• double-precision, floating-point values
y
yv
w
V
Pref N
xv
zv
P0
xw
zw
OpenGL – 3D Viewing

gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz)
Default parameters are:
• P0=(0,0,0)
• Pref=(0,0,-1)
• V=(0,1,0)
yw
Pref
xw
zw
OpenGL
float eye_x, eye_y, eye_z;
void init (void) {
glClearColor (1.0, 1.0, 1.0, 0.0);
// Set display-window color to white.
glMatrixMode (GL_PROJECTION);
// Set projection parameters.
glLoadIdentity();
gluPerspective(80.0, 1.0, 50.0, 500.0);
// degree, aspect, near, far
glMatrixMode (GL_MODELVIEW);
eye_x = eye_y = eye_z = 60;
gluLookAt(eye_x, eye_y, eye_z,
0,0,0,
0,1,0);
// eye, look-at, view-up
}
void main (int argc, char** argv) {
glutInit (&argc, argv);
// Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
// Set display mode.
glutInitWindowPosition (50, 500);
// Set top-left display-window position.
glutInitWindowSize (400, 300);
// Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.
glViewport (0, 0, 400, 300);
init ( );
// Execute initialization procedure.
glutDisplayFunc (my_objects);
// Send graphics to display window.
glutReshapeFunc(reshape);
glutKeyboardFunc(keybrd);
glutMainLoop ( );
// Display everything and wait.
}
OpenGL Functions
Cube

glutSolidCube (size)

glutWireCube (size)
Cone

glutSolidCone (base_radius, height, slices, stacks)

glutWireCone (base_radius, height, slices, stacks)
Sphere

glutSolidSphere (radius, slices, stacks)

glutWireSphere (radius, slices, stacks)
Teapot

glutSolidTeapot (size)

glutWireTeapot (size)
Torus

glutSolidTorus (inner_radius, outer_radius, nsides, rings)

glutWireTorus (inner_radius, outer_radius, nsides, rings)
OpenGL Functions
Tetrahedron

glutSolidTetrahedron ()

glutWireTetrahedron ()
• 4 sided
Octahedron

glutSolidOctahedron ()

glutWireOctahedron ()
• 8 sided
Dodecahedron

glutSolidDodecahedron ()

glutWireDodecahedron ()
• 12 sided
Icosahedron

glutSolidIcosahedron ()

glutWireIcosahedron ()
• 20 sided
Download