Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Last active June 20, 2022 01:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kylemcdonald/bedfba64d4c8fa4010b282471ad59735 to your computer and use it in GitHub Desktop.
Save kylemcdonald/bedfba64d4c8fa4010b282471ad59735 to your computer and use it in GitHub Desktop.
Depth in shader using openFrameworks.
#include "ofMain.h"
class ofApp : public ofBaseApp {
public:
ofShader shader;
ofEasyCam cam;
ofFbo fbo;
void setup() {
ofBackground(0);
ofFbo::Settings s;
s.width = ofGetWidth();
s.height = ofGetHeight();
s.useDepth = true;
s.depthStencilAsTexture = true;
fbo.allocate(s);
shader.load("shader");
}
void draw() {
ofEnableDepthTest();
fbo.begin();
cam.begin();
ofClear(0);
ofSetColor(255);
ofIcoSpherePrimitive(400, 2).draw();
cam.end();
fbo.end();
ofDisableDepthTest();
if(ofGetKeyPressed(' ')) {
fbo.draw(0, 0);
} else {
shader.begin();
shader.setUniformTexture("tex0", fbo.getDepthTexture(), 1);
fbo.draw(0, 0);
shader.end();
}
}
};
int main() {
ofGLWindowSettings settings;
settings.setSize(1920, 1080);
// settings.setGLVersion(3,2); // GL3
settings.setGLVersion(2,1); // GL2
ofCreateWindow(settings);
ofRunApp(new ofApp());
}
// #version 150 // GL3
#version 120 // GL2
const float near = 0.9835;
const float range = .005;
uniform sampler2DRect tex0;
// in vec2 texCoordVarying; // GL3
// out vec4 outputColor; // GL3
varying vec2 texCoordVarying; // GL2
void main (void) {
// float depth = texture(tex0, texCoordVarying).r; // GL3
float depth = texture2DRect(tex0, texCoordVarying).r;
float b = (depth - near) / range;
// outputColor = vec4(b,b,b,1.); // GL3
gl_FragColor = vec4(b,b,b,1.); // GL2
}
// #version 150 // GL3
#version 120 // GL2
uniform sampler2DRect tex0;
// uniform mat4 modelViewProjectionMatrix; // GL3
// in vec4 position; // GL3
// in vec2 texcoord; // GL3
// out vec2 texCoordVarying; // GL3
varying vec2 texCoordVarying; // GL2
void main() {
// gl_Position = modelViewProjectionMatrix * position; // GL3
// texCoordVarying = texcoord; // GL3
texCoordVarying = gl_MultiTexCoord0.xy; // GL2
gl_Position = ftransform(); // GL2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment