我正在尝试使用我无法控制它的设置方式的帧缓冲区对象。所以我正在创建一个有深度的屏幕外 fbo 来绘制我的纹理。然后将其绘制到主帧缓冲区。我现在可以使用 OpenGL 分析器从资源中看到纹理,但我似乎无法让它绘制到主帧缓冲区。
这是我的代码
GLsizei glSizeWidthEquiv = width;
GLsizei glSizeHeightEquiv = height;
GLint originalFramebuffer;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &originalFramebuffer);
if(fbo == 0){
glGenFramebuffers(1, &fbo);
}
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
//Creating texture for framebuffer
if(texColorBuffer == 0){
glGenTextures(1, &texColorBuffer);
}
glBindTexture([outTex target], texColorBuffer);
//Having it as null means the texture's data will be created dynamically
//Using RGBA since the alpha layer is going to be needed
glTexImage2D([outTex target], 0, GL_RGBA_FLOAT16_APPLE, glSizeWidthEquiv, glSizeHeightEquiv, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri([outTex target], GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri([outTex target], GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Attach texture to framebuffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, [outTex target], texColorBuffer, 0);
//Creating render buffer
if(renderBuffer == 0){
glGenRenderbuffers(1, &renderBuffer);
}
glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, glSizeWidthEquiv, glSizeHeightEquiv);
//Binding the renderbuffer to the framebuffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);
//Check framebuffer
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
NSLog(@"Framebuffer incomplete noob\n");
}else{
NSLog(@"Framebuffer COMPLETE!\n");
}
//Start to use the frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0, 0, glSizeWidthEquiv, glSizeHeightEquiv);
//cleaning texture: cleaning code here
//initating glTex0
glActiveTexture(GL_TEXTURE0);
//setting up texture stuff
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
glDisable(GL_CULL_FACE);
glColor3f(1,0,0);
glBegin(GL_QUADS);
//box 1 in front
glVertex3f(0, 0, 0);
glVertex3f(500, 0, 0);
glVertex3f(500, 500, 0);
glVertex3f(0, 500, 0);
//box 2 at the back
glColor3f(0, 1, 0);
glVertex3f(-250, -250, 50);
glVertex3f(250, -250, 50);
glVertex3f(250, 250, 50);
glVertex3f(-250, 250, 50);
glEnd();
glFogCoordf(9);
//End of framebuffer usage
glBindFramebuffer(GL_FRAMEBUFFER, originalFramebuffer);
glViewport(0, 0, (GLsizei)[outTex width], (GLsizei)[outTex height]);
[self glGetError];
glActiveTexture(GL_TEXTURE0);
glBindTexture([outTex target], texColorBuffer);
glCopyTexSubImage2D([outTex target], 0, 0, 0, 0, 0, glSizeWidthEquiv, glSizeHeightEquiv);
glFlush();
这部分程序位于渲染部分,我在初始化时将 fbo、renderBuffer 和 texColorBuffer 设置为 0,以防止它生成新的缓冲区。
所以基本上我正在创建缓冲区直到它完成,然后重新绑定我创建的 fbo 以绘制我的形状。切换回原来的 fbo 并尝试绘制它。
这里使用的 [outTex 目标] 是 GL_TEXTURE_RECTANGLE_ARB。
这就是我在 OpenGL Profiler 的纹理部分中的内容:http: //img.photobucket.com/albums/v442/ardo/ScreenShot2013-10-27at112742PM.png
@GMasucci
我不完全确定这是否是创建 sscce 的正确方法,但这里有一个很容易复制粘贴的代码,包括库、GLUT 和 OpenGL。这里使用的代码是从 www.videotutorialsrock.com 照明课程中借鉴而来的,因此我有一个类似的环境。
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
GLuint fbo,texColorBuffer,renderBuffer;
float camMatrix [4][4];
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -8.0f);
if(fbo == 0){
glGenFramebuffers(1, &fbo);
}
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
if(texColorBuffer == 0){
glGenTextures(1, &texColorBuffer);
}
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 400, 400, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
if(renderBuffer == 0){
glGenRenderbuffers(1, &renderBuffer);
}
glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 400, 400);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
printf("Error in framebuffer completeness \n");
}
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0, 0, 400, 400);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBegin(GL_QUADS);
glColor3f(1,0,0);
glVertex3f(0, 0, 0);
glVertex3f(200, 0, 0);
glVertex3f(200, 200, 0);
glVertex3f(0, 200, 0);
glColor3f(0, 1, 0);
glVertex3f(-100, -100, -1);
glVertex3f(100, -100, -1);
glVertex3f(100, 100, -1);
glVertex3f(-100, 100, -1);
glEnd();
glFogCoordf(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, (GLsizei)400, (GLsizei)400);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 400, 400);
glutSwapBuffers();
}
void update(int value) {
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
//set fbo,texBuffer and renderbuffer to 0
fbo = 0;
texColorBuffer = 0;
renderBuffer = 0;
//Create the window
glutCreateWindow("Lighting - videotutorialsrock.com");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0); //Add a timer
glutMainLoop();
return 0;
}