0

我正在尝试使用 6 个纹理制作一个带有立方体映射的天空盒。在我使用的所有立方体贴图纹理中,只有一组 6 个纹理可以正常工作。我不确定是什么导致了问题。

这就是我的做法:

- 创建 CubeMap 纹理 ID

glGenTextures(1, &m_texHandle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_texHandle);

for(int i = 0; i < 6; ++i)
{
    //create image/pixel buffer
    TextureLoader tex = TextureLoader(fileNames[i].c_str(), extension);

    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB,  tex.GetWidth(), tex.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, tex.GetBuffer());

}

glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

-初始化和渲染代码

void MeshData::InitializeSkyBox()
{

    GLfloat skyboxVertices[] = 
    {
        // Positions          
        -1.0f,  1.0f, -1.0f,
        -1.0f, -1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,
         1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,

        -1.0f, -1.0f,  1.0f,
        -1.0f, -1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f,  1.0f,
        -1.0f, -1.0f,  1.0f,

         1.0f, -1.0f, -1.0f,
         1.0f, -1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,

        -1.0f, -1.0f,  1.0f,
        -1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f, -1.0f,  1.0f,
        -1.0f, -1.0f,  1.0f,

        -1.0f,  1.0f, -1.0f,
         1.0f,  1.0f, -1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
        -1.0f,  1.0f,  1.0f,
        -1.0f,  1.0f, -1.0f,

        -1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f,  1.0f,
         1.0f, -1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f,  1.0f,
         1.0f, -1.0f,  1.0f
    };

    m_indicieCount = 36;

    // Allocate an OpenGL vertex array object.
    glGenVertexArrays(1, &m_vertexArrayID);

    // Bind the vertex array object to store all the buffers and vertex attributes we create here.
    glBindVertexArray(m_vertexArrayID);

    // Generate an ID for the vertex buffer.
    glGenBuffers(1, &m_vertexBufferID);

    // Bind the vertex buffer and load the vertex position data into the vertex buffer.
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, m_indicieCount * 3 * sizeof(float), &skyboxVertices[0], GL_STREAM_DRAW);

    // Enable the two vertex array attributes.
    glEnableVertexAttribArray(0);  // Vertex position.

    // Specify the location and format of the position portion of the vertex buffer.
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
}

void MeshData::Render()
{
    //set cubemap texture for shader
    m_shader->SetShaderSampler("shaderTexture", 0, TextureManager::GetInstance()->GetTexture("skyBox"));

    glBindVertexArray(m_vertexArrayID);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID);

    glDrawArrays(GL_TRIANGLES, 0, m_indicieCount);

    glBindVertexArray(0);
}

SetShaderSampler 函数:

bool Shader::SetShaderSampler(const char* name, int slot, TextureLoader* texture)
{
    if(texture == NULL)
    {
        cout << "Shader::SetShaderSampler setting a null texture" << endl;
        return true;
    }

    int loc = glGetUniformLocation(m_shaderProgram, name);
    if(loc >= 0)
    {
        glActiveTexture(GL_TEXTURE0 + slot);

        GLenum type = (texture->GetTextureType() == TextureLoader::CUBE_MAP_TGA) ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;

        glBindTexture(type, texture->GetHandle());

        glUniform1i(loc, slot);
    }

    return true;
}

-着色器代码

////////////////////////////////////////////////////////////////////////////////
// Filename: cubeMap.vs
////////////////////////////////////////////////////////////////////////////////
#version 400


/////////////////////
// INPUT VARIABLES //
/////////////////////
layout(location = 0)in vec3 inputPosition;

//////////////////////
// OUTPUT VARIABLES //
//////////////////////
out vec3 texCoord;

///////////////////////
// UNIFORM VARIABLES //
///////////////////////
uniform mat4 worldMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;


////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
    // Calculate the position of the vertex against the view, and projection matrices.
    mat4 mv = projectionMatrix * mat4(mat3(viewMatrix));
    gl_Position = mv * vec4(inputPosition, 1.0);
    texCoord = inputPosition;
}


////////////////////////////////////////////////////////////////////////////////
// Filename: cubeMap.ps
////////////////////////////////////////////////////////////////////////////////
#version 400


/////////////////////
// INPUT VARIABLES //
/////////////////////
in vec3 texCoord;


//////////////////////
// OUTPUT VARIABLES //
//////////////////////
out vec4 outputColor;


///////////////////////
// UNIFORM VARIABLES //
///////////////////////
uniform samplerCube shaderTexture;


////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
    outputColor = texture(shaderTexture, texCoord);
}

- 我得到的结果

这是唯一适合我的立方体贴图纹理集 在此处输入图像描述

现在这就是问题所在。我尝试了很多不同的纹理集,要么它什么都不显示,要么我得到下面的问题。

这是 6 个纹理的原始天空盒立方体贴图集 在此处输入图像描述

但是当我玩游戏时,它会显示像这样的线条 在此处输入图像描述

那么关于为什么会发生这种情况的任何见解?我相信我做错了什么,因为在大多数纹理中我只尝试过一种作品。

4

1 回答 1

0

我犯了一个愚蠢的错误,但我的问题的解决方案是我在纹理上有一个 alpha 通道。一旦我删除了 alpha 通道,它就可以正常工作了!

于 2015-08-17T22:37:59.403 回答