Commit 7e843b04 authored by MURRAY David's avatar MURRAY David
Browse files

working on parsing multimat and transposing to optix

parent 6e4a3eb2
......@@ -136,14 +136,17 @@ void OptixGeometryHandler::importMrfScene(
//DIRECTLY USE THE CORRECT ONE HERE
//apply a default lambert material to avoid optix error due to loadMesh with a geometry
//that hasn't got any material or closesthit/
optix_mesh.material = material_handler.lambert();
//optix_mesh.material = material_handler.lambert();
std::vector<optix::Material> optix_materials;
optix_materials.push_back(material_handler.lambert());
optix::Matrix4x4 transform = optix::Matrix4x4((*shape_it)->worldToObjectTransform().ptr());
//transform = transform.transpose();
//loadMesh(file_name, optix_mesh, transform);
loadMeshFromMRF(*mrf_mesh, optix_mesh, transform);
loadMeshFromMRF(*mrf_mesh, optix_mesh, transform, optix_materials);
//optix_mesh.has_texcoords = mesh.has_texcoords;
......@@ -244,7 +247,8 @@ std::vector<std::pair<OptiXMesh, mrf::geom::Shape *>> &OptixGeometryHandler::geo
void OptixGeometryHandler::loadMeshFromMRF(
mrf::geom::Mesh const & mrf_mesh,
OptiXMesh & optix_mesh,
const optix::Matrix4x4 &load_xform)
const optix::Matrix4x4 &load_xform,
std::vector<optix::Material> optix_materials)
{
if (!optix_mesh.context)
{
......@@ -313,17 +317,17 @@ void OptixGeometryHandler::loadMeshFromMRF(
optix_mesh.bbox_max = mrf::optix_backend::make_float3(mesh.bbox_max);
optix_mesh.num_triangles = mesh.num_triangles;
std::vector<optix::Material> optix_materials;
if (optix_mesh.material)
//std::vector<optix::Material> optix_materials;
if (optix_materials.size() == 1)
{
// Rewrite all mat_indices to point to single override material
memset(mesh.mat_indices, 0, mesh.num_triangles * sizeof(int32_t));
optix_materials.push_back(optix_mesh.material);
}
else
{
//ERROR
//optix_materials.push_back(optix_mesh.material);
}
//else
//{
// optix_materials.resize(nb_mat);
//}
optix::GeometryTriangles geometry = optix_mesh.context->createGeometryTriangles();
......
......@@ -129,7 +129,11 @@ public:
inline std::vector<std::pair<optix::GeometryInstance, unsigned int>> & analyticalShapes();
void loadMeshFromMRF(mrf::geom::Mesh const &mrf_mesh, OptiXMesh &optix_mesh, const optix::Matrix4x4 &load_xform);
void loadMeshFromMRF(
mrf::geom::Mesh const & mrf_mesh,
OptiXMesh & optix_mesh,
const optix::Matrix4x4 &load_xform,
std::vector<optix::Material> optix_materials);
void setupMeshLoaderInputs(optix::Context context, OptixMeshBuffers &buffers, Mesh &mesh);
......
......@@ -44,6 +44,8 @@ public:
void releaseMemory();
void createMaterials(std::vector<std::string> const &cuda_compile_options);
void getMaterialFromMRF();
#ifdef MRF_RENDERING_MODE_SPECTRAL
/**
* Update all the Optix texture that holds the data of the PhongTextured materials
......
......@@ -228,6 +228,12 @@ public:
static constexpr char const *const ANISOTROPIC_MK = "anisotropic";
static constexpr char const *const METALLIC_MK = "metallic";
static constexpr char const *const COUNT_AT = "count";
static constexpr char const *const INDEX_AT = "index";
//LIGHTS
static constexpr char const *const LIGHTS_MK = "lights";
static constexpr char const *const POINT_LIGHT_MK = "point_light";
......
......@@ -13,7 +13,6 @@
#include <mrf_core/io/extensions.hpp>
//#include <mrf_core/io/ply_loader.hpp>
#include <mrf_core/io/obj_parser.hpp>
#include <mrf_core/materials/all_materials.hpp>
#include <mrf_core/color/spectrum_converter.hpp>
#include <mrf_core/radiometry/d65.hpp>
#include <mrf_core/lighting/light.hpp>
......@@ -793,6 +792,16 @@ bool SceneParser::parseShapes(mrf::rendering::Scene &scene)
scene.addShape(shape);
is_mat_used[current_material_index] = true;
auto multi_mat = dynamic_cast<mrf::materials::MultiMaterial *>(&(scene.umaterial(current_material_index)));
if (multi_mat)
{
for (auto &name : multi_mat->getAllNames())
{
auto current = _brdf_map.find(name);
auto end = _brdf_map.end();
if (current != end) is_mat_used[current->second] = true;
}
}
}
else if (shapes->FirstChildElement(SceneLexer::SPHERE_MK))
{
......@@ -927,7 +936,7 @@ SceneParser::addMesh(XMLElement *a_mesh_element, mrf::rendering::Scene &scene, i
_loger->trace("........................................................................");
// Tinyply treats parsed data as untyped byte buffers. See below for examples.
std::shared_ptr<tinyply::PlyData> tiny_vertices, tiny_normals, tiny_faces, tiny_texcoords;
std::shared_ptr<tinyply::PlyData> tiny_vertices, tiny_normals, tiny_faces, tiny_texcoords, tiny_mat;
// The header information can be used to programmatically extract properties on elements
// known to exist in the header prior to reading the data. For brevity of this sample, properties
......@@ -976,12 +985,25 @@ SceneParser::addMesh(XMLElement *a_mesh_element, mrf::rendering::Scene &scene, i
_loger->fatal("tinyply exception: ", e.what());
}
try
{
tiny_mat = file.request_properties_from_element("face", {"mat_idx"});
}
catch (const std::exception &e)
{
_loger->warn("tinyply exception: ", e.what());
//Allow to load mesh without UVs
//return scene.meshes().end();
tiny_mat = nullptr;
}
file.read(ss);
if (tiny_vertices) _loger->trace("\tRead vertices:", tiny_vertices->count);
if (tiny_normals) _loger->trace("\tRead normals:", tiny_normals->count);
if (tiny_texcoords) _loger->trace("\tRead texcoords:", tiny_texcoords->count);
if (tiny_faces) _loger->trace("\tRead faces(triangles):", tiny_faces->count);
if (tiny_mat) _loger->trace("\tRead material indices per face:", tiny_mat->count);
assert(tiny_vertices->buffer.size_bytes() == tiny_vertices->count * 3 * sizeof(float));
assert(tiny_normals->buffer.size_bytes() == tiny_normals->count * 3 * sizeof(float));
......@@ -1055,9 +1077,22 @@ SceneParser::addMesh(XMLElement *a_mesh_element, mrf::rendering::Scene &scene, i
}
}
//Retrieve the current material
enforced_materials.resize(index_faces.size() / 3);
std::fill(enforced_materials.begin(), enforced_materials.end(), current_material_index);
if (tiny_mat)
{
assert(tiny_mat->count == tiny_faces->count);
const size_t numMatBytes = tiny_mat->buffer.size_bytes();
const float *ply_mat = reinterpret_cast<float *>(tiny_mat->buffer.get());
enforced_materials.resize(tiny_mat->count);
for (size_t face_idx = 0; face_idx < enforced_materials.size(); face_idx++)
enforced_materials[face_idx] = ply_mat[face_idx];
}
else
{
//Retrieve the current material
enforced_materials.resize(index_faces.size() / 3);
std::fill(enforced_materials.begin(), enforced_materials.end(), current_material_index);
}
// Check if the mesh has a tangent method attribute
......@@ -1490,6 +1525,12 @@ bool SceneParser::addMaterial(XMLElement *a_mat_element, mrf::rendering::Scene &
}
std::string name(a_mat_element->Attribute(SceneLexer::NAME_AT));
if (_brdf_map.count(name) == 1)
{
_loger->info("Material " + name + " is already in the list.");
return false;
}
std::string type;
if (!a_mat_element->Attribute(SceneLexer::TYPE_AT))
......@@ -1505,31 +1546,40 @@ bool SceneParser::addMaterial(XMLElement *a_mat_element, mrf::rendering::Scene &
_loger->trace(" Material name is ", name);
_loger->trace(" Material type is ", type);
if (type.compare(BRDFTypes::MULTI_MAT) == 0)
{
_loger->trace("Multi-Material Detected, parsing them");
unsigned int count;
int rOk = a_mat_element->QueryUnsignedAttribute(SceneLexer::COUNT_AT, &count);
MultiMaterial *multimat = new MultiMaterial(name, count);
parseMultiMaterial(a_mat_element, multimat, scene);
_brdf_map[name] = scene.addMaterial(multimat);
}
if (type.compare(BRDFTypes::LAMBERT) == 0)
{
_loger->trace("Lambertian Material Detected");
Lambert *lamber_mat = new Lambert(name);
Lambert *lambert_mat = new Lambert(name);
//PARSE COLOR AND REFLECTIVITY
COLOR color;
if (retrieveColor(a_mat_element, color))
{
lamber_mat->setColor(color);
lambert_mat->setColor(color);
}
float reflectivity;
if (retrieveReflectivity(a_mat_element, reflectivity))
{
lamber_mat->setReflectivity(reflectivity);
lambert_mat->setReflectivity(reflectivity);
}
else
{
_loger->warn(" default value=" + std::to_string(lamber_mat->diffuseAlbedo()));
_loger->warn(" default value=" + std::to_string(lambert_mat->diffuseAlbedo()));
}
_brdf_map[name] = scene.addMaterial(lamber_mat);
_brdf_map[name] = scene.addMaterial(lambert_mat);
_loger->trace("One Lambertian Material Added to Scene !!!!");
}
else if (type.compare(BRDFTypes::PHONG_NORMALIZED) == 0)
......@@ -2685,6 +2735,41 @@ void SceneParser::parsePrincipled(tinyxml2::XMLElement *a_mat_element, mrf::mate
void SceneParser::parseMultiMaterial(
tinyxml2::XMLElement * a_mat_element,
mrf::materials::MultiMaterial *multi_mat,
mrf::rendering::Scene & scene)
{
XMLElement *material = a_mat_element->FirstChildElement();
_loger->trace("Materials Found ! Parsing it....");
for (; material; material = material->NextSiblingElement())
{
std::string element_name = material->Name();
if (element_name.compare(SceneLexer::MATERIAL_MK) == 0)
{
std::string name(material->Attribute(SceneLexer::NAME_AT));
unsigned int mat_index;
material->QueryUnsignedAttribute(SceneLexer::INDEX_AT, &mat_index);
multi_mat->addEntry(mat_index, name);
addMaterial(material, scene);
}
//TODO
//else if (element_name.compare(SceneLexer::EMITTANCE_MK) == 0)
//{
// if (!addEmittance(material, scene))
// {
// _loger->warn("Problem while trying to add an emittance in parse materials");
// }
//}
//else
//{
// _loger->warn("Unrecognized element in materials ");
//}
}
}
bool SceneParser::parseTexture(XMLElement *a_mat_element, char const *texture_markup, std::string &path)
{
XMLElement *texture_elem = a_mat_element->FirstChildElement(texture_markup);
......
......@@ -18,10 +18,7 @@
#include <mrf_core/rendering/scene.hpp>
#include <mrf_core/io/scene_lexer.hpp>
#include <mrf_core/io/parsing_errors.hpp>
#include <mrf_core/materials/emittance.hpp>
#include <mrf_core/materials/diffuse_emittance.hpp>
#include <mrf_core/materials/l_phong.hpp>
#include <mrf_core/materials/principled.hpp>
#include <mrf_core/materials/all_materials.hpp>
#include <mrf_core/lighting/envmap.hpp>
#include <mrf_core/util/precision_timer.hpp>
......@@ -125,6 +122,11 @@ protected:
void parsePrincipled(tinyxml2::XMLElement *a_mat_element, mrf::materials::PrincipledDisney *principled_mat);
void parseMultiMaterial(
tinyxml2::XMLElement * a_mat_element,
mrf::materials::MultiMaterial *multi_mat,
mrf::rendering::Scene & scene);
bool parseTexture(tinyxml2::XMLElement *a_mat_element, char const *texture_markup, std::string &path);
......
......@@ -58,3 +58,5 @@
#include <mrf_core/materials/walter_bsdf.hpp>
#include <mrf_core/materials/measured_isotropic.hpp>
#include <mrf_core/materials/principled.hpp>
#include <mrf_core/materials/multi_material.hpp>
......@@ -62,5 +62,7 @@ char const *BRDFTypes::SMOOTH_DIELECTRIC = "smooth_dielectric";
char const *BRDFTypes::RATIONAL_HC = "Rational_HC";
char const *BRDFTypes::PRINCIPLED = "principled";
char const *BRDFTypes::MULTI_MAT = "multi-material";
} // namespace materials
} // namespace mrf
......@@ -92,6 +92,9 @@ public:
//Principled BRDF, blending physical results and artistic purposes
static char const *PRINCIPLED;
static char const *MULTI_MAT;
};
} // namespace materials
......
......@@ -16,17 +16,31 @@ namespace materials
class MRF_CORE_EXPORT MultiMaterial: public BRDF
{
protected:
std::map<unsigned int, UMat *> _material_map;
std::map<unsigned int, std::string> _material_map;
//std::map<unsigned int, UMat *> _material_map;
unsigned int _index_offset;
unsigned int _material_count;
std::vector<unsigned int> _normalized_indices;
std::vector<unsigned int> _indices;
std::vector<std::string> _names;
public:
MultiMaterial(std::string const &name, unsigned int nb_mat);
~MultiMaterial();
//void addEntry(unsigned int idx, std::string name) { _material_map[idx] = name; }
void addEntry(unsigned int idx, std::string name)
{
_indices.push_back(idx);
_names.push_back(name);
}
void addIdEntry(unsigned int idx) { _indices.push_back(idx); }
void addNameEntry(std::string name) { _names.push_back(name); }
std::vector<unsigned int> &getAllId() { return _indices; }
std::vector<std::string> & getAllNames() { return _names; }
};
} // namespace materials
} // namespace mrf
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment