I’m new to OpenGL and I’ve been trying to draw a texture on a 2D quad using OpenGL 3.1 functions. I’ve done a lot of research and googling before posting this because I’m quite sure I’m making a silly newbie mistake, but I haven’t been able to figure anything out, mostly because it’s hard to tell exactly what the code I’ve found does, and it’s even harder to know whether most of it still applies to OpenGL 3.1.
As you can see below, something is wrong with my texture coords. The quad is just slightly smaller than the drawing window (-0.9 to 0.9) and the texture is meant to take up the same space:
This is the code I use for setting up the vbo:
glBindVertexArray(m_vaoID[2]);
glGenBuffers(1, &m_vboID[3]);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[3]);
glBufferData(GL_ARRAY_BUFFER, 8*sizeof(GLfloat), vert, GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 2, GL_FLOAT, GL_FALSE, 0, 0); //position
glEnableVertexAttribArray(0);
glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); //texture
glEnableVertexAttribArray(2);
glBindVertexArray(0);
this is the code I use for drawing:
glBindVertexArray(m_vaoID[2]);
glUniform1i(my_sampler_uniform_location, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_ID);
glDrawArrays(GL_QUADS, 0, 4);
and these are my vert/frag shaders:
#version 140
precision mediump int;
precision highp float;
in vec3 in_Position;
in vec2 in_TexCoord0;
out vec2 TexCoord0;
void main()
{
gl_Position = ftransform();
TexCoord0 = in_TexCoord0;
}
#version 140
precision highp int;
precision highp float;
uniform sampler2D baseMap;
in vec2 TexCoord0;
out vec4 out_Color;
void main()
{
out_Color = texture2D(baseMap, TexCoord0.st);
}
I’d be extremely happy if someone could point me in the right direction. Thanks!