Menu
Home
DS
PC
Tutorials
Links
Contact


Affiliates
NeHe 4 - Translations and Rotations

 

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()					// Here's Where We Do All The Drawing
{
	glLoadIdentity();				// Reset The Current Modelview Matrix
	glTranslatef(-1.5f,0.0f,-6.0f);			// Move Left 1.5 Units And Into The Screen 6.0
	glRotatef(rtri,0.0f,1.0f,0.0f);			// Rotate The Triangle On The Y axis ( NEW )

	glBegin(GL_TRIANGLES);				// Start Drawing A Triangle
		glColor3f(1.0f,0.0f,0.0f);		// Set Top Point Of Triangle To Red
		glVertex3f( 0.0f, 1.0f, 0.0f);		// First Point Of The Triangle
		glColor3f(0.0f,1.0f,0.0f);		// Set Left Point Of Triangle To Green
		glVertex3f(-1.0f,-1.0f, 0.0f);		// Second Point Of The Triangle
		glColor3f(0.0f,0.0f,1.0f);		// Set Right Point Of Triangle To Blue

		glVertex3f( 1.0f,-1.0f, 0.0f);		// Third Point Of The Triangle
	glEnd();					// Done Drawing The Triangle
	glLoadIdentity();				// Reset The Current Modelview Matrix
	glTranslatef(1.5f,0.0f,-6.0f);			// Move Right 1.5 Units And Into The Screen 6.0
	glRotatef(rquad,1.0f,0.0f,0.0f);		// Rotate The Quad On The X axis ( NEW )
	glColor3f(0.5f,0.5f,1.0f);			// Set The Color To Blue One Time Only

	glBegin(GL_QUADS);				// Draw A Quad
		glVertex3f(-1.0f, 1.0f, 0.0f);		// Top Left
		glVertex3f( 1.0f, 1.0f, 0.0f);		// Top Right
		glVertex3f( 1.0f,-1.0f, 0.0f);		// Bottom Right
		glVertex3f(-1.0f,-1.0f, 0.0f);		// Bottom Left
	glEnd();					// Done Drawing The Quad

	rtri+=0.2f;					// Increase The Rotation Variable For The Triangle ( NEW )
	rquad-=0.15f;					// Decrease The Rotation Variable For The Quad ( NEW )
	return TRUE;					// Keep Going
}

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!