PDA

View Full Version : Why do i need glRotatef()?



openujs
11-11-2005, 09:43 AM
In the code below, I am trying to draw a red triangle. Funny thing is-nothing draws if I take out the glRotatef( 10.0, 1.0, 0.0, 0.0 ), also, if I rotate 10 degree in y or z axis, again nothing draws - glRotatef( 10.0, 0.0, 1.0, 0.0 ) -nothing draws. Help solve the logic :)

glViewport( 0,0, width , height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();//reset projection matrix
gluPerspective( 54.0, (GLfloat)width/(GLfloat)height, 1.0, 1000.0);
glLoadIdentity();
glTranslatef( 0.0, 0.0, -5.0 );

glRotatef( 10.0, 1.0, 0.0, 0.0 );//I need this, why????!!!!!!Without this I think atleast I should see a straight line. part of triangle.

glBegin( GL_TRIANGLES );
glColorf( 1.0f, 0.0f, 0.0f );//red
glVertex3f( -3.0,0.0, -3.0);
glVertex3f( -3.0,0.0, 3.0 );
glVertex3f( 3.0,0.0, 3.0);

glVertex3f( -3.0,0.0, -3.0);
glVertex3f( 3.0,0.0, -3.0 );
glVertex3f( 3.0,0.0, 3.0);

glEnd();

openujs
11-11-2005, 10:32 AM
second question to that.

this code here - did not display anything at all. what's wrong with this ????!!??!!!!

glBegin( GL_TRIANGLES );
setColor( 1.0f, 0.0f, 0.0f );//red

glVertex3f( -3.0,0.0, -3.0);
glVertex3f( 3.0,0.0, -3.0 );
glVertex3f( -3.0,0.0, -3.0);

glVertex3f( -3.0,3.0, -3.0);
glVertex3f( 3.0,0.0, -3.0 );
glVertex3f( 3.0,3.0, 3.0);

glEnd();

jide
11-11-2005, 10:46 AM
OpenGL default view is along the z-axis and as you draw a triangle on the plane x-z, you'll need to rotate around the x or z axis to see something, here around the x axis because you translate on the z axis.

When you call to translate and rotate, the 'camera' is changing regarding to them.

If, for beeing more easier, you'd like to avoid calling to translate and rotate, just comment them out and call to gluLookAt (eye_x, eye_y, eye_z, aim_x, aim_y, aim_z, up_x, up_y, upz) before drawing your triangle. [ up is generally 0,1,0 ]

Hope that helps.

openujs
11-11-2005, 11:04 AM
Jide, so how come for the second piece of code, it did not draw at all? for the second peice of code, i am not drawing triangle at x-z plane, i am drawing tat x-y place with depth 3.0 ( -3.0 along z axis) from 0,0,0.
I should not be rotate or do anything there? because the triangle with be straight front from viewer eyes or camera.

songho
11-11-2005, 11:55 AM
openujs,
In your code, projection matrix is reset by the second glLoadIdentity(). you missed glMatrixMode(GL_MODELVIEW) to switch from projection matrix to modelview matrix.

Put glMatrixMode(GL_MODELVIEW) right after gluPerspective() and before the second glLoadIdentity().

==song==

jide
11-11-2005, 11:56 AM
It should draw something. For ensuring it, I tested it, and I can see a portion of the triangle on the upper-right corner.

Here is the full display function:


glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();

glBegin( GL_TRIANGLES );
glColor3f( 1.0f, 0.0f, 0.0f );

glVertex3f( -3.0,0.0, -3.0);
glVertex3f( 3.0,0.0, -3.0 );
glVertex3f( -3.0,0.0, -3.0);

glVertex3f( -3.0,3.0, -3.0);
glVertex3f( 3.0,0.0, -3.0 );
glVertex3f( 3.0,3.0, 3.0);

glEnd();

glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();EDIT: Well done songho, I didn't see that.

diegus
11-13-2005, 02:26 AM
Originally posted by openujs:
second question to that.

this code here - did not display anything at all. what's wrong with this ????!!??!!!!

glBegin( GL_TRIANGLES );
setColor( 1.0f, 0.0f, 0.0f );//red

glVertex3f( -3.0,0.0, -3.0);
glVertex3f( 3.0,0.0, -3.0 );
glVertex3f( -3.0,0.0, -3.0);

glVertex3f( -3.0,3.0, -3.0);
glVertex3f( 3.0,0.0, -3.0 );
glVertex3f( 3.0,3.0, 3.0);

glEnd();First 3 vertex are not a triangle... 1st and 3rd points are the same.

openujs
11-14-2005, 06:33 AM
I also realized all 3 vertex donot represent a triangle. once i fixed that it worked.
I noticed it is lot easier to work with GluLookat() .
Thanks a lot for all of you.