diff --git a/examples/gl/materialPBRAdvanced/bin/data/shaders/main.vert b/examples/gl/materialPBRAdvanced/bin/data/shaders/main.vert index a11008ed5a4..b8aa9ec2ef3 100644 --- a/examples/gl/materialPBRAdvanced/bin/data/shaders/main.vert +++ b/examples/gl/materialPBRAdvanced/bin/data/shaders/main.vert @@ -1,12 +1,25 @@ - +// we set this define in the ofApp class +#if defined(SHADOW_DEPTH_PASS) +uniform mat4 modelMatrix; +in vec4 position; +uniform float iElapsedTime; +uniform float uWiggleVerts; +#else // the mesh we are using does not have texture coordinates // so we are going to send the local vertex positions to use for animation OUT vec3 v_localPos; +#endif + void main (void){ + vec4 npos = position; + npos.xyz += uWiggleVerts * 0.25 * vec3( cos(iElapsedTime*1.3+(position.z*1.25)+position.y*1.25), 0.0, 0.0); +#if defined(SHADOW_DEPTH_PASS) + vec3 worldPosition = (modelMatrix * vec4(npos.xyz, 1.0)).xyz; + sendShadowDepthWorldPosition(worldPosition); +#else v_localPos = position.xyz; - vec4 npos = getTransformedPosition(); -// npos.xyz += vec3( cos(iElapsedTime+v_localPos.y*1.85), 0.0, 0.0); sendVaryings(npos); gl_Position = modelViewProjectionMatrix * npos; +#endif } diff --git a/examples/gl/materialPBRAdvanced/src/ofApp.cpp b/examples/gl/materialPBRAdvanced/src/ofApp.cpp index 13b8e10e97b..6965fe462f5 100644 --- a/examples/gl/materialPBRAdvanced/src/ofApp.cpp +++ b/examples/gl/materialPBRAdvanced/src/ofApp.cpp @@ -72,7 +72,7 @@ void ofApp::draw(){ int numShadowPasses = light.getNumShadowDepthPasses(); for( int j = 0; j < numShadowPasses; j++ ) { light.beginShadowDepthPass(j); - renderScene(); + renderScene(true); light.endShadowDepthPass(j); } } @@ -80,7 +80,7 @@ void ofApp::draw(){ camera.begin(); { - renderScene(); + renderScene(false); if( cubeMap.hasPrefilteredMap() ) { cubeMap.drawPrefilteredCube(0.2f); @@ -108,11 +108,12 @@ void ofApp::draw(){ stringstream ss; ss << "Reload shader(r): make changes to shader in data/shaders/main.frag and then press 'r' to see changes."; + ss << endl << "Wiggle verts(w): " << (bWiggleVerts ? "yes" : "no"); ofDrawBitmapStringHighlight(ss.str(), 40, 40); } //-------------------------------------------------------------- -void ofApp::renderScene() { +void ofApp::renderScene(bool bShadowPass) { matFloor.setMetallic(0.0); matFloor.setReflectance(0.01); @@ -140,7 +141,14 @@ void ofApp::renderScene() { ofPopMatrix(); matSphere.end(); + if( bShadowPass && bWiggleVerts ) { + mDepthShader.begin(); + mDepthShader.setUniform1f("iElapsedTime", ofGetElapsedTimef()); + mDepthShader.setUniform1f("uWiggleVerts", bWiggleVerts ? 1.0f : 0.0f); + } + // setting custom uniforms on a material automatically adds it to the shader matLogo.setCustomUniform1f("iElapsedTime", ofGetElapsedTimef()); + matLogo.setCustomUniform1f("uWiggleVerts", bWiggleVerts ? 1.0f : 0.0f); matLogo.begin(); ofPushMatrix(); ofTranslate( -70, -250, 0 ); @@ -149,6 +157,9 @@ void ofApp::renderScene() { meshLogoHollow.draw(); ofPopMatrix(); matLogo.end(); + if(bShadowPass && bWiggleVerts ) { + mDepthShader.end(); + } } //-------------------------------------------------------------- @@ -161,6 +172,9 @@ bool ofApp::reloadShader() { if( vbuffer.size() && fbuffer.size() ) { matLogo.setShaderMain(vbuffer.getText(), GL_VERTEX_SHADER, "main.vert"); matLogo.setShaderMain(fbuffer.getText(), GL_FRAGMENT_SHADER, "main.frag"); + // configure the shader to include shadow functions for passing depth + // declare a define so we can use the same shader file and run different bits of code + light.getShadow().setupShadowDepthShader(mDepthShader, "#define SHADOW_DEPTH_PASS\n"+vbuffer.getText()); return true; } return false; @@ -174,6 +188,9 @@ void ofApp::keyPressed(int key){ if( key == 'd' ) { bDebug = !bDebug; } + if( key == 'w') { + bWiggleVerts = !bWiggleVerts; + } } //-------------------------------------------------------------- diff --git a/examples/gl/materialPBRAdvanced/src/ofApp.h b/examples/gl/materialPBRAdvanced/src/ofApp.h index fabb9b96b9d..d496acf732b 100644 --- a/examples/gl/materialPBRAdvanced/src/ofApp.h +++ b/examples/gl/materialPBRAdvanced/src/ofApp.h @@ -9,7 +9,7 @@ class ofApp : public ofBaseApp{ void update(); void draw(); - void renderScene(); + void renderScene(bool bShadowPass); bool reloadShader(); @@ -35,9 +35,12 @@ class ofApp : public ofBaseApp{ ofVboMesh meshPlySphere; + ofShader mDepthShader; + int mode = 0; ofLight light; bool bDebug = false; + bool bWiggleVerts = false; };