Skip to content

Commit

Permalink
Added loading of material from source code. (AcademySoftwareFoundatio…
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasharrysson committed Apr 1, 2019
1 parent 58f6acc commit 14fa9b4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
24 changes: 24 additions & 0 deletions source/MaterialXView/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ng::GLShader>();
}

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)
Expand Down
3 changes: 3 additions & 0 deletions source/MaterialXView/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
35 changes: 35 additions & 0 deletions source/MaterialXView/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXView/Viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 14fa9b4

Please sign in to comment.