@@ -32,7 +32,7 @@ MeshData* Resource::loadOBJMesh(const std::string& filename) {
3232
3333 std::cout << " \t\t [Model loading " << filename << " ...]" << std::endl;
3434 if (!tinyobj::LoadObj (&attrib, &shapes, nullptr , &warn, &err, filename.c_str ())) {
35- std::cout << " \t\t\t [Fail Error msg [ " << err << " ]" << std::endl;
35+ std::cout << " \t\t\t [Fail Error msg " << err << " ]" << std::endl;
3636 return nullptr ;
3737 }
3838 bool hasTexcoord = !attrib.texcoords .empty ();
@@ -184,6 +184,11 @@ void Scene::buildDevData() {
184184 }
185185 }
186186#endif
187+ if (primId == 0 ) {
188+ std::cout << " [No mesh data loaded, quit]" << std::endl;
189+ exit (-1 );
190+ }
191+
187192 createLightSampler ();
188193 BVHSize = BVHBuilder::build (meshData.vertices , boundingBoxes, BVHNodes);
189194 checkCUDAError (" BVH Build" );
@@ -219,16 +224,30 @@ void Scene::loadModel(const std::string& objId) {
219224 std::cout << " \t\t [File " << filename << " ]" << std::endl;
220225 instance.meshData = Resource::loadModelMeshData (filename);
221226
227+ if (!instance.meshData ) {
228+ std::cout << " \t\t [Fail to load, skipped]" << std::endl;
229+ while (!line.empty () && fpIn.good ()) {
230+ utilityCore::safeGetline (fpIn, line);
231+ }
232+ return ;
233+ }
234+
222235 // link material
223236 utilityCore::safeGetline (fpIn, line);
224237 if (!line.empty () && fpIn.good ()) {
225238 std::vector<std::string> tokens = utilityCore::tokenizeString (line);
226- if (materialMap.find (tokens[1 ]) == materialMap.end ()) {
227- std::cout << " \t\t [Material " << tokens[1 ] << " doesn't exist]" << std::endl;
228- throw ;
239+ if (tokens[1 ] == " Null" ) {
240+ // Null material, create new one
241+ instance.materialId = addMaterial (Material ());
242+ }
243+ else {
244+ if (materialMap.find (tokens[1 ]) == materialMap.end ()) {
245+ std::cout << " \t\t [Material " << tokens[1 ] << " doesn't exist]" << std::endl;
246+ throw ;
247+ }
248+ instance.materialId = materialMap[tokens[1 ]];
249+ std::cout << " \t\t [Link to Material " << tokens[1 ] << " {" << instance.materialId << " } ...]" << std::endl;
229250 }
230- instance.materialId = materialMap[tokens[1 ]];
231- std::cout << " \t\t [Link to Material " << tokens[1 ] << " {" << instance.materialId << " } ...]" << std::endl;
232251 }
233252
234253 // load transformations
@@ -239,7 +258,6 @@ void Scene::loadModel(const std::string& objId) {
239258 // load tranformations
240259 if (tokens[0 ] == " Translate" ) {
241260 instance.translation = glm::vec3 (std::stof (tokens[1 ]), std::stof (tokens[2 ]), std::stof (tokens[3 ]));
242- std::cout << vec3ToString (instance.translation ) << " \n " ;
243261 }
244262 else if (tokens[0 ] == " Rotate" ) {
245263 instance.rotation = glm::vec3 (std::stof (tokens[1 ]), std::stof (tokens[2 ]), std::stof (tokens[3 ]));
@@ -322,6 +340,25 @@ void Scene::loadCamera() {
322340 std::fill (state.image .begin (), state.image .end (), glm::vec3 ());
323341}
324342
343+ int Scene::addMaterial (const Material& material) {
344+ materials.push_back (material);
345+ return materials.size () - 1 ;
346+ }
347+
348+ int Scene::addTexture (const std::string& filename) {
349+ auto texture = Resource::loadTexture (filename);
350+ auto find = textureMap.find (texture);
351+ if (find != textureMap.end ()) {
352+ return find->second ;
353+ }
354+ else {
355+ int size = textureMap.size ();
356+ textureMap[texture] = size;
357+ textures.push_back (texture);
358+ return size;
359+ }
360+ }
361+
325362void Scene::loadMaterial (const std::string& matId) {
326363 std::cout << " \t [Material " << matId << " ]" << std::endl;
327364 Material material;
@@ -336,8 +373,14 @@ void Scene::loadMaterial(const std::string& matId) {
336373 std::cout << " \t\t [Type " << tokens[1 ] << " ]" << std::endl;
337374 }
338375 else if (tokens[0 ] == " BaseColor" ) {
339- glm::vec3 baseColor (std::stof (tokens[1 ]), std::stof (tokens[2 ]), std::stof (tokens[3 ]));
340- material.baseColor = baseColor;
376+ if (tokens.size () > 2 ) {
377+ glm::vec3 baseColor (std::stof (tokens[1 ]), std::stof (tokens[2 ]), std::stof (tokens[3 ]));
378+ material.baseColor = baseColor;
379+ }
380+ else {
381+ material.baseColorMapId = addTexture (tokens[1 ]);
382+ std::cout << " \t\t [BaseColor use texture " << tokens[1 ] << " ]" << std::endl;
383+ }
341384 }
342385 else if (tokens[0 ] == " Metallic" ) {
343386 material.metallic = std::stof (tokens[1 ]);
@@ -366,16 +409,20 @@ void DevScene::create(const Scene& scene) {
366409 textureTotalSize += tex->byteSize ();
367410 }
368411 cudaMalloc (&devTextureData, textureTotalSize);
412+ checkCUDAError (" DevScene::texture" );
369413
370- size_t textureOffset = 0 ;
414+ int textureOffset = 0 ;
371415 for (auto tex : scene.textures ) {
372- cudaMemcpy (devTextureData + textureOffset, tex->data (), tex->byteSize (), cudaMemcpyKind::cudaMemcpyHostToDevice);
416+ cudaMemcpyHostToDev (devTextureData + textureOffset, tex->data (), tex->byteSize ());
417+ std::cout << tex->byteSize () << " \n " ;
418+ checkCUDAError (" DevScene::texture::copy" );
373419 textureObjs.push_back ({ tex, devTextureData + textureOffset });
374- textureOffset += tex->byteSize ();
420+ textureOffset += tex->byteSize () / sizeof (glm::vec3) ;
375421 }
376422 cudaMalloc (&devTextureObjs, textureObjs.size () * sizeof (DevTextureObj));
423+ checkCUDAError (" DevScene::textureObjs::malloc" );
377424 cudaMemcpyHostToDev (devTextureObjs, textureObjs.data (), textureObjs.size () * sizeof (DevTextureObj));
378- checkCUDAError (" DevScene::texture " );
425+ checkCUDAError (" DevScene::textureObjs::copy " );
379426
380427 cudaMalloc (&devMaterials, byteSizeOf (scene.materials ));
381428 cudaMemcpyHostToDev (devMaterials, scene.materials .data (), byteSizeOf (scene.materials ));
0 commit comments