NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Scene.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "Scene.hpp"
29 
30 using namespace FW;
31 
32 //------------------------------------------------------------------------
33 
34 Scene::Scene(const MeshBase& mesh)
35 {
36  Vec3f light = Vec3f(1.0f, 2.0f, 3.0f).normalized();
37 
38  mesh.getBBox(m_AABBMin, m_AABBMax);
39 
40  // Convert mesh and allocate buffers.
41 
42  Mesh<VertexPNT> meshP(mesh);
43  m_numTriangles = meshP.numTriangles();
44  m_numVertices = meshP.numVertices();
45  m_numEmissive = 0;
46 
47  m_triVtxIndex.resizeDiscard(m_numTriangles * sizeof(Vec3i));
48  m_triNormal.resizeDiscard(m_numTriangles * sizeof(Vec3f));
49  m_triMaterialColor.resizeDiscard(m_numTriangles * sizeof(U32));
50  m_triShadedColor.resizeDiscard(m_numTriangles * sizeof(U32));
51  m_vtxPos.resizeDiscard(m_numVertices * sizeof(Vec3f));
52  m_vtxTC.resizeDiscard(m_numVertices * sizeof(Vec2f));
53  m_vtxNorm.resizeDiscard(m_numVertices * sizeof(Vec3f));
54  m_atlasInfo.resizeDiscard(m_numTriangles * sizeof(Vec4f));
55  m_matId.resizeDiscard(m_numTriangles * sizeof(U32));
56  m_matInfo.resizeDiscard(meshP.numSubmeshes() * sizeof(Vec4f));
57 
58  Vec3i* triVtxIndex = (Vec3i*)m_triVtxIndex.getMutablePtr();
59  Vec3f* triNormal = (Vec3f*)m_triNormal.getMutablePtr();
60  U32* triMaterialColor = (U32*)m_triMaterialColor.getMutablePtr();
61  U32* triShadedColor = (U32*)m_triShadedColor.getMutablePtr();
62  Vec3f* vtxPos = (Vec3f*)m_vtxPos.getMutablePtr();
63  Vec2f* vtxTC = (Vec2f*)m_vtxTC.getMutablePtr();
64  Vec3f* vtxNorm = (Vec3f*)m_vtxNorm.getMutablePtr();
65  Vec4f* texInfo = (Vec4f*)m_atlasInfo.getMutablePtr();
66  U32* matId = (U32*)m_matId.getMutablePtr();
67  Vec4f* matInfo = (Vec4f*)m_matInfo.getMutablePtr();
68 
69  // Copy vertices.
70 
71  const VertexPNT* v = meshP.getVertexPtr();
72  for (int i = 0; i < m_numVertices; i++)
73  {
74  vtxPos[i] = v[i].p;
75  vtxTC[i] = v[i].t;
76  vtxNorm[i] = v[i].n;
77  }
78 
79  // Create texture atlas
80  m_texture = new TextureAtlas(ImageFormat::RGBA_Vec4f);
81 
82  // Fill texture atlas with ALL textures
83  for (int submesh = 0; submesh < meshP.numSubmeshes(); submesh++)
84  {
85  const MeshBase::Material& material = meshP.material(submesh);
86  if(material.textures[MeshBase::TextureType::TextureType_Diffuse].getImage() != NULL)
87  {
88  m_texture->addTexture(material.textures[MeshBase::TextureType::TextureType_Diffuse], 0);
89 
90  int val = 1;
91  matInfo[submesh].w = 1.0f;
92  }
93  else
94  {
95  int val = 0;
96  matInfo[submesh].w = 0.0f;
97  }
98 
99  matInfo[submesh].x = material.emissivity;
100  matInfo[submesh].y = material.reflectivity;
101  matInfo[submesh].z = material.refractivity;
102  }
103 
104  // Collapse submeshes to a single triangle list.
105  for (int submesh = 0; submesh < meshP.numSubmeshes(); submesh++)
106  {
107  const Array<Vec3i>& indices = meshP.indices(submesh);
108  const MeshBase::Material& material = meshP.material(submesh);
109  U32 colorU32 = material.diffuse.toABGR();
110  Vec3f colorVec3f = material.diffuse.getXYZ();
111 
112  for (int i = 0; i < indices.getSize(); i++)
113  {
114  const Vec3i& vi = indices[i];
115  Vec3f normal = normalize(cross(vtxPos[vi.y] - vtxPos[vi.x], vtxPos[vi.z] - vtxPos[vi.x]));
116 
117  *triVtxIndex++ = vi;
118  *triNormal++ = normal;
119  *triMaterialColor++ = colorU32;
120  *triShadedColor++ = Vec4f(colorVec3f * (dot(normal, light) * 0.5f + 0.5f), 1.0f).toABGR();
121 
122  *matId++ = submesh;
123 
124  if(material.textures[MeshBase::TextureType::TextureType_Diffuse] != NULL)
125  {
126  *texInfo++ = Vec4f(m_texture->getTexturePosF(material.textures[MeshBase::TextureType::TextureType_Diffuse]),
127  m_texture->getTextureSizeF(material.textures[MeshBase::TextureType::TextureType_Diffuse]));
128  }
129  else
130  {
131  *texInfo++ = Vec4f(0.0f, 0.0f, 0.0f, 0.0f);
132  }
133 
134  if(material.emissivity > 0.0f)
135  {
136  m_numEmissive++;
137  }
138  }
139  }
140 
141  // Store emissive triangles
142  U32 counter = 0;
143 
144  m_emissiveTris.resizeDiscard(m_numEmissive * sizeof(Vec3i));
145 
146  Vec3i* emissiveTris = (Vec3i*)m_emissiveTris.getMutablePtr();
147 
148  for (int submesh = 0; submesh < meshP.numSubmeshes(); submesh++)
149  {
150  const Array<Vec3i>& indices = meshP.indices(submesh);
151  const MeshBase::Material& material = meshP.material(submesh);
152 
153  for (int i = 0; i < indices.getSize(); i++)
154  {
155  if(material.emissivity > 0.0f)
156  {
157  const Vec3i& vi = indices[i];
158 
159  *emissiveTris++ = vi;
160  counter++;
161  }
162  }
163  }
164 }
165 
166 //------------------------------------------------------------------------
167 
169 {
170 }
171 
172 //------------------------------------------------------------------------
173 
175 {
176  return hashBits(
177  hashBuffer(m_triVtxIndex.getPtr(), (int)m_triVtxIndex.getSize()),
178  hashBuffer(m_triNormal.getPtr(), (int)m_triNormal.getSize()),
179  hashBuffer(m_triMaterialColor.getPtr(), (int)m_triMaterialColor.getSize()),
180  hashBuffer(m_triShadedColor.getPtr(), (int)m_triShadedColor.getSize()),
181  hashBuffer(m_vtxPos.getPtr(), (int)m_vtxPos.getSize()));
182 }
183 
184 //------------------------------------------------------------------------
#define NULL
Definition: Defs.hpp:39
U32 hashBuffer(const void *ptr, int size)
Definition: Hash.cpp:34
void getBBox(Vec3f &lo, Vec3f &hi) const
Definition: Mesh.cpp:597
const Image * getImage(void) const
Definition: Texture.hpp:64
Scene(const MeshBase &mesh)
Constructor.
Definition: Scene.cpp:34
Vec2f getTextureSizeF(const Texture &tex)
int numSubmeshes(void) const
Definition: Mesh.hpp:152
U32 toABGR(void) const
Definition: Math.cpp:45
FW_CUDA_FUNC Vec3f getXYZ(void) const
Definition: Math.hpp:365
const Material & material(int submesh) const
Definition: Mesh.hpp:161
Vec2f getTexturePosF(const Texture &tex)
bool addTexture(const Texture &tex, int border=1, bool wrap=true)
S64 getSize(void) const
Definition: Buffer.hpp:69
const U8 * getPtr(S64 ofs=0)
Definition: Buffer.hpp:106
FW_CUDA_FUNC T dot(const VectorBase< T, L, S > &a, const VectorBase< T, L, V > &b)
Definition: Math.hpp:477
Vec3f p
Definition: Mesh.hpp:291
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v
Definition: DLLImports.inl:329
U8 * getMutablePtr(S64 ofs=0)
Definition: Buffer.hpp:110
~Scene(void)
Destructor.
Definition: Scene.cpp:168
const V * getVertexPtr(int idx=0) const
Definition: Mesh.hpp:221
Vec3f n
Definition: Mesh.hpp:292
int numVertices(void) const
Definition: Mesh.hpp:136
Vec2f t
Definition: Mesh.hpp:293
unsigned int U32
Definition: Defs.hpp:85
U32 hashBits(U32 a, U32 b=FW_HASH_MAGIC, U32 c=0)
Definition: Hash.hpp:183
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction f
Definition: DLLImports.inl:88
FW_CUDA_FUNC F32 cross(const Vec2f &a, const Vec2f &b)
Definition: Math.hpp:481
const Array< Vec3i > & indices(int submesh) const
Definition: Mesh.hpp:156
Texture textures[TextureType_Max]
Definition: Mesh.hpp:92
FW_CUDA_FUNC S normalize(const VectorBase< T, L, S > &v, T len=(T) 1)
Definition: Math.hpp:460
int numTriangles(void) const
Definition: Mesh.hpp:153
FW_CUDA_FUNC S normalized(T len=(T) 1) const
Definition: Math.hpp:144
U32 hash(void)
Definition: Scene.cpp:174
void resizeDiscard(S64 size)
Definition: Buffer.hpp:83
S32 getSize(void) const