Created
February 5, 2012 12:43
-
-
Save roxlu/1745339 to your computer and use it in GitHub Desktop.
Light Rays 0.6 - drawing into FBO + showing the "render to texture" result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| uniform sampler2D texture; | |
| uniform int mode; | |
| varying vec2 tex_v; | |
| void main() { | |
| if(mode == 1) { | |
| gl_FragColor = vec4(1.0, 0.0, 0.4, 1.0); | |
| } | |
| else if(mode == 2) { | |
| gl_FragColor = vec4(1.0, 0.6, 0.0, 1.0); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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"); | |
| tex_attrib = glGetAttribLocation(shader.prog_id, "tex"); | |
| // create VBO | |
| VertexPT* vertex_data = plane_data.getVertexPT(); | |
| glGenBuffers(1, &plane_vbo); eglGetError(); | |
| glBindBuffer(GL_ARRAY_BUFFER, plane_vbo); eglGetError(); | |
| glBufferData(GL_ARRAY_BUFFER, sizeof(VertexPT) * 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(); // | |
| glEnableVertexAttribArray(tex_attrib); eglGetError(); | |
| glVertexAttribPointer( | |
| pos_attrib | |
| ,3 | |
| ,GL_FLOAT | |
| ,GL_FALSE | |
| ,sizeof(VertexPT) | |
| ,(GLvoid*)offsetof(VertexPT, pos) | |
| ); eglGetError(); | |
| glVertexAttribPointer( | |
| tex_attrib | |
| ,3 | |
| ,GL_FLOAT | |
| ,GL_FALSE | |
| ,sizeof(VertexPT) | |
| ,(GLvoid*)offsetof(VertexPT, tex) | |
| ); 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(); | |
| // draw the scene just to show how it looks like | |
| glDrawArrays(GL_QUADS, 0, 4); | |
| // draw tiny version of "captured" scene (with fbo). | |
| // ------------------------------------------------- | |
| // 1. setup texture for shader. | |
| glActiveTexture(GL_TEXTURE0); // use the first texture unit | |
| glBindTexture(GL_TEXTURE_2D, fbo.getTextureID(0)); // make this our active texture | |
| shader.uniform1i("tex", 0); // tell the shader we're using the first texture unit. | |
| // 2. scale model a bit | |
| Mat4 model; | |
| model.scale(0.5, 0.5, 0.5); | |
| model = model * view; | |
| model.setPosition(0.0, 0.0, -3.9); | |
| shader.uniformMat4fv("view_matrix", model.getPtr()); // using modelview here. | |
| shader.uniform1i("mode", 2); // draw using texcoords | |
| // 3. draw again | |
| glBindVertexArrayAPPLE(plane_vao); | |
| glDrawArrays(GL_QUADS, 0, 4); | |
| if(take_screenshot) { | |
| ofSaveScreen("light_rays_"+ofToString(ofGetHours()) +"_" +ofToString(ofGetMinutes()) +"_" +ofToString(ofGetSeconds()) +".png"); | |
| take_screenshot = false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| GLint tex_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); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| uniform mat4 projection_matrix; | |
| uniform mat4 view_matrix; | |
| varying vec2 tex_v; | |
| attribute vec2 tex; | |
| attribute vec4 pos; | |
| void main() { | |
| tex_v = tex; | |
| gl_Position = projection_matrix * view_matrix * pos; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result
