Created
February 5, 2012 11:47
-
-
Save roxlu/1744957 to your computer and use it in GitHub Desktop.
Light Rays 0.4 - using VAO
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); | |
| // 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"); | |
| 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); | |
| } | |
| 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()); | |
| glBindVertexArrayAPPLE(plane_vao); | |
| glDrawArrays(GL_QUADS, 0, 4); | |
| 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; | |
| } | |
| } |
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; | |
| 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); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment