[英]render offscreen framebuffer texture to main framebuffer
我正在尝试使用无法控制其设置方式的framebuffer对象。 因此,我要创建一个具有深度的离屏fbo来绘制纹理。 然后将其绘制到主帧缓冲区。 我现在可以使用OpenGL Profiler从资源中看到纹理,但我似乎无法将其绘制到主帧缓冲区。
这是我的代码
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;
}
以前从未使用过glCopyTexSubImage2D
,这是我对opengl手册页的最佳解释。
来自http://www.opengl.org/sdk/docs/man/xhtml/glCopyTexSubImage2D.xml
Blockquote glCopyTexSubImage2D用当前GL_READ_BUFFER(而不是主存储器)中的像素替换二维纹理图像,立方体贴图纹理图像的矩形部分或一维数组纹理的多个切片的线性部分。 glTexSubImage2D)。
这听起来像您要更换什么是在texColorBuffer
与originalFramebuffer
。
我将framebuffer对象绘制到屏幕的方式是使用带纹理的全屏四边形,如下所示:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
glPushMatrix();
GLfloat identity[4][4];
for (int i=0; i<4; ++i){
for (int j=0; j<4; ++j){
identity[i][j] = 0;
}
}
identity[0][0] = identity[1][1] = identity[2][2] = identity[3][3] = 1;
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(identity);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(identity);
glActiveTexture(GL_TEXTURE0);
glBindTexture([outTex target], texColorBuffer);
glBegin(GL_QUADS);
glVertex2f(-1,-1);
glTexCoord2f(0, 0);
glVertex2f( 1,-1);
glTexCoord2f(1, 0);
glVertex2f( 1, 1);
glTexCoord2f(1, 1);
glVertex2f(-1, 1);
glTexCoord2f(0, 1);
glEnd();
glPopMatrix();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.