Professional Documents
Culture Documents
Graphics Programming
Graphics Programming
OpenGL 3D Drawing
Budditha Hettige
3D Graphics
Projections Getting 3D to 2D
3D scene
Budditha Hettige
2D image
3
Projections
Orthographic Projection Perspective Projection
Perspective
Orthographic
Budditha Hettige
Orthographic Projections
glOrtho ( double left, double right, double bottom, double top, double near, double far);
Top Left Right
Bottom Near
Budditha Hettige
Perspective Projection
gluPerspective ( double angle, double aspect, double near, double far);
aspect = w/h
w
Observer
map
map
zoom in
Budditha Hettige
zoom out
Draw a 3D Model
Object Composition
Polygon
3D Model
Budditha Hettige
Object Composition
Use Transformation
glPushMatrix ( ); glPopMatrix ( ); Save the current transformation state and then restore it after some objects have been placed
Budditha Hettige
Wheele 4
Budditha Hettige
10
Drawing 3D Objects
Cube
void glutWireCube(GLdouble size); void glutSolidCube(GLdouble size);
Sphere
void glutWireSphere(GLdouble radius, GLint slices, GLint stacks); void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks);
Cone
Budditha Hettige
11
Three-dimensional Applications
In OpenGL, two-dimensional applications are a special case of three-dimensional graphics
Not much changes Use glVertex3*( )
Have to worry about the order in which polygons are drawn or use hidden-surface removal Polygons should be simple, convex, flat
Budditha Hettige
12
13
Moving to 3D
We can easily make the program threedimensional by using
typedef Glfloat point3[3] glVertex3f glOrtho
But that would not be very interesting Instead, we can start with a tetrahedron
Budditha Hettige
14
3D Gasket
We can subdivide each of the four faces
Budditha Hettige
15
Example
After 4 interations
Budditha Hettige
16
triangle code
void triangle( point a, point b, point c) {// right-hand rule glBegin(GL_POLYGON); glVertex3fv(a); glVertex3fv(b); glVertex3fv(c); glEnd(); }
Budditha Hettige
17
subdivision code
void divide_triangle(point int m) { point v1, v2, v3; int j; if(m>0) { for(j=0; j<3; j++) for(j=0; j<3; j++) for(j=0; j<3; j++) divide_triangle(a, divide_triangle(c, divide_triangle(b, } else(triangle(a,b,c)); } a, point b, point c,
v1[j]=(a[j]+b[j])/2; v2[j]=(a[j]+c[j])/2; v3[j]=(b[j]+c[j])/2; v1, v2, m-1); v2, v3, m-1); v3, v1, m-1);
Budditha Hettige
18
tetrahedron code
void tetrahedron( int m) { glColor3f(1.0,0.0,0.0); divide_triangle(v[0], v[1], glColor3f(0.0,1.0,0.0); divide_triangle(v[3], v[2], glColor3f(0.0,0.0,1.0); divide_triangle(v[0], v[3], glColor3f(0.0,0.0,0.0); divide_triangle(v[0], v[2], }
v[1], m);
v[3], m);
Budditha Hettige
19
Problem
Because the triangles are drawn in the order they are defined in the program, the front triangles are not always rendered in front of triangles behind them
get this
want this
Budditha Hettige
20
Budditha Hettige
21
Budditha Hettige
22
Example a cube
Glu function
void gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz) gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
Budditha Hettige
23
Budditha Hettige
24