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?