GL Lab2

advertisement
LAB 2
OpenGL
Outline


Transformations
Modeling Transformations
 Draw
3D objects
 Translate
 Rotate
 Scale
2
Transformations


All transformations in OpenGL are carried out by
matrices
There are different matrices for different purposes

The modelview matrix


The projection matrix


GL_TEXTURE
All matrices are post-multiplied


GL_PROJECTION
The texture matrix


GL_MODELVIEW
I.e. the current matrix M becomes MN when N is performed
Post-multiplication is closely linked to matrix stacks
3
Transformations

Matrices
 All
matrices in OpenGL are 4x4
 Why use 4-D matrices for 3-D graphics?
A 4x4 matrix can rotate and translate the same
vector in one operation
 A 4x4 matrix can make the otherwise non-linear
perspective projection linear
 Modern CPUs are very efficient at handling 4x4
matrices

4
Transformations

Specify which matrix is the current matrix
void glMatrixMode(

GLenum
mode);
Mode

GL_MODELVIEW


GL_PROJECTION


Applies subsequent matrix operations to the model view matrix stack
Applies subsequent matrix operations to the projection matrix stack
etc.
5
Set a view
int main( int argc, char* argv[] )
{
…
glutReshapeFunc( reshape );
…
}
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double aspect = width/double(height);
gluPerspective(45, aspect, 1, 1024);
}
6
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double aspect = width/double(height);
gluPerspective(45, aspect, 1, 1024);
Set a view
void gluPerspective(

aspect,
GLdouble
zNear,
GLdouble
zFar);
View angle, in degrees, in the y direction
Ratio of x (width) to y (height)
zNear


GLdouble
aspect


fovy,
fovy


GLdouble
Distance from the viewer to the near clipping plane (positive)
zFar

Distance from the viewer to the far clipping plane (positive)
7
Set a view

Detailed explanations about Viewing
Transformation will be provided in the next
week.
8
Draw 3D objects


Use a set of OpenGL primitives
e.g.,









glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
glBegin(GL_TRIANGLES);
…
glEnd();
…
9
Draw 3D objects


GLUT provide simple 3D objects
e.g., Teapot object
 void
glutSolidTeapot(GLdouble size);
 void glutWireTeapot(GLdouble size);
10
Draw 3D objects









void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-1.0);
glColor3f(r,g,b);
glutWireTeapot(0.5);
glFlush();
}
11
Translations

Multiply the current matrix by a translation
matrix
void glTranslatef(

GLfloat
GLfloat
GLfloat
x,
y,
z);
x, y, z
 Specify
the x, y, and z coordinates of a
translation vector
12
Translations

Multiply the current matrix by a translation
matrix
void glTranslatef(
GLfloat
GLfloat
GLfloat
Translation matrix (4x4)
x,
y,
z);
1
0
0
X
0
1
0
Y
0
0
1
Z
0
0
0
1
13
Review of Translations



glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-1.0);
14
Review of Translations



glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-1.0);
Model view matrix
15
Review of Translations



glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-1.0);
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
Model view matrix
16
Review of Translations



glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-1.0);
1
0
0
0
1
0
0
0
1
0
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
0
1
0
0
0
1
-1
0
0
1
-1
0
0
0
1
0
0
0
1
0
0
0
1
Model view matrix
17
Review of Translations




glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-1.0);
Again??
glTranslatef(0,0,-1.0);
1
0
0
0
1
0
0
0
1
0
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
0
1
-1
0
0
1
-1
0
0
1
-2
0
0
0
1
0
0
0
1
0
0
0
1
Model view matrix
18
Review of Translations










void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-1.0);
glTranslatef(0,0,-1.0);
glColor3f(r,g,b);
glutWireTeapot(0.5);
glFlush();
}
19
Tutorials

Move your teapot based on key inputs
 ‘l’:
left
 ‘r’: right
 ‘u’: up
 ‘d’: down
 ‘f’: move out
 ‘n’: move in
‘l’
‘f’
20
Tutorials
float xpos = 0.0;
float ypos = 0.0;
float zpos = -2.0;
void display(){
…
glTranslatef(xpos,ypos,zpos);
…
}
void keyboard(unsigned char key, int x, int y){
if (key == 'l')
xpos -= 0.1;
else if (key == 'r')
xpos += 0.1;
else if (key == 'u')
ypos += 0.1;
else if (key == 'd')
ypos -= 0.1;
else if (key == 'f')
zpos -= 0.1;
else if (key == 'n')
zpos += 0.1;
glutPostRedisplay();
}
21
Rotations
void glRotatef(

angle,
x,
y,
z);
angle
 angle

GLfloat
GLfloat
GLfloat
GLfloat
of rotation, in degrees
x, y, z
 x,
y, and z coordinates of a vector, respectively
 Normalized vector (If not, GL will normalize it)
22
Rotations
void glRotatef(
GLfloat
GLfloat
GLfloat
GLfloat
?
?
?
0
?
?
?
0
?
?
?
0
0
0
0
1
angle,
x,
y,
z);
The questions will be resolved in the CS380
lecture “Modeling Transformations“.
23
Rotations






void display(){
…
glTranslatef(xpos,ypos,zpos);
glRotatef(30,0,0,1);
…
}
30 degrees rotation based on
a normalized vector (0, 0, 1)
24
Tutorials

Rotate your teapot on X axis

e.g., 30, 60, and 90 degrees
30 degrees
60 degrees
90 degrees
25
Tutorials

30 degrees on X axis
 glRotatef(30,1,0,0);

60 degrees on X axis
 glRotatef(60,1,0,0);

90 degrees on X axis
 glRotatef(90,1,0,0);
26
Scaling
void glScalef(

GLfloat
GLfloat
GLfloat
x,
y,
z);
x, y, z
 scale
factors along the x, y, and z axes,
respectively
27
Scaling
void glScalef(
GLfloat
GLfloat
GLfloat
x,
y,
z);
X
0
0
0
0
Y
0
0
0
0
Z
0
0
0
0
1
28
Scaling
void display(){
…
glLoadIdentity();
glTranslatef(0,0,-3);
glScalef(2,1,1);
…
}
29
Review of Scaling
void display(){
…
glLoadIdentity();
glTranslatef(0,0,-3);
glScalef(2,1,1);
…
}
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
Model view matrix
30
Review of Scaling
void display(){
…
glLoadIdentity();
glTranslatef(0,0,-3);
glScalef(2,1,1);
…
}
1
0
0
0
1
0
0
0
1
0
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
0
1
0
0
0
1
-3
0
0
1
-3
0
0
0
1
0
0
0
1
0
0
0
1
Model view matrix
31
Review of Scaling
void display(){
…
glLoadIdentity();
glTranslatef(0,0,-3);
glScalef(2,1,1);
…
}
1
0
0
0
2
0
0
0
2
0
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
0
1
-3
0
0
1
0
0
0
1
-3
0
0
0
1
0
0
0
1
0
0
0
1
Model view matrix
32
Questions

Why use 4-D matrices for 3-D graphics?
 Combine
multiple transformations into a 4x4
matrix
1
0
0
0
2
0
0
0
2
0
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
0
1
-3
0
0
1
0
0
0
1
-3
0
0
0
1
0
0
0
1
0
0
0
1
Model view matrix
33
Next time

Viewing Transformation in OpenGL
34
Download