Viewing and Transformation
Viewing and Transformation
Viewing and Transformation
OpenGL program -3
#include glut.h
static GLfloat year=0.0f, day=0.0f;
void display();
void reshape(GLsizei , GLsizei );
void idle();
1/7
OpenGL program -3
2/7
void display()
{
// clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
glutWireSphere(1.0, 20, 16);
glRotatef(year, 0.0, 1.0, 0.0);
glTranslatef(3.0, 0.0, 0.0);
glRotatef(day, 0.0, 1.0, 0.0);
glutWireSphere(0.5, 10, 8);
glPopMatrix();
// swap the front and back buffers
glutSwapBuffers();
}
// the Sun
// the Planet
OpenGL program -3
3/7
OpenGL program -3
// GLUT idle function
void idle()
{
day += 10.0;
if(day > 360.0)
day -= 360.0;
year += 1.0;
if(year > 360.0)
year -= 360.0;
// recall GL_display() function
glutPostRedisplay();
}
4/7
OpenGL program -3
// GLUT keyboard function
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'd':
day += 10.0;
if(day > 360.0)
day -= 360.0;
glutPostRedisplay();
break;
case 'y':
year += 1.0;
if(year > 360.0)
year -= 360.0;
glutPostRedisplay();
break;
case 'a':
// assign idle function
glutIdleFunc(GL_idle);
break;
case 'A':
glutIdleFunc(0);
break;
case 27:
exit(0);
}
}
5/7
OpenGL program -3
6/7
OpenGL program -3
7/7
1/2
2/2
Overview
Vertex
Vertex
Modelview
Modelview
Matrix
Matrix
X
Y
Z
W
Object
coordinat
es
TODO:
Projection
Projection
Matrix
Matrix
Perspective
Division
Viewport
Viewport
Transformation
Transformation
X
Y
eye
coordinat
es
TODO:
1. Switch matrix
mode to
GL_MODELVIEW and
call glLoadIdentity().
2. Call gluLookAt().
3. Your own modeling
transformations.
clip
coordinat
es
normalized
device
coordinates
TODO:
window
coordinat
es
1. Call
glViewport().
Matrix in OpenGL
Viewing transformations
1/2
gluLookAt(
);
eyex, eyey, eyez is where the camera
is positioned.
centerx, centery, centerz is where the
camera looks at.
Upx, upy, upz is the up-vector of the
camera.
http://msdn.microsoft.com/library/default.asp?url=/library/enus/opengl/glufnc01_8wtw.asp
Viewing transformations
2/2
Modeling transformation
1/4
Modeling transformation
2/4
http://msdn.microsoft.com/library/default.asp?url=/library/enus/opengl/glfunc03_9a05.asp
glTranslatef( 0, 0, -1
);
Modeling transformation
glRotate{fd}(
);
3/4
1);
Modeling transformation
4/4
Projection transformation
1/5
Projection transformation
2/5
Perspective Projection
http://msdn.microsoft.com/library/default.asp?url=/library/enus/opengl/glfunc02_0oj1.asp
Projection transformation
3/5
http://msdn.microsoft.com/library/default.asp?url=/library/enus/opengl/glufnc01_6m79.asp
Projection transformation
4/5
Orthographic projection
http://msdn.microsoft.com/library/default.asp?url=/library/enus/opengl/glfunc03_8qnj.asp
Projection transformation
5/5
Viewport transformations
Matrix Manipulation
1/4
void glLoadIdentity();
Set current matrix to the 4x4 identity
matrix
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_6h2x.asp
Matrix Manipulation
2/4
Matrix Manipulation
3/4
Matrix Manipulation
void glPushMatrix();
4/4
Void glPopMatrix();