Skip to content

Instantly share code, notes, and snippets.

@indefinit
Last active April 6, 2018 01:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indefinit/266b3d0894f60e09210c2947c7e84984 to your computer and use it in GitHub Desktop.
Save indefinit/266b3d0894f60e09210c2947c7e84984 to your computer and use it in GitHub Desktop.
Steps to follow for using shaders in OpenFrameworks v0.9.8

#General rules for working with shaders in OpenFrameworks

  1. in main.cpp, set the GL version to 3,2:
  ofGLWindowSettings settings;
	settings.setGLVersion(3,2);
	ofCreateWindow(settings);
  1. in ofApp.h, declare your shader member variable:
  ofShader shader;
  1. create your .vert and .frag shader files inside bin/data/:
//sample myshader.vert
#version 150
uniform mat4 mvPMat;
in vec4 pos;
void main(){
	gl_Position = mvPMat * pos;
}

//sample myshader.frag
#version 150
out vec4 output_Color;
void main(){
	float windowWidth = 1024.0;
	float windowHeight = 768.0;
	float r = gl_FragCoord.x /windowWidth;
	float g = gl_FragCoord.y / windowHeight;
	float b = 1.0;
	float a = 1.0;
	output_Color = vec4(r,g,b,a);
}
  1. in ofApp::setup(), load your shaders:
//OF knows to load both your vert and frag shaders together
if(ofIsGLProgrammableRenderer()){
shader.load("myshader");
}

  1. in ofApp::draw(), draw your shapes inside the shader:
ofSetColor(255);
shader.begin();
  //try other drawing methods here instead of rectangle
  ofDrawRectangle(0,0,ofGetWidth(), ofGetHeight());
shader.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment