2013-02-16 30 views
2

我使用Assimp渲染三維模型的OpenGL ES 2.0。我目前有一個奇怪的問題,其中模型的某些部分不可見,即使它們應該是。人們很容易看到它在這些圖片:的OpenGL ES 2.0:模型的部分,其中它們不應該遮擋。是z緩衝區的責任?

Textured model

在我這個渲染第二圖像(的線性化版本)的z緩衝爲屏幕,看它是否可能是Z緩衝問題。黑色像素攝像頭附近:

zbuffer render

我試圖改變Z-近和z遠值無任何影響。現在我做的初始化:

glEnable(GL_CULL_FACE);// Cull back facing polygons 
glEnable(GL_DEPTH_TEST); 

而且我也這樣做,對每一幀:

glClearColor(0.7f, 0.7f, 0.7f, 1.0f); 
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 

我認爲這可能是一個面纏繞的問題,所以我試圖禁用GL_CULL_FACE,但它不起作用。我很確定這個模型很好,因爲Blender可以正確渲染它。

我使用這些着色器現在:

// vertex shader 
uniform mat4 u_ModelMatrix; // A constant representing the model matrix. 
uniform mat4 u_ViewMatrix; // A constant representing the view matrix. 
uniform mat4 u_ProjectionMatrix; // A constant representing the projection matrix. 

attribute vec4 a_Position; // Per-vertex position information we will pass in. 
attribute vec3 a_Normal; // Per-vertex normal information we will pass in. 
attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in. 

varying vec3 v_Position; // This will be passed into the fragment shader. 
varying vec3 v_Normal; // This will be passed into the fragment shader. 
varying vec2 v_TexCoordinate; // This will be passed into the fragment shader. 


void main() 
{ 
    // Transform the vertex into eye space. 
    mat4 u_ModelViewMatrix = u_ViewMatrix * u_ModelMatrix; 
    v_Position = vec3(u_ModelViewMatrix * a_Position); 

    // Pass through the texture coordinate. 
    v_TexCoordinate = a_TexCoordinate; 

    // Transform the normal's orientation into eye space. 
    v_Normal = vec3(u_ModelViewMatrix * vec4(a_Normal, 0.0)); 

    // gl_Position is a special variable used to store the final position. 
    // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. 
    gl_Position = u_ProjectionMatrix * u_ModelViewMatrix * a_Position; 
} 

這是片段着色器:

// fragment shader 
uniform sampler2D u_Texture; // The input texture. 
uniform int u_TexCount; 

varying vec3 v_Position; // Interpolated position for this fragment. 
varying vec3 v_Normal; // Interpolated normal for this fragment. 
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment. 

// The entry point for our fragment shader. 
void main() 
{ 
    vec3 u_LightPos = vec3(1.0); 
    // Will be used for attenuation. 
    float distance = length(u_LightPos - v_Position); 

    // Get a lighting direction vector from the light to the vertex. 
    vec3 lightVector = normalize(u_LightPos - v_Position); 

    // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are 
    // pointing in the same direction then it will get max illumination. 
    float diffuse = max(dot(v_Normal, lightVector), 0.0); 

    // Add attenuation. 
    diffuse = diffuse * (1.0/distance); 

    // Add ambient lighting 
    diffuse = diffuse + 0.2; 
    diffuse = 1.0; 

    //gl_FragColor = (diffuse * texture2D(u_Texture, v_TexCoordinate));// Textured version 

    float d = (2.0 * 0.1)/(100.0 + 0.1 - gl_FragCoord.z * (100.0 - 0.1)); 
    gl_FragColor = vec4(d, d, d, 1.0);// z-buffer render 
} 

我使用VBO與指數加載幾何形狀和材料。

我當然可以粘貼你認爲這可能是相關的一些其他的代碼,但現在我很高興能得到的是什麼導致這種奇怪的行爲的一些想法,或者一些可能的測試中,我能做到的。

回答

2

好吧,我解決了這個問題。我發佈的解決方案,因爲它可能對未來的谷歌有用。

基本上,我沒有要求深度緩衝。我在本地代碼中執行所有渲染東西,但所有Open GL上下文初始化都是在Java端完成的。我用過Android樣品(GL2JNIActivity)爲起點的一個,但他們並沒有要求任何深度緩衝器和我沒有注意到。

我解決設定ConfigChooser當它設置深度緩衝區大小爲24:

setEGLConfigChooser(translucent ? 
         new ConfigChooser(8, 8, 8, 8, 24 /*depth*/, 0) : 
         new ConfigChooser(5, 6, 5, 0, 24 /*depth*/, 0); 
相關問題