2011-11-09 06:54:38 +08:00
|
|
|
/*
|
|
|
|
This file is part of Mitsuba, a physically based rendering system.
|
|
|
|
|
2012-09-28 00:43:51 +08:00
|
|
|
Copyright (c) 2007-2012 by Wenzel Jakob and others.
|
2011-11-09 06:54:38 +08:00
|
|
|
|
|
|
|
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/texture.h>
|
|
|
|
#include <mitsuba/render/trimesh.h>
|
|
|
|
#include <mitsuba/core/properties.h>
|
|
|
|
#include <mitsuba/hw/basicshader.h>
|
|
|
|
|
|
|
|
MTS_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/*!\plugin{wireframe}{Wireframe texture}
|
2012-09-28 00:43:51 +08:00
|
|
|
* \order{6}
|
2011-11-09 07:33:41 +08:00
|
|
|
* \parameters{
|
|
|
|
* \parameter{interiorColor}{\Spectrum}{
|
|
|
|
* Color value of the interior of triangles
|
|
|
|
* \default{0.5}
|
|
|
|
* }
|
2012-09-28 00:43:51 +08:00
|
|
|
* \parameter{edgeColor}{\Spectrum}{
|
2011-11-09 07:33:41 +08:00
|
|
|
* Edge color value
|
|
|
|
* \default{0.1}
|
|
|
|
* }
|
|
|
|
* \parameter{lineWidth}{\Float}{
|
|
|
|
* World-space width of the mesh edges
|
|
|
|
* \default{automatic}
|
|
|
|
* }
|
2011-11-12 01:09:11 +08:00
|
|
|
* \parameter{stepWidth}{\Float}{
|
2012-10-21 02:04:13 +08:00
|
|
|
* Controls the width of of step function used for the
|
2011-11-12 01:09:11 +08:00
|
|
|
* color transition. It is specified as a value between zero
|
|
|
|
* and one (relative to the \code{lineWidth} parameter)
|
|
|
|
* \default{0.5}
|
|
|
|
* }
|
2011-11-09 07:33:41 +08:00
|
|
|
* }
|
2012-01-20 08:50:45 +08:00
|
|
|
* \renderings{
|
|
|
|
* \rendering{Wireframe texture applied to the material test object}{tex_wireframe}
|
|
|
|
* }
|
2011-11-09 07:33:41 +08:00
|
|
|
*
|
2012-10-21 02:04:13 +08:00
|
|
|
* This plugin implements a simple two-color wireframe texture map
|
2011-11-09 07:33:41 +08:00
|
|
|
* that reveals the structure of a triangular mesh.
|
2011-11-09 06:54:38 +08:00
|
|
|
*/
|
|
|
|
class WireFrame : public Texture {
|
|
|
|
public:
|
2012-09-28 00:43:51 +08:00
|
|
|
WireFrame(const Properties &props) : Texture(props) {
|
|
|
|
m_lineWidth = props.getFloat("lineWidth", 0.0f);
|
|
|
|
m_stepWidth = props.getFloat("stepWidth", 0.5f);
|
|
|
|
m_edgeColor = props.getSpectrum("edgeColor", Spectrum(0.1f));
|
|
|
|
m_interiorColor = props.getSpectrum("interiorColor", Spectrum(.5f));
|
2011-11-20 13:08:26 +08:00
|
|
|
m_stepWidth = std::max((Float) 0.0f, std::min(m_stepWidth, (Float) 1.0f));
|
2012-09-28 00:43:51 +08:00
|
|
|
m_mutex = new Mutex();
|
2011-11-09 06:54:38 +08:00
|
|
|
}
|
|
|
|
|
2012-10-21 02:04:13 +08:00
|
|
|
WireFrame(Stream *stream, InstanceManager *manager)
|
2011-11-09 06:54:38 +08:00
|
|
|
: Texture(stream, manager) {
|
2011-11-09 07:33:41 +08:00
|
|
|
m_mutex = new Mutex();
|
2011-11-09 06:54:38 +08:00
|
|
|
m_edgeColor = Spectrum(stream);
|
|
|
|
m_interiorColor = Spectrum(stream);
|
|
|
|
m_lineWidth = stream->readFloat();
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(Stream *stream, InstanceManager *manager) const {
|
|
|
|
Texture::serialize(stream, manager);
|
|
|
|
m_edgeColor.serialize(stream);
|
|
|
|
m_interiorColor.serialize(stream);
|
|
|
|
stream->writeFloat(m_lineWidth);
|
|
|
|
}
|
|
|
|
|
2012-09-28 00:43:51 +08:00
|
|
|
Spectrum eval(const Intersection &its, bool /* unused */) const {
|
2011-11-09 06:54:38 +08:00
|
|
|
if (!its.shape->getClass()->derivesFrom(MTS_CLASS(TriMesh)))
|
|
|
|
return m_interiorColor;
|
|
|
|
|
2011-11-09 07:33:41 +08:00
|
|
|
const TriMesh *triMesh = static_cast<const TriMesh *>(its.shape);
|
|
|
|
const Point *positions = triMesh->getVertexPositions();
|
|
|
|
if (its.primIndex >= triMesh->getTriangleCount())
|
2011-11-09 06:54:38 +08:00
|
|
|
return m_interiorColor;
|
|
|
|
|
2011-11-09 07:33:41 +08:00
|
|
|
if (m_lineWidth == 0) {
|
|
|
|
/* Somewhat hacky but probably helpful in many cases.
|
|
|
|
This tries to find a suitable line width, which is set
|
2012-10-21 02:04:13 +08:00
|
|
|
to 10% of the average average edge length */
|
2012-09-28 00:43:51 +08:00
|
|
|
LockGuard lock(m_mutex);
|
2011-11-09 07:33:41 +08:00
|
|
|
if (m_lineWidth == 0) {
|
2011-12-31 05:18:33 +08:00
|
|
|
Float lineWidth = 0;
|
2011-11-09 07:33:41 +08:00
|
|
|
for (size_t i=0; i<triMesh->getTriangleCount(); ++i) {
|
|
|
|
const Triangle &tri = triMesh->getTriangles()[i];
|
|
|
|
for (int j=0; j<3; ++j)
|
2012-10-21 02:04:13 +08:00
|
|
|
lineWidth += (positions[tri.idx[j]]
|
2011-11-12 01:09:11 +08:00
|
|
|
- positions[tri.idx[(j+1)%3]]).length();
|
2011-11-09 07:33:41 +08:00
|
|
|
}
|
|
|
|
|
2011-12-31 05:18:33 +08:00
|
|
|
m_lineWidth = 0.1f * lineWidth / (3 * triMesh->getTriangleCount());
|
2011-11-09 07:33:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Triangle &tri = triMesh->getTriangles()[its.primIndex];
|
2011-11-09 06:54:38 +08:00
|
|
|
|
|
|
|
Float minDist = std::numeric_limits<Float>::infinity();
|
|
|
|
for (int i=0; i<3; ++i) {
|
2011-11-12 01:09:11 +08:00
|
|
|
const Point& cur = positions[tri.idx[i]];
|
|
|
|
const Point& next = positions[tri.idx[(i+1)%3]];
|
2011-11-09 06:54:38 +08:00
|
|
|
|
|
|
|
Vector d1 = normalize(next - cur),
|
2011-11-12 01:09:11 +08:00
|
|
|
d2 = its.p - cur;
|
2011-11-09 06:54:38 +08:00
|
|
|
|
2011-11-09 07:33:41 +08:00
|
|
|
minDist = std::min(minDist, (cur + d1 * dot(d1, d2) - its.p).lengthSquared());
|
2011-11-09 06:54:38 +08:00
|
|
|
}
|
|
|
|
|
2011-11-12 01:09:11 +08:00
|
|
|
Float a = smoothStep(m_lineWidth*(1.f-m_stepWidth), m_lineWidth, std::sqrt(minDist));
|
|
|
|
return m_edgeColor*(1-a) + m_interiorColor*a;
|
2011-11-09 06:54:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool usesRayDifferentials() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Spectrum getAverage() const {
|
|
|
|
Spectrum value;
|
|
|
|
/* Approximate ... */
|
2012-09-28 00:43:51 +08:00
|
|
|
for (int i=0; i<SPECTRUM_SAMPLES; ++i)
|
2011-11-09 06:54:38 +08:00
|
|
|
value[i] = 0.5f * (m_edgeColor[i] + m_interiorColor[i]);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Spectrum getMinimum() const {
|
|
|
|
Spectrum value;
|
2012-09-28 00:43:51 +08:00
|
|
|
for (int i=0; i<SPECTRUM_SAMPLES; ++i)
|
2011-11-09 06:54:38 +08:00
|
|
|
value[i] = std::min(m_edgeColor[i], m_interiorColor[i]);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Spectrum getMaximum() const {
|
|
|
|
Spectrum value;
|
2012-09-28 00:43:51 +08:00
|
|
|
for (int i=0; i<SPECTRUM_SAMPLES; ++i)
|
2011-11-09 06:54:38 +08:00
|
|
|
value[i] = std::max(m_edgeColor[i], m_interiorColor[i]);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isConstant() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString() const {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "WireFrame[" << endl
|
2011-11-09 07:33:41 +08:00
|
|
|
<< " edgeColor = " << m_edgeColor.toString() << "," << endl
|
2011-11-12 01:09:11 +08:00
|
|
|
<< " interiorColor = " << m_interiorColor.toString() << "," << endl
|
2011-11-09 06:54:38 +08:00
|
|
|
<< " lineWidth = " << m_lineWidth << endl
|
2011-11-12 01:09:11 +08:00
|
|
|
<< " stepWidth = " << m_stepWidth << endl
|
2011-11-09 06:54:38 +08:00
|
|
|
<< "]";
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
Shader *createShader(Renderer *renderer) const;
|
|
|
|
|
|
|
|
MTS_DECLARE_CLASS()
|
|
|
|
protected:
|
2011-11-09 07:33:41 +08:00
|
|
|
mutable Float m_lineWidth;
|
|
|
|
mutable ref<Mutex> m_mutex;
|
2012-09-28 00:43:51 +08:00
|
|
|
Float m_stepWidth;
|
2011-11-09 06:54:38 +08:00
|
|
|
Spectrum m_edgeColor;
|
|
|
|
Spectrum m_interiorColor;
|
|
|
|
};
|
|
|
|
|
2012-10-21 02:04:13 +08:00
|
|
|
// ================ Hardware shader implementation ================
|
2011-11-09 06:54:38 +08:00
|
|
|
|
|
|
|
class WireFrameShader : public Shader {
|
|
|
|
public:
|
2012-10-21 02:04:13 +08:00
|
|
|
WireFrameShader(Renderer *renderer, const Spectrum &value)
|
2011-11-09 06:54:38 +08:00
|
|
|
: Shader(renderer, ETextureShader), m_value(value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void generateCode(std::ostringstream &oss,
|
|
|
|
const std::string &evalName,
|
|
|
|
const std::vector<std::string> &depNames) const {
|
|
|
|
oss << "uniform vec3 " << evalName << "_value;" << endl
|
|
|
|
<< endl
|
|
|
|
<< "vec3 " << evalName << "(vec2 uv) {" << endl
|
|
|
|
<< " return " << evalName << "_value;" << endl
|
|
|
|
<< "}" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resolve(const GPUProgram *program, const std::string &evalName, std::vector<int> ¶meterIDs) const {
|
|
|
|
parameterIDs.push_back(program->getParameterID(evalName + "_value", false));
|
|
|
|
}
|
|
|
|
|
|
|
|
void bind(GPUProgram *program, const std::vector<int> ¶meterIDs, int &textureUnitOffset) const {
|
|
|
|
program->setParameter(parameterIDs[0], m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
MTS_DECLARE_CLASS()
|
|
|
|
private:
|
|
|
|
Spectrum m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
Shader *WireFrame::createShader(Renderer *renderer) const {
|
|
|
|
return new WireFrameShader(renderer, m_interiorColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
MTS_IMPLEMENT_CLASS(WireFrameShader, false, Shader)
|
|
|
|
MTS_IMPLEMENT_CLASS_S(WireFrame, false, Texture)
|
2011-11-12 01:09:11 +08:00
|
|
|
MTS_EXPORT_PLUGIN(WireFrame, "Wireframe texture");
|
2011-11-09 06:54:38 +08:00
|
|
|
MTS_NAMESPACE_END
|