Advertisement

Simple Rotation Problem

Started by August 20, 2003 03:21 AM
7 comments, last by Steve-B 21 years, 6 months ago
I have an odd problem that I''ve been unable to put right. I''ve created 3D text using the wglUseFontOutlines function and it works fine. I can display the text with no problem. The problem occurs when I try to rotate the text about it''s origin. In order for the text to be centered in the display I need to translate the scene to the left and back. This changes the coordinate system as you all know. How do I retore the original coordinate system without the text being pushed off the screen. I take it I need to use the PushMatrix/PopMatrix combination but nothing I''ve tried works. When the program runs it display the text in the centre of the screen but rotates it around the transformed axis so it does a big orbit which is not what I want. I want it to spin on it''s own axis. It''s probably really easy but anyway here is my RenderScene code:

GLint RenderScene(GLvoid)								// Here''s where we do all the drawing

{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear the screen and depth buffer


	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glTranslatef(-1.8f, -0.2f, -4.0f);
	
	glColor3f(0.0f, 0.0f, 1.0f);						// Set the drawing colour to blue


	glPushMatrix();
		glRotatef(rotation, 0.0f, 1.0f, 0.0f);
		glListBase(nFontList);
		glCallLists(6, GL_UNSIGNED_BYTE, "OpenGL");
	glPopMatrix();

	rotation+=1.0f;

	return TRUE;										// Everything Went OK

}
I believe glLoadIdentity() resets the screen back to normal. I am still learning though, so I may be wrong, but it's worth a try.

EDIT Actually, after reading your code, try rotating first, then transforming

[edited by - desertcube on August 20, 2003 5:40:37 AM]
Advertisement
No, glLoadIdentity sets your current matrix back to the identity matrix. So all previous applied rotation and translations are void from that point onward.
|1 0 0 0|
|0 1 0 0|
|0 0 1 0|
|0 0 0 1|

quote:
MSDN
The glRotate function computes a matrix that performs a counterclockwise rotation of angle degrees about the vector from the origin through the point (x, y, z).


Desertcube is right about the second point. You need to rotate first, then translate if you want the rotation to look like what you want.
How do I set my laser printer on stun?
Thanks for the replies. I''ve already tried the rotate before translate method and it doesn''t fix the problem. It just rotates around the camera in a massive orbit that takes it a few seconds to return back to the screen.

Here is the new code:

GLint RenderScene(GLvoid)								// Here''s where we do all the drawing{	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear the screen and depth buffer	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	glColor3f(0.0f, 0.0f, 1.0f);						// Set the drawing colour to blue	glPushMatrix();		glRotatef(rotation, 0.0f, 1.0f, 0.0f);		glTranslatef(-1.8f, -0.2f, -14.0f);		glListBase(nFontList);		glCallLists(6, GL_UNSIGNED_BYTE, "OpenGL");	glPopMatrix();	rotation+=1.0f;	return TRUE;										// Everything Went OK}
have you first tried moving into the screen (glTranslatef(0.0f,0.0f,-10.0f)) and then tried you rotation? This is the way it is done in NeHe''s lesson 14.
Sorry about the glLoadIdentity, i didn''t read your full post before replying...oops :S
how about switching to ortho?
and use rasterpos for moving the text. instead, of your scene.

Bobby

ps: switch the order you rotate and translate in the last source you sent here!!!

[edited by - BGCJR on August 20, 2003 11:31:43 AM]
Game Core
Advertisement
glLoadIdentify reset both GlRotate and glTranslatef. So if you put:


glloadidentify;
gltranslatef( 0.0, 0.0, -1.0);
glRotate(rotation, 0, 1, 0); // this makes a rotation on z=-1.0 axis, because u the draw point is on z=-1.0;
gltranslatef( 0.0, 0.0, 1.0);//reset the point. if u use glloadidentify, it will reset ur rotate, and the object never will rotate
gltranslatef( 0.0, 0.0, -10.0);
(*Draw your object here*)


Just it!

"There are people who live in the reality. We recreate it!"
"There are people who live in the reality. We recreate it!"
I''m afraid I''m a real novice at OpenGL (and this is my first gamedev posting)so I apologise in advance if I''m talking cobblers.

I believe your problem stems from your lumping of the text centring translation in with the position-text-in-screen translation.

[potential rubbish]
The way I''ve learnt to think of it is in reverse order of the transformations you do:-
Currently you create your text at the origin (left edge of text on the origin), then you rotate about the origin (hence the undesired rotation), then finally you plonk the text down in the screen and to the left.
What you want is to create the text starting to the left of the origin (to centre it), then rotate it about the origin, then push it back into the screen.
[/potential rubbish]

I''ve grafted your code into NeHe''s lesson 14 and I think it works with the following mods

GLint RenderScene(GLvoid)								// Here''s where we do all the drawing{		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear the screen and depth buffer		glMatrixMode(GL_MODELVIEW);		glLoadIdentity();		glTranslatef(0.0f, 0.0f, -4.0f);	//<-- Do the translate into screen here		glColor3f(0.0f, 0.0f, 1.0f);						// Set the drawing colour to blue		glPushMatrix();		glRotatef(rot, 0.0f, 1.0f, 0.0f);   	glTranslatef(-1.8f,-0.2f,0.0f);  // <--Do the Text Centring translate here	glListBase(base);			glCallLists(6, GL_UNSIGNED_BYTE, "OpenGL");		glPopMatrix();		rot+=1.0f;		return TRUE;										// Everything Went OK}


(I hope that pastes in ok)

NeHe''s lesson 14 code contains the centring translation as part of the glPrint function and includes a neat bit of auto-centring (your -1.8f seems a bit too far to me, -1.2f seems better, NeHes code is best !!)

Hope that helps
By jove I think he's done it!

shuttlem you little beauty!! :D

The first person to solve my problem correctly.

If you are an OpenGL novice then you're doing very well in my book. I'm not sure why it works but it works. I'll have to look at the code a bit closer tonight to fathom out why it works. Thanks a lot though! Great first post!

And thanks to everyone else who replied. Much appreciated!


[edited by - Steve-B on August 21, 2003 8:10:44 AM]

This topic is closed to new replies.

Advertisement