From 14fa9b4c12f034a5146562a1244d7f93e1c3a988 Mon Sep 17 00:00:00 2001 From: Niklas Harrysson Date: Mon, 1 Apr 2019 14:05:18 +0200 Subject: [PATCH] Added loading of material from source code. (#344) --- source/MaterialXView/Material.cpp | 24 +++++++++++++++++++++ source/MaterialXView/Material.h | 3 +++ source/MaterialXView/Viewer.cpp | 35 +++++++++++++++++++++++++++++++ source/MaterialXView/Viewer.h | 1 + 4 files changed, 63 insertions(+) diff --git a/source/MaterialXView/Material.cpp b/source/MaterialXView/Material.cpp index 242c05d59a..c330252bac 100644 --- a/source/MaterialXView/Material.cpp +++ b/source/MaterialXView/Material.cpp @@ -190,6 +190,30 @@ bool Material::generateConstantShader(mx::GenContext& context, return _glShader->init(shaderName, vertexShader, pixelShader); } +bool Material::loadSource(const mx::FilePath& vertexShaderFile, const mx::FilePath& pixelShaderFile, const std::string& shaderName, bool hasTransparency) +{ + _hasTransparency = hasTransparency; + + if (!_glShader) + { + _glShader = std::make_shared(); + } + + std::string vertexShader; + if (!mx::readFile(vertexShaderFile, vertexShader)) + { + return false; + } + + std::string pixelShader; + if (!mx::readFile(pixelShaderFile, pixelShader)) + { + return false; + } + + return _glShader->init(shaderName, vertexShader, pixelShader); +} + mx::ShaderPtr Material::generateSource(mx::GenContext& context, mx::ElementPtr elem) { if (!elem) diff --git a/source/MaterialXView/Material.h b/source/MaterialXView/Material.h index 7587081e1c..a9a5627cbc 100644 --- a/source/MaterialXView/Material.h +++ b/source/MaterialXView/Material.h @@ -73,6 +73,9 @@ class Material _udim = val; } + /// Load shader source from file. + bool loadSource(const mx::FilePath& vertexStage, const mx::FilePath& pixelStage, const std::string& shaderName, bool hasTransparency); + /// Generate shader source for a given element and generation context. mx::ShaderPtr generateSource(mx::GenContext& context, mx::ElementPtr elem); diff --git a/source/MaterialXView/Viewer.cpp b/source/MaterialXView/Viewer.cpp index ea7d69773c..eccf3686fe 100644 --- a/source/MaterialXView/Viewer.cpp +++ b/source/MaterialXView/Viewer.cpp @@ -741,6 +741,32 @@ void Viewer::saveActiveMaterialSource() } } +void Viewer::loadActiveMaterialSource() +{ + try + { + MaterialPtr material = getSelectedMaterial(); + mx::TypedElementPtr elem = material ? material->getElement() : nullptr; + if (elem) + { + std::string baseName = elem->getName(); + std::string vertexShaderFile = _searchPath[0] / (baseName + "_vs.glsl"); + std::string pixelShaderFile = _searchPath[0] / (baseName + "_ps.glsl"); + // Ignore transparency for now as we can't know from the source code + // if the shader is transparent or not. + if (material->loadSource(vertexShaderFile, pixelShaderFile, baseName, false)) + { + assignMaterial(material, _geometryList[_selectedGeom]); + } + } + } + catch (std::exception& e) + { + new ng::MessageDialog(this, ng::MessageDialog::Type::Warning, "Cannot load source for material", e.what()); + } +} + + bool Viewer::keyboardEvent(int key, int scancode, int action, int modifiers) { if (Screen::keyboardEvent(key, scancode, action, modifiers)) @@ -790,6 +816,15 @@ bool Viewer::keyboardEvent(int key, int scancode, int action, int modifiers) return true; } + // Load a material previously saved to file. + // Editing the source files before loading gives a way to debug + // and experiment with shader source code. + if (key == GLFW_KEY_L && action == GLFW_PRESS) + { + loadActiveMaterialSource(); + return true; + } + // Allow left and right keys to cycle through the renderable elements if ((key == GLFW_KEY_RIGHT || key == GLFW_KEY_LEFT) && action == GLFW_PRESS) { diff --git a/source/MaterialXView/Viewer.h b/source/MaterialXView/Viewer.h index 20971f3a91..b0a6d7a1cb 100644 --- a/source/MaterialXView/Viewer.h +++ b/source/MaterialXView/Viewer.h @@ -61,6 +61,7 @@ class Viewer : public ng::Screen void setupLights(mx::DocumentPtr doc, const std::string& envRadiancePath, const std::string& envIrradiancePath); void initializeDocument(mx::DocumentPtr libraries); void saveActiveMaterialSource(); + void loadActiveMaterialSource(); /// Assign the given material to a geometry, or to all geometries if no /// target is specified.