纹理管OpenGL

时间:2018-05-27 15:48:15

标签: c++ opengl textures render

我试图在OpenGL中为一个项目构建一个管对象,并且有问题纹理化它。质地很好,但在管子后面的中间有一条白线,我无法摆脱。我使用的标准纹理类是根据我阅读的教程构建的。网格和纹理正常上传 - 意味着没有什么不寻常的。

管子后面Back of the tube

管前Front 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;
}

1 个答案:

答案 0 :(得分:4)

问题几乎肯定来自顶点着色器的以下部分:

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

我不完全确定你要用这个来完成什么,但它会使邻近的顶点具有很远的值,这意味着几乎整个纹理都被挤压在这两个顶点之间。

通常,您只想应用偏移量,并让渲染管道处理模数运算。所以我本来希望看到这个:

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

N.B。

如果您在管的整个圆周上共享顶点,您可能仍会看到问题。纹理坐标“环绕”的网格点应具有不同UV的重复顶点。