Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PolySortExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using SiliconStudio.Paradox.Effects.Data;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Mathematics;
9 using SiliconStudio.Paradox.Graphics;
10 using SiliconStudio.Paradox.Graphics.Data;
11 
12 namespace SiliconStudio.Paradox.Extensions
13 {
14  public static class PolySortExtensions
15  {
16  public unsafe static void SortMeshPolygons(this MeshDrawData meshData, Vector3 viewDirectionForSorting)
17  {
18  // need to have alreade an vertex buffer
19  if (meshData.VertexBuffers == null)
20  throw new ArgumentException();
21  // For now, require a MeshData with an index buffer
22  if (meshData.IndexBuffer == null)
23  throw new NotImplementedException("The mesh Data needs to have index buffer");
24  if(meshData.VertexBuffers.Length != 1)
25  throw new NotImplementedException("Sorting not implemented for multiple vertex buffers by submeshdata");
26 
27  if (viewDirectionForSorting == Vector3.Zero)
28  {
29  // By default to -Z if sorting is set to null
30  viewDirectionForSorting = -Vector3.UnitZ;
31  }
32 
33  const int PolySize = 3; // currently only triangle list are supported
34  var polyIndicesSize = PolySize * Utilities.SizeOf<int>();
35  var vertexBuffer = meshData.VertexBuffers[0];
36  var oldIndexBuffer = meshData.IndexBuffer;
37  var vertexStride = vertexBuffer.Declaration.VertexStride;
38 
39  // Generate the sort list
40  var sortList = new List<KeyValuePair<int, Vector3>>();
41  var pointList = new List<Vector3>();
42 
43  fixed (byte* vertexBufferPointerStart = &vertexBuffer.Buffer.Value.Content[vertexBuffer.Offset])
44  fixed (byte* indexBufferPointerStart = &oldIndexBuffer.Buffer.Value.Content[oldIndexBuffer.Offset])
45  {
46  for (var i = 0; i < oldIndexBuffer.Count / PolySize; ++i)
47  {
48  // fill the point list of the polygon vertices
49  pointList.Clear();
50  for (var u = 0; u < PolySize; ++u)
51  {
52  var curIndex = *(int*)(indexBufferPointerStart + Utilities.SizeOf<int>() * (i * PolySize + u));
53  var pVertexPos = (Vector3*)(vertexBufferPointerStart + vertexStride * curIndex);
54  pointList.Add(*pVertexPos);
55  }
56 
57  // compute the bary-center
58  var accu = Vector3.Zero;
59  foreach (var pt in pointList) //linq do not seems to work on Vector3 type, so compute the mean by hand ...
60  accu += pt;
61  var center = accu / pointList.Count;
62 
63  // add to the list to sort
64  sortList.Add(new KeyValuePair<int, Vector3>(i,center));
65  }
66  }
67 
68  // sort the list
69  var sortedIndices = sortList.OrderBy(x => Vector3.Dot(x.Value, viewDirectionForSorting)).Select(x=>x.Key).ToList(); // TODO have a generic delegate for sorting
70 
71  // re-write the index buffer
72  var newIndexBufferData = new byte[oldIndexBuffer.Count * Utilities.SizeOf<int>()];
73  fixed (byte* newIndexDataStart = &newIndexBufferData[0])
74  fixed (byte* oldIndexDataStart = &oldIndexBuffer.Buffer.Value.Content[0])
75  {
76  var newIndexBufferPointer = newIndexDataStart;
77 
78  foreach (var index in sortedIndices)
79  {
80  Utilities.CopyMemory((IntPtr)(newIndexBufferPointer), (IntPtr)(oldIndexDataStart + index * polyIndicesSize), polyIndicesSize);
81 
82  newIndexBufferPointer += polyIndicesSize;
83  }
84  }
85  meshData.IndexBuffer = new IndexBufferBindingData(new BufferData(BufferFlags.IndexBuffer, newIndexBufferData), oldIndexBuffer.Is32Bit, oldIndexBuffer.Count);
86  }
87  }
88 }
static unsafe void SortMeshPolygons(this MeshDrawData meshData, Vector3 viewDirectionForSorting)
SiliconStudio.Paradox.Graphics.Data.VertexBufferBindingData[] VertexBuffers
Data field for SiliconStudio.Paradox.Effects.MeshDraw.VertexBuffers.
Definition: EngineData.cs:185
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
SiliconStudio.Paradox.Graphics.Data.IndexBufferBindingData IndexBuffer
Data field for SiliconStudio.Paradox.Effects.MeshDraw.IndexBuffer.
Definition: EngineData.cs:190
static readonly Vector3 Zero
A SiliconStudio.Core.Mathematics.Vector3 with all of its components set to zero.
Definition: Vector3.cs:52
Data type for SiliconStudio.Paradox.Graphics.IndexBufferBinding.
Data type for SiliconStudio.Paradox.Effects.MeshDraw.
Definition: EngineData.cs:165
Content of a GPU buffer (vertex buffer, index buffer, etc...).
Definition: BufferData.cs:10