Sign in to follow this  

Transformation tutorial?

This topic is 4744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this piece of code below which uses the quaternion camera (slightly modified to return camera coords) found on Neon Helium (http://nehe.gamedev.net). Now I have yet to find a tutorial which explains in simple terms how the translations affect rendering position for various objects and have found all kinds of strange anomolys when I experiment. Anomoly 1: When you invert the camera (that is to say, flip upside down) the left and right controls are also reversed. Left becomes right, right turns left. Anomoly 2: Creating a HUD at the position of the camera (I have attempted both positioning at camera coords and at the origin) displays nothing. I figure it is because I do not yet have the understanding I need about how translations and rotations affect one another. If anyone could point me to a tutorial I would be very appreciative. For those of you who prefer to assist in a more 'hands-on' way, I have listed the trouble source codes below.

void TurnLeft()
{
	cam.ChangeHeading(-5.0f);
}

void NoseDown()
{
	cam.ChangePitch(5.0f);
}

void NoseUp()
{
	cam.ChangePitch(-5.0f);
}

void TurnRight()
{
	cam.ChangeHeading(5.0f);
}

void MoveForward()
{
	cam.m_ForwardVelocity = 0.01f;
}

void MoveBackward()
{
	cam.m_ForwardVelocity = -0.01f;
}

// ...

void RenderWorld()
{
	int i;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();		// Reset transformation matrix

	glPushMatrix();
		glRotatef(cam.m_HeadingDegrees, 0.0, 1.0, 0.0);
		glRotatef(cam.m_PitchDegrees, 1.0, 0.0, 0.0);
		glBindTexture(GL_TEXTURE_2D, texture[1]);
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_LIGHTING);
		DrawSkybox();
		glEnable(GL_DEPTH_TEST);
		glEnable(GL_LIGHTING);
	glPopMatrix();

	cam.SetPerspective();		// Perform translation

	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glPushMatrix();
		glDisable(GL_LIGHTING);
		glEnable(GL_COLOR_MATERIAL);
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_TEXTURE_2D);
		glColor4f(1.0, 0.0, 0.0, 0.0);
		glTranslatef(-WORLD_INNER_RADIUS, -WORLD_INNER_RADIUS, -WORLD_OUTER_RADIUS);
		glutSolidSphere(10, 16, 16);
		starrot += starspd;
		glRotatef(starrot, 0.0, 1.0, 0.0);
		glTranslatef(-15, -2, 0);
		glColor4f(1.0, 0.5, 0.0, 1.0);
		glutSolidSphere(7, 16, 16);

		glEnable(GL_TEXTURE_2D);
		glEnable(GL_LIGHTING);
		glEnable(GL_DEPTH_TEST);
		glDisable(GL_COLOR_MATERIAL);
	glPopMatrix();

	glPushMatrix();
	for (i = 0; i < MAX_ROIDS; i++)
		{
			if (roids[i] != 0)
			{
				glLoadIdentity();
				cam.SetPerspective();
				roids[i]->Render();
			};
		};
	glPopMatrix();
	
	glColor4f(1.0, 1.0, 1.0, 1.0);

	// Attempt to draw a simple HUD
	glDisable(GL_DEPTH_TEST);

	glBegin(GL_QUADS);
		glVertex3f(x-.5, y+0.5, z-.01);
		glVertex3f(x+0.5, y+0.5, z-.01);
		glVertex3f(x+0.5, y-.5, z-.01);
		glVertex3f(x-.5, y-.5, z-.01);
	glEnd();
	glEnable(GL_DEPTH_TEST);


	glutSwapBuffers();
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Disclaimer: I've never used NeHe's quaternion code, but I do understand how quaternions work.

Anomoly 1:
There are two way to invert the camera. One, you rotate the camera around by 180 degrees. Then, pushing right will make you seem to move right, but it's actually left (because you're upside down). Two, you reflect the camera across the x-z plane (up becomes down). Then, pushing right will make you seem to move left, but it's actually right. This is probably not what you want.

Anomoly 2:
Don't try to create a HUD in perspective move, you'll go nuts. Instead, after you've finished drawing the scene (in perspective), switch to Orthographic to draw your HUD. NeHe's code probably has something like cam.Ortho().

You might want to consider learning how to use glRotatef and gluLookAt before diving into quaternions, as quaternions are fairly hard to understand.

Here's the general outline of what I do each frame:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, screen_width / screen_height, 0.1, 100 ); // same as cam.SetPerpective()??
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

cam.DoMyTransformation(); // or use gluLookAt() for starters

DrawMyWorld();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, screen_width, screen_height, 0); // Set ortho mode
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

DrawMyHud();


Share this post


Link to post
Share on other sites

This topic is 4744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this