What's wrong with this code?!

Started by
4 comments, last by JohnyB 20 years, 7 months ago
Okay I''ve been trying to figure out why this doesn''t work for a while I can''t figure out what''s wrong here''s the code...

GLuint DrawGLWindow()
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
	glTranslatef(x,y,z);

	if(keys[VK_UP])
	{
		z+=1.0f;
	}
	if(keys[VK_DOWN])
	{
		z-=1.0f;
	}

	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-6.0f);
	glRotatef(move,0.0f,1.0f,1.0f);
	p.SolidCube(1.0f);
	move+=1.0f;

	return TRUE;
}
As you can see the code is supposed to let you move only foward and backwards but it doesn''t move at all...I''m a noob here so please explain in plain english
Advertisement
The problem is because you pretty much reset the transformation (all the movements) matrix with the loadidentity call...... and after you have done your own custom transformations, you reset it again and do some more.. before the object is drawn.. so the code you used to have control over the translation doesn''t do jack..
Disclaimer: "I am in no way qualified to present advice on any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)"
Well first off, after setting the translation for movement you load the identity matrix again which kills that previous translation. Now, thats what happens assuming this code is exactly as it appears in your program.

If this is not the problem then I''ll be happy to take another look once you provide more code.

-out
-out
GLuint DrawGLWindow(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

if(keys[VK_UP])
{
z+=1.0f;
}
if(keys[VK_DOWN])
{
z-=1.0f;
}

glTranslatef(x,y,z);
glRotatef(move,0.0f,1.0f,1.0f);

p.SolidCube(1.0f);

move+=1.0f;

return TRUE;
}

What you wanna do is make the glTranslate call after the input is gathered so that the new Z value will be used. Also for a single object, only make one call to glLoadIdentity at the beginning of drawing each object to reset the modelview matrix before moving an object each frame. Your second call to glLoadIdentity would wipe out the translation before it, and end up only applying your rotation.

Give that a shot.

________________________________________________
Chris McGuirk
Lead Programmer - Axiom Engine

C# gaming, the way of the future!
http://axiomengine.sourceforge.net

[edited by - leedgitar on September 3, 2003 10:07:55 PM]
________________________________________________Chris McGuirkLead Programmer - Axiom 3D EngineC# gaming, the way of the future!http://www.axiom3d.org
Ah I c I didn't understand where I needed to use glLoadIdentity() in there but I think I get it now...It works as it should now thanks for the help. And to think I spend almost an hour to solve such a stupid mistake...

[edited by - JohnyB on September 3, 2003 10:18:16 PM]
Don''t Worry, I would Have Spent A week
Only Destroy If You Can Pay For The Damages

This topic is closed to new replies.

Advertisement