Public Group

rotating in openGL

This topic is 4165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

hi, looked up the nehe tutorials and cant see where going wrong so is possible to not only help me but explain how it happened please thanks.. sorry for longish paste job :S
#include <windows.h>
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library
#include <gl\glut.h>
#include <stdio.h>

//bool textureM;
BOOL textureM=FALSE;
GLfloat rtri;

void display();
void drawCube();
void setupCube();

/* Callback function that is invoked when the window's size is changed,
and when the system starts up */
void keys(unsigned char, int, int);
void mouse(int, int, int, int);
void init(void);

int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D Programming Assignment");
//init();
glutDisplayFunc(display);
glutReshapeFunc(AdjustSize); /* tells the Open GL system what the
Reshape Callback function is called*/
//setupCube();
//drawCube();

glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glutMainLoop();
return 0;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
//glRectf(0.0f,0.0f,250.0f,200.0f); /* draw a rectangle */

//glBegin(GL_POLYGON);
//glColor3f(0.0f,1.0f,0.0f);

glBegin(GL_TRIANGLES);								// Start Drawing A Triangle
glColor3f(1.0f,0.0f,0.0f);						// Set Top Point Of Triangle To Red
glVertex3f( 75.0f, 100.0f, 0.0f);					// First Point Of The Triangle
glColor3f(0.0f,1.0f,0.0f);						// Set Left Point Of Triangle To Green
glVertex3f(50.0f,50.0f, 0.0f);					// Second Point Of The Triangle
glColor3f(0.0f,0.0f,1.0f);						// Set Right Point Of Triangle To Blue
glVertex3f( 100.0f,50.0f, 0.0f);					// Third Point Of The Triangle
glEnd();

//glVertex3f(-1.0f, 1.0f, 0.0f);				// Top Left
//glVertex3f( 1.0f, 1.0f, 0.0f);				// Top Right
//glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
//glVertex3f(-1.0f,-1.0f, 0.0f);
glFlush();
}

void drawCube() {
}

void setupCube() {

}

{
glViewport(0,0,w,h); 				/* Create a viewport to view the scene */
glMatrixMode(GL_PROJECTION); 		/*Load the projection matrix */
glOrtho(0.0f,250.0f,0.0f,250.0f,1.0f,-1.0f); /* This sets the matrix up for orthogonal 									viewing */
glMatrixMode(GL_MODELVIEW); 			/* Reload the modelview matrix */
}

void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
//do something
glRotatef(rtri,0.0f,1.0f,0.0f); //This is performed first angle x,y,z
rtri+=0.2f;
}
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
//do something
}
/*If what you did alters the screen in anyway call the display
function */
display();
}

void keys(unsigned char key, int x, int y) {
{
if(key == 'r')
{
// glScalef(2.0f,1.0f,1.0f); //change the values using these transforms.
//glTranslatef(5.0f,0.0f,0.0);
//glTranslatef(-1.5f,0.0f,-6.0f);
glRotatef(rtri,0.0f,1.0f,0.0f); //This is performed first angle x,y,z
rtri+=0.2f;
}   //If this results in the image changing in anyway, call the display callback function
if(key == 't')
{
if(textureM == FALSE)
{
textureM = TRUE;
//put textures on all objects
//break;
}
else {
textureM = FALSE;
//switch back to state without any textures
//break;
}
}
display();
}
}

void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}

FILE * pFile;
}


what happens is hard to explain, just wrecks the image when on screen alot of time not even there so unsure as to why tbh. think could be because displaying a 2d image and think trying to rotate a 3d image. but new to this, so probably wrong

Share on other sites
HI,

This is from a NeHe tutorial right ??

Does he even use GLUT in any of his examples ?

Can you explain the problem a bit more, like does it 'wreck' the image as soon as it's rendered?, when you resize, when mouse down, when key down?
Whats the trigger for the problem.
(Screenshot might help)

I've not used GLUT myself, but whats going on in your main loop ?

Share on other sites
well actually with the triangle just disappears from screen.

no he doesnt use glut sorry was my bad, i been reading from a tuturial elsewhere although i have looked at nehe tutorials quite abit. this way is supposed to work though.

in main loop just the usual really but looks for mouse and keyboard input.

Share on other sites
OK,

Well rule out possible causes first, and just get a triangle rendering.

Remove lighting, remove rotation, what about culling, is it enabled ?

Share on other sites
thanks for help

but everything is fine, until you press 'r', its all perfect.
pressing r causes it to disappear, so know its that exact code around that part.

