Skip to content

Commit

Permalink
Add support for Ptex textures
Browse files Browse the repository at this point in the history
  • Loading branch information
mmp committed Jul 21, 2017
1 parent f6edb5f commit b57b56c
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "src/ext/glog"]
path = src/ext/glog
url = https://github.com/google/glog.git
[submodule "src/ext/ptex"]
path = src/ext/ptex
url = https://github.com/wdas/ptex.git
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/ext/glog")
"\"git submodule update --init --recursive\"")
endif()

if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/ext/ptex")
message(FATAL_ERROR "The ptex submodule directory is missing! "
"You probably did not clone the project with --recursive, or first checked out "
"pbrt before it was added. It is possible to recover by running "
"\"git submodule update --init --recursive\"")
endif()

IF(CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-conversion-null")
Expand Down Expand Up @@ -147,6 +154,13 @@ INCLUDE_DIRECTORIES (
${CMAKE_BINARY_DIR}/src/ext/glog
)

SET(CMAKE_MACOSX_RPATH 1)
ADD_SUBDIRECTORY(src/ext/ptex)
SET_PROPERTY(TARGET Ptex_static PROPERTY FOLDER "dependencies")
INCLUDE_DIRECTORIES (
src/ext/ptex/src/ptex
)

SET ( PBRT_CORE_SOURCE
src/core/api.cpp
src/core/bssrdf.cpp
Expand Down Expand Up @@ -281,6 +295,7 @@ TARGET_LINK_LIBRARIES ( pbrt_exe
${CMAKE_THREAD_LIBS_INIT}
${OPENEXR_LIBS}
glog
Ptex_static
)


Expand Down Expand Up @@ -310,13 +325,15 @@ TARGET_LINK_LIBRARIES ( bsdftest
${CMAKE_THREAD_LIBS_INIT}
${OPENEXR_LIBS}
glog
Ptex_static
)

TARGET_LINK_LIBRARIES ( imgtool
pbrt
${CMAKE_THREAD_LIBS_INIT}
${OPENEXR_LIBS}
glog
Ptex_static
)

# Unit test
Expand All @@ -336,6 +353,7 @@ TARGET_LINK_LIBRARIES ( pbrt_test
${CMAKE_THREAD_LIBS_INIT}
${OPENEXR_LIBS}
glog
Ptex_static
)

ADD_TEST ( pbrt_unit_test
Expand Down
5 changes: 5 additions & 0 deletions src/core/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
#include "textures/imagemap.h"
#include "textures/marble.h"
#include "textures/mix.h"
#include "textures/ptex.h"
#include "textures/scale.h"
#include "textures/uv.h"
#include "textures/windy.h"
Expand Down Expand Up @@ -503,6 +504,8 @@ std::shared_ptr<Texture<Float>> MakeFloatTexture(const std::string &name,
tex = CreateMarbleFloatTexture(tex2world, tp);
else if (name == "windy")
tex = CreateWindyFloatTexture(tex2world, tp);
else if (name == "ptex")
tex = CreatePtexFloatTexture(tex2world, tp);
else
Warning("Float texture \"%s\" unknown.", name.c_str());
tp.ReportUnused();
Expand Down Expand Up @@ -537,6 +540,8 @@ std::shared_ptr<Texture<Spectrum>> MakeSpectrumTexture(
tex = CreateMarbleSpectrumTexture(tex2world, tp);
else if (name == "windy")
tex = CreateWindySpectrumTexture(tex2world, tp);
else if (name == "ptex")
tex = CreatePtexSpectrumTexture(tex2world, tp);
else
Warning("Spectrum texture \"%s\" unknown.", name.c_str());
tp.ReportUnused();
Expand Down
6 changes: 4 additions & 2 deletions src/core/interaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ namespace pbrt {
SurfaceInteraction::SurfaceInteraction(
const Point3f &p, const Vector3f &pError, const Point2f &uv,
const Vector3f &wo, const Vector3f &dpdu, const Vector3f &dpdv,
const Normal3f &dndu, const Normal3f &dndv, Float time, const Shape *shape)
const Normal3f &dndu, const Normal3f &dndv, Float time, const Shape *shape,
int faceIndex)
: Interaction(p, Normal3f(Normalize(Cross(dpdu, dpdv))), pError, wo, time,
nullptr),
uv(uv),
dpdu(dpdu),
dpdv(dpdv),
dndu(dndu),
dndv(dndv),
shape(shape) {
shape(shape),
faceIndex(faceIndex) {
// Initialize shading geometry from true geometry
shading.n = n;
shading.dpdu = dpdu;
Expand Down
8 changes: 7 additions & 1 deletion src/core/interaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ class SurfaceInteraction : public Interaction {
const Point2f &uv, const Vector3f &wo,
const Vector3f &dpdu, const Vector3f &dpdv,
const Normal3f &dndu, const Normal3f &dndv, Float time,
const Shape *sh);
const Shape *sh,
int faceIndex = 0);
void SetShadingGeometry(const Vector3f &dpdu, const Vector3f &dpdv,
const Normal3f &dndu, const Normal3f &dndv,
bool orientationIsAuthoritative);
Expand All @@ -148,6 +149,11 @@ class SurfaceInteraction : public Interaction {
BSSRDF *bssrdf = nullptr;
mutable Vector3f dpdx, dpdy;
mutable Float dudx = 0, dvdx = 0, dudy = 0, dvdy = 0;

// Added after book publication. Shapes can optionally provide a face
// index with an intersection point for use in Ptex texture lookups.
// If Ptex isn't being used, then this value is ignored.
int faceIndex = 0;
};

} // namespace pbrt
Expand Down
2 changes: 2 additions & 0 deletions src/core/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ enum class Prof {
GetSample,
TexFiltTrilerp,
TexFiltEWA,
TexFiltPtex,
NumProfCategories
};

Expand Down Expand Up @@ -235,6 +236,7 @@ static const char *ProfNames[] = {
"Sampler::GetSample[12]D()",
"MIPMap::Lookup() (trilinear)",
"MIPMap::Lookup() (EWA)",
"Ptex lookup",
};

static_assert((int)Prof::NumProfCategories ==
Expand Down
1 change: 1 addition & 0 deletions src/ext/ptex
Submodule ptex added at 59eac3
23 changes: 18 additions & 5 deletions src/shapes/triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ TriangleMesh::TriangleMesh(
const Transform &ObjectToWorld, int nTriangles, const int *vertexIndices,
int nVertices, const Point3f *P, const Vector3f *S, const Normal3f *N,
const Point2f *UV, const std::shared_ptr<Texture<Float>> &alphaMask,
const std::shared_ptr<Texture<Float>> &shadowAlphaMask)
const std::shared_ptr<Texture<Float>> &shadowAlphaMask,
const int *fIndices)
: nTriangles(nTriangles),
nVertices(nVertices),
vertexIndices(vertexIndices, vertexIndices + 3 * nTriangles),
Expand Down Expand Up @@ -84,17 +85,21 @@ TriangleMesh::TriangleMesh(
s.reset(new Vector3f[nVertices]);
for (int i = 0; i < nVertices; ++i) s[i] = ObjectToWorld(S[i]);
}

if (fIndices)
faceIndices = std::vector<int>(fIndices, fIndices + nTriangles);
}

std::vector<std::shared_ptr<Shape>> CreateTriangleMesh(
const Transform *ObjectToWorld, const Transform *WorldToObject,
bool reverseOrientation, int nTriangles, const int *vertexIndices,
int nVertices, const Point3f *p, const Vector3f *s, const Normal3f *n,
const Point2f *uv, const std::shared_ptr<Texture<Float>> &alphaMask,
const std::shared_ptr<Texture<Float>> &shadowAlphaMask) {
const std::shared_ptr<Texture<Float>> &shadowAlphaMask,
const int *faceIndices) {
std::shared_ptr<TriangleMesh> mesh = std::make_shared<TriangleMesh>(
*ObjectToWorld, nTriangles, vertexIndices, nVertices, p, s, n, uv,
alphaMask, shadowAlphaMask);
alphaMask, shadowAlphaMask, faceIndices);
std::vector<std::shared_ptr<Shape>> tris;
tris.reserve(nTriangles);
for (int i = 0; i < nTriangles; ++i)
Expand Down Expand Up @@ -322,7 +327,7 @@ bool Triangle::Intersect(const Ray &ray, Float *tHit, SurfaceInteraction *isect,
// Fill in _SurfaceInteraction_ from triangle hit
*isect = SurfaceInteraction(pHit, pError, uvHit, -ray.d, dpdu, dpdv,
Normal3f(0, 0, 0), Normal3f(0, 0, 0), ray.time,
this);
this, faceIndex);

// Override surface normal in _isect_ for triangle
isect->n = isect->shading.n = Normal3f(Normalize(Cross(dp02, dp12)));
Expand Down Expand Up @@ -667,6 +672,14 @@ std::vector<std::shared_ptr<Shape>> CreateTriangleMeshShape(
return std::vector<std::shared_ptr<Shape>>();
}

int nfi;
const int *faceIndices = params.FindInt("faceIndices", &nfi);
if (faceIndices && nfi != nvi / 3) {
Error("Number of face indices, %d, doesn't match number of faces, %d",
nfi, nvi / 3);
faceIndices = nullptr;
}

std::shared_ptr<Texture<Float>> alphaTex;
std::string alphaTexName = params.FindTexture("alpha");
if (alphaTexName != "") {
Expand All @@ -692,7 +705,7 @@ std::vector<std::shared_ptr<Shape>> CreateTriangleMeshShape(
shadowAlphaTex.reset(new ConstantTexture<Float>(0.f));

return CreateTriangleMesh(o2w, w2o, reverseOrientation, nvi / 3, vi, npi, P,
S, N, uvs, alphaTex, shadowAlphaTex);
S, N, uvs, alphaTex, shadowAlphaTex, faceIndices);
}

} // namespace pbrt
9 changes: 7 additions & 2 deletions src/shapes/triangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ struct TriangleMesh {
const int *vertexIndices, int nVertices, const Point3f *P,
const Vector3f *S, const Normal3f *N, const Point2f *uv,
const std::shared_ptr<Texture<Float>> &alphaMask,
const std::shared_ptr<Texture<Float>> &shadowAlphaMask);
const std::shared_ptr<Texture<Float>> &shadowAlphaMask,
const int *faceIndices);

// TriangleMesh Data
const int nTriangles, nVertices;
Expand All @@ -64,6 +65,7 @@ struct TriangleMesh {
std::unique_ptr<Vector3f[]> s;
std::unique_ptr<Point2f[]> uv;
std::shared_ptr<Texture<Float>> alphaMask, shadowAlphaMask;
std::vector<int> faceIndices;
};

class Triangle : public Shape {
Expand All @@ -75,6 +77,7 @@ class Triangle : public Shape {
: Shape(ObjectToWorld, WorldToObject, reverseOrientation), mesh(mesh) {
v = &mesh->vertexIndices[3 * triNumber];
triMeshBytes += sizeof(*this);
faceIndex = mesh->faceIndices.size() ? mesh->faceIndices[triNumber] : 0;
}
Bounds3f ObjectBound() const;
Bounds3f WorldBound() const;
Expand Down Expand Up @@ -107,14 +110,16 @@ class Triangle : public Shape {
// Triangle Private Data
std::shared_ptr<TriangleMesh> mesh;
const int *v;
int faceIndex;
};

std::vector<std::shared_ptr<Shape>> CreateTriangleMesh(
const Transform *o2w, const Transform *w2o, bool reverseOrientation,
int nTriangles, const int *vertexIndices, int nVertices, const Point3f *p,
const Vector3f *s, const Normal3f *n, const Point2f *uv,
const std::shared_ptr<Texture<Float>> &alphaTexture,
const std::shared_ptr<Texture<Float>> &shadowAlphaTexture);
const std::shared_ptr<Texture<Float>> &shadowAlphaTexture,
const int *faceIndices = nullptr);
std::vector<std::shared_ptr<Shape>> CreateTriangleMeshShape(
const Transform *o2w, const Transform *w2o, bool reverseOrientation,
const ParamSet &params,
Expand Down
Loading

0 comments on commit b57b56c

Please sign in to comment.