Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

Only rotating and translating by constants [fixed:arrrgh!]

This topic is 4859 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'm just doing some simple rotations and translations, and for some reason, these movements only take place if I specify them with constants. Here is the essential code (C++):
//Global vars
float rot[3] = {0.0f, 0.0f, 0.0f};
float trans[3] = {0.0f, 0.0f, 0.0f};


//The render function
void Render()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    
    glLoadIdentity();
    
    /*In this code, it would ALWAYS translate 20 in the Y direction, and rotate 20 around the X axis, but no matter how the values of trans and rot change, it will not change the rotation or translation! */
    glTranslatef(trans[X], 20.0f + trans[Y], trans[Z]);
    glRotatef(20.0f + rot[X], 1.0f, 0.0f, 0.0f);
    glRotatef(rot[Y], 0.0f, 1.0f, 0.0f);
    glRotatef(rot[Z], 0.0f, 0.0f, 1.0f);
      
    terrain.BindTexture();
    
    //Draws a simple heightmap (performs no rotations/translations in this function
    heightmap.Draw(0.1f, 0.0f);
    
    //Swap buffers to render to screen
    SDL_GL_SwapBuffers();
}


I've checked three ways from sunday to make sure the values of trans and rot really are changing (they change on keypresses or mouse movements - I wrote the values in realtime to a text file and also stepped through the code to make sure it worked). For some reason, though, they don't affect what the translation ends up being. If you could help, I would be very appreciative. Thanks! [Edited by - silverphyre673 on December 1, 2005 8:01:19 PM]

Share this post


Link to post
Share on other sites
Advertisement
To increate the rotation:

if (mouse_x < 20)
rot[Y] += 1.0f;
else if (mouse_x > screen->w - 20)
rot[Y] -= 1.0f;
if (mouse_y < 20)
rot[X] += 1.0f;
else if (mouse_y > screen->h - 20)
rot[X] -= 1.0f;


As I said, though, I'm 100% sure the translation values are changing as they should be.

Share this post


Link to post
Share on other sites
StPapyna's post might be a debugging suggestion. I looked at your code and didn't immediately see why the transformations wouldn't be taking effect, although it could be something obvious that we're all just overlooking. In any case, here are some things you might try (forgive me if you've already thought of them):

1. Print out the values of trans and rot *inside the render function*, to make sure that the correct variables are being accessed.

2. Leave out the rotation while you're debugging, just to narrow the problem down to translation.

3. Make the translation amount an argument to the render function, and see if that makes a difference.

4. Do something like StPapyna's suggestion; make a static local translation or rotation variable and update it within the function.

Basically, try to narrow the problem down to an OpenGL issue, or something within your code, such as a scope or variable access problem. Also, I assume that the matrix mode is GL_MODELVIEW before the render function is called? Are there any other transformations being applied before Render() is called? Again, I fear I'm missing the obvious, but from briefly looking over the code the source of the problem wasn't apparent to me.

Share this post


Link to post
Share on other sites
I know you said your heightmap.Draw(0.1f, 0.0f) doesn't do any transformations. Make sure it doesn't do any glLoadIdentity() calls in there either! it would be a simple, but frustrating mistake!

Share this post


Link to post
Share on other sites
Yup, I checked several ways (including immediately before and after the rotation and translation, by printing the translation and rotation distances to a text file) to make sure that the rotation and translation values are changing correctly. I also made a float value, rx, which increases by .35 each frame, and replaces the X axis rotation value. This didn't affect anything either, so the problem isn't with the input gathering.

I also printed glGetError() to a text file, and it came up as having the value zero every time, which I assume means no error. Nothing has really worked. I'm just going to throw up the whole render function and the heightmap drawing function, although I'm 99% sure the problem doesn't lie there. Also, I'll throw in my OpenGL initialization function, although it's copied straight from another project in which the translation and rotation work normally.

Render function:

void Render()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(trans[X], trans[Y], trans[Z]);
glRotatef(rot[X], 1.0f, 0.0f, 0.0f);
glRotatef(rot[Y], 0.0f, 1.0f, 0.0f);
glRotatef(rot[Z], 0.0f, 0.0f, 1.0f);

terrain.BindTexture();

heightmap.Draw(0.1f, 0.0f);

SDL_GL_SwapBuffers();
}



Heightmap drawing function (this does draw the heightmap properly):

void Heightmap::Draw(Scalar scale, Scalar z)
{
unsigned long offset1 = 0, offset2 = w;
float float_x, float_y;
float float_w = (float)w, float_h = (float)h;

if ( height_array == NULL )
return;

for (int y = 0; y < h; ++y)
{
float_y = (float)y;

glBegin(GL_QUAD_STRIP);
for (int x = 0; x < w; ++x)
{
float_x = (float)x;

glTexCoord2f(float_x / float_w, float_y / float_h);
glVertex3f(float_x * scale, float_y * scale, height_array[offset1] + z);

glTexCoord2f(float_x / float_w, float_y / (float_h + 1.0f));
glVertex3f(float_x * scale, (float_y + 1.0f) * scale, height_array[offset2] + z);
++offset1;
++offset2;
}
}
}



OpenGL initialization:

bool InitGL(int Width, int Height)
{
glViewport(0, 0, Width, Height);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading

glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window

glMatrixMode(GL_MODELVIEW);
}



No OpenGL functions are called anywhere else in the project. Remember, if I initialize one of the floating-point values to some value, or use a numeric constant to do the translation or rotation, it does translate/rotate by that amount. However, the translations or rotations don't seem to be working when their values are changed in realtime. So I initialize the rotation and translation values like this:

float rot[3] = {0.0f, 0.0f, 0.0f};
float trans[3] = {-10.0f, -13.0f, -35.0f};

And it does translate it [-10, -13, -35], but if I change any of the values of rot or trans, no additional rotation or translation takes place. I need help badly! Thanks for your time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!