我想使用可编程管道(使用 glsl)而不是固定管道进行体积渲染。我用 2 个着色器程序和 2 个通道来实现它,exitPointProg
并且rayCastProg
. 第一遍是获取立方体边界框的出口点(即背面),它将在下一次进行光线投射的过程中用作纹理。进行光线投射体积渲染的想法来自这里。我认为我已经做了大部分正确的事情,因为我已经使用固定管道和可编程管道实现了这一点。让我感到困惑的是,我在第一帧就达到了效果,并且随着闪光灯的闪烁,屏幕上的显示变成了全白(我设置了glClearColor(1.0f, 1.0f, 1.0f, 1.0f)
. 我认为着色器程序或 FBO 的切换可能存在一些问题。这是我在 render() 函数中所做的。
- 第一遍,使用着色器程序渲染到绑定了纹理的 fbo
exitPoints
以获得立方体边界框的出口点exitPointProg
。 2ed pass,将
exitPoints
纹理(绑定到)作为统一的 sampler2D 变量映射GL_TEXTURE1
到着色器程序中,该变量将在着色器中使用。rayCastProg
这里是setupFBO
andrender
和两个子例程:setupFBO()
函数:void SimpleRayCasting::setupFBO() { GLuint textureHandles[1]; glGenTextures(1, textureHandles); entryPoints = textureHandles[0]; glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, exitPoints); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL); GLuint depthRenderBuffer; glGenRenderbuffers(1, &depthRenderBuffer); glBindRenderbuffer(GL_RENDERBUFFER, depthRenderBuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); glGenFramebuffers(1, &frameBuffer); glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, exitPoints, 0/* level */); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer); checkFramebufferState(__FILE__, __LINE__); GLenum drawbufs[] = {GL_COLOR_ATTACHMENT0}; glDrawBuffers(1, drawbufs); glBindFramebuffer(GL_FRAMEBUFFER,0); }
render()
功能:void SimpleRayCasting::render() { getExitPoints(); GLUtils::checkForOpenGLError(__FILE__,__LINE__); rayCasting(); GLUtils::checkForOpenGLError(__FILE__,__LINE__); }
getExitPoints()
功能:void SimpleRayCasting::getExitPoints() { // render to the texture glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); glEnable(GL_DEPTH_TEST); glViewport(0, 0, width, height); // checkFramebufferState(__FILE__, __LINE__); glClearColor(0.2f, 0.2f, 0.2f, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); exitPointsProg.use(); // note that the model will recalculated. model = mat4(1.0f); model *= glm::rotate(angle , vec3(0.0f,1.0f,0.0f)); model *= glm::rotate(90.0f, vec3(1.0f, 0.0f, 0.0f)); model *= glm::translate(vec3(-0.5f, -0.5f, -0.5f)); view = glm::lookAt(vec3(0.0f,0.0f,2.0f), vec3(0.0f,0.0f,0.0f), vec3(0.0f,1.0f,0.0f)); projection = mat4(1.0f); projection = glm::perspective(60.0f, (float)width/(float)height,0.01f, 400.0f); setMatrices(exitPointsProg); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); drawBoundBox(); glDisable(GL_CULL_FACE); glBindFramebuffer(GL_FRAMEBUFFER, 0); }
rayCasting
功能:void SimpleRayCasting::rayCasting() { // Directly render to the screen glBindFramebuffer(GL_FRAMEBUFFER, 0); glEnable(GL_DEPTH_TEST); glViewport(0, 0, width, height); glClearColor(1.0f, 1.0f, 1.0f, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); rayCastProg.use(); model = mat4(1.0f); model *= glm::rotate(angle, vec3(0.0f,1.0f,0.0f)); model *= glm::rotate(90.0f, vec3(1.0f, 0.0f, 0.0f)); model *= glm::translate(vec3(-0.5f, -0.5f, -0.5f)); view = glm::lookAt(vec3(0.0f,0.0f,2.0f), vec3(0.0f,0.0f,0.0f), vec3(0.0f,1.0f,0.0f)); projection = mat4(1.0f); projection = glm::perspective(60.0f, (float)width/(float)height,0.01f, 400.0f); setMatrices(rayCastProg); rayCastProg.setUniform("StepSize", stepSize); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, exitPoints); rayCastProg.setUniform("ExitPoints", 1); glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_3D, volume_to); rayCastProg.setUniform("VolumeTex", 4); glActiveTexture(GL_TEXTURE5); glBindTexture(GL_TEXTURE_1D, transferFunc_to); rayCastProg.setUniform("TransferFunc", 5); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); drawBoundBox(); glDisable(GL_CULL_FACE); glBindFramebuffer(GL_FRAMEBUFFER, 0); }
我在 Qt 中使用从 QGLWidget 继承的类。正如我之前提到的,我只用第一帧就得到了正确的结果,然后它很快就消失了,变成了全白。不知道这是怎么回事,是shader程序的切换还是FBO的东西有问题?我一直在研究这个很长时间,但无法弄清楚,任何帮助将不胜感激!
编辑询问 是否有任何演示或教程演示如何将多个 glsl 着色器程序与 FBO 一起使用?