Jump to content
  • Advertisement

Archived

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

Bagpuss

OpenGL OpenGL Rotational Problem

This topic is 5552 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

OK, I have a simple piece of trial code, as below.
  
	glLoadIdentity();						glTranslatef(100.0f,100.0f,2.0f);

	const float *Data = new float;

	Data = Shape->getShapeData();

	glColor3f(0.50f,0.125f,0.5f);
 
	glRotatef( m_Rot, 1.0f, 0.0f, 0.0f );


	glBegin(GL_QUADS);
		glVertex3f(Data[0],Data[1],0.0f);
		glVertex3f(Data[2],Data[3],0.0f);
		glVertex3f(Data[4],Data[5],0.0f);
		glVertex3f(Data[6],Data[7],0.0f);
	glEnd();

	glPopMatrix();


	SwapBuffers(this->hDC);

  
All I want to do is to Draw my shape (it''s a long rectangle), and rotate it via a value (m_Rot). m_Rot is set elsewhere on a key press, and after debugging, I know that this is incrementing 90 degrees each key press. I am getting a strange behaviour in my rotation, in that sometimes the shape never moves, and sometimes it disappears, and sometimes it rotates through 90 or 180 degrees. I suspect it is the coordinates in my glRotate() function. As I am drawing at 100,100,2 I assumed that if I rotated from 0,0,2 I would rotate my shape 90 in the plane it is in, from the origin. Changing the coords to 100,100,2 should rotate it around one corner of the object. Am I correct in my assumptions for the rotation coordinates, and if so, why am I getting unexpected behaviour. Tia, BP.

Share this post


Link to post
Share on other sites
Advertisement
Looks like you''re rotating in the YZ plane instead of the XY plane. Try changing the arguments to the rotate function to 0,0,1.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Thats solved some of my problem, but I still can''t see my rotated shape all the time.

Any more ideas ?

Thanks so far,

Bp

Share this post


Link to post
Share on other sites
glLoadIdentity();
glTranslatef(100.0f,100.0f,2.0f);

WTF? In plain english, this would be "Move the object behind me and way the f*ck up and to the right." Why are you translating to this location?



const float *Data = new float;
Data = Shape->getShapeData();

I don''t even want to know what you were trying to do.



glVertex3f(Data[0],Data[1],0.0f);
glVertex3f(Data[2],Data[3],0.0f);
glVertex3f(Data[4],Data[5],0.0f);
glVertex3f(Data[6],Data[7],0.0f);

Do you know what buffer over-run is?



glPopMatrix();
Are you calling glPushMatrix() before this?



SwapBuffers(this->hDC);
Can''t you just say "SwapBuffers(hDC);"?



How long have you been programming?

Share this post


Link to post
Share on other sites
Jonski.
1) My Coordinate system is converted to my screen res (1024 * 768), and the simple shape appears fine, so I guess whilst you may look over your shoulder, I can see the shape. I only posted what was relevant, sorry if you are not able to understand that.

2)Data->shape.getData() - maybe irrelevant, but my shape returns a pointer to an array of floats.

3)No Buffer overrun, as this is me playing, I know exactly how big my shape is.

4) Yes, Sorry, it looks like my cut & paste went awry.

5) Yes I could. I started off with a program I found on the net, which was a mix of a class and global var''s, and in the process of making a class I found an identical variable was giving me a problem. It doesn''t really matter whether I user this-> or just teh function. I am so sorry for not cleaning up my personal code to a production standard before releasing it. I am trying to learn, If I wanted to be shot down in flames I am sure I could post a better question than this one. If you can''t help someone, why the f* are you posting here ?

I know that my code is not too efficient, or maybe even the best methodm but I am playing around, trying to learn something. Try not to criticise something you don''t understand. If you can''t say something nice, then at least keep it shut.

Share this post


Link to post
Share on other sites
quote:
Original post by Bagpuss
2)Data->shape.getData() - maybe irrelevant, but my shape returns a pointer to an array of floats.


which was quite obvious from the context.. i think he just wanted to demonstrate that the smartass business doesnt work if you''re lacking in the smart department.

anyway: gltranslate probably doesnt do what you want. either you call it before you rotate, then the object will still rotate around its local origin (its position) or you rotate before you translate, but then the translation will be along its local axes and it wont appear where you might expect it. example: rotate 90 deg to the left and translate 100 along (0,0,1 -pretending inverted z-). it will now appear at (world coords) (-100,0,0)

Share this post


Link to post
Share on other sites
quote:
Original post by Trienco
i think he just wanted to demonstrate that the smartass business doesnt work if you''re lacking in the smart department.


I deserved that. I shouldn''t have been condescending. Anyways...


About my first comment.
In order for
glLoadIdentity();
glTranslatef(100.0f,100.0f,2.0f);
to allow you to show anything you would first need either:
a) a z-inverted (aka left-handed coordinate system) projection matrix
b) pre-translated geometry
c) a custom vertex program
d) you object is large enough to poke through near clipping plane
Otherwise, the z-axis points out of your screen. Just for debugging purposes, see if you can see your object by translating (0, 0, -z), where ''z'' is any value between your near and far clipping planes.


const float *Data = new float;
Data = Shape->getShapeData();

I''m pretty sure this leaks 4 bytes. If it''s called in a loop, then it will leak 4 bytes every iteration. I don''t think you also don''t need the ''const'' keyword, either. I think you should simply say:
float *Data = Shape->getShapeData();



I also want to re-iterate what Trienco said, but in different words about translate/rotation order.
Think about a basketball. If you translate then rotate, it''s like spinning the ball on you finger over the point where you translated.
If you rotate and then translate, it''s like holding the ball out where you translated and then turning your whole body in circles.

Share this post


Link to post
Share on other sites
OK, thanks for the replys guys,
and sorry if I was a bit touchy. Been a bad weekend for me :-/

I will try your suggestions, and if none of them work then no doubt I will be back !

Thanks again,

Bp.

Share this post


Link to post
Share on other sites
hope it will help.. the basketball explanation is pretty good and definitely better than me babbling about local and global axes (i couldnt explain things like that if my life depended on it.. at least not in a way somebody can understand if he''s not already knowing about it ,-) )

if you still have strange behaviour when rotating try and rotate and very small steps so you can watch where it goes.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!