• Advertisement
Sign in to follow this  

glRotatef behaves strange

This topic is 2094 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 several 2D objects (simple squares with bitmaps on them) which I need to rotate on y axis (i.e. from left to right) all at the same angle.

However, when I use glRotatef command, the objects on the right side of the screen are rotated the most, and progressively towards the left side of the screen, they rotate less and less.

If I have let say 15 objects side-by-side, the difference in rotation between the leftmost and rightmost object is quite substantial and clearly visible.

I assume this is something to do with the perspective, but cannot figure out what I am doing wrong.

Just for interest, if I rotate the same objects on x axis (i.e. from forward to backward) the angle is the same for all of them !

Anybody knows how to achieve rotation at the same angle for all the objects on y axis ?

Thanks in advance for any suggestions.

Share this post


Link to post
Share on other sites
Advertisement

You have to reset the rotation or "the modelview matrix". each rotation adds up if you dont call glLoadIdentity().


I of course use glLoadIdentity as well as glPushMatrix & glPopMatrix.

Share this post


Link to post
Share on other sites

[quote name='dpadam450' timestamp='1343145700' post='4962635']
You have to reset the rotation or "the modelview matrix". each rotation adds up if you dont call glLoadIdentity().


I of course use glLoadIdentity as well as glPushMatrix & glPopMatrix.
[/quote]

Yes, after some experimenting I found that the rotations really ad up.

I started with glRotatef(45.0f, 0.0f, 1.0f, 0.0f)

and I got objects on the right side of the screen rotated the most and on left side the least (just what would you expect if the rotations gradually add up)

Just to confirm that, I then tried gl.glRotatef(675.0f, 0.0f, 1.0f, 0.0f) (as I have 15 objects => 15 * 45 = 675)

and lo and behold, now it was opposite - i.e. objects on the left side of the screen were rotated the most and on right side the least !!!

So that is clear. Now there are just 2 questions remaining:

1) Why is this not happening with x or z axis, only with y ?!? blink.png

2) How to avoid this adding-up of rotation ?

Share this post


Link to post
Share on other sites

1) Why is this not happening with x or z axis, only with y ?!? blink.png

It is happening on all axes. You say you're rotating about the Y-axis, so if you're not rotating about the X or Z-axes, then of course you're not going to accumulate any rotation on those axes either.
2) How to avoid this adding-up of rotation ?

As dpadam450 said with glLoadIdentity, and as you said you're already doing with glPushMatrix/glPopMatrix.

It sounds very much like you are accumulating rotations for each object. Show how you're doing the transformations, because it sounds like you're not resetting the rotations between the objects, but rather keep accumulating rotations.

Share this post


Link to post
Share on other sites

It is happening on all axes. You say you're rotating about the Y-axis, so if you're not rotating about the X or Z-axes, then of course you're not going to accumulate any rotation on those axes either.


No, you did not get me - it is not happening on all the axes - that is the weird thing. Of course I meant it is not happening when I rotate around those two axes, i.e. when I use:

gl.glRotatef(45.0f, 1.0f, 0.0f, 0.0f)

or

gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f)

All 15 objects rotate at the same angle, no accumulation there. And the code is the very same - that is what confuses me.

Share this post


Link to post
Share on other sites

It sounds very much like you are accumulating rotations for each object. Show how you're doing the transformations, because it sounds like you're not resetting the rotations between the objects, but rather keep accumulating rotations.


This is how the code looks like:

while (column <= 15)
{
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);
gl.glTranslatef(column, row, -5.0f);
gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
gl.glScalef(0.25f, 0.25f, 0.0f);
object.draw(gl, bitmap_index);
gl.glPopMatrix();
}

The 15 objects are identical 2D squares with the same bitmap mapped on them.

Share this post


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

  • Advertisement