James
James

Reputation: 97

OpenGL Texture Coordinates Have No Effect

I cannot get texture coordinates to have any effect on what is rendered on screen. My texture constantly seemed to rendering from 0,0 to 1,1.

The method I am using is to send the following buffer layout: x,y,u,v (xy: vertex position and u,v texture coordinates).

However, changing the values u & v have no effect on the rendered texture.

I've removed a lot of surrounding code to try and make it easier to read, but I can post it in full if the problem isn't immediately obvious to someone.


Vertex Shader:

#version 330 core

layout(location = 0) in vec2 position;
layout(location = 1) in vec2 mytextpos;

out vec2 v_TexCoord;

uniform mat4 u_Model;

void main()
{
gl_Position = u_Model * vec4(position, 0.0f, 1.0f);
v_TexCoord = mytextpos;
}



Fragment Shader:

#version 330 core

layout(location = 0) out vec4 color;

in vec2 v_TexCoord;

uniform sampler2D u_Texture;

void main()
{
color = texture(u_Texture, v_TexCoord);
}



My Rectangle Class Constructor:

// GENERATE BUFFERS
glGenVertexArrays(1, &VertexArrayObject);
glGenBuffers(1, &VertexBufferId);
glGenBuffers(1, &IndexBufferObjectId);


Indices = {
    0, 1, 2,
    2, 3, 0
};

unsigned int numberOfVertices = Vertices.size();
unsigned int numberOfIndices = Indices.size();

glBindVertexArray(VertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferObjectId);

Vertices = {
    0.2f, 0.0f, 0.0f, 0.5f,
    1.0f, 0.0f, 1.5f, 0.5f,
    1.0f, 1.0f, 1.5f, 1.0f,
    0.0f, 1.0f, 0.0f, 1.0f
};


// ADD BUFFER DATA
glBufferData( GL_ARRAY_BUFFER, Vertices.size() * sizeof(float), Vertices.data(), GL_STATIC_DRAW );


// ARRANGE ATTRIBUTES
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
glEnableVertexAttribArray(1);

glBufferData( GL_ELEMENT_ARRAY_BUFFER, numberOfIndices * sizeof(unsigned int), Indices.data(), GL_STATIC_DRAW );`



Render Function:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

GLint u_Texture = glGetUniformLocation( shader.getId(), "u_Texture" );
glUniform1i( u_Texture, 0 );

GLint u_Model = glGetUniformLocation( shader.getId(), "u_Model" );
glUniformMatrix4fv( u_Model, 1, GL_FALSE, glm::value_ptr( rect.TransformMatrix() ) );

glDrawElements(GL_TRIANGLES, rect.Indices.size(), GL_UNSIGNED_INT, nullptr);

// SWAP BUFFERS
glfwSwapBuffers( _window->WindowInstance );
glfwPollEvents();



My program runs, but my texture is mapped between 0,0 and 1,0 no matter what my vertex or u,v positions are. I would expect the texture to be interpolated between the u,v coordinates for each vertex?

Upvotes: 1

Views: 622

Answers (1)

BDL
BDL

Reputation: 22175

You attribute setup is wrong:

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);

This tells OpenGL that it should attach the first two floats of each vertex to the mytextpos attribute. But you actually want it to read the 3rd and 4th float. Thus you have to set the offset such that it skips the first two floats:

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));

Upvotes: 1

Related Questions