I am woring on a C++ OpengL program and GLSL Vertex and Fragment shaders.
I am creating several instances of the same object. I just have to change object position between instances.
Here is what i've done: I am working with an uniform variable which is an array of transformation matrix. Each matrix stands for an object instance.
MVP is a transformation matrix too but MVP is set by camera position, orientation and properties.
Here is my vertex shader:
#version 330 core
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_color;
uniform mat4 object_positions[20];
out vec3 fragment_color;
uniform mat4 MVP;
void main()
{
gl_Position = object_positions[gl_InstanceID] * MVP * vec4(vertex_position,1.0);
fragment_color = vertex_color;
}
Here is what i have to do in C++ program to set objects positions:
glm::mat4 object_positions[20];
object_positions[0] = glm::translate(glm::mat4(1), glm::vec3(0.4f,0.2f,0.0f));
object_positions[1] = glm::translate(glm::mat4(1), glm::vec3(0.5f,1.4f,0.0f));
...
object_positions[19] = glm::translate(glm::mat4(1), glm::vec3(-10.6f,0.2f,0.0f));
GLuint object_positions_id = glGetUniformLocation(program_id, "object_positions");
...
glUniformMatrix4fv(object_positions_id, 7, GL_FALSE, glm::value_ptr(object_positions[0]));
The vec3 you see as second argument of glm::translate contains each object position. Every thing works fine at this point.
What i want to do is to compute of glm::translte in shader. What i want in fact is to send a vec3 instead of a mat4 for each position. I want the GPU to compute the transformation matrix instead of the CPU. Everything i tried does not work.
Thanks
w=0
? You leavew
alone. Your inputw
is guaranteed to be 1.0 with that shader anyway, so you can directly apply an offset inxyz
. – derhass