| 42 | } |
| 43 | |
| 44 | bool Visual::UpdateModelNormals() |
| 45 | { |
| 46 | LogAssert(mVBuffer != nullptr && mIBuffer != nullptr, "Buffer not attached."); |
| 47 | |
| 48 | // Get vertex positions. |
| 49 | std::set<uint32_t> required; |
| 50 | required.insert(DF_R32G32B32_FLOAT); |
| 51 | required.insert(DF_R32G32B32A32_FLOAT); |
| 52 | char const* positions = mVBuffer->GetChannel(VASemantic::POSITION, 0, required); |
| 53 | if (!positions) |
| 54 | { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // Get vertex normals. |
| 59 | char* normals = mVBuffer->GetChannel(VASemantic::NORMAL, 0, required); |
| 60 | if (!normals) |
| 61 | { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // Get triangle primitives. |
| 66 | uint32_t primitiveType = mIBuffer->GetPrimitiveType(); |
| 67 | if ((primitiveType & IP_HAS_TRIANGLES) == 0) |
| 68 | { |
| 69 | // Normal vectors are not defined for point or segment primitives. |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | uint32_t const numVertices = mVBuffer->GetNumElements(); |
| 74 | uint32_t const stride = (int32_t)mVBuffer->GetElementSize(); |
| 75 | uint32_t i; |
| 76 | for (i = 0; i < numVertices; ++i) |
| 77 | { |
| 78 | Vector3<float>& normal = *(Vector3<float>*)(normals + static_cast<size_t>(i) * stride); |
| 79 | normal = { 0.0f, 0.0f, 0.0f }; |
| 80 | } |
| 81 | |
| 82 | uint32_t const numTriangles = mIBuffer->GetNumPrimitives(); |
| 83 | bool isIndexed = mIBuffer->IsIndexed(); |
| 84 | for (i = 0; i < numTriangles; ++i) |
| 85 | { |
| 86 | // Get the vertex indices for the triangle. |
| 87 | uint32_t v0, v1, v2; |
| 88 | if (isIndexed) |
| 89 | { |
| 90 | mIBuffer->GetTriangle(i, v0, v1, v2); |
| 91 | } |
| 92 | else if (primitiveType == IP_TRIMESH) |
| 93 | { |
| 94 | v0 = 3 * i; |
| 95 | v1 = v0 + 1; |
| 96 | v2 = v0 + 2; |
| 97 | } |
| 98 | else // primitiveType == IP_TRISTRIP |
| 99 | { |
| 100 | int32_t offset = (i & 1); |
| 101 | v0 = i + offset; |
no test coverage detected