diff options
Diffstat (limited to 'tools/platform/model.cpp')
| -rw-r--r-- | tools/platform/model.cpp | 231 |
1 files changed, 116 insertions, 115 deletions
diff --git a/tools/platform/model.cpp b/tools/platform/model.cpp index a48d499b9..3649db9dc 100644 --- a/tools/platform/model.cpp +++ b/tools/platform/model.cpp @@ -10,17 +10,17 @@ #include "../../external/stb/stb_image.h" #define STB_IMAGE_RESIZE_IMPLEMENTATION -#include "../../external/stb/stb_image_resize.h" - #include "../../external/glm/glm/glm.hpp" -#include "../../external/glm/glm/gtc/matrix_transform.hpp" #include "../../external/glm/glm/gtc/constants.hpp" +#include "../../external/glm/glm/gtc/matrix_transform.hpp" +#include "../../external/stb/stb_image_resize.h" #include <memory> #include <unordered_map> #include <unordered_set> -namespace platform { +namespace platform +{ using namespace gfx; using namespace Slang; @@ -43,9 +43,9 @@ struct ObjIndexKey bool operator==(ObjIndexKey const& left, ObjIndexKey const& right) { - return left.index.vertex_index == right.index.vertex_index - && left.index.normal_index == right.index.normal_index - && left.index.texcoord_index == right.index.texcoord_index; + return left.index.vertex_index == right.index.vertex_index && + left.index.normal_index == right.index.normal_index && + left.index.texcoord_index == right.index.texcoord_index; } struct Hasher @@ -65,70 +65,65 @@ struct SmoothingGroupVertexID }; bool operator==(SmoothingGroupVertexID const& left, SmoothingGroupVertexID const& right) { - return left.smoothingGroup == right.smoothingGroup - && left.positionID == right.positionID; + return left.smoothingGroup == right.smoothingGroup && left.positionID == right.positionID; } -} +} // namespace platform namespace std { - template<> struct hash<platform::ObjIndexKey> +template<> +struct hash<platform::ObjIndexKey> +{ + size_t operator()(platform::ObjIndexKey const& key) const { - size_t operator()(platform::ObjIndexKey const& key) const - { - platform::Hasher hasher; - hasher.add(key.index.vertex_index); - hasher.add(key.index.normal_index); - hasher.add(key.index.texcoord_index); - return hasher.state; - } - }; + platform::Hasher hasher; + hasher.add(key.index.vertex_index); + hasher.add(key.index.normal_index); + hasher.add(key.index.texcoord_index); + return hasher.state; + } +}; - template <> struct hash<platform::SmoothingGroupVertexID> +template<> +struct hash<platform::SmoothingGroupVertexID> +{ + size_t operator()(platform::SmoothingGroupVertexID const& id) const { - size_t operator()(platform::SmoothingGroupVertexID const& id) const - { - platform::Hasher hasher; - hasher.add(id.smoothingGroup); - hasher.add(id.positionID); - return hasher.state; - } - }; -} + platform::Hasher hasher; + hasher.add(id.smoothingGroup); + hasher.add(id.positionID); + return hasher.state; + } +}; +} // namespace std namespace platform { -ComPtr<ITextureResource> loadTextureImage( - IDevice* device, - char const* path) +ComPtr<ITextureResource> loadTextureImage(IDevice* device, char const* path) { int extentX = 0; int extentY = 0; int originalChannelCount = 0; int requestedChannelCount = 4; // force to 4-component result - stbi_uc* data = stbi_load( - path, - &extentX, - &extentY, - &originalChannelCount, - requestedChannelCount); - if(!data) + stbi_uc* data = + stbi_load(path, &extentX, &extentY, &originalChannelCount, requestedChannelCount); + if (!data) return nullptr; int channelCount = requestedChannelCount ? requestedChannelCount : originalChannelCount; Format format; - switch(channelCount) + switch (channelCount) { - default: - return nullptr; + default: return nullptr; - case 4: format = Format::R8G8B8A8_UNORM; + case 4: + format = Format::R8G8B8A8_UNORM; - // TODO: handle other cases here if/when we stop forcing 4-component - // results when loading the image with stb_image. + // TODO: handle other cases here if/when we stop forcing 4-component + // results when loading the image with stb_image. } std::vector<ITextureResource::SubresourceData> subresourceInitData; @@ -144,30 +139,39 @@ ComPtr<ITextureResource> loadTextureImage( // create down-sampled images for the different mip levels bool generateMips = true; - if(generateMips) + if (generateMips) { int prevExtentX = extentX; int prevExtentY = extentY; stbi_uc* prevData = data; int prevStride = int(stride); - for(;;) + for (;;) { - if(prevExtentX == 1 && prevExtentY == 1) + if (prevExtentX == 1 && prevExtentY == 1) break; int newExtentX = prevExtentX / 2; int newExtentY = prevExtentY / 2; - if(!newExtentX) newExtentX = 1; - if(!newExtentY) newExtentY = 1; + if (!newExtentX) + newExtentX = 1; + if (!newExtentY) + newExtentY = 1; - stbi_uc* newData = (stbi_uc*) malloc(newExtentX * newExtentY * channelCount * sizeof(stbi_uc)); + stbi_uc* newData = + (stbi_uc*)malloc(newExtentX * newExtentY * channelCount * sizeof(stbi_uc)); int newStride = int(newExtentX * channelCount * sizeof(stbi_uc)); stbir_resize_uint8_srgb( - prevData, prevExtentX, prevExtentY, prevStride, - newData, newExtentX, newExtentY, newStride, + prevData, + prevExtentX, + prevExtentY, + prevStride, + newData, + newExtentX, + newExtentY, + newStride, channelCount, STBIR_ALPHA_CHANNEL_NONE, STBIR_FLAG_ALPHA_PREMULTIPLIED); @@ -187,7 +191,7 @@ ComPtr<ITextureResource> loadTextureImage( } } - int mipCount = (int) subresourceInitData.size(); + int mipCount = (int)subresourceInitData.size(); ITextureResource::Desc desc = {}; desc.type = IResource::Type::Texture2D; @@ -209,9 +213,7 @@ static std::string makeString(const char* start, const char* end) return std::string(start, size_t(end - start)); } -SlangResult ModelLoader::load( - char const* inputPath, - void** outModel) +SlangResult ModelLoader::load(char const* inputPath, void** outModel) { // TODO: need to actually allocate/load the data @@ -220,7 +222,7 @@ SlangResult ModelLoader::load( std::vector<tinyobj::material_t> objMaterials; std::string baseDir; - if( auto lastSlash = strrchr(inputPath, '/') ) + if (auto lastSlash = strrchr(inputPath, '/')) { baseDir = makeString(inputPath, lastSlash); } @@ -236,11 +238,11 @@ SlangResult ModelLoader::load( baseDir.size() ? baseDir.c_str() : nullptr, shouldTriangulate); - if(!diagnostics.empty()) + if (!diagnostics.empty()) { printf("%s", diagnostics.c_str()); } - if(!success) + if (!success) { return SLANG_FAIL; } @@ -249,28 +251,22 @@ SlangResult ModelLoader::load( // we can actually use for rendering. // std::vector<void*> materials; - for(auto& objMaterial : objMaterials) + for (auto& objMaterial : objMaterials) { MaterialData materialData; - materialData.diffuseColor = glm::vec3( - objMaterial.diffuse[0], - objMaterial.diffuse[1], - objMaterial.diffuse[2]); + materialData.diffuseColor = + glm::vec3(objMaterial.diffuse[0], objMaterial.diffuse[1], objMaterial.diffuse[2]); - materialData.specularColor = glm::vec3( - objMaterial.specular[0], - objMaterial.specular[1], - objMaterial.specular[2]); + materialData.specularColor = + glm::vec3(objMaterial.specular[0], objMaterial.specular[1], objMaterial.specular[2]); materialData.specularity = objMaterial.shininess; // load any referenced textures here - if(objMaterial.diffuse_texname.length()) + if (objMaterial.diffuse_texname.length()) { - materialData.diffuseMap = loadTextureImage( - device, - objMaterial.diffuse_texname.c_str()); + materialData.diffuseMap = loadTextureImage(device, objMaterial.diffuse_texname.c_str()); } auto material = callbacks->createMaterial(materialData); @@ -279,20 +275,20 @@ SlangResult ModelLoader::load( // Flip the winding order on all faces if we are asked to... // - if(loadFlags & LoadFlag::FlipWinding) + if (loadFlags & LoadFlag::FlipWinding) { - for(auto& objShape : objShapes) + for (auto& objShape : objShapes) { size_t objIndexCounter = 0; size_t objFaceCounter = 0; - for(auto objFaceVertexCount : objShape.mesh.num_face_vertices) + for (auto objFaceVertexCount : objShape.mesh.num_face_vertices) { size_t beginIndex = objIndexCounter; size_t endIndex = beginIndex + objFaceVertexCount; objIndexCounter = endIndex; size_t halfCount = objFaceVertexCount / 2; - for(size_t ii = 0; ii < halfCount; ++ii) + for (size_t ii = 0; ii < halfCount; ++ii) { std::swap( objShape.mesh.indices[beginIndex + ii], @@ -300,7 +296,6 @@ SlangResult ModelLoader::load( } } } - } // Identify cases where a face has a vertex without a normal, and in that @@ -311,31 +306,31 @@ SlangResult ModelLoader::load( std::unordered_map<SmoothingGroupVertexID, size_t> smoothedVertexNormals; size_t firstSmoothedNormalID = objVertexAttributes.normals.size() / 3; size_t flatFaceCounter = 0; - for(auto& objShape : objShapes) + for (auto& objShape : objShapes) { size_t objIndexCounter = 0; size_t objFaceCounter = 0; - for(auto objFaceVertexCount : objShape.mesh.num_face_vertices) + for (auto objFaceVertexCount : objShape.mesh.num_face_vertices) { const size_t flatFaceIndex = flatFaceCounter++; const size_t objFaceIndex = objFaceCounter++; size_t smoothingGroup = objShape.mesh.smoothing_group_ids[objFaceIndex]; - if(!smoothingGroup) + if (!smoothingGroup) { smoothingGroup = ~flatFaceIndex; } - for(size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) + for (size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) { tinyobj::index_t& objIndex = objShape.mesh.indices[objIndexCounter++]; - if(objIndex.normal_index < 0) + if (objIndex.normal_index < 0) { SmoothingGroupVertexID smoothVertexID; smoothVertexID.positionID = objIndex.vertex_index; smoothVertexID.smoothingGroup = smoothingGroup; - if(smoothedVertexNormals.find(smoothVertexID) == smoothedVertexNormals.end()) + if (smoothedVertexNormals.find(smoothVertexID) == smoothedVertexNormals.end()) { size_t normalID = objVertexAttributes.normals.size() / 3; objVertexAttributes.normals.push_back(0); @@ -356,28 +351,29 @@ SlangResult ModelLoader::load( // to the same smoothing group. // flatFaceCounter = 0; - for(auto& objShape : objShapes) + for (auto& objShape : objShapes) { size_t objIndexCounter = 0; size_t objFaceCounter = 0; - for(auto objFaceVertexCount : objShape.mesh.num_face_vertices) + for (auto objFaceVertexCount : objShape.mesh.num_face_vertices) { const size_t flatFaceIndex = flatFaceCounter++; const size_t objFaceIndex = objFaceCounter++; size_t smoothingGroup = objShape.mesh.smoothing_group_ids[objFaceIndex]; - if(!smoothingGroup) + if (!smoothingGroup) { smoothingGroup = ~flatFaceIndex; } glm::vec3 faceNormal; - if(objFaceVertexCount >= 3) + if (objFaceVertexCount >= 3) { glm::vec3 v[3]; - for(size_t objFaceVertex = 0; objFaceVertex < 3; ++objFaceVertex) + for (size_t objFaceVertex = 0; objFaceVertex < 3; ++objFaceVertex) { - tinyobj::index_t objIndex = objShape.mesh.indices[objIndexCounter + objFaceVertex]; - if(objIndex.vertex_index >= 0) + tinyobj::index_t objIndex = + objShape.mesh.indices[objIndexCounter + objFaceVertex]; + if (objIndex.vertex_index >= 0) { v[objFaceVertex] = glm::vec3( objVertexAttributes.vertices[3 * objIndex.vertex_index + 0], @@ -389,7 +385,7 @@ SlangResult ModelLoader::load( } // Add this face normal to any to-be-smoothed vertex on the face. - for(size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) + for (size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) { tinyobj::index_t objIndex = objShape.mesh.indices[objIndexCounter++]; @@ -398,7 +394,7 @@ SlangResult ModelLoader::load( smoothVertexID.smoothingGroup = smoothingGroup; auto ii = smoothedVertexNormals.find(smoothVertexID); - if(ii != smoothedVertexNormals.end()) + if (ii != smoothedVertexNormals.end()) { size_t normalID = ii->second; objVertexAttributes.normals[normalID * 3 + 0] += faceNormal.x; @@ -413,7 +409,7 @@ SlangResult ModelLoader::load( // we can normalize the normals to compute the area-weighted average. // size_t normalCount = objVertexAttributes.normals.size() / 3; - for(size_t ii = firstSmoothedNormalID; ii < normalCount; ++ii) + for (size_t ii = firstSmoothedNormalID; ii < normalCount; ++ii) { glm::vec3 normal = glm::vec3( objVertexAttributes.normals[3 * ii + 0], @@ -445,18 +441,18 @@ SlangResult ModelLoader::load( void* defaultMaterial = nullptr; - for(auto& objShape : objShapes) + for (auto& objShape : objShapes) { size_t objIndexCounter = 0; size_t objFaceCounter = 0; - for(auto objFaceVertexCount : objShape.mesh.num_face_vertices) + for (auto objFaceVertexCount : objShape.mesh.num_face_vertices) { size_t objFaceIndex = objFaceCounter++; int faceMaterialID = objShape.mesh.material_ids[objFaceIndex]; void* faceMaterial = nullptr; - if( faceMaterialID < 0 ) + if (faceMaterialID < 0) { - if( !defaultMaterial ) + if (!defaultMaterial) { MaterialData defaultMaterialData; defaultMaterialData.diffuseColor = glm::vec3(0.5, 0.5, 0.5); @@ -469,10 +465,10 @@ SlangResult ModelLoader::load( faceMaterial = materials[faceMaterialID]; } - if(!currentMesh || (faceMaterial != currentMesh->material)) + if (!currentMesh || (faceMaterial != currentMesh->material)) { // finish old mesh. - if(currentMesh) + if (currentMesh) { meshes.push_back(callbacks->createMesh(*currentMesh)); } @@ -484,36 +480,39 @@ SlangResult ModelLoader::load( currentMesh->indexCount = 0; } - for(size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) + for (size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) { tinyobj::index_t objIndex = objShape.mesh.indices[objIndexCounter++]; - ObjIndexKey objIndexKey; objIndexKey.index = objIndex; + ObjIndexKey objIndexKey; + objIndexKey.index = objIndex; Index flatIndex = Index(-1); auto iter = mapObjIndexToFlatIndex.find(objIndexKey); - if(iter != mapObjIndexToFlatIndex.end()) + if (iter != mapObjIndexToFlatIndex.end()) { flatIndex = iter->second; } else { Vertex flatVertex; - if(objIndex.vertex_index >= 0) + if (objIndex.vertex_index >= 0) { - flatVertex.position = scale * glm::vec3( - objVertexAttributes.vertices[3 * objIndex.vertex_index + 0], - objVertexAttributes.vertices[3 * objIndex.vertex_index + 1], - objVertexAttributes.vertices[3 * objIndex.vertex_index + 2]); + flatVertex.position = + scale * + glm::vec3( + objVertexAttributes.vertices[3 * objIndex.vertex_index + 0], + objVertexAttributes.vertices[3 * objIndex.vertex_index + 1], + objVertexAttributes.vertices[3 * objIndex.vertex_index + 2]); } - if(objIndex.normal_index >= 0) + if (objIndex.normal_index >= 0) { flatVertex.normal = glm::vec3( objVertexAttributes.normals[3 * objIndex.normal_index + 0], objVertexAttributes.normals[3 * objIndex.normal_index + 1], objVertexAttributes.normals[3 * objIndex.normal_index + 2]); } - if(objIndex.texcoord_index >= 0) + if (objIndex.texcoord_index >= 0) { flatVertex.uv = glm::vec2( objVertexAttributes.texcoords[2 * objIndex.texcoord_index + 0], @@ -532,7 +531,7 @@ SlangResult ModelLoader::load( } // finish last mesh. - if(currentMesh) + if (currentMesh) { meshes.push_back(callbacks->createMesh(*currentMesh)); } @@ -553,7 +552,8 @@ SlangResult ModelLoader::load( vertexBufferDesc.defaultState = ResourceState::VertexBuffer; modelData.vertexBuffer = device->createBufferResource(vertexBufferDesc, flatVertices.data()); - if(!modelData.vertexBuffer) return SLANG_FAIL; + if (!modelData.vertexBuffer) + return SLANG_FAIL; IBufferResource::Desc indexBufferDesc; indexBufferDesc.type = IResource::Type::Buffer; @@ -563,11 +563,12 @@ SlangResult ModelLoader::load( indexBufferDesc.defaultState = ResourceState::IndexBuffer; modelData.indexBuffer = device->createBufferResource(indexBufferDesc, flatIndices.data()); - if(!modelData.indexBuffer) return SLANG_FAIL; + if (!modelData.indexBuffer) + return SLANG_FAIL; *outModel = callbacks->createModel(modelData); return SLANG_OK; } -} // gfx +} // namespace platform |
