senham
senham

Reputation: 131

Opengl rotation doesn't work

I try understand opengl, but I have many simple problems... I try rotate my object and I'm doing this:

glBindVertexArray(VAOs[1]);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glUniform1i(glGetUniformLocation(theProgram.get_programID(), "Texture1"), 1);

glMatrixMode(GL_MODELVIEW_MATRIX);
glPushMatrix();
glRotatef(10.0f, 0.0f, 0.0f, -0.1f);
glDrawElements(GL_TRIANGLES, _countof(tableIndices2), GL_UNSIGNED_INT, 0);
glPopMatrix();

And my object is still in the same position. What is wrong?

Upvotes: 0

Views: 1534

Answers (3)

Rabbid76
Rabbid76

Reputation: 210928

Functions like glRotate are part of the See Fixed Function Pipeline which is deprecated. This functions do not affect the vertices which are processed by a shader program.

See Khronos wiki - Legacy OpenGL:

In 2008, version 3.0 of the OpenGL specification was released. With this revision, the Fixed Function Pipeline as well as most of the related OpenGL functions and constants were declared deprecated. These deprecated elements and concepts are now commonly referred to as legacy OpenGL. ...

See Khronos wiki - Fixed Function Pipeline:

OpenGL 3.0 was the last revision of the specification which fully supported both fixed and programmable functionality. Even so, most hardware since the OpenGL 2.0 generation lacked the actual fixed-function hardware. Instead, fixed-function processes are emulated with shaders built by the system. ...


In modern OpenGL you have to do this stuff by yourself.

If you switch from Fixed Function Pipeline to today's OpenGL (in C++), then I recommend to use a library like glm OpenGL Mathematics for the matrix operations.

First you have to create a vertex shader with a matrix uniform and you have to do the multiplivation of the vertex coordinate and the model matrix in the vertex shader:

in vec3 vert_pos;

uniform mat4 model_matrix;

void main()
{
    gl_Position = model * vec4(vert_pos.xyz, 1.0);
}

Of course you can declare further matrices like projection matrix and view matrix:

in vec3 vert_pos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(vert_pos.xyz, 1.0);
}

In the c++ code you have to set set up the matrix and you have to set the matrix uniform:

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> // glm::rotate
#include <glm/gtc/type_ptr.hpp>         // glm::value_ptr


glm::mat4 modelMat = glm::rotate(
    glm::mat4(1.0f),
    glm::radians(10.0f),
    glm::vec3(0.0f, 0.0f, -1.0f) );

GLint model_loc = glGetUniformLocation(theProgram.get_programID(), "model");

glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(modelMat)); 

See also the documentation of the glm matrix transformations.

Upvotes: 2

Tom&#225;š Dejmek
Tom&#225;š Dejmek

Reputation: 151

Firstly

glMatrixMode(GL_MODELVIEW); // don't change to GL_MODELVIEW_MATRIX
glLoadIdentity(); // good habit to initialize matrix by identity.
glPushMatrix();

Your call of function

glRotatef(10.0f, 0.0f, 0.0f, -0.1f);

should be same as

glRotatef(10.0f, 0.0f, 0.0f, -1f);

meaning ten degrees by z-axis. According to documentation of glRotatef(angle, x, y, z) - vector (x, y, z) is normalized to size 1 (if is not of size 1).

It should work. Is 10 degrees really significant to see result? Try to do something movable like.

glRotatef(rotZ, 0.0f, 0.0f, -1f);
rotZ += 5;
if (rotZ > 360) rotZ -= 360;

Upvotes: 0

Cpp plus 1
Cpp plus 1

Reputation: 1010

The problem with your code is that you are mixing the old fixed-function calls (like glPushMatrix()) with the newer shader calls (like glUniform()). Functions like glPushMatrix() assume that you are giving the OpenGL driver more control over the graphics card (in other words, these functions take care of some newer OpenGL complications for you), which means that using newer function calls, like glUniform() can lead to random results (since you have no idea if you are disturbing the OpenGL driver's code).

Basically, all of the old fixed-function calls can be found here (this is actually OpenGL ES, which is opengl for embedded systems, or phones, in other words, but it is practically the same as opengl on the computer): Fixed-Function Calls.

I would recommend that you start learning the OpenGL ES 2 functions, which can be found here: OpenGL ES 2 (Shader Function Calls)

I think this is a good site to learn OpenGL ES 2 (even though you're not using OpenGL ES, this site teaches the concepts well). This site also uses Java, so ignore all the buffer information in the first tutorial (not to be confused with vertex and texture buffers, which are actually OpenGL, and not Java details): Learn OpenGL Concepts

Upvotes: 1

Related Questions