-1

Im actually learning some OpenGL, i managed to load a mesh and show it in a window. Then i made a function with SDL that let me rotate the mesh with the arrow keys. But when i dived into Shaders i made some changes, but i cannot get a simple rotation for the 3d model.

Here is my drawing function:

        glMatrixMode(GL_MODELVIEW); //set the matrix to model view mode

        glPushMatrix(); // push the matrix

        glRotatef(1.0,0.0,0.0,1.0); //apply transformation

        //Primer buffer, modelo
        glBindBuffer(GL_ARRAY_BUFFER, _vboID);

        glEnableVertexAttribArray(0);

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

        glDrawArrays(GL_TRIANGLES, 0,vertices.size());

        glDisableVertexAttribArray(0);

        glBindBuffer(GL_ARRAY_BUFFER, 0);

        glPopMatrix();//pop the matrix

        glMatrixMode(GL_PROJECTION); // Apply projection matrix again

Theese are my shaders:

Fragment Shader( only for give color)

#version 130

out vec3 color;

void main()
{
    color = vec3(0.0,0.5,1.0);

}

Vertex Shader(Used it to mess around and try):

    #version 130

in vec3 vertexPosition;

void main()
{
    gl_Position.x = vertexPosition.x;
    gl_Position.y = vertexPosition.y-1.0;
    gl_Position.z = vertexPosition.z;
    gl_Position.w = 1.0;
}

SOURCE: I actually used this to get a reference, but didnt do the trick: OpenGL - Object Transformations and VBOs

EDIT: The problem is that the "glRotatef" command in the drawing function doesn't rotate the mesh...

EDIT2: see comments

void GLSLProgram::use()
{
    glUseProgram(_shaderEnteroID);
    for(int i = 0; i< _numeroAtributos; i++)
    {
        glEnableVertexAttribArray(i);
    }
}
void GLSLProgram::unuse()
{
    glUseProgram(0);
    for(int i = 0; i< _numeroAtributos; i++)
    {
        glDisableVertexAttribArray(i);
    }
}
Community
  • 1
  • 1
Azokasion
  • 3
  • 4
  • 3
    You're not actually applying a transformation in your vertex shader. Once you use shaders, you need to apply transformations in your GLSL code. It's not going to just happen automatically. Any tutorial should show you how to do that. – Reto Koradi Jul 13 '15 at 04:17
  • Sorry, i forget to put the two functions that i use in the drawing process: _colorShader.use() and _colorShader.unuse(). i dont understand what you mean, i use "glUseProgram(_ShaderEnteroID) " ( its my shader program), and then i do the drawing, with the transformations, is that wrong? – Azokasion Jul 13 '15 at 04:29
  • 1
    When you use shaders, the shaders do the transformation. You're supposed to multiply the vertex positions by a rotation matrix inside the shader. Immediate mode stuff won't work with shaders. Or at least not very well I think – Alex Jul 13 '15 at 04:36
  • What happens is that the vertex data you buffered into the GPU is then passed into the vertex shader. You then shifted every vertex over by 1 in the y axis in the shader, which becomes the output of the vertex shader. That gets rendered. I don't know if `glRotatef()` will do anything at all when there's a vertex shader. I don't think mixing immediate mode and programmable pipeline is a good idea either – Alex Jul 13 '15 at 04:41
  • And how can i do this? sorry for the trobules, im a newb. Some documentation at least?, maybe I'm googling with wrong parameters. I see now that ive been mixing immediate mode with the pipeline, that was my error – Azokasion Jul 13 '15 at 04:42
  • http://learnopengl.com/ and https://open.gl/ are the sites that I really recommend for modern OpenGL. There's also http://www.opengl-tutorial.org/ and http://ogldev.atspace.co.uk/ . I really like open.gl because it's very simple and to the point. I'm following through learnopengl.com and OpenGL Superbible currently for the depth – Alex Jul 13 '15 at 04:50
  • Ty!, i'll bookmark them! – Azokasion Jul 13 '15 at 04:57

1 Answers1

6

Your problem is, that you're essentially mixing two styles of OpenGL, which are, well not incompatible, but one did replace the other.

So there is the old style, fixed function pipeline OpenGL (without shaders). In this you'd set the transformation matrix with the matrix manipulation functions of which glRotate is one (the others are glLoadIdentity, glLoadMatrix, glMultMatrix, glTranslate, glScale and glOrtho). These functions effect on the value of either the modelview, the projection, the texture or the color matrix. Which of these is determined by the matrix mode, which is set using glMatrixMode. Upon drawing something, these matrices' values are then used in a fixed chain of transformations (vertex positions get multiplied by first the modelview matrix, some illumination values are determined then the projection matrix gets applied).

Shaders do not care about these fixed function matrices! Or rather, in OpenGL-2.x and compatibility profile OpenGL-3 the fixed function matrices are available to the shaders as a set of predefined variables. In new code you should not use them. Which means, that the whole bunch of old style OpenGL matrix functions should not be used; actually must not be used if you want your code to be future proof, as these functions are not available in core profile (or forward compatible) OpenGL versions.

Rather you're supposed to construct the transformation matrices with a proper matrix math library (the old style OpenGL functions always have been a poor substitute for such one) and pass the resulting matrices to the shaders as a couple of custom defined uniform variables. Then in the shader you're applying the appropriate transformation operations yourself.


To recapitulate: glRotate merely effects on values in the currently selected old style, fixed function matrix. glRotate does not affect vertex positions. Vertex positions are only transformed upon drawing with the currently set values of the set of transformations matrices.

With shaders everything of the above was removed and is your responsibility of being the programmer to program the desired transformation chain and all the required steps to set it up (creating transformation matrices, applying the transformations in the shader).

datenwolf
  • 159,371
  • 13
  • 185
  • 298