简体   繁体   English

OpenGL 4.0 Cubemap问题

[英]OpenGL 4.0 Cubemap issues

Been reading 'OpenGL 4.0 Shading Language Cookbook'. 正在阅读“ OpenGL 4.0着色语言食谱”。 But I've run into a wall with the cubemap tutorial. 但是我在立方贴图教程中遇到了麻烦。

The issue is that model I'm drawing appears completely grey. 问题是我正在绘制的模型完全显示为灰色。 As if it's not getting any data from the samplerCube texture. 好像没有从samplerCube纹理中获取任何数据。

All my code seems to be correct. 我所有的代码似乎都是正确的。 I've looked at other tutorials and it's the same thing. 我看过其他教程,也是一样。 Don't know if my Intel HD Graphics 4000 is responsible, but I have made certain that I do have the GL_ARB_texture_cube_map extension. 不知道我的Intel HD Graphics 4000是否负责,但是我已经确定我确实具有GL_ARB_texture_cube_map扩展名。

I'm using the DevIL library for loading images from file, which it seems to do just fine, but from what I can tell something is going wrong in transferring the data to OpenGL. 我正在使用DevIL库从文件加载图像,这似乎还不错,但是据我所知,将数据传输到OpenGL时出现了问题。

I'm posting the loading where I get the data from the files. 我正在发布加载文件,从文件中获取数据。 All files are loading correctly as well. 所有文件也正确加载。 I'm also posting the drawing code, where I bind the texture to the pipeline. 我还要发布绘图代码,将纹理绑定到管道。 And I'm also posting my vertex and fragment shader just in case, but they do appear to be working as they should. 而且我还发布了顶点和片段着色器,以防万一,但是它们确实可以正常工作。

Any ideas? 有任何想法吗?

Loading code 正在加载代码

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 );

Draw code 抽奖码

    // 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 );

Vertex Shader 顶点着色器

#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;
}

Fragment shader 片段着色器

#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 );
    }
}

Your cube map texture is not texture complete. 您的立方体贴图纹理不完整。 All 6 sides need to be specified for a cube map texture to be complete. 必须指定所有6个面,立方体图纹理才能完整。 From the specs: 从规格:

Additionally, a cube map texture is cube complete if the following conditions all hold true: [..] The level_base arrays of each of the six texture images making up the cube map have identical, positive, and square dimensions. 此外,如果满足以下所有条件,则立方体贴图纹理将是立方体完整的:[..]组成立方体贴图的六个纹理图像的每一个的level_base数组均具有相同,正和正方形的尺寸。

Your code does not specify an image for NEGATIVE_X : 您的代码未指定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
};

Using this table, the image for NEGATIVE_Y is specified twice, but it's missing NEGATIVE_X . 使用此表,为NEGATIVE_Y的图像指定了两次,但缺少NEGATIVE_X It should be: 它应该是:

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
};

Instead of enumerating the 6 targets, you can also use GL_TEXTURE_CUBE_MAP_POSITIVE_X + i for i in the range 0..5 to address the 6 targets. 除了列举6个目标外,还可以使用GL_TEXTURE_CUBE_MAP_POSITIVE_X + i iGL_TEXTURE_CUBE_MAP_POSITIVE_X + i范围内的i来寻址6个目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM