How to apply position conversion

How to apply the position of drawing in the world through shaders?

My vertex shader looks like this:

in vec2 position; uniform mat4x4 model; uniform mat4x4 view; uniform mat4x4 projection; void main() { gl_Position = projection * view * model * vec4(position, 0.0, 1.0); } 

Where position are the positions of the vertices of the triangles.

I link the matrices as follows.

View:

 glm::mat4x4 view = glm::lookAt( glm::vec3(0.0f, 1.2f, 1.2f), // camera position glm::vec3(0.0f, 0.0f, 0.0f), // camera target glm::vec3(0.0f, 0.0f, 1.0f)); // camera up axis GLint view_uniform = glGetUniformLocation(shader, "view"); glUniformMatrix4fv(view_uniform, 1, GL_FALSE, glm::value_ptr(view)); 

projection:

 glm::mat4x4 projection = glm::perspective(80.0f, 640.0f/480.0f, 0.1f, 100.0f); GLint projection_uniform = glGetUniformLocation(shader, "projection"); glUniformMatrix4fv(projection_uniform, 1, GL_FALSE, glm::value_ptr(projection)); 

model conversion:

 glm::mat4x4 model; model = glm::translate(model, glm::vec3(1.0f, 0.0f, 0.0f)); model = glm::rotate(model, static_cast<float>((glm::sin(currenttime)) * 360.0), glm::vec3(0.0, 0.0, 1.0)); GLint trans_uniform = glGetUniformLocation(shader, "model"); glUniformMatrix4fv(trans_uniform, 1, GL_FALSE, glm::value_ptr(model)); 

Thus, I have to calculate the conversion of the position of each frame in the CPU. Is this the recommended way or is there a better one?

+4
source share
2 answers

Thus, I have to calculate the conversion of the position of each frame in the CPU. Is this the recommended way or is there a better one?

Yes. Calculating a new transformation once for the grid on the CPU, and then applying it to all the vertices inside the grid inside the vertex shader will not be a very slow operation and should be performed in each frame.

+2
source

In the render() method, you usually do the following

  • create a matrix for the camera (once for each frame)
  • for each object in the scene:
    • create a transformation matrix (position)
    • drawing object

a projection matrix can be created once per windowResize or when creating a matrix for the camera.

Answer: Good code , this is the basic way to draw / update objects.

You can enter some system / system that automatically controls it. You do not have to worry (right now) about completing these matrix creation procedures ... it is very fast. Drawing is more problematic.

as jozxyqk wrote in one comment, you can create a ModelViewProjMatrix and send one combo matrix instead of three different ones.

+2
source

Source: https://habr.com/ru/post/1499383/


All Articles