I have described my problem here https://stackoverflow.com/questions/50658037/uv-mapping-opengl-wrong-colors
I am loading the data into VBO buffers in the code like this:
void createMeshVAO(Context &ctx, const Mesh &mesh, MeshVAO *meshVAO)
{
// Generates and populates a VBO for the vertices
glGenBuffers(1, &(meshVAO->vertexVBO));
glBindBuffer(GL_ARRAY_BUFFER, meshVAO->vertexVBO);
auto verticesNBytes = mesh.vertices.size() * sizeof(mesh.vertices[0]);
glBufferData(GL_ARRAY_BUFFER, verticesNBytes, mesh.vertices.data(), GL_STATIC_DRAW);
// Generates and populates a VBO for the vertex normals
glGenBuffers(1, &(meshVAO->normalVBO));
glBindBuffer(GL_ARRAY_BUFFER, meshVAO->normalVBO);
auto normalsNBytes = mesh.normals.size() * sizeof(mesh.normals[0]);
glBufferData(GL_ARRAY_BUFFER, normalsNBytes, mesh.normals.data(), GL_STATIC_DRAW);
// Generates and populates a VBO for the element indices
glGenBuffers(1, &(meshVAO->indexVBO));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshVAO->indexVBO);
auto indicesNBytes = mesh.indices.size() * sizeof(mesh.indices[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesNBytes, mesh.indices.data(), GL_STATIC_DRAW);
// Generates and populates a VBO for the UV indices
glGenBuffers(1, &(meshVAO->textcoordsVBO));
glBindBuffer(GL_ARRAY_BUFFER, meshVAO->textcoordsVBO);
auto uvsNBytes = mesh.texcoords.size() * sizeof(mesh.texcoords[0]);
glBufferData(GL_ARRAY_BUFFER, uvsNBytes, mesh.texcoords.data(), GL_STATIC_DRAW);
// Creates a vertex array object (VAO) for drawing the mesh
glGenVertexArrays(1, &(meshVAO->vao));
glBindVertexArray(meshVAO->vao);
glBindBuffer(GL_ARRAY_BUFFER, meshVAO->vertexVBO);
glEnableVertexAttribArray(POSITION);
glVertexAttribPointer(POSITION, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, meshVAO->textcoordsVBO);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, meshVAO->normalVBO);
glEnableVertexAttribArray(NORMAL);
glVertexAttribPointer(NORMAL, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshVAO->indexVBO);
glBindVertexArray(ctx.defaultVAO); // unbinds the VAO
// Additional information required by draw calls
meshVAO->numVertices = mesh.vertices.size();
meshVAO->numIndices = mesh.indices.size();
meshVAO->numtextcoords = mesh.texcoords.size();
}
Then the GLSL catches the vertice-array and texturecoordinates array which I use to render the model.
//fragment shader
in vec2 UV;
uniform sampler2D u_testtext;
frag_color = texture( u_testtext, UV );
If anyone has any ideas, I’m really interested to hear about it.