NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MeshBinaryIO.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 "io/MeshBinaryIO.hpp"
29 #include "3d/Mesh.hpp"
30 #include "io/Stream.hpp"
31 #include "io/ImageBinaryIO.hpp"
32 
33 using namespace FW;
34 
35 //------------------------------------------------------------------------
36 
38 {
39  MeshBase* mesh = new MeshBase;
40 
41  // MeshHeader.
42 
43  char formatID[9];
44  stream.readFully(formatID, 8);
45  formatID[8] = '\0';
46  if (String(formatID) != "BinMesh ")
47  setError("Not a binary mesh file!");
48 
49  S32 version;
50  stream >> version;
51 
52  int numTex;
53  switch (version)
54  {
55  case 1: numTex = 0; break;
56  case 2: numTex = MeshBase::TextureType_Alpha + 1; break;
57  case 3: numTex = MeshBase::TextureType_Displacement + 1; break;
58  case 4: numTex = MeshBase::TextureType_Environment + 1; break;
59  default: numTex = 0; setError("Unsupported binary mesh version!"); break;
60  }
61 
62  S32 numAttribs, numVertices, numSubmeshes, numTextures = 0;
63  stream >> numAttribs >> numVertices >> numSubmeshes;
64  if (version >= 2)
65  stream >> numTextures;
66  if (numAttribs < 0 || numVertices < 0 || numSubmeshes < 0 || numTextures < 0)
67  setError("Corrupt binary mesh data!");
68 
69  // Array of AttribSpec.
70 
71  for (int i = 0; i < numAttribs && !hasError(); i++)
72  {
74  stream >> type >> format >> length;
75  if (type < 0 || format < 0 || format >= MeshBase::AttribFormat_Max || length < 1 || length > 4)
76  setError("Corrupt binary mesh data!");
77  else
78  mesh->addAttrib((MeshBase::AttribType)type, (MeshBase::AttribFormat)format, length);
79  }
80 
81  // Array of Vertex.
82 
83  if (!hasError())
84  {
85  mesh->resetVertices(numVertices);
86  stream.readFully(mesh->getMutableVertexPtr(), numVertices * mesh->vertexStride());
87  }
88 
89  // Array of Texture.
90 
91  Array<Texture> textures(NULL, numTextures);
92  for (int i = 0; i < numTextures && !hasError(); i++)
93  {
94  String id;
95  stream >> id;
96  Image* image = importBinaryImage(stream);
97  textures[i] = Texture::find(id);
98  if (textures[i].exists())
99  delete image;
100  else
101  textures[i] = Texture(image, id);
102  }
103 
104  // Array of Submesh.
105 
106  for (int i = 0; i < numSubmeshes && !hasError(); i++)
107  {
108  mesh->addSubmesh();
109  MeshBase::Material& mat = mesh->material(i);
110  Vec3f ambient;
111  stream >> ambient >> mat.diffuse >> mat.specular >> mat.glossiness;
112  if (version >= 3)
113  stream >> mat.displacementCoef >> mat.displacementBias;
114 
115  for (int j = 0; j < numTex; j++)
116  {
117  S32 texIdx;
118  stream >> texIdx;
119  if (texIdx < -1 || texIdx >= numTextures)
120  setError("Corrupt binary mesh data!");
121  else if (texIdx != -1)
122  mat.textures[j] = textures[texIdx];
123  }
124 
125  S32 numTriangles;
126  stream >> numTriangles;
127  if (numTriangles < 0)
128  setError("Corrupt binary mesh data!");
129  else
130  {
131  Array<Vec3i>& inds = mesh->mutableIndices(i);
132  inds.reset(numTriangles);
133  stream.readFully(inds.getPtr(), inds.getNumBytes());
134  }
135  }
136 
137  // Handle errors.
138 
139  if (hasError())
140  {
141  delete mesh;
142  return NULL;
143  }
144  return mesh;
145 }
146 
147 //------------------------------------------------------------------------
148 
149 void FW::exportBinaryMesh(OutputStream& stream, const MeshBase* mesh)
150 {
151  FW_ASSERT(mesh);
152 
153  // Collapse duplicate textures.
154 
155  int numTex = MeshBase::TextureType_Environment + 1;
156  Array<Texture> textures;
157  Hash<const Image*, S32> texHash;
158  texHash.add(NULL, -1);
159 
160  for (int i = 0; i < mesh->numSubmeshes(); i++)
161  {
162  const MeshBase::Material& mat = mesh->material(i);
163  for (int j = 0; j < numTex; j++)
164  {
165  const Image* key = mat.textures[j].getImage();
166  if (texHash.contains(key))
167  continue;
168 
169  texHash.add(key, textures.getSize());
170  textures.add(mat.textures[j]);
171  }
172  }
173 
174  // MeshHeader.
175 
176  stream.write("BinMesh ", 8);
177  stream << (S32)4 << (S32)mesh->numAttribs() << (S32)mesh->numVertices() << (S32)mesh->numSubmeshes() << (S32)textures.getSize();
178 
179  // Array of AttribSpec.
180 
181  for (int i = 0; i < mesh->numAttribs(); i++)
182  {
183  const MeshBase::AttribSpec& spec = mesh->attribSpec(i);
184  stream << (S32)spec.type << (S32)spec.format << spec.length;
185  }
186 
187  // Array of Vertex.
188 
189  stream.write(mesh->getVertexPtr(), mesh->numVertices() * mesh->vertexStride());
190 
191  // Array of Texture.
192 
193  for (int i = 0; i < textures.getSize(); i++)
194  {
195  stream << textures[i].getID();
196  exportBinaryImage(stream, textures[i].getImage());
197  }
198 
199  // Array of Submesh.
200 
201  for (int i = 0; i < mesh->numSubmeshes(); i++)
202  {
203  const Array<Vec3i>& inds = mesh->indices(i);
204  const MeshBase::Material& mat = mesh->material(i);
205  stream << Vec3f(0.0f) << mat.diffuse << mat.specular << mat.glossiness;
206  stream << mat.displacementCoef << mat.displacementBias;
207 
208  for (int j = 0; j < numTex; j++)
209  stream << texHash[mat.textures[j].getImage()];
210  stream << (S32)inds.getSize();
211  stream.write(inds.getPtr(), inds.getNumBytes());
212  }
213 }
214 
215 //------------------------------------------------------------------------
static Texture find(const String &id)
Definition: Texture.cpp:47
FW_CUDA_FUNC T length(const VectorBase< T, L, S > &v)
Definition: Math.hpp:459
bool contains(const K &key) const
Definition: Hash.hpp:123
#define NULL
Definition: Defs.hpp:39
const Image * getImage(void) const
Definition: Texture.hpp:64
const AttribSpec & attribSpec(int attrib) const
Definition: Mesh.hpp:126
void setError(const char *fmt,...)
Definition: Defs.cpp:253
int numSubmeshes(void) const
Definition: Mesh.hpp:152
const Material & material(int submesh) const
Definition: Mesh.hpp:161
virtual void write(const void *ptr, int size)=0
S32 getNumBytes(void) const
Image * importBinaryImage(InputStream &stream)
AttribFormat format
Definition: Mesh.hpp:74
void reset(S32size=0)
U8 * getMutableVertexPtr(int idx=0)
Definition: Mesh.hpp:142
Array< Vec3i > & mutableIndices(int submesh)
Definition: Mesh.hpp:157
int addAttrib(AttribType type, AttribFormat format, int length)
Definition: Mesh.cpp:39
void readFully(void *ptr, int size)
Definition: Stream.cpp:36
void resetVertices(int num)
Definition: Mesh.cpp:213
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
int numAttribs(void) const
Definition: Mesh.hpp:125
V & add(const K &key, const V &value)
Definition: Hash.hpp:143
int numVertices(void) const
Definition: Mesh.hpp:136
bool hasError(void)
Definition: Defs.cpp:289
int vertexStride(void) const
Definition: Mesh.hpp:137
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
const Array< Vec3i > & indices(int submesh) const
Definition: Mesh.hpp:156
int addSubmesh(void)
Definition: Mesh.hpp:163
void exportBinaryMesh(OutputStream &stream, const MeshBase *mesh)
Texture textures[TextureType_Max]
Definition: Mesh.hpp:92
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 GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: DLLImports.inl:349
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
Definition: DLLImports.inl:323
void exportBinaryImage(OutputStream &stream, const Image *image)
const T * getPtr(S32idx=0) const
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void * image
Definition: DLLImports.inl:60
const U8 * getVertexPtr(int idx=0) const
Definition: Mesh.hpp:141
S32 getSize(void) const
MeshBase * importBinaryMesh(InputStream &stream)