Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created February 5, 2012 12:05
Show Gist options
  • Select an option

  • Save roxlu/1745031 to your computer and use it in GitHub Desktop.

Select an option

Save roxlu/1745031 to your computer and use it in GitHub Desktop.
Light Rays 0.5 - drawing into FBO
#include "testApp.h"
void testApp::setup(){
ofBackground(33);
ofSetVerticalSync(true);
take_screenshot = false ;
// create quad width size "s", centered
float s = 1;
float hs = s * 0.5;
plane_data.addVertex(-hs,-hs,0);
plane_data.addVertex(hs,-hs,0);
plane_data.addVertex(hs,hs,0);
plane_data.addVertex(-hs,hs,0);
// add texture coordinates
plane_data.addTexCoord(0,0);
plane_data.addTexCoord(1,0);
plane_data.addTexCoord(1,1);
plane_data.addTexCoord(0,1);
// create and bind VAO, which will remember:
// - the next vbo we bind (and the state)
// - the state of glEnableVertexAttribArray
// - the specs of glVertexAttribPointer
glGenVertexArraysAPPLE(1, &plane_vao); eglGetError();
glBindVertexArrayAPPLE(plane_vao);
// create shader
// -------------
shader.load("rays");
shader.enable();
shader.addUniform("projection_matrix");
shader.addUniform("view_matrix");
shader.addUniform("mode");
shader.addUniform("texture");
pos_attrib = glGetAttribLocation(shader.prog_id, "pos");
// create VBO
VertexP* vertex_data = plane_data.getVertexP();
glGenBuffers(1, &plane_vbo); eglGetError();
glBindBuffer(GL_ARRAY_BUFFER, plane_vbo); eglGetError();
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexP) * plane_data.size(), vertex_data, GL_STATIC_DRAW); eglGetError();
// define how we use attributes and the buffer
glBindBuffer(GL_ARRAY_BUFFER, plane_vbo);
glEnableVertexAttribArray(pos_attrib); eglGetError(); //
glVertexAttribPointer(
pos_attrib
,3
,GL_FLOAT
,GL_FALSE
,sizeof(VertexP)
,(GLvoid*)offsetof(VertexP, pos)
); eglGetError();
// setup matrices
projection.perspective(45.0f, 625.0f/469.0f, 0.1,100);
model.identity();
view.translate(0,0,-4);
// setup FBO
fbo.setup(625.0f, 469.0f);
fbo.addTexture(0);
}
void testApp::draw(){
// pass the projection matrix + view matrix to the shader.
shader.enable();
shader.uniformMat4fv("projection_matrix", projection.getPtr());
shader.uniformMat4fv("view_matrix", view.getPtr());
// draw the scene into a texture
fbo.begin();
shader.uniform1i("mode", 1); // draw w/o textures
glBindVertexArrayAPPLE(plane_vao);
glDrawArrays(GL_QUADS, 0, 4);
fbo.end();
if(take_screenshot) {
ofSaveScreen("light_rays_"+ofToString(ofGetHours()) +"_" +ofToString(ofGetMinutes()) +"_" +ofToString(ofGetSeconds()) +".png");
take_screenshot = false;
}
}
void testApp::keyPressed(int key){
if(key == ' ') {
take_screenshot = !take_screenshot;
}
}
#pragma once
#include "ofMain.h"
#include "Roxlu.h"
using namespace roxlu;
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
bool take_screenshot;
Mat4 projection;
Mat4 view;
Mat4 model;
VertexData plane_data;
Shader shader;
FBO fbo;
GLuint plane_vbo;
GLuint plane_vao;
GLint pos_attrib;
// not used in example
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
};
@roxlu
Copy link
Author

roxlu commented Feb 5, 2012

result is black because we're only drawing the scene into a fbo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment