Fermat
vtl_mesh_view.h
1 /*
2  * Fermat
3  *
4  * Copyright (c) 2016-2019, NVIDIA CORPORATION. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the NVIDIA CORPORATION nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <mesh/MeshView.h>
32 #include <cugar/basic/vector.h>
34 #include <cugar/bvh/bvh_node.h>
35 #include <uv_bvh_view.h>
36 
41 {
42  FERMAT_HOST_DEVICE
43  VTLMeshView() :
44  n_vtls(0), vtls(NULL), uvbvh(), mesh(), textures(NULL) {}
45 
46  FERMAT_HOST_DEVICE
47  VTLMeshView(const uint32 _n_vtls, const VTL* _vtls, const UVBvhView& _uvbvh, const MeshView& _mesh, const MipMapView* _textures) :
48  n_vtls(_n_vtls), vtls(_vtls), uvbvh(_uvbvh), mesh(_mesh), textures(_textures) {}
49 
50 
53  FERMAT_HOST_DEVICE
54  void sample(
55  const uint32 vtl_idx,
56  const cugar::Vector2f vtl_uv,
57  uint32_t* prim_id,
58  cugar::Vector2f* uv,
59  VertexGeometry* geom,
60  float* pdf,
61  Edf* edf) const
62  {
63  //const float one = cugar::binary_cast<float>(FERMAT_ALMOST_ONE_AS_INT);
64 
65  // sample one of the VTLs
66  *prim_id = vtls[vtl_idx].prim_id;
67  *uv = vtls[vtl_idx].interpolate_uv( vtl_uv );
68  *pdf = 1.0f / vtls[vtl_idx].area;
69 
70  FERMAT_ASSERT(*prim_id < uint32(mesh.num_triangles));
71  setup_differential_geometry(mesh, *prim_id, uv->x, uv->y, geom);
72 
73  const int material_id = mesh.material_indices[*prim_id];
74  MeshMaterial material = mesh.materials[material_id];
75 
76  material.emissive = cugar::Vector4f(material.emissive) * texture_lookup(geom->texture_coords, material.emissive_map, textures, cugar::Vector4f(1.0f));
77 
78  *edf = Edf(material);
79  }
80 
86  FERMAT_HOST_DEVICE
87  uint32 map(const uint32_t prim_id, const cugar::Vector2f& uv, const VertexGeometry& geom, float* pdf, Edf* edf) const
88  {
89  if (n_vtls)
90  {
91  FERMAT_ASSERT(prim_id < uint32(mesh.num_triangles));
92  const int material_id = mesh.material_indices[prim_id];
93  MeshMaterial material = mesh.materials[material_id];
94 
95  material.emissive = cugar::Vector4f(material.emissive) * texture_lookup(geom.texture_coords, material.emissive_map, textures, cugar::Vector4f(1.0f));
96 
97  *edf = Edf(material);
98 
99  // find the VTL containing this uv
100  const uint32 vtl_idx = locate( uvbvh, vtls, prim_id, uv );
101  *pdf = vtl_idx != uint32(-1) ? 1.0f / vtls[vtl_idx].area : 0.0f;
102  return vtl_idx;
103  }
104  else
105  {
106  *pdf = 1.0f;
107  *edf = Edf();
108  return uint32(-1);
109  }
110  }
111 
112  FERMAT_HOST_DEVICE
113  uint32 vtl_count() const { return n_vtls; }
114 
115  FERMAT_HOST_DEVICE
116  VTL get_vtl(const uint32 i) const { return vtls[i]; }
117 
118  uint32 n_vtls;
119  const VTL* vtls;
120  UVBvhView uvbvh;
121  MeshView mesh;
122  const MipMapView* textures;
123 };
FERMAT_HOST_DEVICE uint32 map(const uint32_t prim_id, const cugar::Vector2f &uv, const VertexGeometry &geom, float *pdf, Edf *edf) const
Definition: vtl_mesh_view.h:87
Entry point to the generic Bounding Volume Hierarchy library.
Defines various spherical mappings.
Definition: MeshView.h:55
Definition: vertex.h:92
FERMAT_HOST_DEVICE void setup_differential_geometry(const MeshView &mesh, const uint32 tri_id, const float u, const float v, VertexGeometry *geom, float *pdf=0)
Definition: mesh_utils.h:185
FERMAT_HOST_DEVICE void sample(const uint32 vtl_idx, const cugar::Vector2f vtl_uv, uint32_t *prim_id, cugar::Vector2f *uv, VertexGeometry *geom, float *pdf, Edf *edf) const
Definition: vtl_mesh_view.h:54
Definition: vtl.h:44
Definition: vtl_mesh_view.h:40
Definition: MeshView.h:96
Definition: texture_view.h:73
Definition: edf.h:49
Definition: uv_bvh_view.h:97