0
votes

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

enter image description here

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:

enter image description here

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.

enter image description here

1
You may check, is it texture or another problme. Simple replace shader body to this: Colour = vec4(0.0, 1.0, 0.0, 1.0);Unick
Probably should've put in the question that I have already tried it. It does work as expected if you hard code the colour.Chemicalk1d
I have also checked the texture in an opengl debugger and proved that the texture is loaded correctly into the texture unit.Chemicalk1d
And how are you assigning the texture coordinates? I don't see anything that does so.funkysidd
could it be the UVs are quite big, the texture has borders or some kind of gradient and you're seeing just the result of clamping?jozxyqk

1 Answers

0
votes

It appears that one of the problems is with SOIL itself. Setting up the texture state myself rather than letting SOIL do it seemed to fix the problem. now the textures appear to show normally. The problem is probably somewhere inside the function "SOIL_load_OGL_texture". Instead now I just ask SOIL to load the image into memory before after I generate the texture.