work on hair primitive

metadata
Wenzel Jakob 2010-10-22 21:42:16 +02:00
parent 914b959ef9
commit aede861fb3
4 changed files with 232 additions and 82 deletions

View File

@ -511,7 +511,6 @@ plugins += env.SharedLibrary('plugins/roughglass', ['src/bsdfs/roughglass.cpp'])
plugins += env.SharedLibrary('plugins/roughmetal', ['src/bsdfs/roughmetal.cpp']) plugins += env.SharedLibrary('plugins/roughmetal', ['src/bsdfs/roughmetal.cpp'])
plugins += env.SharedLibrary('plugins/composite', ['src/bsdfs/composite.cpp']) plugins += env.SharedLibrary('plugins/composite', ['src/bsdfs/composite.cpp'])
# Phase functions # Phase functions
plugins += env.SharedLibrary('plugins/isotropic', ['src/phase/isotropic.cpp']) plugins += env.SharedLibrary('plugins/isotropic', ['src/phase/isotropic.cpp'])
plugins += env.SharedLibrary('plugins/hg', ['src/phase/hg.cpp']) plugins += env.SharedLibrary('plugins/hg', ['src/phase/hg.cpp'])
@ -524,7 +523,7 @@ plugins += env.SharedLibrary('plugins/ply', ['src/shapes/ply/ply.cpp', 'src/shap
plugins += env.SharedLibrary('plugins/serialized', ['src/shapes/serialized.cpp']) plugins += env.SharedLibrary('plugins/serialized', ['src/shapes/serialized.cpp'])
plugins += env.SharedLibrary('plugins/sphere', ['src/shapes/sphere.cpp']) plugins += env.SharedLibrary('plugins/sphere', ['src/shapes/sphere.cpp'])
plugins += env.SharedLibrary('plugins/cylinder', ['src/shapes/cylinder.cpp']) plugins += env.SharedLibrary('plugins/cylinder', ['src/shapes/cylinder.cpp'])
#plugins += env.SharedLibrary('plugins/hair', ['src/shapes/hair.cpp', 'src/shapes/miterseg.cpp']) #plugins += env.SharedLibrary('plugins/hair', ['src/shapes/hair.cpp'])
#plugins += env.SharedLibrary('plugins/group', ['src/shapes/group.cpp']) #plugins += env.SharedLibrary('plugins/group', ['src/shapes/group.cpp'])
# Samplers # Samplers

View File

@ -21,7 +21,6 @@
#include <mitsuba/render/subsurface.h> #include <mitsuba/render/subsurface.h>
#include <mitsuba/render/luminaire.h> #include <mitsuba/render/luminaire.h>
#include <mitsuba/core/properties.h> #include <mitsuba/core/properties.h>
#include <mitsuba/core/random.h>
MTS_NAMESPACE_BEGIN MTS_NAMESPACE_BEGIN

View File

@ -1,30 +1,49 @@
#include <fstream> /*
This file is part of Mitsuba, a physically based rendering system.
Copyright (c) 2007-2010 by Wenzel Jakob and others.
Mitsuba is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License Version 3
as published by the Free Software Foundation.
Mitsuba is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mitsuba/render/shape.h> #include <mitsuba/render/shape.h>
#include <mitsuba/render/bsdf.h> #include <mitsuba/render/bsdf.h>
#include <mitsuba/core/plugin.h> #include <mitsuba/render/subsurface.h>
#include <mitsuba/render/luminaire.h>
#include <mitsuba/core/properties.h>
#include <mitsuba/core/fstream.h>
#include <mitsuba/core/fresolver.h> #include <mitsuba/core/fresolver.h>
#include "hair.h"
#include "miterseg.h"
MTS_NAMESPACE_BEGIN MTS_NAMESPACE_BEGIN
Hair::Hair(const Properties &props) : Shape(props) { class Hair : public Shape {
std::string filename = props.getString("filename"); public:
Hair(const Properties &props) : Shape(props) {
fs::path path = Thread::getThread()->getFileResolver()->resolve(
props.getString("filename")); props.getString("filename"));
m_radius = (Float) props.getFloat("radius", 0.05f); m_radius = (Float) props.getFloat("radius", 0.05f);
m_name = FileResolver::getInstance()->resolve(filename);
Log(EInfo, "Loading hair geometry from \"%s\" ..", m_name.c_str()); Log(EInfo, "Loading hair geometry from \"%s\" ..", path.leaf().c_str());
std::ifstream is(m_name.c_str()); fs::ifstream is(path);
if (is.fail()) if (is.fail())
Log(EError, "Could not open \"%s\"!", m_name.c_str()); Log(EError, "Could not open \"%s\"!", path.file_string().c_str());
std::string line; std::string line;
bool newFiber = true; bool newFiber = true;
Point p; Point p;
size_t segmentCount = 0;
while (is.good()) { while (is.good()) {
std::getline(is, line); std::getline(is, line);
if (line.length() > 0 && line[0] == '#') if (line.length() > 0 && line[0] == '#')
@ -35,6 +54,8 @@ Hair::Hair(const Properties &props) : Shape(props) {
std::istringstream iss(line); std::istringstream iss(line);
iss >> p.x >> p.y >> p.z; iss >> p.x >> p.y >> p.z;
if (!iss.fail()) { if (!iss.fail()) {
if (newFiber)
segmentCount++;
m_vertices.push_back(p); m_vertices.push_back(p);
m_startFiber.push_back(newFiber); m_startFiber.push_back(newFiber);
newFiber = false; newFiber = false;
@ -43,61 +64,89 @@ Hair::Hair(const Properties &props) : Shape(props) {
} }
m_startFiber.push_back(true); m_startFiber.push_back(true);
buildSegIndex(); for (size_t i=0; i<m_vertices.size()-1; ++i) {
const Point a = firstVertex();
const Point b = secondVertex();
// cosine of steepest miter angle
const Float cos0 = dot(firstMiterNormal(), tangent());
const Float cos1 = dot(secondMiterNormal(), tangent());
const Float maxInvCos = 1.0 / std::min(cos0, cos1);
const Vector expandVec(m_radius * maxIvCos);
result.expandBy(a - expandVec);
result.expandBy(a + expandVec);
result.expandBy(b - expandVec);
result.expandBy(b + expandVec);
Log(EDebug, "Read %i hair vertices, %i segments,", m_vertices.size(), m_segIndex.size());
} }
Hair::Hair(Stream *stream, InstanceManager *manager) : Shape(stream, manager) { Log(EDebug, "Read " SIZE_T_FMT " hair vertices, " SIZE_T_FMT " segments,",
m_radius = stream->readFloat(); m_vertices.size(), segmentCount);
size_t segmentCount = stream->readUInt();
m_vertices.reserve(segmentCount);
for (size_t i=0; i<segmentCount; ++i)
m_vertices.push_back(Point(stream));
m_startFiber.reserve(segmentCount+1);
for (size_t i=0; i<segmentCount+1; ++i)
m_startFiber.push_back(stream->readBool());
buildSegIndex();
} }
void Hair::serialize(Stream *stream, InstanceManager *manager) const { Hair(Stream *stream, InstanceManager *manager)
: Shape(stream, manager) {
}
inline Vector tangent(int iv) const {
return normalize(m_vertices[iv+1] - m_vertices[iv]);
}
inline Vector firstMiterNormal(int iv) const {
if (!m_startFiber[iv])
return normalize(tangent(iv - 1) + tangent(iv));
else
return tangent(iv);
}
inline Vector secondMiterNormal(int iv) const {
if (!m_startFiber[iv+2])
return normalize(tangent(iv) + tangent(iv+1));
else
return tangent();
}
void serialize(Stream *stream, InstanceManager *manager) const {
Shape::serialize(stream, manager); Shape::serialize(stream, manager);
stream->writeFloat(m_radius);
size_t segmentCount = m_vertices.size();
stream->writeUInt(segmentCount);
for (size_t i=0; i<segmentCount; ++i)
m_vertices[i].serialize(stream);
for (size_t i=0; i<segmentCount; ++i)
stream->writeBool(m_startFiber[i]);
} }
Shape *Hair::getElement(int index) { bool rayIntersect(const Ray &_ray, Float mint, Float maxt, Float &t, void *temp) const {
if ((size_t) index >= m_segIndex.size()) return false;
return NULL;
MiterHairSegment *segment = new MiterHairSegment(this, m_segIndex[index]);
segment->addChild("bsdf", m_bsdf);
segment->configure();
return segment;
} }
void Hair::buildSegIndex() { void fillIntersectionRecord(const Ray &ray, Float t,
// Compute the index of the first vertex in each segment. const void *temp, Intersection &its) const {
m_segIndex.clear();
for (size_t i=0; i<m_vertices.size(); i++)
if (!m_startFiber[i+1])
m_segIndex.push_back(i);
} }
inline AABB getAABB() const {
return m_aabb;
}
Float getSurfaceArea() const {
Log(EError, "Hair::getSurfaceArea(): Not implemented.");
return -1;
}
std::string toString() const {
std::ostringstream oss;
oss << "Hair[" << endl
<< " radius = " << m_radius << endl
<< "]";
return oss.str();
}
MTS_DECLARE_CLASS()
private:
std::vector<bool> m_startFiber;
std::vector<int> m_segIndex;
std::vector<Point> m_vertices;
AABB m_aabb;
Float m_radius;
};
MTS_IMPLEMENT_CLASS_S(Hair, false, Shape) MTS_IMPLEMENT_CLASS_S(Hair, false, Shape)
MTS_EXPORT_PLUGIN(Hair, "Hair geometry"); MTS_EXPORT_PLUGIN(Hair, "Hair intersection primitive");
MTS_NAMESPACE_END MTS_NAMESPACE_END

103
src/shapes/hair_old.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <fstream>
#include <mitsuba/render/shape.h>
#include <mitsuba/render/bsdf.h>
#include <mitsuba/core/plugin.h>
#include <mitsuba/core/fresolver.h>
#include "hair.h"
#include "miterseg.h"
MTS_NAMESPACE_BEGIN
Hair::Hair(const Properties &props) : Shape(props) {
std::string filename = props.getString("filename");
props.getString("filename"));
m_radius = (Float) props.getFloat("radius", 0.05f);
m_name = FileResolver::getInstance()->resolve(filename);
Log(EInfo, "Loading hair geometry from \"%s\" ..", m_name.c_str());
std::ifstream is(m_name.c_str());
if (is.fail())
Log(EError, "Could not open \"%s\"!", m_name.c_str());
std::string line;
bool newFiber = true;
Point p;
while (is.good()) {
std::getline(is, line);
if (line.length() > 0 && line[0] == '#')
continue;
if (line.length() == 0) {
newFiber = true;
} else {
std::istringstream iss(line);
iss >> p.x >> p.y >> p.z;
if (!iss.fail()) {
m_vertices.push_back(p);
m_startFiber.push_back(newFiber);
newFiber = false;
}
}
}
m_startFiber.push_back(true);
buildSegIndex();
Log(EDebug, "Read %i hair vertices, %i segments,", m_vertices.size(), m_segIndex.size());
}
Hair::Hair(Stream *stream, InstanceManager *manager) : Shape(stream, manager) {
m_radius = stream->readFloat();
size_t segmentCount = stream->readUInt();
m_vertices.reserve(segmentCount);
for (size_t i=0; i<segmentCount; ++i)
m_vertices.push_back(Point(stream));
m_startFiber.reserve(segmentCount+1);
for (size_t i=0; i<segmentCount+1; ++i)
m_startFiber.push_back(stream->readBool());
buildSegIndex();
}
void Hair::serialize(Stream *stream, InstanceManager *manager) const {
Shape::serialize(stream, manager);
stream->writeFloat(m_radius);
size_t segmentCount = m_vertices.size();
stream->writeUInt(segmentCount);
for (size_t i=0; i<segmentCount; ++i)
m_vertices[i].serialize(stream);
for (size_t i=0; i<segmentCount; ++i)
stream->writeBool(m_startFiber[i]);
}
Shape *Hair::getElement(int index) {
if ((size_t) index >= m_segIndex.size())
return NULL;
MiterHairSegment *segment = new MiterHairSegment(this, m_segIndex[index]);
segment->addChild("bsdf", m_bsdf);
segment->configure();
return segment;
}
void Hair::buildSegIndex() {
// Compute the index of the first vertex in each segment.
m_segIndex.clear();
for (size_t i=0; i<m_vertices.size(); i++)
if (!m_startFiber[i+1])
m_segIndex.push_back(i);
}
MTS_IMPLEMENT_CLASS_S(Hair, false, Shape)
MTS_EXPORT_PLUGIN(Hair, "Hair geometry");
MTS_NAMESPACE_END