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();
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 ]
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.
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().
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.