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(){
    glRotatef(rotate, inX, inY, inZ);
    glColor3f(0.0, 0.0, 1.0);
    glVertex2f(LENGTH-LENGTH, LENGTH);
    glVertex2f(LENGTH, -LENGTH);

  • \$\begingroup\$ 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 \$\endgroup\$ Commented Nov 2, 2012 at 15:02
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 2, 2012 at 15:09
  • \$\begingroup\$ try rotating first and then translating. \$\endgroup\$
    – Ray Tayek
    Commented Nov 7, 2012 at 5:52

2 Answers 2


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).


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.

  • \$\begingroup\$ 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! \$\endgroup\$ Commented Nov 2, 2012 at 15:11
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 2, 2012 at 16:09
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Nov 2, 2012 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
glOrtho(0, screenWidth, 0, screenHeight, -1, 1);

// Set up View matrix
glTranslatef(-cameraX, -cameraY, -cameraZ);

// Set up Model * View matrix
glTranslatef(modelX, modelY, modelZ);
glRotatef(modelAngle, 0, 0, 1);
// draw model

Pay attention to the order of matrix operations.

  • \$\begingroup\$ 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! \$\endgroup\$ Commented Nov 2, 2012 at 15:50
  • \$\begingroup\$ The default camera in OpenGL is set up to show everything inside a cube [-1 .. 1] in all dimensions, looking down the positive Z axis. Anything outside this cube will get clipped away. Set up the projection (size of the world to be shown on the screen) with the GL_PROJECTION matrix and glOrtho(). Then move the camera to where you want it using the GL_MODELVIEW matrix and glRotate and glTranslate (or gluLookAt). Then rotate your objects, again using the GL_MODELVIEW matrix. \$\endgroup\$
    – ccxvii
    Commented Nov 6, 2012 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .