Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created February 5, 2012 11:31
Show Gist options
  • Select an option

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

Select an option

Save roxlu/1744884 to your computer and use it in GitHub Desktop.
Light Rays 0.3 - complete using shaders and VBO
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.4, 1.0);
}
#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 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();
// 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());
// bind the vbo, enable vertex attributes and
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();
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;
}
}
#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;
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);
};
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
attribute vec4 pos;
void main() {
gl_Position = projection_matrix * view_matrix * pos;
}
@roxlu
Copy link
Author

roxlu commented Feb 5, 2012

Result

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