Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created June 26, 2012 08:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roxlu/2994484 to your computer and use it in GitHub Desktop.
Save roxlu/2994484 to your computer and use it in GitHub Desktop.
Raw FBO, openFrameworks
#include "testApp.h"
#include "Error.h"
//--------------------------------------------------------------
void testApp::setup(){
ofEnableNormalizedTexCoords();
ofDisableArbTex();
int w = ofGetWidth();
int h = ofGetHeight();
// CREATE TEXTURE FOR FBO
// ----------------------
glEnable(GL_TEXTURE_2D); eglGetError();
glGenTextures(1, &tex); eglGetError();
glActiveTexture(GL_TEXTURE0); eglGetError();
glBindTexture(GL_TEXTURE_2D, tex); eglGetError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); eglGetError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); eglGetError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); eglGetError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); eglGetError();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); eglGetError();
// CREATE FBO
// -----------
glGenFramebuffers(1, &fbo); eglGetError();
glBindFramebuffer(GL_FRAMEBUFFER, fbo); eglGetError();
// .. fbo with depth attachment.
glGenRenderbuffers(1, &rbo_depth); eglGetError();
glBindRenderbuffer(GL_RENDERBUFFER, rbo_depth); eglGetError();
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, w, h); eglGetError();
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo_depth); eglGetError();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0); eglGetError();
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
printf("Incomplete fbo.\n");
::exit(0);
}
eglCheckFramebufferStatus();
glBindFramebuffer(GL_FRAMEBUFFER, 0); eglGetError();
glBindRenderbuffer(GL_RENDERBUFFER, 0); eglGetError();
glBindTexture(GL_TEXTURE_2D, 0);
}
//--------------------------------------------------------------
void testApp::update(){
}
// Just draws a quad which we "capture" in a texture
void testApp::drawScene() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, float(ofGetWidth())/ofGetHeight(), 0.1, 100);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-10);
float s = 4;
glColor3f(1,0,1); eglGetError();
glBegin(GL_QUADS);
glVertex2f(-s,s);
glVertex2f(s,s);
glVertex2f(s,-s);
glVertex2f(-s,-s);
glEnd();
glPointSize(10.0f);
glColor3f(0,1,0);
glBegin(GL_POINTS);
glVertex3f(0.5,0,-1);
glEnd();
glColor3f(1,1,1);
eglGetError();
}
//--------------------------------------------------------------
void testApp::draw(){
// DRAW INTO FBO
int w = ofGetWidth();
int h = ofGetHeight();
// DRAW INTO TEXTURE USING FBO
// ---------------------------
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_DEPTH_TEST);
glBindFramebuffer(GL_FRAMEBUFFER, fbo); eglGetError();
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawScene();
// DRAW TEXTURE WITH CONTENTS OF SCENE (RENDER TO TEXTURE)
// -------------------------------------------------------
float s = 256;
glBindFramebuffer(GL_FRAMEBUFFER, 0); eglGetError();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_COLOR);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex); eglGetError();
glDisable(GL_LIGHTING);
float ww, hh;
ww = hh = 1.0;
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-ww,-hh, -1.0f);
glTexCoord2f(1,0); glVertex3f(ww,-hh, -1.0f);
glTexCoord2f(1,1); glVertex3f(ww,hh, -1.0f);
glTexCoord2f(0,1); glVertex3f(-ww,hh, -1.0f);
glEnd();
glGetError();
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment