我正在尝试将深度剥离的示例(一种与顺序无关的透明度技术)移植到所谓的现代 OpenGL(3.3+),但由于我是初学者,所以这并不容易..
在这里您可以找到一个工作版本(GL2)和一个正在进行中的版本(GL3)
https://github.com/elect86/modern-jogl-examples/tree/master/modern-jogl-examples/src/depthPeeling
我看不到后面的任何层...
我猜alpha值有一些问题..
我试图调试它并在核心部分
private void renderDepthPeeling(GL3 gl3) {
/**
* (1) Initialize min depth buffer.
*/
gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, colorBlenderFboId[0]);
gl3.glDrawBuffer(GL3.GL_COLOR_ATTACHMENT0);
gl3.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl3.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
gl3.glEnable(GL3.GL_DEPTH_TEST);
dpInit.bind(gl3);
{
gl3.glUniform1f(dpInit.getAlphaUnLoc(), opacity);
drawModel(gl3);
}
dpInit.unbind(gl3);
/**
* (2) Depth peeling + blending.
*/
int layersNumber = (passesNumber - 1) * 2;
// System.out.println("layersNumber: " + layersNumber);
for (int layer = 1; layer < 2; layer++) {
int currentId = layer % 2;
int previousId = 1 - currentId;
// gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, fboId[currentId]);
gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, 0);
gl3.glDrawBuffer(GL3.GL_COLOR_ATTACHMENT0);
gl3.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl3.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
gl3.glDisable(GL3.GL_BLEND);
gl3.glEnable(GL3.GL_DEPTH_TEST);
{
dpPeel.bind(gl3);
{
gl3.glActiveTexture(GL3.GL_TEXTURE0);
gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, depthTextureId[previousId]);
gl3.glUniform1i(dpPeel.getDepthTexUnLoc(), 0);
{
gl3.glUniform1f(dpPeel.getAlphaUnLoc(), opacity);
drawModel(gl3);
}
gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, 0);
}
dpPeel.unbind(gl3);
gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, colorBlenderFboId[0]);
gl3.glDrawBuffer(GL3.GL_COLOR_ATTACHMENT0);
}
gl3.glDisable(GL3.GL_DEPTH_TEST);
gl3.glEnable(GL3.GL_BLEND);
{
gl3.glBlendEquation(GL3.GL_FUNC_ADD);
gl3.glBlendFuncSeparate(GL3.GL_DST_ALPHA, GL3.GL_ONE, GL3.GL_ZERO, GL3.GL_ONE_MINUS_SRC_ALPHA);
dpBlend.bind(gl3);
dpBlend.bindTextureRECT(gl3, "TempTex", colorTextureId[currentId], 0);
{
// gl3.glCallList(quadDisplayList);
drawFullScreenQuad(gl3);
}
dpBlend.unbind(gl3);
}
gl3.glDisable(GL3.GL_BLEND);
}
/**
* (3) Final pass.
*/
// gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, 0);
// gl3.glDrawBuffer(GL3.GL_BACK);
// gl3.glDisable(GL3.GL_DEPTH_TEST);
//
// dpFinal.bind(gl3);
// {
// gl3.glUniform3f(dpFinal.getBackgroundColorUnLoc(), 1.0f, 1.0f, 1.0f);
//
//// dpFinal.bindTextureRECT(gl3, "ColorTex", colorBlenderTextureId[0], 0);
// gl3.glActiveTexture(GL3.GL_TEXTURE0);
// gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, colorBlenderTextureId[0]);
// gl3.glUniform1i(dpFinal.getColorTexUnLoc(), 0);
// {
//// gl3.glCallList(quadDisplayList);
// drawFullScreenQuad(gl3);
// }
// gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, 0);
// }
// dpFinal.unbind(gl3);
}
GL2和GL3程序版本之间的第一段和最后一段(1和3)看起来是正确的,所以问题出在2
我修改了 for cicle 为了只得到一个 cicle
for (int layer = 1; layer < 2; layer++) {
和
// gl2.glBindFramebuffer(GL2.GL_FRAMEBUFFER, fboId[currentId]);
gl2.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);
为了直观地看到中间结果
好吧,在 GL2 我得到
在 GL3 中
我的 dpPeel 程序是基于 dpPeel_VS
#version 330
layout (location = 0) in vec4 position;
layout(std140) uniform mvpMatrixes {
mat4 projectionMatrix;
mat4 cameraMatrix;
};
void main(void)
{
gl_Position = projectionMatrix * cameraMatrix * position;
}
和 dpPeel_FS 加上 shade_FS
#version 330
uniform samplerRect DepthTex;
vec4 ShadeFragment();
out vec4 outputColor;
void main(void)
{
// Bit-exact comparison between FP32 z-buffer and fragment depth
float frontDepth = texture(DepthTex, gl_FragCoord.xy).r;
if (gl_FragCoord.z <= frontDepth) {
discard;
}
// Shade all the fragments behind the z-buffer
vec4 color = ShadeFragment();
outputColor = vec4(color.rgb * color.a, color.a);
}
#version 330
uniform float Alpha;
vec4 ShadeFragment()
{
vec4 color;
color.rgb = vec3(.4,.85,.0);
color.a = Alpha;
return color;
}
你看到错误了吗?