我正在做一个项目,我需要从模拟水的 SPH 粒子中获取表面,以便将其渲染为卡通风格。为了做到这一点,我遵循一种方法,其中第一步是从这个粒子集中获取深度图,我试图在屏幕外渲染,在帧缓冲区对象中,并在屏幕上渲染为测试,看看结果。现在我还没有渲染任何粒子,因为我正在尝试首先在这个 FBO 上渲染一些东西,然后按照 Swiftless 网站上的教程在四边形屏幕上渲染它。当然,我已经将那里解释的方法添加到我正在处理的项目中。
我的第一步是渲染一个绿色屏幕,上面有一个灰色的茶壶,它没有问题,正如你在这里看到的那样。
然后我尝试在屏幕外将其渲染为 FBO,然后他们将其渲染为屏幕中间的四边形,但我得到的只是一个绿色四边形,如您在此处看到的。
我使用的代码直接来自我之前提到的教程,是这样的:
变量:
GLuint fbo;
GLuint fbo_depth;
GLuint fbo_texture;
int windowWidth = 1024;
int windowHeight = 768;
设置 FBO 及其纹理:
void initFrameBufferDepthBuffer ()
{
glGenRenderbuffersEXT(1, &fbo_depth); // Generate one render buffer and store the ID in fbo_depth
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo_depth); // Bind the fbo_depth render buffer
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, windowWidth, windowHeight); // Set the render buffer storage to be a depth component, with a width and height of the window
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbo_depth); // Set the render buffer of this buffer to the depth buffer
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); // Unbind the render buffer
}
void initFrameBufferTexture ()
{
glGenTextures(1, &fbo_texture); // Generate one texture
glBindTexture(GL_TEXTURE_2D, fbo_texture); // Bind the texture fbo_texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, windowWidth, windowHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // Create a standard texture with the width and height of our window
// Setup the basic texture parameters
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Unbind the texture
glBindTexture(GL_TEXTURE_2D, 0);
}
void initFrameBuffer () {
glEnable(GL_TEXTURE_2D); // Enable texturing so we can bind our frame buffer texture
glEnable(GL_DEPTH_TEST); // Enable depth testing
initFrameBufferDepthBuffer(); // Initialize our frame buffer depth buffer
initFrameBufferTexture(); // Initialize our frame buffer texture
glGenFramebuffersEXT(1, &fbo); // Generate one frame buffer and store the ID in fbo
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); // Bind our frame buffer
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fbo_texture, 0); // Attach the texture fbo_texture to the color buffer in our frame buffer
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbo_depth); // Attach the depth buffer fbo_depth to our frame buffer
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); // Check that status of our generated frame buffer
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) // If the frame buffer does not report back as complete
{
printf ( "Couldn't create frame buffer\n");
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // Unbind our frame buffer
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
}
在 FBO 上渲染茶壶:
void renderTeapotScene ()
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); // Bind our frame buffer for rendering
glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT); // Push our glEnable and glViewport states
glViewport(0, 0, windowWidth, windowHeight); // Set the size of the frame buffer view port
glClearColor (0.0f, 1.0f, 0.0f, 1.0f); // Set the clear color
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the depth and color buffers
glLoadIdentity(); // Reset the modelview matrix
glTranslatef(0.0f, 0.0f, -5.0f); // Translate back 5 units
glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
glutSolidTeapot(1.0f); // Render a teapot
glPopAttrib(); // Restore our glEnable and glViewport states
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // Unbind our texture
}
在应用程序开始时仅发生一次的进程上设置 FBO:
initFrameBuffer(); // Create our frame buffer object
以及每一帧都会发生的整个过程:
glEnable(GL_TEXTURE_2D); // Enable texturing so we can bind our frame buffer texture
glEnable(GL_DEPTH_TEST); // Enable depth testing
renderTeapotScene();
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations
glTranslatef(0.0f, 0.0f, -10.0f);
glBindTexture(GL_TEXTURE_2D, fbo_texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); // The top left corner
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
当然,这些代码片段是在一个更大的项目中,我认为这可能会导致一些可能导致这种行为的东西,但我不知道它可能是什么。我认为重要的是要说我的 FBO 与屏幕帧缓冲区具有相同的宽度和高度。
好吧,这就是我现在的情况。有人知道如何将第一个图像渲染到第二个图像中间的四边形作为纹理?