Reputation: 913
I'm following this site to learn OpenGL. In core profile mode to render a quad along with texture coordinates, I'm defining data like this:
float vertices[] = {
// positions // texture coords
0.5f, 0.5f, 0.0f, s0, t1, // top right
0.5f, -0.5f, 0.0f, s1, t1, // bottom right
-0.5f, -0.5f, 0.0f, s1, t0, // bottom left
-0.5f, 0.5f, 0.0f, s0, t0 // top left
};
Fragment shader
#version 330 core
// FRAGMENT SHADER
out vec4 frag_color;
in vec2 tex_coord;
uniform sampler2D texture;
void main()
{
frag_color = texture(texture, tex_coord);
}
What I'm looking for is to define only the position data:
float vertices[] = {
// positions
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};
Then pass a vec4 externally as uniform
glUniform4f( glGetUniformLocation( shader, "texcoord" ),
s0, t0, s1, t1);
then inside fragment shader or vertex shader (don't know where this calculation will happen), create texture coordinates data from (s0,t0,s1,t1) and pass it to calculate final color. My question is how do create texcoords and where? Using this technique I can render multiple icons using a single vertex buffer and icon atlas.
Upvotes: 1
Views: 470
Reputation: 210877
I suggest to specify the texture coordinate attribute in range [0.0, 1.0]. Map the texture coordinates from the range [0.0, 1.0] to the range [s0
, s1
] and [t0
, t1
] in the vertex or fragment shader. e.g:
out vec4 frag_color;
in vec2 tex_coord; // 0.0 .. 1.0
uniform vec4 texcoord; // s0, t0, s1, t1
uniform sampler2D texture;
void main()
{
vec2 uv = mix(texcoord.xy, texcoord.zw, tex_coord.xy)
frag_color = texture(texture, uv);
}
Upvotes: 1