Reputation: 197
I have a small problem, when I have a texture and I want to render it on my screen with opengl, the texture is rendered to small even if the texture has the same size as the screen. For rendering the texture to screen I use following shaders:
vertex shader:
#version 120
attribute vec2 point;
varying vec2 fr_point;
void main(void) {
gl_Position = vec4(point, 0.0, 1.0);
fr_point = point;
}
fragment shader:
#version 120
varying vec2 fr_point;
uniform sampler2D texture;
void main(void) {
gl_FragColor = texture2D(texture, fr_point);
};
the vertex attribute quad is a quad in the range of -1 and 1
my render function
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(p);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(uni_texture, 0);
glEnableVertexAttribArray(att_point);
GLfloat triangle_vertices[] = {
-1., -1.,
-1., 1.,
1., -1,
1., 1,
-1., 1.,
1., -1.
};
glVertexAttribPointer(
att_point,
2,
GL_FLOAT,
GL_FALSE,
0,
triangle_vertices
);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(att_point);
glutSwapBuffers();
Why is the texture always rendered so small, even if the texture is taller or has the same size?
Upvotes: 1
Views: 572
Reputation: 2539
The texture coordinates scale from 0 to 1, so you have to transform your quad coordinates to texture coordinates.
fr_point = (1+point)/2; //fr_point = (vec2(1)+point.xy)/2
so the
(-1,-1) -> (0,0)
(-1,1) -> (0,1)
(1,1) -> (1,1)
(1,-1) -> (1,0)
coordinates will be the texture coordinates.
Upvotes: 2