Rotating 2D Object

Well I am trying to learn openGL and want to make a triangle move one unit (0.1) everytime I press one of the keyboard arrows. However i want the triangle to turn first pointing the direction where i will move one unit. So if my triangle is pointing up and I press right the it should point right first and then move one unit in the x axis. I have implemented the code to move the object one unit in any direction, however I can not get it to turn pointing to the direction it is going. The initial position of the Triangle is pointing up.

#define LENGTH 0.05

float posX = -0.5, posY = -0.5, posZ = 0;
float inX = 0.0 ,inY = 0.0 ,inZ = 0.0; // what values????

void rect(){
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glTranslatef(posX,posY,posZ);
glRotatef(rotate, inX, inY, inZ);
glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(-LENGTH,-LENGTH);
glVertex2f(LENGTH-LENGTH, LENGTH);
glVertex2f(LENGTH, -LENGTH);
glEnd();
glPopMatrix();

}
• Try put inZ = 1. the last three values of the glRotatef are the values of the vector to which you want to rotate from. You are looking from up, you want to rotate according to z direction. See Notes of cs.rutgers.edu/~decarlo/428/gl_man/rotate.html Nov 2 '12 at 15:02
• Thank you for your response. I have tried this already, I tried putting one in all (x,y,z) however I cannot get the right image. When i put 1.0 in z and pres right i goes looking 45 degrees and becomes a different triangle. Nov 2 '12 at 15:09
• try rotating first and then translating. Nov 7 '12 at 5:52

Every rotation in gl is according to a given vector. Dependending from where you are looking at the triangle, you will have to rotate according to that vector. For instance, if you are looking from z>0, you have to rotate your triangle with

inZ = 1

such that the triangle rotates according to the vector (0,0,1).

UPDATE:

Generally speaking, this code needs some attention. In openGL, you must understand the concept of MODELVIEW, VIEWPORT, and PROJECTION, all the three are matrixes. You are using a PROJECTION matrix to draw your object, which conceptually is not correct (even if you can). In general lines, you have to define your viewport and projection, and only then you can draw a model (e.g. a triangle, or a cat). See this answer, which has some insight about these concepts.

• Thank you for your response! I tried with every Vector and nothing goes ok. Is there a way to know where I am looking from, or maybe set it up in my code? Thanks again! Nov 2 '12 at 15:11
• Thank you, yeh I am sarting this so I do not have everything really clear. See I think I have achieved something. When I set the x to 1 this works with the up and down button however not with the left or right button. I am finding this really hard to understand. Nov 2 '12 at 16:09
• @Vico Pelaez In this link: sites.google.com/site/jorgecarleitao/code-reservoir/opengl I present a minimal example for using openGL with Qt. Even if you are not using QT, the scene.cpp's methods updateViewPort, updateModelView, updateProjection and drawBackground should be a good starting point to implement in glut. Nov 2 '12 at 16:16

glRotatef() creates a rotation around the axis specified by the vector. Try glRotatef(angle, 0, 0, 1); to rotate around the Z-axis.

For example:

// Set up orthogonal 2D Projection matrix
glMatrixMode(GL_PROJECTION);
glOrtho(0, screenWidth, 0, screenHeight, -1, 1);

// Set up View matrix
glMatrixMode(GL_MODELVIEW);