gpt4 book ai didi

c++ - OpenGL FBO 不渲染到纹理 (C++)

转载 作者:行者123 更新时间:2023-11-28 08:03:22 25 4
gpt4 key购买 nike

我一直在尝试将茶壶渲染到 FBO,然后使用后续纹理作为纹理贴图。似乎什么都没有发生,所以我想知道我做错了什么。下面是主循环:

//switch to fbo
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);

glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glRotatef(a,0,1,1);
glutSolidTeapot(1);

//switch to main

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, img);


//draw cube

glColor4f(1, 1, 1, 1);
glBegin(GL_TRIANGLES);
// front faces
glNormal3f(0,0,1);
// face v0-v1-v2
glTexCoord2f(1,1); glVertex3f(1,1,1);
glTexCoord2f(0,1); glVertex3f(-1,1,1);
glTexCoord2f(0,0); glVertex3f(-1,-1,1);
...draws cube

我已经尝试使用 glFramebufferStatus,它返回了“成功”

   glGenTextures(1, &img); 
glBindTexture(GL_TEXTURE_2D, img);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

glGenFramebuffers(1,&fbo);
glBindFramebuffer(GL_FRAMEBUFFER,fbo);
glGenRenderbuffers(1, &depthbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 512, 512);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer);

glGenTextures(1, &img);
glBindTexture(GL_TEXTURE_2D, img);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, img, 0);

glBindFramebuffer(GL_FRAMEBUFFER, fbo);


GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

if (status==GL_FRAMEBUFFER_COMPLETE) {
printf("success\n");
}

但是,我得到的只是没有纹理映射的立方体。

最佳答案

这里有两个可能的错误:

  1. 渲染到 FBO 失败。要检查是否是这种情况,请使用不同的背景颜色(例如粉红色)清除 FBO 并查看纹理是否受到影响(例如,通过将其读回 CPU)。您还应确保变换矩阵和视口(viewport) (glViewport) 正确。

  2. 纹理映射失败。确保您已为纹理映射设置好一切,并首先使用静态纹理测试纹理映射。

关于c++ - OpenGL FBO 不渲染到纹理 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10791524/

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