Search code examples
c++opengltexturesrender

Texture a tube OpenGL


I'm trying to texture a tube object in OpenGL, for a project, and a have problem texturing it. The texture is coming up nicely but there is a white line in the middle of the back of the tube that I can't get rid of. I'm using standard texture class that I build from a tutorial that I read. the mesh and the texture are upload normally- meaning nothing is unusual.

Back of the tube
Back of the tube

Front of the tubeFront of the tube

Texture::Texture(const std::string& fileName)
{
    int width, height, numComponents;
    unsigned char* data = stbi_load((fileName).c_str(), &width, &height, &numComponents, 4);

    if (data == NULL)
        std::cerr << "Unable to load texture: " << fileName << std::endl;

    glGenTextures(1, &m_texture);
    glBindTexture(GL_TEXTURE_2D, m_texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    stbi_image_free(data);
}

Texture::~Texture()
{
    glDeleteTextures(1, &m_texture);
}

void Texture::Bind()
{
    glBindTexture(GL_TEXTURE_2D, m_texture);
}

#version 130--fragment shader

varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 color0;

uniform sampler2D ourTexture1;  // added

uniform vec3 lightDirection;
uniform vec3 MinMax;

void main()
{
    //vec3 tmp = dot(-lightDirection, normal0) * color0 ;44
    gl_FragColor = texture(ourTexture1, texCoord0);
    if(color0.y<MinMax.x||color0.y>MinMax.y)
        gl_FragColor=vec4(1.0,1.0,1.0,1.0); 

}

#version 120-vertex shader

attribute vec3 position;
attribute vec2 texCoord;
attribute vec3 normal;
attribute vec3 color;

varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 color0;

uniform mat4 MVP;
uniform mat4 Normal;

void main()
{
    gl_Position = MVP * vec4(position, 1.0);

    texCoord0 = texCoord; 
    texCoord0[0]=0.25+texCoord0[0];
    if(texCoord0[0]>=1)
    {
        texCoord0[0]=texCoord0[0]-1;
    }

    texCoord0[1]=1-texCoord0[1];
    color0 = position;
    normal0 = (Normal * vec4(normal, 0.0)).xyz;
}

Solution

  • The problem almost certainly comes from the following part of your vertex shader:

    texCoord0[0]=0.25+texCoord0[0];
    if(texCoord0[0]>=1)
    {
        texCoord0[0]=texCoord0[0]-1;
    }
    

    I'm not entirely sure what you are trying to accomplish with this, but it will cause neighbouring vertices to have values that are very far appart, which means that almost the entire texture gets squeezed in between these two vertices.

    Normally, you would want to just apply the offset, and let the rendering pipeline take care of of the modulus operation. So I would have expected to just see this:

    texCoord0[0]=0.25+texCoord0[0];
    

    N.B.

    You might still see the issue if you are sharing vertices accross the the entire circumference of the tube. The point of the mesh where the texture coordinate "loops" around should have duplicated vertices with different UVs.