OpenGL纹理-纹理渲染两次

时间:2018-07-23 09:37:38

标签: c++ opengl

我有一个问题,纹理仅覆盖对象的一部分高度尺寸,然后重新开始在底部渲染较小的纹理,如图所示。

problem noted in red

我也发布了我的Texture和Shader代码。请注意,我使用glTexSubImage2D作为对象的背景。当我不使用glTexSubImage2D而仅使用glTexImage2D时,会发生相同的问题。

纹理代码

try {

    data = stbi_load((fileName).c_str(), &width, &height, &numComponents, 4);

    if (data == NULL)
        std::cerr << "Unable to load texture: " << fileName << std::endl;
    else
        std::cerr << "texture loaded " << fileName << std::endl;
    std::string fileName2 = t ? filePathe + "//images//" + mediaColor + ".bmp" : "./res/textures/plane1.bmp";
    int width2, height2, numComponents2;
    unsigned char* data2 = stbi_load((fileName2).c_str(), &width2, &height2, &numComponents2, 4);
    if (data2 == NULL)
        std::cerr << "Unable to load texture: " << fileName2 << std::endl;
    else
        std::cerr << "texture loaded " << fileName2 << 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, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height , GL_RGBA, GL_UNSIGNED_BYTE, data);

    stbi_image_free(data);
    stbi_image_free(data2);
}
catch (std::invalid_argument &e)
{

顶点着色器

#version 120

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

varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 color0;
varying float index0;

uniform mat4 MVP;
uniform mat4 Normal;

uniform float index;

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

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

}

片段着色器

#version 130

varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 color0;
varying float index0;

uniform sampler2D ourTexture1;  // added

uniform vec3 lightDirection;
uniform vec3 capOffSet;
uniform vec3 mediaColor;
uniform vec3 capColor;

void main()
{
    gl_FragColor = texture(ourTexture1, texCoord0);
}

0 个答案:

没有答案