diff --git a/4_Example_Materials/src/ofApp.cpp b/4_Example_Materials/src/ofApp.cpp index 7e1b510..06fa63e 100644 --- a/4_Example_Materials/src/ofApp.cpp +++ b/4_Example_Materials/src/ofApp.cpp @@ -15,7 +15,7 @@ void ofApp::setup() { for (size_t i = 0; i < OFAPP_NUM_ITEMS; i++) { std::unique_ptr m; m = std::make_unique(); - string s = ofToString(i); + string s = "MATERIAL_" + ofToString(i); m->setup(s); materials.push_back(std::move(m)); } @@ -62,19 +62,24 @@ void ofApp::draw() { //-------------------------------------------------------------- void ofApp::buildHelp() { - string s = ""; - s += "KEYS\n\n"; - s += "DEBUG LIGHTS\n"; - s += "L : " + string(bEnableLights ? "ON" : "OFF") + "\n\n"; bool b1 = pointLights[0]->getIsEnabled(); bool b2 = pointLights[1]->getIsEnabled(); bool b3 = pointLights[2]->getIsEnabled(); bool b4 = pointLights[3]->getIsEnabled(); + bool b5 = pointLights[4]->getIsEnabled(); + + string s = ""; + s += "KEYS\n\n"; + s += "HELP\n"; + s += "H : " + string(bHelp ? "ON" : "OFF") + "\n\n"; + s += "DEBUG LIGHTS\n"; + s += "L : " + string(bEnableLights ? "ON" : "OFF") + "\n\n"; s += "ENABLE LIGHT\n"; s += "1 : " + string(b1 ? "ON" : "OFF") + "\n"; s += "2 : " + string(b2 ? "ON" : "OFF") + "\n"; s += "3 : " + string(b3 ? "ON" : "OFF") + "\n"; - s += "4 : " + string(b4 ? "ON" : "OFF"); + s += "4 : " + string(b4 ? "ON" : "OFF") + "\n"; + s += "5 : " + string(b5 ? "ON" : "OFF"); sHelp = s; } @@ -87,11 +92,11 @@ void ofApp::drawGui() { // draw gui panels for (size_t i = 0; i < materials.size(); i++) { - materials[i]->gui.draw(); + materials[i]->drawGui(); } for (size_t i = 0; i < objects.size(); i++) { - objects[i]->gui.draw(); + objects[i]->drawGui(); } #ifdef SURFING__USE__LIGHTS @@ -212,8 +217,9 @@ void ofApp::keyPressed(int key) { lindex = 2; } else if (key == '4') { lindex = 3; + }else if (key == '5') { + lindex = 4; } - //.. if (lindex > -1 && lindex < (int)pointLights.size()) { if (pointLights[lindex]->getIsEnabled()) { pointLights[lindex]->disable(); diff --git a/4_Example_Materials/src/ofApp.h b/4_Example_Materials/src/ofApp.h index daee2c5..fb292a9 100644 --- a/4_Example_Materials/src/ofApp.h +++ b/4_Example_Materials/src/ofApp.h @@ -2,7 +2,6 @@ #include "ofMain.h" #include "SurfingMaterial.h" -#include "ofxSurfingPBR.h" #include "SurfingMeshSphereDisplaced.h" @@ -31,7 +30,7 @@ class ofApp : public ofBaseApp { vector> objects; #ifdef SURFING__USE__LIGHTS - const int OFAPP_NUM_LIGHTS = 4; + const int OFAPP_NUM_LIGHTS = 5; bool bEnableLights = true; vector> pointLights; float colorHue = 0.0f; diff --git a/5_Example_Shadows/Capture.PNG b/5_Example_Shadows/Capture.PNG new file mode 100644 index 0000000..fb452ba Binary files /dev/null and b/5_Example_Shadows/Capture.PNG differ diff --git a/5_Example_Shadows/README.md b/5_Example_Shadows/README.md new file mode 100644 index 0000000..a07bba2 --- /dev/null +++ b/5_Example_Shadows/README.md @@ -0,0 +1 @@ +![](Capture.PNG) \ No newline at end of file diff --git a/5_Example_Shadows/bin/data/.gitkeep b/5_Example_Shadows/bin/data/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/5_Example_Shadows/bin/data/Sphere b/5_Example_Shadows/bin/data/Sphere new file mode 100644 index 0000000..5e65a16 --- /dev/null +++ b/5_Example_Shadows/bin/data/Sphere @@ -0,0 +1,22 @@ +{ + "Sphere": { + "Coat": { + "Clear_Coat": "0", + "Clear_Coat_Roughness": "0.0001", + "Clear_Coat_Strength": "0.0001" + }, + "Colors": { + "Ambient_Color": "0.849315, 0.801791, 0.0264705, 1", + "Diffuse_Color": "0.849315, 0.801791, 0.0264705, 1", + "Emissive_Color": "0.849315, 0.801791, 0.0264705, 1", + "Specular_Color": "0.849315, 0.801791, 0.0264705, 1" + }, + "Settings": { + "Metallic": "0", + "Reflectance": "0", + "Roughness": "0", + "Shininess": "0" + }, + "UI_HELPERS": "0" + } +} \ No newline at end of file diff --git a/5_Example_Shadows/src/ofApp.cpp b/5_Example_Shadows/src/ofApp.cpp index 201c376..21a3ec5 100644 --- a/5_Example_Shadows/src/ofApp.cpp +++ b/5_Example_Shadows/src/ofApp.cpp @@ -2,47 +2,64 @@ #include "ofGLProgrammableRenderer.h" //-------------------------------------------------------------- -void ofApp::setup(){ +void ofApp::setup() { + +#if 1 + ofxSurfing::setOfxGuiTheme(); + ofxSurfing::setWindowTitleAsProjectName(); + ofxSurfing::setWindowAtMonitor(-1); +#endif + ofSetLogLevel(OF_LOG_VERBOSE); - + + //-- + + // shadows and lights work with materials + // create some different materials so the lighting and shadows can be applied + + //-- + // add two lights int numLights = 2; - for( int i = 0; i < numLights; i++ ) { + for (int i = 0; i < numLights; i++) { auto light = std::make_shared(); light->enable(); - if( i == 0 ) { + if (i == 0) { light->setPointLight(); -// light->setDirectional(); -// light->setSpotlight(60, 20); -// light->getShadow().setNearClip(200); -// light->getShadow().setFarClip(2000); -// light->setPosition( 210, 330.0, 750 ); -// light->setAmbientColor(ofFloatColor(0.4)); + //light->setDirectional(); + //light->setSpotlight(60, 20); + //light->getShadow().setNearClip(200); + //light->getShadow().setFarClip(2000); + //light->setPosition( 210, 330.0, 750 ); + //light->setAmbientColor(ofFloatColor(0.4)); } else { // set the light to be a spot light with a cutoff (cone/fov) of 30 degrees // and a concentration (softness) of 50 out of a range 0 - 128 light->setSpotlight(60, 20); light->getShadow().setNearClip(200); light->getShadow().setFarClip(2000); - light->setPosition( 210, 330.0, 750 ); + light->setPosition(210, 330.0, 750); light->setAmbientColor(ofFloatColor(0.4)); } // set the strength of the shadow, how visible it is // default is 0.5 light->getShadow().setStrength(0.6f); - - if( light->getType() != OF_LIGHT_POINT ){ - glm::quat xq = glm::angleAxis(glm::radians(-30.0f), glm::vec3(1,0,0)); - glm::quat yq = glm::angleAxis(glm::radians(20.0f), glm::vec3(0,1,0)); - light->setOrientation(yq*xq); + + if (light->getType() != OF_LIGHT_POINT) { + glm::quat xq = glm::angleAxis(glm::radians(-30.0f), glm::vec3(1, 0, 0)); + glm::quat yq = glm::angleAxis(glm::radians(20.0f), glm::vec3(0, 1, 0)); + light->setOrientation(yq * xq); } - - if( !ofIsGLProgrammableRenderer() ) { + + if (!ofIsGLProgrammableRenderer()) { light->enable(); } - + lights.push_back(light); } + + //-- + // shadows are disabled by default // call this function to enable all of them that are attached to lights ofShadow::enableAllShadows(); @@ -54,69 +71,121 @@ void ofApp::setup(){ // normal bias default is 0 // moves the bias along the normal of the mesh, helps reduce shadow acne ofShadow::setAllShadowNormalBias(-4.f); - #ifndef TARGET_OPENGLES - ofShadow::setAllShadowDepthResolutions( 1024, 1024 ); - #endif +#ifndef TARGET_OPENGLES + ofShadow::setAllShadowDepthResolutions(1024, 1024); +#endif shadowType = OF_SHADOW_TYPE_PCF_LOW; ofShadow::setAllShadowTypes(shadowType); // point light shadow depth maps require OF_PRIMITIVE_TRIANGLES // so we grab some meshes here for drawing and make sure they use OF_PRIMITIVE_TRIANGLES // we also use a higher resolution for the meshes than the default + + //-- + sphereMesh = ofMesh::sphere(1.0, 48, OF_PRIMITIVE_TRIANGLES); boxMesh = ofMesh::box(1, 1, 1, 24, 24, 24); - cylinderMesh = ofMesh::cylinder(0.4, 1.0, 48, 12, 4, true, OF_PRIMITIVE_TRIANGLES ); - - // shadows and lights work with materials - // create some different materials so the lighting and shadows can be applied - boxMaterial.setDiffuseColor( ofFloatColor(0.25) ); + cylinderMesh = ofMesh::cylinder(0.4, 1.0, 48, 12, 4, true, OF_PRIMITIVE_TRIANGLES); + + logoMesh.load("models\\ofLogoHollow.ply"); + logoMesh.mergeDuplicateVertices(); + // we need to flip the normals for this mesh // + for (size_t i = 0; i < logoMesh.getNumNormals(); i++) { + logoMesh.getNormals()[i] *= -1.f; + } + + //-- + + setupMaterials(); +} + +//-------------------------------------------------------------- +void ofApp::setupMaterials() { + +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS + + bDrawBg.set("Bg", true); + bDrawLogo.set("Logo", true); + bDrawBox.set("Box", true); + bDrawSphere.set("Sphere", true); + + parameters.setName("ofApp Draw"); + parameters.add(bDrawBg); + parameters.add(bDrawLogo); + parameters.add(bDrawBox); + parameters.add(bDrawSphere); + ofAddListener(parameters.parameterChangedE(), this, &ofApp::Changed); + + gui.setup(parameters); + + // required to handle independent settings + // and use the internal gui panels independently for each instance. + bgMaterial.setup(bDrawBg.getName()); + logoMaterial.setup(bDrawLogo.getName()); + boxMaterial.setup(bDrawBox.getName()); + sphereMaterial.setup(bDrawSphere.getName()); + + refreshGuiAnchor(); + +#else + #if 1 + //TODO: fix overwrite addon settings mode.. + resetMaterials(); + #endif +#endif + + //-- + +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS + callback_t f = std::bind(&ofApp::save, this); + autoSaver.setFunctionSaver(f); + load(); +#endif +} +//-------------------------------------------------------------- +void ofApp::resetMaterials() { + + boxMaterial.setDiffuseColor(ofFloatColor(0.25)); boxMaterial.setShininess(60); boxMaterial.setSpecularColor(ofFloatColor(1)); - - bgMaterial.setDiffuseColor( ofFloatColor(0.15) ); + + bgMaterial.setDiffuseColor(ofFloatColor(0.15)); bgMaterial.setShininess(0.0); bgMaterial.setSpecularColor(ofFloatColor(0.25)); - + sphereMaterial.setAmbientColor(ofFloatColor(0.25)); sphereMaterial.setDiffuseColor(ofFloatColor(0.85, 1.0)); - sphereMaterial.setSpecularColor(ofFloatColor(1.0,1.0,1.0,1.0)); + sphereMaterial.setSpecularColor(ofFloatColor(1.0, 1.0, 1.0, 1.0)); sphereMaterial.setShininess(100); - - logoMesh.load("ofLogoHollow.ply"); - logoMesh.mergeDuplicateVertices(); - // we need to flip the normals for this mesh // - for( size_t i = 0; i < logoMesh.getNumNormals(); i++ ) { - logoMesh.getNormals()[i] *= -1.f; - } - logoMaterial.setAmbientColor(ofFloatColor(0.85, 0.16, 0.43 ) * 0.8); + + logoMaterial.setAmbientColor(ofFloatColor(0.85, 0.16, 0.43) * 0.8); logoMaterial.setDiffuseColor(ofFloatColor(0.85, 0.16, 0.43, 1.0)); - logoMaterial.setSpecularColor(ofFloatColor(1.0,1.0,1.0,1.0)); + logoMaterial.setSpecularColor(ofFloatColor(1.0, 1.0, 1.0, 1.0)); logoMaterial.setShininess(50); - } //-------------------------------------------------------------- -void ofApp::update(){ - float deltaTime = ofClamp(ofGetLastFrameTime(), 1.f/5000.f, 1.f/5.f); +void ofApp::update() { + float deltaTime = ofClamp(ofGetLastFrameTime(), 1.f / 5000.f, 1.f / 5.f); float etimef = ofGetElapsedTimef(); - - if( lights.size() > 0 ){ - if( lights[0]->getType() == OF_LIGHT_POINT ) { + + if (lights.size() > 0) { + if (lights[0]->getType() == OF_LIGHT_POINT) { float tangle = etimef * 1.05; - lights[0]->setPosition( -120, sinf(tangle) * 180.f+150, cosf(tangle) * 100.f ); + lights[0]->setPosition(-120, sinf(tangle) * 180.f + 150, cosf(tangle) * 100.f); } } - + colorHue += deltaTime * 2.0; - if(colorHue >= 255){ + if (colorHue >= 255) { colorHue = 0.f; } lightColor.setHue(colorHue); - - if( lights.size() > 0 ) { - auto& light = lights[0]; - lightColor.setHsb(ofWrap(colorHue, 0, 255.f), 210, 230 ); + + if (lights.size() > 0) { + auto & light = lights[0]; + lightColor.setHsb(ofWrap(colorHue, 0, 255.f), 210, 230); // ambient color applies all over regardless of being lit or not - light->setAmbientColor(lightColor*0.2); + light->setAmbientColor(lightColor * 0.2); light->setDiffuseColor(lightColor); lightColor.setBrightness(250); light->setSpecularColor(lightColor); @@ -124,22 +193,27 @@ void ofApp::update(){ } //-------------------------------------------------------------- -void ofApp::draw(){ - +void ofApp::draw() { + drawScene(); + drawGui(); +} + +//-------------------------------------------------------------- +void ofApp::drawScene() { + ofEnableDepthTest(); - - - for( int i = 0; i < lights.size(); i++ ) { - auto& light = lights[i]; + + for (int i = 0; i < lights.size(); i++) { + auto & light = lights[i]; // query the light to see if it has a depth pass - if( light->shouldRenderShadowDepthPass() ) { + if (light->shouldRenderShadowDepthPass()) { // Get the number of passes required. // By default the number of passes is 1. And we could just call beginShadowDepthPass() or beginShadowDepthPass(0); // It will be more than one pass if it is a pointlight with setSingleOmniPass set to false // or a platform that does not support geometry shaders. // Most likely it will be a single pass, but we get the number of passes to be safe. int numShadowPasses = light->getNumShadowDepthPasses(); - for( int j = 0; j < numShadowPasses; j++ ) { + for (int j = 0; j < numShadowPasses; j++) { light->beginShadowDepthPass(j); // Shadows have the following gl culling enabled by default // this helps reduce z fighting by only rendering the rear facing triangles to the depth map @@ -149,7 +223,7 @@ void ofApp::draw(){ //glFrontFace(mGlFrontFaceWindingOrder); // tells OpenGL to cull front faces //glCullFace(GL_FRONT); - + // the culling can be disabled by calling //light->getShadow().setGlCullingEnabled(false); // or the culling winding order can be changed by calling @@ -159,227 +233,335 @@ void ofApp::draw(){ } } } - + camera.begin(); - - if( !ofIsGLProgrammableRenderer() ) { + + if (!ofIsGLProgrammableRenderer()) { ofEnableLighting(); } - + // CULL the back faces of the geometry for rendering glEnable(GL_CULL_FACE); glFrontFace(GL_CW); glCullFace(GL_BACK); renderScene(); glDisable(GL_CULL_FACE); - - if( !ofIsGLProgrammableRenderer() ) { + + if (!ofIsGLProgrammableRenderer()) { ofDisableLighting(); } - - for( int i = 0; i < lights.size(); i++ ){ - auto& light = lights[i]; - + ofPushStyle(); + for (int i = 0; i < lights.size(); i++) { + auto & light = lights[i]; + ofSetColor(light->getDiffuseColor()); - if(light->getType() == OF_LIGHT_POINT ) { - ofDrawSphere( light->getPosition(), 12 ); + if (light->getType() == OF_LIGHT_POINT) { + ofDrawSphere(light->getPosition(), 12); } else { light->draw(); } - if( light->getIsEnabled() && light->getShadow().getIsEnabled() && bDrawFrustums ){ + if (light->getIsEnabled() && light->getShadow().getIsEnabled() && bDrawFrustums) { light->getShadow().drawFrustum(); } } + ofPopStyle(); camera.end(); - ofDisableDepthTest(); - - +} + +//-------------------------------------------------------------- +void ofApp::buildHelp() { + stringstream ss; - - if( !ofIsGLProgrammableRenderer() ) { - ss << endl << "SHADOWS ONLY WORK WITH PROGRAMMABLE RENDERER!" << endl; - } else if( !ofShadow::areShadowsSupported() ) { - ss << endl << "SHADOWS NOT SUPPORTED ON THIS PLATFORM!" << endl; + ss << "HELP "; + ss << ofxSurfing::getProjectName() << endl + << endl; + + if (!ofIsGLProgrammableRenderer()) { + ss << endl + << "SHADOWS ONLY WORK WITH PROGRAMMABLE RENDERER!" << endl; + } else if (!ofShadow::areShadowsSupported()) { + ss << endl + << "SHADOWS NOT SUPPORTED ON THIS PLATFORM!" << endl; } else { ss << "Shadows enabled (spacebar): " << bEnableShadows; - ss << endl << "Draw frustums (f): " << bDrawFrustums; - ss << endl << "Shadow Type (right): " << ofShadow::getShadowTypeAsString(shadowType); - ss << endl << "Sample Radius (up / down): " << shadowSampleRadius; + ss << endl + << "Draw frustums (f): " << bDrawFrustums; + ss << endl + << "Shadow Type (right): " << ofShadow::getShadowTypeAsString(shadowType); + ss << endl + << "Sample Radius (up / down): " << shadowSampleRadius; } - - - ofDrawBitmapStringHighlight(ss.str(), 20, 20 ); - - ofSetColor(255); - ofEnableAlphaBlending(); - + sHelp = ss.str(); + //sHelp = ofToUpper(s); +} + +//-------------------------------------------------------------- +void ofApp::drawGui() { + ofDisableDepthTest(); + + buildHelp(); + ofxSurfing::ofDrawBitmapStringBox(sHelp, ofxSurfing::SURFING_LAYOUT_BOTTOM_RIGHT); + + //ofSetColor(255); + //ofEnableAlphaBlending(); + +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS + refreshGuiLinks(); + + gui.draw(); + if (bDrawBg) bgMaterial.drawGui(); + if (bDrawBox) boxMaterial.drawGui(); + if (bDrawLogo) logoMaterial.drawGui(); + if (bDrawSphere) sphereMaterial.drawGui(); +#endif } + // create a renderScene() function so the same drawing can happen in both ofApp::draw() // and inside the ofLight::beginShadowDepthPass() //-------------------------------------------------------------- void ofApp::renderScene() { + ofPushStyle(); + float etimef = ofGetElapsedTimef(); - ofSetColor(200, 50, 120 ); - sphereMaterial.begin(); - ofPushMatrix(); - ofTranslate(-120+cosf(etimef*1.4)*20.0, 0, sinf(etimef*1.7)*250 ); - ofScale( 30, 30, 30 ); - sphereMesh.draw(); - sphereMaterial.end(); - ofSetColor( 255 ); - // ofDrawSphere() is triangle strip!!!! - // it will cause issues with rendering of point light depth maps - // ofDrawSphere(0, 0, 0, 100 ); - ofPopMatrix(); - - boxMaterial.begin(); -// ofDrawBox(cosf(etimef) * 200., 0, sinf(etimef*0.452) * 100, 100 ); - ofPushMatrix(); { - ofTranslate(250, cosf(etimef*0.6)*50-80, 200 ); - ofRotateZDeg(ofWrapDegrees((etimef*0.04) * 360)); - ofRotateXDeg(ofWrapDegrees((etimef*0.06) * 360)); - ofScale(100, 100, 100.0); - boxMesh.draw(); - } ofPopMatrix(); - - ofPushMatrix(); { - ofTranslate(-250, cosf(etimef*0.4)*90-30, 200 ); - ofRotateZDeg(ofWrapDegrees((etimef*0.034) * 360)); - ofRotateXDeg(ofWrapDegrees((etimef*0.067) * 360)); - ofScale(100, 100, 100.0); - boxMesh.draw(); - } ofPopMatrix(); - - boxMaterial.end(); - - ofSetColor( 200 ); - - bgMaterial.begin(); - ofPushMatrix(); { - ofTranslate(0, -250, 0); - ofScale(1000, 50, 1000); - boxMesh.draw(); - } ofPopMatrix(); - - ofPushMatrix(); { - ofTranslate(500, 0, 0); - ofScale(50, 1000, 1000); - boxMesh.draw(); - } ofPopMatrix(); - - - ofPushMatrix(); { - ofTranslate(-500, 0, 0); - ofScale(50, 1000, 1000); - boxMesh.draw(); - } ofPopMatrix(); - - ofPushMatrix(); { - ofTranslate(0, 0, -500); - ofScale(1000, 1000, 50); - boxMesh.draw(); - } ofPopMatrix(); - - ofPushMatrix(); { - ofTranslate(0, 500, 0); - ofScale(1000, 50, 1000); - boxMesh.draw(); - } ofPopMatrix(); - - bgMaterial.end(); - - logoMaterial.begin(); - ofPushMatrix(); - ofTranslate( -50, -170, 50 ); - ofRotateXDeg(-90); - ofScale(60,60,60); - logoMesh.draw(); - ofPopMatrix(); - logoMaterial.end(); + +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS + if (bDrawSphere) +#endif + { + //ofSetColor(200, 50, 120); + sphereMaterial.begin(); + + ofPushMatrix(); + ofTranslate(-120 + cosf(etimef * 1.4) * 20.0, 0, sinf(etimef * 1.7) * 250); + ofScale(30, 30, 30); + sphereMesh.draw(); + + sphereMaterial.end(); + + //ofSetColor(255); + // ofDrawSphere() is triangle strip!!!! + // it will cause issues with rendering of point light depth maps + // ofDrawSphere(0, 0, 0, 100 ); + + ofPopMatrix(); + } + +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS + if (bDrawBox) +#endif + { + boxMaterial.begin(); + + //ofDrawBox(cosf(etimef) * 200., 0, sinf(etimef*0.452) * 100, 100 ); + ofPushMatrix(); + { + ofTranslate(250, cosf(etimef * 0.6) * 50 - 80, 200); + ofRotateZDeg(ofWrapDegrees((etimef * 0.04) * 360)); + ofRotateXDeg(ofWrapDegrees((etimef * 0.06) * 360)); + ofScale(100, 100, 100.0); + boxMesh.draw(); + } + ofPopMatrix(); + + ofPushMatrix(); + { + ofTranslate(-250, cosf(etimef * 0.4) * 90 - 30, 200); + ofRotateZDeg(ofWrapDegrees((etimef * 0.034) * 360)); + ofRotateXDeg(ofWrapDegrees((etimef * 0.067) * 360)); + ofScale(100, 100, 100.0); + boxMesh.draw(); + } + ofPopMatrix(); + + boxMaterial.end(); + } + +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS + if (bDrawBg) +#endif + { + //ofSetColor(200); + bgMaterial.begin(); + + ofPushMatrix(); + { + ofTranslate(0, -250, 0); + ofScale(1000, 50, 1000); + boxMesh.draw(); + } + ofPopMatrix(); + + ofPushMatrix(); + { + ofTranslate(500, 0, 0); + ofScale(50, 1000, 1000); + boxMesh.draw(); + } + ofPopMatrix(); + + ofPushMatrix(); + { + ofTranslate(-500, 0, 0); + ofScale(50, 1000, 1000); + boxMesh.draw(); + } + ofPopMatrix(); + + ofPushMatrix(); + { + ofTranslate(0, 0, -500); + ofScale(1000, 1000, 50); + boxMesh.draw(); + } + ofPopMatrix(); + + ofPushMatrix(); + { + ofTranslate(0, 500, 0); + ofScale(1000, 50, 1000); + boxMesh.draw(); + } + ofPopMatrix(); + + bgMaterial.end(); + } + +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS + if (bDrawLogo) +#endif + { + logoMaterial.begin(); + + ofPushMatrix(); + ofTranslate(-50, -170, 50); + ofRotateXDeg(-90); + ofScale(60, 60, 60); + logoMesh.draw(); + ofPopMatrix(); + + logoMaterial.end(); + } + + ofPopStyle(); } //-------------------------------------------------------------- -void ofApp::keyPressed(int key){ - if( key == ' ' ) { +void ofApp::keyPressed(int key) { + if (key == ' ') { bEnableShadows = !bEnableShadows; - if( bEnableShadows ) { + if (bEnableShadows) { ofShadow::enableAllShadows(); } else { ofShadow::disableAllShadows(); } } - if( key == 'f' ) { + if (key == 'f') { bDrawFrustums = !bDrawFrustums; } - if( key == OF_KEY_RIGHT ) { - int stype = (int)shadowType+1; - if( stype == OF_SHADOW_TYPE_TOTAL ) { + if (key == OF_KEY_RIGHT) { + int stype = (int)shadowType + 1; + if (stype == OF_SHADOW_TYPE_TOTAL) { stype = 0; } shadowType = (ofShadowType)stype; ofShadow::setAllShadowTypes(shadowType); } - if( key == OF_KEY_UP ) { + if (key == OF_KEY_LEFT) { + int stype = (int)shadowType - 1; + if (stype == OF_SHADOW_TYPE_HARD - 1) { + stype = OF_SHADOW_TYPE_PCF_HIGH; + } + shadowType = (ofShadowType)stype; + ofShadow::setAllShadowTypes(shadowType); + } + if (key == OF_KEY_UP) { shadowSampleRadius += 1.f; ofShadow::setAllShadowSampleRadius(shadowSampleRadius); } - if( key == OF_KEY_DOWN ) { + if (key == OF_KEY_DOWN) { shadowSampleRadius -= 1.f; - if( shadowSampleRadius < 0 ) { + if (shadowSampleRadius < 0) { shadowSampleRadius = 0.f; } ofShadow::setAllShadowSampleRadius(shadowSampleRadius); } -} -//-------------------------------------------------------------- -void ofApp::keyReleased(int key){ - + if (key == OF_KEY_BACKSPACE) { + resetMaterials(); + } } //-------------------------------------------------------------- -void ofApp::mouseMoved(int x, int y ){ - +void ofApp::exit() { +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS + ofRemoveListener(parameters.parameterChangedE(), this, &ofApp::Changed); + save(); +#endif } +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS //-------------------------------------------------------------- -void ofApp::mouseDragged(int x, int y, int button){ +void ofApp::refreshGuiAnchor() { + //ofxSurfing::setGuiPositionToLayoutPanelsCentered(gui, 5, ofxSurfing::SURFING_LAYOUT_TOP_CENTER); + ofxSurfing::setGuiPositionToLayout(gui, ofxSurfing::SURFING_LAYOUT_TOP_LEFT); } //-------------------------------------------------------------- -void ofApp::mousePressed(int x, int y, int button){ - -} +void ofApp::refreshGuiLinks() { -//-------------------------------------------------------------- -void ofApp::mouseReleased(int x, int y, int button){ - + ofxSurfing::setGuiPositionRightTo(bgMaterial.gui, gui); + ofxSurfing::setGuiPositionRightTo(logoMaterial.gui, bgMaterial.gui); + ofxSurfing::setGuiPositionRightTo(boxMaterial.gui, logoMaterial.gui); + ofxSurfing::setGuiPositionRightTo(sphereMaterial.gui, boxMaterial.gui); } //-------------------------------------------------------------- -void ofApp::mouseEntered(int x, int y){ - -} +void ofApp::Changed(ofAbstractParameter & e) { -//-------------------------------------------------------------- -void ofApp::mouseExited(int x, int y){ + std::string name = e.getName(); -} + ofLogNotice() << "Changed: " << name << ": " << e; -//-------------------------------------------------------------- -void ofApp::windowResized(int w, int h){ + autoSaver.saveSoon(); + #if 0 + if (name == bDrawBg.getName()) { + if (bDrawBg) + bgMaterial.gui.maximize(); + else + bgMaterial.gui.minimize(); + } else if (name == bDrawLogo.getName()) { + if (bDrawLogo) + logoMaterial.gui.maximize(); + else + logoMaterial.gui.minimize(); + } else if (name == bDrawBox.getName()) { + if (bDrawBox) + boxMaterial.gui.maximize(); + else + boxMaterial.gui.minimize(); + } else if (name == bDrawSphere.getName()) { + if (bDrawSphere) + sphereMaterial.gui.maximize(); + else + sphereMaterial.gui.minimize(); + } + #endif } //-------------------------------------------------------------- -void ofApp::gotMessage(ofMessage msg){ - +void ofApp::load() { + autoSaver.pause(); //stop saving + ofxSurfing::loadSettings(parameters, "ofApp.json"); + autoSaver.start(); //start } //-------------------------------------------------------------- -void ofApp::dragEvent(ofDragInfo dragInfo){ - +void ofApp::save() { + ofxSurfing::saveSettings(parameters, "ofApp.json"); } + +#endif diff --git a/5_Example_Shadows/src/ofApp.h b/5_Example_Shadows/src/ofApp.h index c48cdc2..ad7c2e3 100644 --- a/5_Example_Shadows/src/ofApp.h +++ b/5_Example_Shadows/src/ofApp.h @@ -2,47 +2,71 @@ #include "ofMain.h" -class ofApp : public ofBaseApp{ - public: - - void setup(); - void update(); - void draw(); - - void renderScene(); - - void keyPressed (int key); - void keyReleased(int key); - void mouseMoved(int x, int y ); - void mouseDragged(int x, int y, int button); - void mousePressed(int x, int y, int button); - void mouseReleased(int x, int y, int button); - void mouseEntered(int x, int y); - void mouseExited(int x, int y); - void windowResized(int w, int h); - void dragEvent(ofDragInfo dragInfo); - void gotMessage(ofMessage msg); - - vector< shared_ptr > lights; - - ofEasyCam camera; - ofMesh logoMesh; - ofMesh sphereMesh; - ofMesh boxMesh; - ofMesh cylinderMesh; - - ofMaterial logoMaterial; - ofMaterial boxMaterial; - ofMaterial bgMaterial; - ofMaterial sphereMaterial; - - ofColor lightColor; - float colorHue = 0.0f; - - bool bDrawFrustums = false; - bool bEnableShadows = true; - - ofShadowType shadowType; - float shadowSampleRadius = 2.; - +#include "SurfingMaterial.h" + +//#define SURFING__USE__OF_CORE_PBR_MATERIALS +// uncomment to disable the addon features and switch to "vanilla" OF core. + +class ofApp : public ofBaseApp { +public: + void setup(); + void update(); + void draw(); + void keyPressed(int key); + void exit(); + + void buildHelp(); + string sHelp; + + void drawScene(); + void drawGui(); + + void renderScene(); + void setupMaterials(); + + void resetMaterials(); + + vector> lights; + + ofEasyCam camera; + ofMesh logoMesh; + ofMesh sphereMesh; + ofMesh boxMesh; + ofMesh cylinderMesh; + +#ifndef SURFING__USE__OF_CORE_PBR_MATERIALS + SurfingMaterial bgMaterial; + SurfingMaterial logoMaterial; + SurfingMaterial boxMaterial; + SurfingMaterial sphereMaterial; + + ofParameter bDrawBg; + ofParameter bDrawLogo; + ofParameter bDrawBox; + ofParameter bDrawSphere; + ofParameterGroup parameters; + void Changed(ofAbstractParameter & e); + + ofxPanel gui; + void refreshGuiLinks(); + void refreshGuiAnchor(); + + SurfingAutoSaver autoSaver; + void save(); + void load(); +#else + ofMaterial bgMaterial; + ofMaterial boxMaterial; + ofMaterial logoMaterial; + ofMaterial sphereMaterial; +#endif + + ofColor lightColor; + float colorHue = 0.0f; + + bool bDrawFrustums = false; + bool bEnableShadows = true; + + ofShadowType shadowType; + float shadowSampleRadius = 2.; }; diff --git a/README.md b/README.md index 3c35639..d2f7658 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,13 @@ All this code is copied from and hardly based on this [OF forum topic](https://forum.openframeworks.cc/t/ofshadow-and-ofshader-issue-on-of-0-12/42600/19 ). Original authors: [@NickHardeman](https://github.com/NickHardeman) and [@paolo-scoppola](https://github.com/paolo-scoppola). -## SCREENSHOTS +## VIDEO + +https://github.com/moebiussurfing/ofxSurfingPBR/assets/2106149/43ac19b8-da41-4dd5-ad4d-b77f885d3e24 + +
+ SCREENSHOTS +

### 1_Example_Basic ![](1_Example_Basic/Capture.PNG) @@ -17,9 +23,17 @@ Original authors: [@NickHardeman](https://github.com/NickHardeman) and [@paolo-s ### 2_Example_Models ![](2_Example_Models/Capture.PNG) -## VIDEO +### 3_Example_Lights / WIP +![](3_Example_Lights/Capture.PNG) -https://github.com/moebiussurfing/ofxSurfingPBR/assets/2106149/43ac19b8-da41-4dd5-ad4d-b77f885d3e24 +### 4_Example_Materials +![](4_Example_Materials/Capture.PNG) + +### 5_Example_Shadows +![](5_Example_Shadows/Capture.PNG) + +

+
## FEATURES @@ -28,7 +42,7 @@ https://github.com/moebiussurfing/ofxSurfingPBR/assets/2106149/43ac19b8-da41-4dd - **Material B**. For the **other Objects**. - A positionable Ambient **light** with **Shadows**. - **Cubemaps** (**HDR**) files loader. - - Alternative background color. + - Alternative background color and sphere/box container using material. - Added **Shader** for *testing/code*. - **Displacement** applied to the plane and his material/mesh. - Persistent settings: @@ -42,7 +56,8 @@ https://github.com/moebiussurfing/ofxSurfingPBR/assets/2106149/43ac19b8-da41-4dd - **Presets**/**Snapshots** (Store/Recall) explorer system. - Included **3D models** browser/loader class. - Included [ofxSurfingHelpersLite](https://github.com/moebiussurfing/ofxSurfingHelpersLite) into `/libs`. - - **Helper classes** for settings serializers, auto saver, ofxGui customizer, layout helpers, ofDrawBitmapStringBox..etc + - **Helper classes** for settings serializers, auto saver, ofxGui customizer, layout helpers, ofDrawBitmapStringBox...etc. +- Standalone materials can be used independently of the main class/scene manager. ### TODO - Add more **Shaders** related stuff. / HELP IS WELCOME! @@ -79,9 +94,6 @@ int main() { #include "ofxSurfingPBR.h" -#include -using callback_t = std::function; - class ofApp : public ofBaseApp { public: void setup(); @@ -140,8 +152,8 @@ void ofApp::renderScene() bin $~$ └ data -$~~~~~~$ ├ cubemaps $~~~~~~$ exr, hdr, jpg -$~~~~~~$ ├ models $~~~~~~~~$ obj, ply, fbx +$~~~~~~$ ├ cubemaps $~~~~$ exr, hdr, jpg +$~~~~~~$ ├ models $~~~~$ obj, ply, fbx $~~~~~~$ ├ shadersGL2 $~~~~$ frag, vert $~~~~~~$ └ shadersGL3 $~~~~$ frag, vert diff --git a/libs/ofxSurfingHelpersLite/src/ofxSurfingHelpersLite.h b/libs/ofxSurfingHelpersLite/src/ofxSurfingHelpersLite.h index d0e6acb..17caefc 100644 --- a/libs/ofxSurfingHelpersLite/src/ofxSurfingHelpersLite.h +++ b/libs/ofxSurfingHelpersLite/src/ofxSurfingHelpersLite.h @@ -50,7 +50,9 @@ namespace ofxSurfing { // Load //-------------------------------------------------------------- -inline bool loadSettings(ofParameterGroup & parameters, string path) { +inline bool loadSettings(ofParameterGroup & parameters, string path = "") { + if (path == "") path = parameters.getName(); + //verify that the file exist ofFile f; bool b = f.doesFileExist(path); @@ -71,7 +73,9 @@ inline bool loadSettings(ofParameterGroup & parameters, string path) { // Save //-------------------------------------------------------------- -inline bool saveSettings(ofParameterGroup & parameters, string path) { +inline bool saveSettings(ofParameterGroup & parameters, string path = "") { + if (path == "") path = parameters.getName(); + //if the destination folder do not exists, will be created. if (!ofDirectory::doesDirectoryExist(ofFilePath::getEnclosingDirectory(path))) { if (ofFilePath::createEnclosingDirectory(path)) @@ -104,10 +108,10 @@ inline void checkFolderOrCreate(string path) { // LEGACY // for ofxSurfingHelpers -inline bool loadGroup(ofParameterGroup & parameters, string path) { +inline bool loadGroup(ofParameterGroup & parameters, string path = "") { return loadSettings(parameters, path); } -inline bool saveGroup(ofParameterGroup & parameters, string path) { +inline bool saveGroup(ofParameterGroup & parameters, string path = "") { return saveSettings(parameters, path); } @@ -608,7 +612,7 @@ inline glm::vec2 getBitmapStringBoxPosToLayout(string s, int layout) { } //-------------------------------------------------------------- -inline void ofDrawBitmapStringBox(string s, SURFING_LAYOUT layout = SURFING_LAYOUT_CENTER) { +inline void ofDrawBitmapStringBox(string s, SURFING_LAYOUT layout = SURFING_LAYOUT_TOP_LEFT) { glm::vec2 p = ofxSurfing::getBitmapStringBoxPosToLayout(s, layout); ofxSurfing::ofDrawBitmapStringBox(s, p.x, p.y); } @@ -775,7 +779,7 @@ inline void setGuiPositionToLayoutPanelsCentered(ofxPanel & gui1, size_t amount, //TODO: add other layouts? // to be used for a landscape layout // each panel right to the previous. - + //SURFING_LAYOUT_BOTTOM_CENTER //SURFING_LAYOUT_TOP_CENTER @@ -797,8 +801,10 @@ inline void setGuiPositionToLayoutPanelsCentered(ofxPanel & gui1, size_t amount, x = ofGetWidth() / 2 - (w * amount) / 2; y = SURFING__PAD_TO_WINDOW_BORDERS; } - + else { + ofLogWarning("ofxSurfing") << "Layout not implemented to be used by setGuiPositionToLayoutPanelsCentered()"; + x = SURFING__PAD_TO_WINDOW_BORDERS; y = SURFING__PAD_TO_WINDOW_BORDERS; } diff --git a/src/SurfingMaterial.cpp b/src/SurfingMaterial.cpp index 2190d72..ea8ed5b 100644 --- a/src/SurfingMaterial.cpp +++ b/src/SurfingMaterial.cpp @@ -40,15 +40,18 @@ void SurfingMaterial::setupParams() { ofLogNotice("ofxSurfingPBR") << "SurfingMaterial:setupParams()"; //-- - - string n = "PBR_MATERIAL"; - if (name != "") { + + if (name == "") { + path = pathRoot + ext; + } else { + string n = "PBR_MATERIAL"; n += string("_"); n += name; + path = pathRoot + "_" + name + ext; } - path = path + "_" + name + ext; - parameters.setName(n); + //parameters.setName(n); + parameters.setName(name); colorParams.setName("Colors"); settingsParams.setName("Settings"); @@ -114,13 +117,14 @@ void SurfingMaterial::setupParams() { autoSaver.bEnable.setSerializable(false); #endif + parameters.add(vRandomMaterial); parameters.add(vResetMaterial); //workflow //parameters.add(indexHistory); bGui.set("UI MATERIAL", true); - bGuiHelpers.set("UI HELPERS", true); + bGuiHelpers.set("UI HELPERS", false); //bGuiHelpers.setSerializable(false); //workflow: to exclude from states. trouble bc we would like to save & include into gui.. @@ -139,6 +143,7 @@ void SurfingMaterial::setupGui() { gui.setup(parameters); +#if 1 static ofEventListener listenerSave; static ofEventListener listenerLoad; listenerSave = gui.savePressedE.newListener([this] { @@ -147,6 +152,7 @@ void SurfingMaterial::setupGui() { listenerLoad = gui.loadPressedE.newListener([this] { load(); }); +#endif guiHelpers.setup(helpersParams); @@ -177,6 +183,8 @@ void SurfingMaterial::update() { //-------------------------------------------------------------- void SurfingMaterial::begin() { + ofSetColor(255); + material.begin(); } //-------------------------------------------------------------- @@ -539,8 +547,8 @@ void SurfingMaterial::removeHistoryFolder() { } //-------------------------------------------------------------- -const string SurfingMaterial::getFilepathHistoryState(const int & i) { - ofLogNotice("ofxSurfingPBR") << "SurfingMaterial:getFilepathHistoryState() " << i; +const string SurfingMaterial::getFilePathHistoryState(const int & i) { + ofLogNotice("ofxSurfingPBR") << "SurfingMaterial:getFilePathHistoryState() " << i; // Check for valid index if (i < 0 || i > sizeHistory - 1) { @@ -612,7 +620,7 @@ void SurfingMaterial::reorganizeHistoryFolder() { for (std::size_t i = 0; i < dir.size(); ++i) { string p_ = dir.getPath(i); ofFile file(p_); - string p = getFilepathHistoryState(i); + string p = getFilePathHistoryState(i); file.renameTo(p, true, true); ofLogNotice("ofxSurfingPBR") << p; @@ -681,9 +689,9 @@ void SurfingMaterial::doSaveState(int i) { //save file if (i == -1) { - p = getFilepathHistoryState(indexHistory); + p = getFilePathHistoryState(indexHistory); } else { - p = getFilepathHistoryState(i); + p = getFilePathHistoryState(i); } ofxSurfing::saveSettings(parameters, p); @@ -703,7 +711,7 @@ void SurfingMaterial::doStoreNewState() { //save file int i = indexHistory; - string p = getFilepathHistoryState(i); + string p = getFilePathHistoryState(i); ofxSurfing::saveSettings(parameters, p); } @@ -734,7 +742,7 @@ void SurfingMaterial::doRecallState(int i) { if (i >= 0 && i < sizeHistory) { //load file int i = indexHistory; - string p = getFilepathHistoryState(i); + string p = getFilePathHistoryState(i); restoreGlobal(); ofxSurfing::loadSettings(parameters, p); @@ -750,7 +758,7 @@ void SurfingMaterial::doRemoveState(int i) { if (i >= 0 && i < sizeHistory) { //remove file int i = indexHistory; - string p = getFilepathHistoryState(i); + string p = getFilePathHistoryState(i); ofFile file(p); if (file.remove()) { @@ -774,6 +782,10 @@ void SurfingMaterial::doRemoveState(int i) { void SurfingMaterial::save() { ofLogNotice("ofxSurfingPBR") << "SurfingMaterial:Save: " << path; + if (path == "") { + ofLogWarning("ofxSurfingPBR") << "Settings path is empty!"; + } + // Save { ofxSurfing::saveSettings(parameters, path); @@ -789,11 +801,16 @@ void SurfingMaterial::save() { void SurfingMaterial::setName(const string & n) { ofLogNotice("ofxSurfingPBR") << "SurfingMaterial:setName: " << n; name = n; + pathHistory = pathHistory + "_" + name; } //-------------------------------------------------------------- void SurfingMaterial::load() { ofLogNotice("ofxSurfingPBR") << "SurfingMaterial:Load: " << path; + + if (path == "") { + ofLogWarning("ofxSurfingPBR") << "Settings path is empty!"; + } #ifdef SURFING__USE_AUTOSAVE_SETTINGS_ENGINE autoSaver.pause(); diff --git a/src/SurfingMaterial.h b/src/SurfingMaterial.h index 584bfe0..e7c690f 100644 --- a/src/SurfingMaterial.h +++ b/src/SurfingMaterial.h @@ -2,9 +2,8 @@ TODO - draw helpers gui bundled to improve materials standalone workflow. - add custom/name path for history browser. - add bang to update global color from other colors. + add bang to update/link global color from other colors. + fix coat for standalone material */ @@ -86,7 +85,8 @@ class SurfingMaterial { ofParameter & getIndexStateParam(); public: - string path = "ofxSurfingPBR_Material"; + string pathRoot = "ofxSurfingPBR_Material"; + string path = ""; string name = ""; string ext= ".json"; void setName(const string &n);//call before setup @@ -138,7 +138,7 @@ class SurfingMaterial { void removeHistoryFolder(); void restoreGlobal(); - const string getFilepathHistoryState(const int &i); + const string getFilePathHistoryState(const int &i); string pathHistory = "ofxSurfing_Temp"; string pathHistoryNameRoot = "ofxSurfing_Material_"; @@ -148,4 +148,26 @@ class SurfingMaterial { void doStoreNewState(); void doRecallState(); void doSaveState(int i=-1); + +public: + //TODO: startup fix overwrite settings from file.. + // Helpers to pair with OF core ofMaterial + // Useful to replace code from your projects or OF bunbled examples. + void setAmbientColor(ofFloatColor c) { + ambientColor.set(c); + } + void setDiffuseColor(ofFloatColor c) { + diffuseColor.set(c); + } + void setSpecularColor(ofFloatColor c) { + specularColor.set(c); + } + void setEmissiveColor(ofFloatColor c) { + emissiveColor.set(c); + } + void setShininess(float shininess) { + this->shininess.set(shininess); + } + + };