|
In this lesson, we will be dealing with translations and rotations. This tutorial will make more sense if you have read the Basic 3D lesson
Lesson 4 Code:
(rtri and rquad are floats, defined at the top of the file)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 |
int DrawGLScene() {
glLoadIdentity(); glTranslatef(-1.5f,0.0f,-6.0f); glRotatef(rtri,0.0f,1.0f,0.0f); glBegin(GL_TRIANGLES); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glEnd(); glLoadIdentity(); glTranslatef(1.5f,0.0f,-6.0f); glRotatef(rquad,1.0f,0.0f,0.0f); glColor3f(0.5f,0.5f,1.0f); glBegin(GL_QUADS); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd(); rtri+=0.2f; rquad-=0.15f; return TRUE; } |
Analyze:
Line 3 / 16: glLoadIdentity();
This sets the current matrix in our current mode (GL_MODELVIEW) to an identity, thus, resetting all translations, rotations, and scaling that may have taken place prior to the call.
Line 4: glTranslatef(-1.5f,0.0f,-6.0f);
As stated in NeHe 2:
This shifts the entire scene (anything rendered after this call) over 1.5 units left and 6.0 units into the screen (assuming there is no view change). Objects can be drawn locally, around the origin, then their positions are multiplied by the current matrix modified by translations and rotations. If you are unfamiliar with the axis for OpenGL, take a look at the Basic 3D lesson in the Tutorials section.
Line 5: glRotatef(rtri,0.0f,1.0f,0.0f);
glRotatef( degrees, x, y, z ): specify how much each axis is affected. Again, to learn more about rotations, check out the Basic 3D lesson. rTri is incremented each frame to rotate the objects even more. Remember, even though the transformations are relative, it has to be incremented because the scene turns into an identity before all this is rendered.
In the next lesson, you will draw a "real" 3D object, a cube! |