-1

「OpenGL 4.0 Shading Language Cookbook」を読んでいます。しかし、キューブマップのチュートリアルで壁にぶつかりました。

問題は、私が描いているモデルが完全に灰色に見えることです。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 );
    }
}
4

1 に答える 1