it's just wrong been messing with the figures but unsure what is wrong. maybe something significant is missing :S . probably in fact but some more info on subject be awesome]

thanks

Share on other sites
ok, that clears up the problem a little more.

are you trying to increament the rotation slowly, like each key 'press'
or increament kind of smoothly, by 'holding' the key down.

keys['r'] = FALSE;

right after :
rtri+=0.2f;

maybe the rotation speed increaments so fast, by holding the key it could be problem (wild guess)

or slow down the rotation, and you might get a more 'visual' idea of what happens to it, before the problem (set rotaion to 0.002f)

Share on other sites
Hi!

For starters, you aren't doing steady rotations here, each rotation you do is multiplied with the current one.

I'd recommend changing your code a bit:
- do only the rotation increment on keypress ( rtri+=0.2f; )
- move the glRotatef call to your display() function
- put a glLoadIdentity() call at the beginning of your display() function

Remember, if you don't load the identity matrix each frame, you will be adding to the last rotation you made.

See what it does like this, also try playing around with the amount of rotation you do each keypress.

[EDITed for a little clarity]

Share on other sites

First of all, I would *strongly* recommend you call glLoadIdentity() at the beginning of every frame. I believe this is just a good habit to get into.

Second, the rotations you are performing aren't wrong; they're just odd. Every time you want to rotate, you are adding 0.2 to the global rotation value, and then adding the global rotation value to the current rotation already applied to the triangle from previous calls.

I would suggest making this change in the keyboard function (and the mouse function, but its basically the same thing):

   if(key == 'r')    {		rtri+=0.2f;   }

I would then change the display function: (a couple new lines indicated with // New stuff, and the triangle vertices indicated with // Changed):

void display(){   glClear(GL_COLOR_BUFFER_BIT);   glLoadIdentity()     // New stuff   //glRectf(0.0f,0.0f,250.0f,200.0f); /* draw a rectangle */   //glBegin(GL_POLYGON);   //glColor3f(0.0f,1.0f,0.0f);   glTranslatef(150.0, 150.0, 0.0)       // New stuff   glRotatef(rtri, 0.0f, 1.0f, 0.0f)     // New stuff	glBegin(GL_TRIANGLES);								// Start Drawing A Triangle		glColor3f(1.0f,0.0f,0.0f);						// Set Top Point Of Triangle To Red		glVertex3f(-25.0f, -25.0f, 0.0f);	// Changed				// First Point Of The Triangle		glColor3f(0.0f,1.0f,0.0f);						// Set Left Point Of Triangle To Green		glVertex3f(25.0f,-25.0f, 0.0f);		// Changed			// Second Point Of The Triangle		glColor3f(0.0f,0.0f,1.0f);						// Set Right Point Of Triangle To Blue		glVertex3f( 0.0, 25.0, 0.0f);		// Changed			// Third Point Of The Triangle	glEnd();	   		//glVertex3f(-1.0f, 1.0f, 0.0f);				// Top Left		//glVertex3f( 1.0f, 1.0f, 0.0f);				// Top Right		//glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right		//glVertex3f(-1.0f,-1.0f, 0.0f);   glFlush();}

I haven't looked at your code too closely, but I'm pretty sure that's what you want. This should cause the triangle to rotate 0.2 degrees every time you click the left mouse button or press 'r'. Also, this should rotate the triangle about its center, rather than about the origin.

Share on other sites
also some additional problems I noticed:

- you have your near and far clipping planes set too near each other (and reversed for that matter). Any point outside the [-1,1] range will be clipped, which most of your stuff will be once you rotate it a little bit. You should change it to a reasonable range (look at the documentation of the glOrtho function if you're not sure how to set it up right)

- you should specifiy the size of your window and use those values in glOrtho to get a viewport matching your window size.

Share on other sites
There is one other thing wrong in your program I beleive :

void mouse(int btn, int state, int x, int y)
{
(...)
/*If what you did alters the screen in anyway call the display
function */
display();
}

no, you don't call display yourself, you mark your windows content to be invalidate !

I think the correct function in glut is

glutPostRedisplay();

So I guess you want to change to

void mouse(int btn, int state, int x, int y)
{
(...)
/*If what you did alters the screen in anyway call the display
function */
glutPostRedisplay();
}

I don't know what it does to aggressively bypass glut processing, maybe it makes some distortion.

was that in the NeHE tutorial or did you add it ?

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
Rutin
10

• 18
• 13
• 14
• 76
• 22
• Forum Statistics

• Total Topics
632140
• Total Posts
3004355

×