Jump to content

  • Log In with Google      Sign In   
  • Create Account

glRotatef behaves strange


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 EasyCoder   Members   -  Reputation: 116

Like
0Likes
Like

Posted 24 July 2012 - 09:57 AM

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.

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8194

Like
0Likes
Like

Posted 24 July 2012 - 10:01 AM

Are you accumulating rotations for each object? Perhaps show code and image because the description is quite vague.

#3 dpadam450   Members   -  Reputation: 923

Like
0Likes
Like

Posted 24 July 2012 - 10:01 AM

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

#4 EasyCoder   Members   -  Reputation: 116

Like
0Likes
Like

Posted 24 July 2012 - 10:07 AM

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.

#5 dpadam450   Members   -  Reputation: 923

Like
0Likes
Like

Posted 24 July 2012 - 10:49 AM

Well the post a video.

#6 EasyCoder   Members   -  Reputation: 116

Like
0Likes
Like

Posted 25 July 2012 - 06:14 AM


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.


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 ?!? Posted Image

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

#7 Brother Bob   Moderators   -  Reputation: 8194

Like
0Likes
Like

Posted 25 July 2012 - 07:02 AM

1) Why is this not happening with x or z axis, only with y ?!? Posted Image

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.

#8 EasyCoder   Members   -  Reputation: 116

Like
0Likes
Like

Posted 25 July 2012 - 09:29 AM

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.

#9 EasyCoder   Members   -  Reputation: 116

Like
0Likes
Like

Posted 25 July 2012 - 09:36 AM

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.

#10 Brother Bob   Moderators   -  Reputation: 8194

Like
0Likes
Like

Posted 25 July 2012 - 09:51 AM

So what does the rendered image look like, and what is not what you expected with it?

#11 scniton   Members   -  Reputation: 252

Like
0Likes
Like

Posted 25 July 2012 - 11:55 AM

Also, please post what you're using for your projection matrix (you suspect that it is related to perspective.)
Stop twiddling your bits and use them already!

#12 dpadam450   Members   -  Reputation: 923

Like
0Likes
Like

Posted 25 July 2012 - 12:36 PM

Is your box centered around the origin?

gl.glScalef(0.25f, 0.25f, 0.0f);

You realize this is collapsing the z-axis to 0. If you want any depth to your 2d Images, this should be 1 for the z-component, otherwise they will have have a z-value of zero.

You should post a video because nobody knows really what is going on.

Edited by dpadam450, 25 July 2012 - 12:37 PM.


#13 Brother Bob   Moderators   -  Reputation: 8194

Like
0Likes
Like

Posted 25 July 2012 - 12:40 PM

You realize this is collapsing the z-axis to 0. If you want any depth to your 2d Images, this should be 1 for the z-component, otherwise they will have have a z-value of zero.

It only collapses the Z-value of the model up to the point where the scaling takes place in the sequence of transformations. There are no problems collapsing the object onto a 2D plane and then translate it into its correct depth value so that depth testing of the 2D object can take place properly.

#14 dpadam450   Members   -  Reputation: 923

Like
0Likes
Like

Posted 25 July 2012 - 12:52 PM

Yup I edited right after, It should be fine. Which is why I edited to say post a video because we need to see it happen.

#15 EasyCoder   Members   -  Reputation: 116

Like
0Likes
Like

Posted 26 July 2012 - 03:05 AM

glRotatef-y.jpg

So this is how it looks like, when I use the above code.

I am using GL_MODELVIEW in glMatrixMode and fovy (field of view angle in y direction) in gluPerspective is set to 45.0f.

Could it have anything to do with fovy, as it is also in y direction ? Posted Image

#16 Brother Bob   Moderators   -  Reputation: 8194

Like
0Likes
Like

Posted 26 July 2012 - 03:34 AM

If those quads are rotated 45 degrees about the Y-axis, then the only reason I can see for that behaviour is normal perspective effects. Objects to the left will be viewed more from the right that centered objects and thus appear, due to perspective, slightly narrower. Think of a cube; the further to the right on the screen the cube is, the more of its left side you can see. They are not rotated, but perspective shifted.

#17 EasyCoder   Members   -  Reputation: 116

Like
0Likes
Like

Posted 26 July 2012 - 09:58 AM

If those quads are rotated 45 degrees about the Y-axis, then the only reason I can see for that behaviour is normal perspective effects. Objects to the left will be viewed more from the right that centered objects and thus appear, due to perspective, slightly narrower. Think of a cube; the further to the right on the screen the cube is, the more of its left side you can see. They are not rotated, but perspective shifted.


I see. Is there any way to change the perspective, so they all appear the same ?

#18 dpadam450   Members   -  Reputation: 923

Like
1Likes
Like

Posted 26 July 2012 - 10:45 AM

Use glOrtho instead of gluPerspective.

If you hold your hand 45 degrees like the image and put it far left of your head and them move it towards the right, you will see the same thing. On the left your hand is facing you, on the right it is facing away from you and distorted because you are looking down the side of the hand and cant see the front anymore.

#19 EasyCoder   Members   -  Reputation: 116

Like
0Likes
Like

Posted 27 July 2012 - 06:42 AM

Use glOrtho instead of gluPerspective.

If you hold your hand 45 degrees like the image and put it far left of your head and them move it towards the right, you will see the same thing. On the left your hand is facing you, on the right it is facing away from you and distorted because you are looking down the side of the hand and cant see the front anymore.


Yes, your explanation makes perfect sense to me.

And gluOrtho2D works like a charm ! Posted Image

Now all 15 objects rotate exactly at the same angle. Thanks for your help & suggestion.

Only one more question: can you swap back and forth between gluOrtho2D and gluPerspective within the onDrawFrame ?

This is for a game which I am developing, and the engine is already finished and working fine in gluPerspective, so I am not very keen to change it to gluOrtho2D.

I just want to do that rotation effect in gluOrtho2D, then get rid of the objects and swap back to gluPerspective.

Is this possible ?

#20 Brother Bob   Moderators   -  Reputation: 8194

Like
1Likes
Like

Posted 27 July 2012 - 07:00 AM

You can change any matrix at any time as many times you like.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS