Skip to content

Commit a24747c

Browse files
committed
Fix light position calculation for deeper hierarchy levels
1 parent e6e9aa3 commit a24747c

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

Source/RunActivity/Viewer3D/Lights.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,29 @@ public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
226226

227227
// Calculate XNA matrix for shape file objects by offsetting from car's location
228228
// The new List<int> is intentional, this allows the dictionary to be changed while iterating
229+
int maxDepth = trainCarShape.Hierarchy.Max();
229230
foreach (int index in new List<int>(ShapeXNATranslations.Keys))
230-
ShapeXNATranslations[index] = trainCarShape.XNAMatrices[index] * xnaDTileTranslation;
231+
{
232+
Matrix res = trainCarShape.XNAMatrices[index];
233+
int hIndex = trainCarShape.Hierarchy[index];
234+
235+
int i = 0;
236+
237+
// Transform the matrix repeatedly for all of its parents
238+
while (hIndex > -1 && hIndex < trainCarShape.Hierarchy.Length && i < maxDepth)
239+
{
240+
res = res * trainCarShape.XNAMatrices[hIndex];
241+
// Prevent potential infinite loop due to faulty hierarchy definition
242+
if (hIndex != trainCarShape.Hierarchy[hIndex])
243+
hIndex = trainCarShape.Hierarchy[hIndex];
244+
else
245+
break;
246+
247+
i++;
248+
}
249+
250+
ShapeXNATranslations[index] = res * xnaDTileTranslation;
251+
}
231252

232253
float objectRadius = 20; // Even more arbitrary.
233254
float objectViewingDistance = Viewer.Settings.ViewingDistance; // Arbitrary.

0 commit comments

Comments
 (0)