• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# What's wrong with this code?!

## Recommended Posts

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);

glTranslatef(x,y,z);

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

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

##### Share on other sites
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..

##### Share on other sites
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

##### Share on other sites
GLuint DrawGLWindow(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

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

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

[edited by - leedgitar on September 3, 2003 10:07:55 PM]

##### Share on other sites
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]

##### Share on other sites
Don''t Worry, I would Have Spent A week

• ### Forum Statistics

• Total Topics
628282
• Total Posts
2981805

• 9
• 10
• 11
• 17
• 14