| 152 | } |
| 153 | |
| 154 | void DeformableBall::Update(float time) |
| 155 | { |
| 156 | uint32_t const numVertices = mMesh->GetVertexBuffer()->GetNumElements(); |
| 157 | Vertex* vertices = mMesh->GetVertexBuffer()->Get<Vertex>(); |
| 158 | uint32_t const numTriangles = mMesh->GetIndexBuffer()->GetNumElements() / 3; |
| 159 | uint32_t const* indices = mMesh->GetIndexBuffer()->Get<uint32_t>(); |
| 160 | |
| 161 | Vector3<float> zero = Vector3<float>::Zero(); |
| 162 | std::fill(mNormal.begin(), mNormal.end(), zero); |
| 163 | std::fill(mMean.begin(), mMean.end(), zero); |
| 164 | |
| 165 | for (uint32_t i = 0; i < numTriangles; ++i) |
| 166 | { |
| 167 | int32_t i0 = *indices++; |
| 168 | int32_t i1 = *indices++; |
| 169 | int32_t i2 = *indices++; |
| 170 | |
| 171 | Vector3<float> v0 = vertices[i0].position; |
| 172 | Vector3<float> v1 = vertices[i1].position; |
| 173 | Vector3<float> v2 = vertices[i2].position; |
| 174 | |
| 175 | Vector3<float> edge1 = v1 - v0; |
| 176 | Vector3<float> edge2 = v2 - v0; |
| 177 | Vector3<float> normal = Cross(edge1, edge2); |
| 178 | |
| 179 | mNormal[i0] += normal; |
| 180 | mNormal[i1] += normal; |
| 181 | mNormal[i2] += normal; |
| 182 | |
| 183 | mMean[i0] += v1 + v2; |
| 184 | mMean[i1] += v2 + v0; |
| 185 | mMean[i2] += v0 + v1; |
| 186 | } |
| 187 | |
| 188 | for (uint32_t i = 0; i < numVertices; ++i) |
| 189 | { |
| 190 | Normalize(mNormal[i]); |
| 191 | mMean[i] /= static_cast<float>(mNeighborCount[i]); |
| 192 | } |
| 193 | |
| 194 | for (uint32_t i = 0; i < numVertices; ++i) |
| 195 | { |
| 196 | Vector3<float> position = vertices[i].position; |
| 197 | if (VertexInfluenced(i, time, position)) |
| 198 | { |
| 199 | Vector3<float> localDiff = mMean[i] - position; |
| 200 | Vector3<float> surfaceNormal = Dot(localDiff, mNormal[i]) * mNormal[i]; |
| 201 | Vector3<float> tangent = localDiff - surfaceNormal; |
| 202 | float tWeight = GetTangentWeight(i, time, position); |
| 203 | float nWeight = GetNormalWeight(i, time, position); |
| 204 | position += tWeight * tangent + nWeight * mNormal[i]; |
| 205 | vertices[i].position = position; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | bool DeformableBall::DoSimulationStep(float realTime) |
| 211 | { |
no test coverage detected