I'm trying to animate 2d sprites using glDrawArraysInstanced()
, and I'm having trouble separating for each "cycle" ran through the pipeline.
according to one of the tutorials I'm reading, it says:
"Send the age of each particle along with the position, and in the shaders, compute the UVs"
But without clarifying exactly how to do so. This is what my vert shader looks like atm:
#version 400 core
layout ( location = 0 ) in vec3 vertex_position; //vertices, 1 box
layout ( location = 1 ) in vec2 tex_cord; //1 tex cord
layout ( location = 2 ) in vec4 color_vec4; //1 vec4 color per texture
layout ( location = 3 ) in mat4 in_mat; //4 vec4 to make 1 mat4
//flat out int InstanceID; //use but how?
out vec2 UV;
out vec4 color4;
void main()
{
gl_Position=in_mat*vec4(vertex_position,1.0);
color4=color_vec4;
UV=tex_cord;
}
And my frag shader:
#version 400 core
in vec2 UV;
in vec4 color4;
uniform sampler2D png_tex; //texture atlas
void main()
{
gl_FragColor=texture(png_tex,UV)*color4;
}
Does anyone know how to get the vec2-uv coordinate to change/cycle, when using instanced drawing?
Solution:
adding this to my vert shader:
UV=vec2
(
(tex_cord.x/info_vec4.z)+(1.0/info_vec4.z*info_vec4.x),
(tex_cord.y/info_vec4.w)+(1.0/info_vec4.w*info_vec4.y)
);
I know this can be done cleaner, but I'm not sure exactly how atm. This works however, and its alot faster than having to pass a new png/texture per update. The explanation for it is: info_vec4 is an ivec that I pass as an attribute with information of the current primitive/triangle. It holds: x=xcord (1-5), y=ycord (only 1), z=number of x frames total, and w=number of y total. The trick was figuring out how to iron out UV.x=0, basically by adding 1*somevalue to 0.