Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The aim of my program is to render a simple colored triangle in rotation. The rotations are correct if all Z values of my vertices are equals to 0.0f. If one of these values are different of 0.0f the transformations are not correct (if I retreat the camera, the triangle seems to be in the same place).

The following declaration works correctly :

static GLfloat vertices[9] =
{
   -1.000000f, 0.000000f, 0.000000f,
    0.000000f, 1.000000f, 0.000000f,
    1.000000f, 0.000000f, 0.000000f
};

But the following one not works correctly :

static GLfloat vertices[9] =
{
    -1.000000f, 1.000000f, 1.000000f,
    -1.000000f, 1.000000f, -1.000000f,
    -1.000000f, -1.000000f, -1.000000f
};

So if the polygon is coplanar with the plane (x0y) it's ok, but in the other case it's not correct. I searched to find the problem in my matrix usage in vain. Here's a part of my code :

//Main loop

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

    glClearDepth(1.0f);
    glClearColor(0.13f, 0.12f, 0.13f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(programID);

    //Projection matrix

    glm::mat4 projection = glm::perspective(70.0f, ((float)WIDTH)/(float)HEIGHT, 1.0f, 1000.0f);

    //View matrix

    glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, trans), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));

    //Model matrix

    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(1.0f, 1.0f, 1.0f));

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

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);

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

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

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    glUseProgram(0);

    angle += 0.050000f;
    trans += 0.00010f;

    glFlush();
    SDL_GL_SwapBuffers();
}

and my vertex shader code :

#version 330

in vec3 VertexPosition;
in vec3 VertexColor;

uniform mat4 MV;
uniform mat4 MVP;

out vec3 Color;

void main()
{
    Color = VertexColor;

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

I tried several combinations of code without any success. I'm lost. Does anyone can help me, please ?

Thanks very much in advance for your help.

share|improve this question
    
Can you explain how this is different from your other question? gamedev.stackexchange.com/questions/53341/… If it's not it should be removed and you should un-accept the other answer and update your question with whatever information it needs. –  Byte56 Apr 5 '13 at 20:20
    
I don't know anything about glm but why do you multiply at this point? model *= glm::rotate(model, angle, glm::vec3(1.0f, 1.0f, 1.0f)); Somehow I would expect this: model = glm::rotate(model, angle, glm::vec3(1.0f, 1.0f, 1.0f)); –  Dirk Apr 5 '13 at 20:27

1 Answer 1

up vote 1 down vote accepted

For me, this code looks flawed:

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(1.0f, 1.0f, 1.0f));

The modelviewmatrix you are creating seems to me to be like something as this:

M = I * T * (T * R)

(since glm::rotate(model, ...) creates the matrix T * R)

but I think you want to have something as this:

M = I * T * R

which would require the code ( = instead of *= ):

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(1.0f, 1.0f, 1.0f));
share|improve this answer
    
Thank you very much for your answer. I'm beginner in 3d space transformations. It works fine now. Good Bye. –  user1450730 Apr 5 '13 at 23:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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