When rendering a plain green texture to the screen in opengl the mesh renders correctly with the texture applied however only part of the texture is correctly coloured(this occurs with other textures as well). The Remainder of The Texture Is In A Different Shade to What is is Supposed to Be
It should all be the same colour as in the top right of the object.
Textures loaded with simple open GL image library:
textureID = SOIL_load_OGL_texture(name.c_str(), SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y );
Basic fragment shader:
#version 330
in vec2 FragmentUV;
out vec4 Colour;
uniform sampler2D MainTexture;
void main(){
Colour = texture(MainTexture, FragmentUV);
}
Binding a texture and setting the sampler inside the shader:
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, t.textureID);
int location = glGetUniformLocation(programID, u.c_str());
glUniform1i(location, textureUnit);
I am at an absolute loss as to what could be causing this.
EDIT:
Code assigning texture coordinates and other vertex information
//generate and bind vertex array
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLErrorCheck();
//vertex data
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, m.GetVerticies().size() * sizeof(Vertex), m.GetVerticies().data(), GL_STATIC_DRAW);
GLErrorCheck();
//index data
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m.GetIndicies().size() * sizeof(GLuint), m.GetIndicies().data(), GL_STATIC_DRAW);
GLErrorCheck();
glEnableVertexAttribArray(0);//Vertex Position
glEnableVertexAttribArray(1);//Vertex Normal
glEnableVertexAttribArray(2);//Vertex Tex Coords
GLErrorCheck();
//setup vertex info
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Pos));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TextureCoords));
GLErrorCheck();
//unbind the vertex array
glBindVertexArray(0);
GLErrorCheck();
Vertex Refers to this structure:
struct Vertex{
glm::vec3 Pos;
glm::vec3 Normal;
glm::vec2 TextureCoords;
};
The variable m is an instance of a mesh class which contains a std::vector of verticies and a of std::vector indicies and the corresponding getters.
Having fiddled around with the mesh itself I have realised that it is probably a UV issue since the object now looks like this:
When looking through the cube.obj file i noticed that the texture coordinates do not look normal. The cube itself was simply exported from blender and loaded into the application by assimp.
vt 0.999820 0.000180
vt 0.666847 0.000180
vt 0.666847 0.333153
vt 0.999820 0.333153
vt 0.666486 0.333514
vt 0.333514 0.333514
vt 0.333514 0.666486
vt 0.666486 0.666486
vt 0.000180 0.666486
vt 0.333153 0.666486
vt 0.333153 0.999459
vt 0.000180 0.999459
vt 0.000180 0.333153
vt 0.333153 0.333153
vt 0.333153 0.666126
vt 0.000180 0.666126
vt 0.666486 0.333153
vt 0.666486 0.000180
vt 0.333514 0.000180
vt 0.333514 0.333153
vt 0.000180 0.000180
vt 0.333153 0.000180
Having tried rendering the UV coordinates as red and green I get this result.