L
L
lmdsfmioi2014-11-14 00:33:39
OpenGL
lmdsfmioi, 2014-11-14 00:33:39

What is the logic behind rotating an object (vbo) with OpenGL using GLSL?

Hello,
I'm using OpenGL 4.x, shaders along the way, drawing Patrick from spongebob to make it more fun =)
Here it is, get acquainted:
a6d081e01b5647d0abac0a78fe4c4322.png
There is obviously a mistake in the rotation implementation . Although the turn itself occurs, but sometimes with jerks, at different speeds.
A bit of theory
It is clear that rotation relative to the origin is achieved by multiplying the Rotation Matrix (MT)
6d8465ea169c0ff442a4b60070e07564.pngby the coordinate vector
|x|
|y|
|z|
Now to the question of the implementation of the
Vertex Shader:

#version 400\n 
     layout(location = 0) in vec3 vertex_position; 
     layout(location = 1) in vec3 vertex_colour; 
     layout(location = 2) in vec3 modifier_pos; 
     layout(location = 3) in int angle; 
     in vec3 vp; 
     smooth out vec3 colour; 
     void main () { 
     	colour = vertex_colour; 
     	vec3 rot1 = vec3(cos(radians(angle)), sin(radians(angle)), 0); 
     	vec3 rot2 = vec3(-sin(radians(angle)), cos(radians(angle)), 0); 
     	vec3 rot3 = vec3(0, 0, 1); 
     	mat3 rot = mat3(rot1, rot2, rot3); 
     	gl_Position = vec4 ( ((vertex_position + modifier_pos) * rot), 1.0); 
     } ;

The ideology is as follows:
  • vertex_position -- set once before first render
  • vertex_colour -- vertex color
  • modifier_pos -- some offset to move the object along the axes
  • three vestors rot1, rot2, rot3 -- MP columns
  • mat3 -- MP itself
The code is implemented as follows: (for some reason, the tab stop was broken)
When rendering each frame, there is a condition on the rotation key ("R")
if (glfwGetKey(window, GLFW_KEY_R)) {
      std::cout << "detected R btn" << std::endl;
      for (int i = 0; i < modelLoader.vertexs_counter; i++) {	
        if (rotArray[i] == 360) {
          rotArray[i] = 0;
        }
        rotArray[i] += 1;
      }
      glBindBuffer(GL_ARRAY_BUFFER, uiVBO[4]);
      glBufferData(GL_ARRAY_BUFFER, modelLoader.vertexs_counter * sizeof(float), &rotArray[0], GL_STATIC_DRAW);
      glEnableVertexAttribArray(3);
      glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 0, 0);
    }

vertexs_counter here is actually not an honest number of vertices, but an honest number of components (x, y, z), i.e. multiplied by three.
Is the place for the turn chosen correctly, and its implementation itself?
In general, he turns, does not beat at the same time. But it turns in jerks, at small angles very quickly, at angles of about 300 degrees it starts to turn more slowly, then again quickly when the angle is reset. Jerks I called sharp jumps of about 90 degrees in the direction opposite to the turn. I notice two such jerks for a full turn.
Here is the final result of Patrick's rotation:
cbc020e2d5754c45b8b801e02f29bc5a.pngPS . I have an idea in my head that I did not understand the essence of using vbo, and as a result, I use the wrong approach to drawing and / or changing an object.
Thanks for reading. The advice of experienced and not very people will be glad.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SHVV, 2014-11-14
@SHVV

VBO is made for that, to load it into the GPU once and not touch it again.
The fact that you are actually modifying the vertices to rotate is absolutely not true.
To transfer the transformation matrix to the shader, you need to use Uniforms.
Here is a simple lesson with a rotating cube and using VBO.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question