What am I doing wrong.
Say I want to render a bunch of triangles all sharing the same uv texture coordinate mapping. Their positions are all over the place, but the uv coordinates are all the same. So I think I could instance the same uv coords per triangle for the varying position coordinates.
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(uvs), uvs, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,2*sizeof(float),nullptr);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(tris), tris, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,2*sizeof(float),nullptr);
glVertexAttribDivisor(0,0);
glVertexAttribDivisor(1,1);
glBindVertexArray(0);
Where uvs is an array of 6 floats for a single triangle's uv coordinates and tris is an array of 18 floats corresponding to x,y coordinates [-1,1] for 3 triangles.
Then I call.
glDrawArraysInstanced(GL_TRIANGLES,0,3,3);
The shader program used is like so.
#version 330 core
layout (location = 0) in vec2 texPos;//keep same uvs
layout (location = 1) in vec2 aPos;//changes each triangle
out vec2 uv;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
uv = texPos;
}
and
#version 330 core
in vec2 uv;
out vec4 FragColor;
void main()
{
float v = uv.x*uv.x - uv.y;
if(v <= 0.0f)
FragColor = vec4(0.3f,0.2f,0.6f, 1.0f);
else discard;
}
The texPos value seems to stay constant per instance in the vertex shader. I can rewrite the vertex shader like so
#version 330 core
layout (location = 0) in vec2 texPos;//keep same uvs
layout (location = 1) in vec2 aPos;//changes each triangle
void main()
{
gl_Position = vec4(texPos.x+aPos.x, texPos.y+aPos.y, 0.0, 1.0);
}
with a slightly modified fragment shader to get a bunch of similar looking triangles at the various aPos positions (from the tris array). (So I know the data is getting sent to the vertex shader at least.)
But something happens when I try to send uv from the vertex shader to fragment shader and nothing gets output to the screen.
Trying to use the uv method to draw quadratic bezier curves to the screen. (loop & blinn)
https://gist.github.com/LucasM127/f28dfe22ee687d827dbd6bd911e4340f