Last active
December 30, 2015 05:19
-
-
Save pizthewiz/7781829 to your computer and use it in GitHub Desktop.
Simple oF application with GL2, GL3 and ES2 passthrough shaders on a non-power-of-two image (1024x768).
This file contains 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 "shadyApp.h" | |
void shadyApp::setup() { | |
ofSetBackgroundColor(ofColor::gray); | |
ofSetFrameRate(60); | |
image.loadImage("checkerboard.jpg"); // 1024x768 | |
string passthroughVertex, passthroughFragment; | |
#ifdef TARGET_OPENGLES | |
ofLogNotice() << "ES2"; | |
// ES2 | |
passthroughVertex = "\n\ | |
attribute vec4 position;\n\ | |
attribute vec2 texcoord;\n\ | |
uniform mat4 projectionMatrix;\n\ | |
uniform mat4 modelViewMatrix;\n\ | |
varying vec2 varyingTexcoord;\n\ | |
void main() {\n\ | |
gl_Position = projectionMatrix * modelViewMatrix * position;\n\ | |
varyingTexcoord = texcoord;\n\ | |
}\n\ | |
"; | |
passthroughFragment = "precision highp float;\n\ | |
\n\ | |
uniform sampler2D tex0;\n\ | |
varying vec2 varyingTexcoord;\n\ | |
void main() {\n\ | |
gl_FragColor = texture2D(tex0, varyingTexcoord);\n\ | |
}\n\ | |
"; | |
#else | |
ofLogNotice() << "NOT ES2"; | |
if (ofIsGLProgrammableRenderer()) { | |
// GL3 | |
passthroughVertex = "#version 150\n\ | |
\n\ | |
uniform mat4 projectionMatrix;\n\ | |
uniform mat4 modelViewMatrix;\n\ | |
in vec4 position;\n\ | |
in vec2 texcoord;\n\ | |
out vec2 varyingTexcoord;\n\ | |
void main() {\n\ | |
gl_Position = projectionMatrix * modelViewMatrix * position;\n\ | |
varyingTexcoord = texcoord;\n\ | |
}\n\ | |
"; | |
// ??? - why gl_FragCoord.xy and not varyingTexcoord? | |
passthroughFragment = "#version 150\n\ | |
\n\ | |
uniform sampler2DRect tex0;\n\ | |
in vec2 varyingTexcoord;\n\ | |
out vec4 outputColor;\n\ | |
void main() {\n\ | |
outputColor = texture(tex0, gl_FragCoord.xy);\n\ | |
}\n\ | |
"; | |
} else { | |
// GL2 | |
passthroughVertex = "#version 120\n\ | |
\n\ | |
void main() {\n\ | |
gl_TexCoord[0] = gl_MultiTexCoord0;\n\ | |
gl_Position = ftransform();\n\ | |
}\n\ | |
"; | |
// ??? - why gl_FragCoord.xy and not gl_TextCoord[0].xy? | |
passthroughFragment = "#version 120\n\ | |
\n\ | |
uniform sampler2DRect tex0;\n\ | |
void main() {\n\ | |
gl_FragColor = texture2DRect(tex0, gl_FragCoord.xy);\n\ | |
}\n\ | |
"; | |
} | |
#endif | |
shader.setupShaderFromSource(GL_VERTEX_SHADER, passthroughVertex); | |
shader.setupShaderFromSource(GL_FRAGMENT_SHADER, passthroughFragment); | |
shader.bindDefaults(); | |
shader.linkProgram(); | |
fbo.allocate(1024, 768); | |
fbo.begin(); | |
ofClear(ofColor::black); | |
fbo.end(); | |
string version = (char*)glGetString(GL_SHADING_LANGUAGE_VERSION); | |
ofLog() << "GLSL version: " << version; | |
} | |
void shadyApp::update() { | |
fbo.begin(); | |
// NB - skip clearing | |
shader.begin(); | |
shader.setUniformTexture("tex0", image.getTextureReference(), image.getTextureReference().getTextureData().textureID); | |
ofSetColor(ofColor::white); | |
// NB - this fails to draw correctly on ES2! ofRect(0, 0, fbo.getWidth(), fbo.getHeight()); | |
// possibly a manifestation of https://github.com/openframeworks/openFrameworks/issues/2593 | |
image.draw(0.0f, 0.0f); | |
shader.end(); | |
fbo.end(); | |
} | |
void shadyApp::draw() { | |
ofSetColor(ofColor::white); | |
fbo.draw(0.0f, 0.0f); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment