OpenGL 4.0 Cubemap issues

OpenGL 4.0 Cubemap issues

本文关键字:issues Cubemap OpenGL      更新时间:2023-10-16

一直在阅读"OpenGL 4.0着色语言食谱"。但我在立方体贴图教程中遇到了麻烦。

问题是我画的模型看起来完全是灰色的。就好像它没有从samplerCube纹理中获取任何数据一样。

我的所有代码似乎都是正确的。我看过其他教程,都是一样的。不知道我的Intel HD Graphics 4000是否对此负责,但我已经确定我确实有GL_ARB_texture_cube_map扩展。

我使用DevIL库从文件中加载图像,这似乎做得很好,但从我可以看出,在将数据传输到OpenGL时出现了问题。

我正在从文件中获取数据的地方发布加载。所有文件也都正确加载。我还发布了绘图代码,在那里我将纹理绑定到管道。我也发布了我的顶点和片段着色器以防万一,但它们似乎确实正常工作。

有什么想法吗?

加载代码

uint TARGETS[6] =
{
    GL_TEXTURE_CUBE_MAP_POSITIVE_X,
    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
    GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
    GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
    GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};
string EXTS[6] = 
{
    "posx",
    "negx",
    "posy",
    "negy",
    "posz",
    "negz"
};
// Create & bind cubemap texture
glGenTextures( 1, &cubemap );
glBindTexture( GL_TEXTURE_CUBE_MAP, cubemap );
for( int i = 0; i < 6; i++ )
{
    string file = "textures/cubemap_" + EXTS[i] + ".png";
    uint image = ilGenImage();
    // Load with DevIL
    ilBindImage( image );
    if( !ilLoadImage( file.c_str() ) )
    {
        cout << "ERROR: Failed to load image " << endl;
        return false;
    }
    // Fetch info from DevIL
    int width   = ilGetInteger( IL_IMAGE_WIDTH );
    int height  = ilGetInteger( IL_IMAGE_HEIGHT );
    uint format = ilGetInteger( IL_IMAGE_FORMAT );
    uint type   = ilGetInteger( IL_IMAGE_TYPE );
    // Send data to OpenGL  
    glTexImage2D(
        TARGETS[i],
        0,
        GL_RGBA,
        width,
        height,
        0,
        format,
        type,
        ilGetData() );
    // Error check
    if( !ErrorCheck("Failed to bind a side of the cubemap!") )
        return false;
    // Get rid of DevIL data
    ilDeleteImage( image );
}
// Parameters
glTexParameterf( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameterf( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameterf( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );

绘制代码

    // Update
glfwPollEvents();
UpdateTime();
// Clear back buffer for new frame
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Bind shader
shader->Bind();
// Cubemap
shader->SetUniform( "cubemapTexture", 0 );
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_CUBE_MAP, cubemap );
// Bind model
if( model->Bind() )
{
    static float angle = 0;
    angle += 25.0f * deltaTime;
    // Matrices
    mat4 world =
            translate( vec3( 0.0f, 0.0f, 0.0f) ) *
            rotateZ( angle * PI / 180 ) *
            rotateX( angle * PI / 180 ) *
            scale( vec3( 1.0f, 1.0f, 1.0f) );
    mat4 view = ViewMatrix(
            cameraPosition,
            cameraTarget,
            vec3( 0.0f,  0.0f,  1.0f) );
    mat4 proj = ProjectionMatrix(
            fov,
            (float)windowX,
            (float)windowY,
            nearPlane,
            farPlane );
    // Uniforms
    shader->SetUniform( "uWorld", world );
    shader->SetUniform( "uView", view );
    shader->SetUniform( "uProj", proj );
    shader->SetUniform( "materialColor", vec3( 0.5f, 0.5f, 0.5f ) );
    shader->SetUniform( "drawSkybox", false );
    shader->SetUniform( "world_cameraPosition", cameraPosition );
    shader->SetUniform( "reflectFactor", 0.5f );
    // Draw
    glDrawElements( GL_TRIANGLES, model->GetIndexCount(), GL_UNSIGNED_SHORT, NULL );
}
// Put the new image on the screen
glfwSwapBuffers( window );

顶点着色器

#version 400
layout(location=0) in vec3 vertex_position;
layout(location=1) in vec3 vertex_normal;
layout(location=2) in vec4 vertex_tangent;
layout(location=3) in vec2 vertex_texCoords;
out vec2 texCoords;
out vec3 reflectDir;
uniform mat4 uWorld;
uniform mat4 uView;
uniform mat4 uProj;
uniform bool drawSkybox;
uniform vec3 world_cameraPosition;
void main()
{
    if( drawSkybox )
    {
        reflectDir = vertex_position;
    }
    else
    {
        vec3 world_pos = vec3( uWorld * vec4(vertex_position,1.0) );
        vec3 world_norm = vec3( uWorld * vec4(vertex_normal,0.0) );
        vec3 world_view = normalize( world_cameraPosition - world_pos );
        reflectDir = reflect( -world_view, world_norm );
    }
    gl_Position = uProj * uView * uWorld * vec4(vertex_position,1.0);
    texCoords = vertex_texCoords;
}

片段着色器

#version 400
out vec4 fragColor;
in vec2 texCoords;
in vec3 reflectDir;
uniform samplerCube cubemapTexture;
uniform vec3 materialColor;
uniform bool drawSkybox;
uniform float reflectFactor;
void main()
{
    vec3 color = texture( cubemapTexture, reflectDir ).rgb;
    if( drawSkybox )
    {
        fragColor = vec4( color, 1.0 );
    }
    else
    {
        fragColor = vec4( mix( materialColor, color, reflectFactor ), 1.0 );
    }
}

立方体贴图纹理不完整。立方体贴图纹理需要指定所有6个边才能完成。根据规格:

此外,如果以下条件都成立,则立方体贴图纹理是立方体完整的:[..]组成立方体贴图的六个纹理图像中的每个纹理图像的level_base数组都具有相同的、正的和正方形的维度。

您的代码没有为NEGATIVE_X:指定图像

uint TARGETS[6] =
{
    GL_TEXTURE_CUBE_MAP_POSITIVE_X,
    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
    GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
    GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
    GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};

使用此表,NEGATIVE_Y的图像被指定了两次,但缺少NEGATIVE_X。应该是:

uint TARGETS[6] =
{
    GL_TEXTURE_CUBE_MAP_POSITIVE_X,
    GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
    GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
    GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
    GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};

您也可以将GL_TEXTURE_CUBE_MAP_POSITIVE_X + i用于0..5范围内的i来寻址6个目标,而不是枚举6个目标。