Skip to content

Commit 1f73675

Browse files
authored
Custom depth shader in materialPBRAdvanced Example (#7643)
#changelog #gl
1 parent 6f5baa1 commit 1f73675

3 files changed

Lines changed: 40 additions & 7 deletions

File tree

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
1+
// we set this define in the ofApp class
2+
#if defined(SHADOW_DEPTH_PASS)
3+
uniform mat4 modelMatrix;
4+
in vec4 position;
5+
uniform float iElapsedTime;
6+
uniform float uWiggleVerts;
7+
#else
28
// the mesh we are using does not have texture coordinates
39
// so we are going to send the local vertex positions to use for animation
410
OUT vec3 v_localPos;
11+
#endif
12+
513

614
void main (void){
15+
vec4 npos = position;
16+
npos.xyz += uWiggleVerts * 0.25 * vec3( cos(iElapsedTime*1.3+(position.z*1.25)+position.y*1.25), 0.0, 0.0);
17+
#if defined(SHADOW_DEPTH_PASS)
18+
vec3 worldPosition = (modelMatrix * vec4(npos.xyz, 1.0)).xyz;
19+
sendShadowDepthWorldPosition(worldPosition);
20+
#else
721
v_localPos = position.xyz;
8-
vec4 npos = getTransformedPosition();
9-
// npos.xyz += vec3( cos(iElapsedTime+v_localPos.y*1.85), 0.0, 0.0);
1022
sendVaryings(npos);
1123
gl_Position = modelViewProjectionMatrix * npos;
24+
#endif
1225
}

examples/gl/materialPBRAdvanced/src/ofApp.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ void ofApp::draw(){
7272
int numShadowPasses = light.getNumShadowDepthPasses();
7373
for( int j = 0; j < numShadowPasses; j++ ) {
7474
light.beginShadowDepthPass(j);
75-
renderScene();
75+
renderScene(true);
7676
light.endShadowDepthPass(j);
7777
}
7878
}
7979

8080

8181
camera.begin(); {
8282

83-
renderScene();
83+
renderScene(false);
8484

8585
if( cubeMap.hasPrefilteredMap() ) {
8686
cubeMap.drawPrefilteredCube(0.2f);
@@ -108,11 +108,12 @@ void ofApp::draw(){
108108

109109
stringstream ss;
110110
ss << "Reload shader(r): make changes to shader in data/shaders/main.frag and then press 'r' to see changes.";
111+
ss << endl << "Wiggle verts(w): " << (bWiggleVerts ? "yes" : "no");
111112
ofDrawBitmapStringHighlight(ss.str(), 40, 40);
112113
}
113114

114115
//--------------------------------------------------------------
115-
void ofApp::renderScene() {
116+
void ofApp::renderScene(bool bShadowPass) {
116117

117118
matFloor.setMetallic(0.0);
118119
matFloor.setReflectance(0.01);
@@ -140,7 +141,14 @@ void ofApp::renderScene() {
140141
ofPopMatrix();
141142
matSphere.end();
142143

144+
if( bShadowPass && bWiggleVerts ) {
145+
mDepthShader.begin();
146+
mDepthShader.setUniform1f("iElapsedTime", ofGetElapsedTimef());
147+
mDepthShader.setUniform1f("uWiggleVerts", bWiggleVerts ? 1.0f : 0.0f);
148+
}
149+
// setting custom uniforms on a material automatically adds it to the shader
143150
matLogo.setCustomUniform1f("iElapsedTime", ofGetElapsedTimef());
151+
matLogo.setCustomUniform1f("uWiggleVerts", bWiggleVerts ? 1.0f : 0.0f);
144152
matLogo.begin();
145153
ofPushMatrix();
146154
ofTranslate( -70, -250, 0 );
@@ -149,6 +157,9 @@ void ofApp::renderScene() {
149157
meshLogoHollow.draw();
150158
ofPopMatrix();
151159
matLogo.end();
160+
if(bShadowPass && bWiggleVerts ) {
161+
mDepthShader.end();
162+
}
152163
}
153164

154165
//--------------------------------------------------------------
@@ -161,6 +172,9 @@ bool ofApp::reloadShader() {
161172
if( vbuffer.size() && fbuffer.size() ) {
162173
matLogo.setShaderMain(vbuffer.getText(), GL_VERTEX_SHADER, "main.vert");
163174
matLogo.setShaderMain(fbuffer.getText(), GL_FRAGMENT_SHADER, "main.frag");
175+
// configure the shader to include shadow functions for passing depth
176+
// declare a define so we can use the same shader file and run different bits of code
177+
light.getShadow().setupShadowDepthShader(mDepthShader, "#define SHADOW_DEPTH_PASS\n"+vbuffer.getText());
164178
return true;
165179
}
166180
return false;
@@ -174,6 +188,9 @@ void ofApp::keyPressed(int key){
174188
if( key == 'd' ) {
175189
bDebug = !bDebug;
176190
}
191+
if( key == 'w') {
192+
bWiggleVerts = !bWiggleVerts;
193+
}
177194
}
178195

179196
//--------------------------------------------------------------

examples/gl/materialPBRAdvanced/src/ofApp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ofApp : public ofBaseApp{
99
void update();
1010
void draw();
1111

12-
void renderScene();
12+
void renderScene(bool bShadowPass);
1313

1414
bool reloadShader();
1515

@@ -35,9 +35,12 @@ class ofApp : public ofBaseApp{
3535

3636
ofVboMesh meshPlySphere;
3737

38+
ofShader mDepthShader;
39+
3840
int mode = 0;
3941

4042
ofLight light;
4143
bool bDebug = false;
44+
bool bWiggleVerts = false;
4245

4346
};

0 commit comments

Comments
 (0)