gpt4 book ai didi

c++ - OpenGL:帧缓冲区,不完整的纹理附件

转载 作者:太空狗 更新时间:2023-10-29 20:23:31 25 4
gpt4 key购买 nike

我已经搜索了几个小时来解决我的问题。首先。我有 HD 7800 系列和 GPU,最新的驱动程序。

我正在尝试创建具有我需要的所有帧缓冲区功能的帧缓冲区类。看起来我让渲染缓冲区工作了,但是绘制到纹理会导致非常奇怪的错误。

FrameBuffer::FrameBuffer(int width, int height)
{
createFrameBuffer();
// createDepthBufferAttachment(width, height); //commented out for testing (THIS WORKS)
createTextureAttachment(width, height);
// createDepthTextureAttachment(width, height); //commented out for testing
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
std::cerr << glCheckFramebufferStatus(GL_FRAMEBUFFER) << "\nFramebuffer failed to generate! 0\n";
}
unbindFrameBuffer();


}

FrameBuffer::~FrameBuffer()
{
if (fbo != NULL)
glDeleteFramebuffers(1, &fbo);
if (colorTexture != NULL)
glDeleteTextures(1, &colorTexture);
if (depthBuffer != NULL)
glDeleteRenderbuffers(1, &depthBuffer);
if (depthTexture != NULL)
glDeleteTextures(1, &depthTexture);
}

void FrameBuffer::createFrameBuffer(){
glGenFramebuffers(1, &fbo);

}

void FrameBuffer::createTextureAttachment(int width, int height){
glGenTextures(1, &colorTexture);
glBindTexture(GL_TEXTURE_2D, colorTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, GL_RGBA, 0, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
std::cerr << glCheckFramebufferStatus(GL_FRAMEBUFFER) << "\nFramebuffer failed to generate! 1\n";
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

}
void FrameBuffer::createDepthTextureAttachment(int width, int height){
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, GL_DEPTH_COMPONENT, 0, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
std::cerr << glCheckFramebufferStatus(GL_FRAMEBUFFER) << "\nFramebuffer failed to generate! 2\n";
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

}

void FrameBuffer::createDepthBufferAttachment(int width, int height){
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
std::cerr << glCheckFramebufferStatus(GL_FRAMEBUFFER) << "\nFramebuffer failed to generate! 3\n";
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void FrameBuffer::bindFrameBuffer( int width, int height){
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0, 0, width, height);
}

void FrameBuffer::unbindFrameBuffer(){
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, DisplayManager::getWidth(), DisplayManager::getHeight());
}

glCheckFrameBufferStatus 返回 GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT在两次检查中。 createDepthTextureAttachment() 也会发生同样的情况两个好像都不行

createDepthBufferAttachment() 工作正常。

更多信息:是的,事实上我实际上是在给函数提供宽度和高度,它们都是 1024。我试过将 GL_RGBA 更改为 GL_RGB,并尝试了不同大小的 GL_DEPTH_COMPONENT。

目前我打算只使用 1 个纹理,而不是所有 3 个函数。

我不知道它是否重要但是 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);已设置。

我也尝试过 glFramebufferTexture() 函数。

提前致谢!

最佳答案

glTexImage2D() 调用中的参数顺序错误。看第一个:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, GL_RGBA, 0, GL_UNSIGNED_BYTE, NULL);

将其与函数定义进行比较:

void glTexImage2D(
GLenum target,
GLint level,
GLint internalFormat,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const GLvoid* data);

可以看到第6个参数是边框宽度,第7个是格式。你把这两个颠倒了。调用应该是:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

同样的事情也适用于另一个调用。

任何时候当您在运行 OpenGL 代码时遇到问题,我强烈建议您调用 glGetError()。它会很快告诉您这些调用有错误。

关于c++ - OpenGL:帧缓冲区,不完整的纹理附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32936597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com