Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TransformExtensions.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.Paradox.Graphics;
8 using SiliconStudio.Core;
9 using SiliconStudio.Core.Mathematics;
10 using SiliconStudio.Paradox.Graphics.Data;
11 
12 namespace SiliconStudio.Paradox.Extensions
13 {
14  public static class TransformExtensions
15  {
16  /// <summary>
17  /// Transform a vertex buffer positions, normals, tangents and bitangents using the given matrix.
18  /// </summary>
19  /// <param name="meshData">The mesh data.</param>
20  public unsafe static void TransformBuffer(this VertexBufferBindingData vertexBufferBinding, ref Matrix matrix)
21  {
22  // List of items that need to be transformed by the matrix
23  var vertexElementsToTransform1 = vertexBufferBinding.Declaration
24  .EnumerateWithOffsets()
25  .Where(x => x.VertexElement.SemanticName == VertexElementUsage.Position
26  && (x.VertexElement.Format == PixelFormat.R32G32B32A32_Float
27  || x.VertexElement.Format == PixelFormat.R32G32B32_Float))
28  .ToArray();
29 
30  // List of items that need to be transformed by the inverse transpose matrix
31  var vertexElementsToTransform2 = vertexBufferBinding.Declaration
32  .EnumerateWithOffsets()
33  .Where(x => (x.VertexElement.SemanticName == VertexElementUsage.Normal
34  || x.VertexElement.SemanticName == VertexElementUsage.Tangent
35  || x.VertexElement.SemanticName == VertexElementUsage.BiTangent)
36  && x.VertexElement.Format == PixelFormat.R32G32B32_Float)
37  .ToArray();
38 
39  // If needed, compute matrix inverse transpose
40  Matrix inverseTransposeMatrix;
41  if (vertexElementsToTransform2.Length > 0)
42  {
43  Matrix.Invert(ref matrix, out inverseTransposeMatrix);
44  Matrix.Transpose(ref inverseTransposeMatrix, out inverseTransposeMatrix);
45  }
46  else
47  {
48  inverseTransposeMatrix = Matrix.Identity;
49  }
50 
51  // Transform buffer data
52  var bufferData = vertexBufferBinding.Buffer.Value.Content;
53  var vertexStride = vertexBufferBinding.Declaration.VertexStride;
54  var vertexCount = vertexBufferBinding.Count;
55  fixed (byte* bufferPointerStart = &bufferData[vertexBufferBinding.Offset])
56  {
57  var bufferPointer = bufferPointerStart;
58 
59  for (int i = 0; i < vertexCount; ++i)
60  {
61  // Transform positions
62  foreach (var vertexElement in vertexElementsToTransform1)
63  {
64  var elementPointer = bufferPointer + vertexElement.Offset;
65  if (vertexElement.VertexElement.Format == PixelFormat.R32G32B32A32_Float)
66  {
67  Vector4.Transform(ref *(Vector4*)elementPointer, ref matrix, out *(Vector4*)elementPointer);
68  }
69  else
70  {
71  Vector3.TransformCoordinate(ref *(Vector3*)elementPointer, ref matrix, out *(Vector3*)elementPointer);
72  }
73  }
74 
75  // Transform normals
76  foreach (var vertexElement in vertexElementsToTransform2)
77  {
78  var elementPointer = bufferPointer + vertexElement.Offset;
79  Vector3.TransformNormal(ref *(Vector3*)elementPointer, ref inverseTransposeMatrix, out *(Vector3*)elementPointer);
80  }
81 
82  bufferPointer += vertexStride;
83  }
84  }
85  }
86  }
87 }
static readonly string BiTangent
Vertex Bitangent data.
static readonly string Tangent
Vertex tangent data.
static unsafe void TransformBuffer(this VertexBufferBindingData vertexBufferBinding, ref Matrix matrix)
Transform a vertex buffer positions, normals, tangents and bitangents using the given matrix...
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
Data type for SiliconStudio.Paradox.Graphics.VertexBufferBinding.
static readonly string Normal
Vertex normal data.
static readonly string Position
Position data.
System.Int32 Offset
Data field for SiliconStudio.Paradox.Graphics.VertexBufferBinding.Offset.
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47