Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The goal of my program is to display a simple colored triangle rotating on the Y axis without any translation. I'm using the GLM library. The problem is that the transformations of my triangle are not correct : it has a strange and not logical behaviour. Here's my c++ code plus the vertex shader and fragment shader code :

C++ code :

float angle = 0.0f;

struct Vertex
    float x, y, z;

static Vertex vertices[9] =
    -1.000000f, 0.000000f, 0.000000f,
    0.000000f, 1.000000f, 0.000000f,
    1.000000f, 0.000000f, 0.000000f

static Vertex colors[9] =
    1.000000f, 0.000000f, 0.000000f,
    0.000000f, 1.000000f, 0.000000f,
    0.000000f, 0.000000f, 1.000000f


main(int ac, char **av)
    bool            continuer = true;
    SDL_Event       event;
    GLuint          vboID[2];
    GLuint          programID = 0;

    //SDL window initialization

    SDL_WM_SetCaption("VBO tests",NULL);
    SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);

    //Viewport initialization

    glViewport(0, 0, WIDTH, HEIGHT);

    //Projection initialization

    glm::mat4 projection = glm::mat4(1.0f);
    projection = glm::perspective<GLfloat>(60.0f, (float)(WIDTH/HEIGHT), 1.0f, 1000.0f);

    //View initialization

    glm::mat4 view = glm::lookAt<GLfloat>(glm::vec3(0.0f, 0.0f, 8.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));


    //Shaders initialization

    programID = initShaders("triangle.vert", "triangle.frag");

    //Main loop

    while (continuer)
        eventListener(&event, &continuer);

        glClearColor(0.13f, 0.12f, 0.13f, 1.0f);


        //Model transformations

        glm::mat4 model = glm::mat4(1.0f);
        model *= glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
        model *= glm::rotate(model, angle, glm::vec3(0.0f, 1.0f, 0.0f));

        glm::mat4 ModelViewMatrix = model * view;
        glm::mat4 ModelViewProjectionMatrix = projection * ModelViewMatrix;

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, colors);


        glUniformMatrix4fv(glGetUniformLocation(programID, "ModelView"), 1, GL_TRUE, glm::value_ptr(ModelViewMatrix));
        glUniformMatrix4fv(glGetUniformLocation(programID, "MVP"), 1, GL_TRUE, glm::value_ptr(ModelViewProjectionMatrix));

        glDrawArrays(GL_TRIANGLES, 0, 3);



        angle += 0.020f;


    return (0);

Vertex shader code :

#version 330

in vec3 VertexPosition;
in vec3 VertexColor;

uniform mat4 ModelViewMatrix;
uniform mat4 MVP;

out vec3 Color;

void main()
    Color = VertexColor;

    gl_Position = MVP * vec4(VertexPosition, 1.0f);

Fragment shader code :

#version 330

in vec3 Color;
out vec4 FragColor;

void main() {
    FragColor = vec4(Color, 1.0);

When I declare just a simple matrix rotation for the model, I send it to the vertex shader and I use it as MVP and it works. But when I add the projection matrix + the view matrix it does not work correctly. Normally, the triangle should be rotating at the center of the screen.The current animation shows the triangle appearing on the right of the screen and disappearing on the left side.

Does anyone can help me, please ?

Thanks in advance for your help.

share|improve this question

closed as too localized by Byte56, bummzack, Josh Petrie, Sean Middleditch, msell Apr 8 '13 at 6:20

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 2 down vote accepted

Just set 'the view matrix' to identity and then translate to like -6.0 on the z axis.

You are not using lookAt() correctly. The first paramater is the position you are at (the camera) and the second is where you look at.

share|improve this answer
I replaced glm::lookat call by glm::mat4 view = glm::mat4(1.0f); view = glm::translate(view, glm::vec3(0.0f, 0.0f, -6.0f)); and this the same result. I always tried that before. My triangle appears on the right side and disappears on the left one. Maybe my vertex shader code is not correct. – user1450730 Apr 5 '13 at 11:07
It works fine now, thank you for your answer. Bye. – user1450730 Apr 5 '13 at 12:36
You're also not setting your aspect ratio correctly; instead of "(float)(WIDTH/HEIGHT)" you should be using "((float)WIDTH/HEIGHT)" - can you see the difference? – Le Comte du Merde-fou Apr 5 '13 at 15:31
Yes I didn't see that mistake. Thank you for this remark. – user1450730 Apr 5 '13 at 19:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.