Sign in to follow this  
eggmatters

OpenGL glRotatef() Transformation doesn't "stick"

Recommended Posts

Hi all,

I am trying to implement user input to navigate around a generated terrain. I am using SDL to create the window, and manage keystroke events. All of the drawing, rendering etc. is in OpenGL. My problem is this:
Instead of using gluLookAt(), I have written custom routines as indicated in the "red book" (Chapter 3 - Viewing Advanced try this section in 'Viewing Transformations' section of that chapter.) My glTranslatef() functions move the POV based on user input, BUT when you 'pivot' you point of view (Rotate about the y-axis) The view rotates around the POV. But - as soon as the keyup event occurs, the viewpoint "Snaps" back to wherever it was before the last tranformation occurred. So basically, it's like walking around, but whenever you spin, you are immediately forced back into the heading you had before you attempted to spin.
I am somewhat new to OpenGL but feel fairly comfortable with most of the concepts. I have attempted a wide variety of fixes to troubleshoot this to no avail. The following is my code, commented to try to illustrate what I'm trying to do. This code is merely proto-type for learning purposes and won't resemble anything I would do in production so any comments concerning optimization, design suggestions are certainly welcome, but I would really appreciate some help concerning this.


//following values are incremented / decremented per event keystroke
float Ang = 0.0;
float x_pos = 0.0;
float y_pos = 0.0;
float z_pos = 0.0;
bool x_rot = false;
bool y_rot = false;

float x_input = 0.0;
float y_input = 4.85;
float z_input = 0.0;

// This function actually builds the terrain
void buildTerrain(){

// Here, an 80X80 array is generated with height values to use as vertices for my GL_QUADS

//implementing glList since these values are static.
glNewList(1, GL_COMPILE);
// Terrain rendering routine goes here

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, myTexture);
//texture mapping for terrain
glBegin(GL_QUADS);
//draws the polygons based the values obtained by the array generated above (or described rather.)
for(int x = 0; x<tWidth-1; x++){
for(int y = 0; y<tHeight-1; y++){
glTexCoord2f(0.0f, 0.0f);
glVertex3f(x+0, terrain[x+0][y+1], y+1);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(x+1, terrain[x+1][y+1], y+1);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(x+1, terrain[x+1][y+0], y+0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(x+0, terrain[x+0][y+0], y+0);

}
}
glEnd();
glEndList();

}

//Here is where I attempt to perform all of my transformations:
void mainLoop() {
while(true) {
processEvents();
//y,x,z *_input are incremented based on keypresses (the events are mapped in code below.)
y_input += y_pos;
x_input += x_pos;
z_input += z_pos;

// Graphical commands...
//We are in the GL_MODELVIEW matrix mode here. This was the last matrix mode set in the function: setupOpenGL().
//I didn't provide that code since I'm fairly certain it's not relavant. It sets the perspective, lighting, loads textures, and fog.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//x_rot set to "true" if the right arrow key is depressed, when true,the polygons above rotate counter clockwise.
if (x_rot) {
Ang += .5;
glRotatef(Ang, 0.0, 1.0, 0.0);
} // if x_rot is false, no rotation occurs but the viewing plans "snaps //back to the world coordinates it had before the rotate call (but after the translate calls.)
//y_rot set to "true" if the left arrow key is depressed, when true,the polygons above rotate clockwise.
if (y_rot) {
Ang += .5;
glRotatef(Ang, 0.0, -1.0, 0.0);
}
//This translate function sets the user at eye level with the rendered terrain. removing this does not affect the rotation bevavior
glTranslatef(-tWidth/2, -12, -tHeight/2);
//This translation works as intended
glTranslatef(x_input, y_input, z_input);
glCallList(1); // <--(Render terrain from list)
SDL_GL_SwapBuffers();

}
}

// Handle SDL keypresses
void handleKeys(SDL_keysym* keysym, bool state) {
//the state variable is true if a key is pressed down, false when the key is released.
if (state) {
switch(keysym->sym) {
case SDLK_ESCAPE:
endProgram(0);
break;
case SDLK_UP:
y_pos -= .10;
break;
case SDLK_DOWN:
y_pos += .10;
break;
//x and z transformations are set similarly.

//tell main() that a rotation is occuring.
case SDLK_RIGHT:
x_rot = true;
break;
case SDLK_LEFT:
y_rot = true;
break;
}
} else {
switch(keysym->sym) {
case SDLK_ESCAPE:
endProgram(0);
break;
case SDLK_UP:
y_pos = 0.0;
break;
case SDLK_DOWN :
y_pos = 0.0;
break;
//tell main() to stop rotating. Reset the Angle about which to rotate to zero.
case SDLK_RIGHT:
Ang = 0;
x_rot = false;
break;
case SDLK_LEFT:
Ang = 0;
y_rot = false;
break;
}
}


}

// Process SDL events
void processEvents() {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
handleKeys(&event.key.keysym, true );
break;
case SDL_KEYUP:
handleKeys(&event.key.keysym, false);
break;
case SDL_QUIT : endProgram(0); break;
}

}

}





Sorry for the amount of code I posted. This issue is really nagging me and I have tried the following means to correct it:
use push/pop matrix to retain the rotated view. As anticipated, this achieved the opposite effect.
Change the Matrix mode to GL_PROJECTION for the rotation transformations only. This worked but the x y and z coords for my translation (even though I switched back to GL_MODELVIEW and called glLoadIdentity(). I was skeptical of this however since the red book and all other sources I've read concerning viewing transformations make absolutely no mention of changing the matrix mode when using rotate as a viewing transformation.

Whew. Sorry for the diatribe but any ideas would help. Thanks!

Share this post


Link to post
Share on other sites
At the beginning of mainLoop, you call glLoadIdentity. This loads the identity matrix on the current stack, wiping any previous values on the stack. You then only apply a rotation if x_rot or y_rot are true. So once you let go of the key and cause those variables to be false, the rotation is not applied any longer.

To fix this, you should be accumulating the rotation amounts into variables like you do with the x/y/z position and applying them via glRotatef always.

In other words, change x_rot and y_rot to floats that represent rotation about those axes. In the key down handling, add some value to x_rot or y_rot, depending. In mainloop, call glRotatef(x_rot, 1,0,0) and glRotate(y_rot,0,1,0) without guarding them with if statements.

Share this post


Link to post
Share on other sites
Thanks,

That makes sense. I had attempted to do something like that, but still had the if statements surrounding the rotation blocks, I believe. And it works. Thank you. Now this begs another question:
Now, my forward / backward translation occurs along the "world" z axis. I will research this but, without using gluLookAt(), what is the best recommended method for "re-aligning" the Z-axis to now be the POV once a rotation is complete? Should I alter the GL_PERSPECTIVE matrix? This makes sense, but is it costly or not the correct way to achieve this?
Thanks much for your help!

Share this post


Link to post
Share on other sites
I'll take a wild stab at the new problem,
Could you just convert your view rotation (y axis?) to radians, use it to come up with a vector along x/z that becomes "forwards/backwards"?

Share this post


Link to post
Share on other sites
to onfu: That's what I was thinking of doing. Just create a callable routine that will do just that. Another option would be changing the viewport to orient towards that new vector, although the vector computation would still be required. It would probably be easiest to keep it in the MODELVIEW matrix. This seems like a pretty straightforward transformation. What is industry standard for this sort of thing? The "red book" uses a flight simulator as an example but it doesn't mention anything about re-orienting your axes for rotational changes - kind of implying that openGL does that for you.

Share this post


Link to post
Share on other sites
Quote:
Original post by eggmatters
to onfu: That's what I was thinking of doing. Just create a callable routine that will do just that. Another option would be changing the viewport to orient towards that new vector, although the vector computation would still be required. It would probably be easiest to keep it in the MODELVIEW matrix. This seems like a pretty straightforward transformation. What is industry standard for this sort of thing? The "red book" uses a flight simulator as an example but it doesn't mention anything about re-orienting your axes for rotational changes - kind of implying that openGL does that for you.
You don't need to modify any matrices or transforms (particularly not the viewport or projection transforms) in order to move your object in the direction it's facing.

Typically, when using the fixed-function pipeline you would specify the position of your object using glTranslate*(). The position itself you can store and update in any way you see fit. It's common to update a position as follows (e.g.):
position += forward_direction * speed * time_delta;
So all you need is a forward direction vector.

Depending on your needs, you can easily construct the forward direction vector using trig as suggested previously. For other cases, you may want to build a transform representing the object's orientation and extract the direction vectors (side, up, and forward) from that transform.

Share this post


Link to post
Share on other sites
Almost there. Trying to update the translate vectors to be the components of the rotated angle. My trig, albeit rusty, is pretty good. I'm not afraid of the math and all. But, can some kind soul point me in the direction of a good 3D tutorial in OpenGL, or Game programming in general? Would much appreciate. Thanks!

Share this post


Link to post
Share on other sites
rough pseudo code should be something like:

camRadians = gameCamera.yRotate*(Math.PI/180);
moveVectorX = moveSpeed*Math.cos(camRadians);
moveVectorZ = moveSpeed*Math.sin(camRadians);

//(move forwards)
glTranslatef(moveVectorX,0,moveVectorZ);

//(move backwards)
glTranslatef(-moveVectorX,0,-moveVectorZ);

-------------

probably some proper tweaking needed since I'm going off memory more to do with how I move game objects around in my 2D engine.

Share this post


Link to post
Share on other sites
Quote:
Original post by onfu
moveVectorX = moveSpeed*Math.cos(camRadians);
moveVectorZ = moveSpeed*Math.sin(camRadians);

//(move forwards)
glTranslatef(moveVectorX,0,moveVectorZ);

//(move backwards)
glTranslatef(-moveVectorX,0,-moveVectorZ);
This is unlikely to work the way you expect it to. What you would typically do here would be to store the position of the camera, update that position using the 'moveVector' variables calculated above, and then submit the position as the argument to glTranslate*().

Share this post


Link to post
Share on other sites
the X and Z might not be correctly laid out either in what I wrote...I just figured it'd be enough to get it on the right track.

Of course since its returning to identity each frame you need to do a lot more than I posted there.

Share this post


Link to post
Share on other sites

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  

  • Announcements

  • Forum Statistics

    • Total Topics
      628350
    • Total Posts
      2982211
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now