簡體   English   中英

深度緩沖區不使用多個着色器程序

[英]Depth Buffer Not Working With Multiple Shader Programs

我正在使用OpenGL-ES為手持系統編寫一個C ++的3D圖形渲染器,我正在使用貼花(紋理)着色器以及顏色填充着色器。 我有兩個渲染過程,我使用相應的程序,並使用數組/元素緩沖區使用glDrawElements繪制數據。

雖然我在我的應用程序中啟用了GL_DEPTH_TEST,但我的貼花多邊形總是在我的顏色填充多邊形前面渲染。

我已經嘗試重新構建我的投影/ modelView矩陣是如何構建並傳遞給着色器的,我嘗試了不同的glDepthFunc參數,但我無法讓它們正確渲染。

這是我的繪圖代碼 - 我用每個框架開始

glClearColor(0.25f, 0.25f, 0.25f, 1.f);
glClearDepthf(1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

然后我計算世界空間多邊形坐標並將其排列成GLfloat向量。 我首先繪制貼花數據:

GLuint programId = decalProgramId;
glUseProgram(programId);

// All of my uniform params are setup here - omitting to save space

// Set proj/mv matrices
glUniformMatrix4fv(glGetUniformLocation(programId, "uProjection"), 1, GL_TRUE, static_cast<f32*>(gameEngine.proj));
glUniformMatrix4fv(glGetUniformLocation(programId, "uModelView"), 1, GL_TRUE, static_cast<f32*>(gameEngine.modelView));

// Then I prepare the buffer data
glBindBuffer(GL_ARRAY_BUFFER, decalBufferId);
glBufferData(GL_ARRAY_BUFFER, coordSize + texCoordSize, 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, coordSize, &coords[0]);
glBufferSubData(GL_ARRAY_BUFFER, coordSize, texCoordSize, &texCoords[0]);

// And element array buffer data
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, decalElementBufferId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize, &indices[0], GL_STATIC_DRAW);

// Enable our vertex attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(coordSize));

// Bind my test texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

// Draw the elements
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, 0);

然后用我的彩色多邊形重復該過程。 接下來是SwapBuffer和WaitVSync命令。

這是我創建一個三角形和立方體正面的代碼:

// Add polygon directly to the scene object
scene.AddPolygon(polygonBuilder
    .AddVertex(Vector3(0.5f, 0.5f, 0.f), Vector4(0.f, 0.f, 1.f, 1.f))
    .AddVertex(Vector3(0.25f, 0.f, 0.f), Vector4(0.f, 1.f, 0.f, 1.f))
    .AddVertex(Vector3(0.75f, 0.f, 0.f), Vector4(1.f, 0.f, 0.f, 1.f))
    .AddIndex(0).AddIndex(1).AddIndex(2)
    .Build()
);

// Create a polygon within the crate object
crateObject.AddPolygon(polygonBuilder
    .AddVertex(Vector3(-0.25f, 0.25f, 0.f), Vector2(0.f, 0.f))
    .AddVertex(Vector3(-0.25f, -0.25f, 0.f), Vector2(0.f, 1.f))
    .AddVertex(Vector3(0.25f, -0.25f, 0.f), Vector2(1.f, 1.f))
    .AddVertex(Vector3(0.25f, 0.25f, 0.f), Vector2(1.f, 0.f))
    .SetTexture("crate")
    .AddIndex(0).AddIndex(1).AddIndex(3)
    .AddIndex(3).AddIndex(1).AddIndex(2)
    .Build()
);

// Add the crate object as a child within the scene
scene.AddChild(crate);

當我創建兩個彩色三角形和一個紋理立方體時,此屏幕截圖是當前輸出:

渲染錯誤

藍色箭頭表示應在前面呈現的三角形。 有任何想法嗎?

我現在覺得很傻。 我的顏色着色器未正確應用模型視圖矩陣。 它始終以固定的深度渲染。

我修改了我的顏色着色器代碼,以與貼花相同的方式應用矩陣,現在它完美地工作。

工作渲染

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM