public static void DrawNormals(Node node, int meshIndex, Mesh mesh, CpuSkinningEvaluator skinner, float invGlobalScale, Matrix4 transform) { if (!mesh.HasNormals) { return; } // The normal directions are transformed using the transpose(inverse(transform)). // This ensures correct direction is used when non-uniform scaling is present. Matrix4 normalMatrix = transform; normalMatrix.Invert(); normalMatrix.Transpose(); // Scale by scene size because the scene will be resized to fit // the unit box, but the normals should have a fixed length. var scale = invGlobalScale * 0.05f; GL.Begin(BeginMode.Lines); GL.Disable(EnableCap.Lighting); GL.Disable(EnableCap.Texture2D); GL.Enable(EnableCap.ColorMaterial); GL.Color4(new Color4(0.0f, 1.0f, 0.0f, 1.0f)); for (uint i = 0; i < mesh.VertexCount; ++i) { Vector3 v; if (skinner != null && mesh.HasBones) { skinner.GetTransformedVertexPosition(node, mesh, i, out v); } else { v = AssimpToOpenTk.FromVector(mesh.Vertices[(int)i]); } v = Vector4.Transform(new Vector4(v, 1.0f), transform).Xyz; // Skip dividing by W component. It should always be 1, here. Vector3 n; if (skinner != null) { skinner.GetTransformedVertexNormal(node, mesh, i, out n); } else { n = AssimpToOpenTk.FromVector(mesh.Normals[(int)i]); } n = Vector4.Transform(new Vector4(n, 0.0f), normalMatrix).Xyz; // Toss the W component. It is non-sensical for normals. n.Normalize(); GL.Vertex3(v); GL.Vertex3(v + n * scale); } GL.End(); GL.Disable(EnableCap.ColorMaterial); }
public static void DrawNormals(Node node, int meshIndex, Mesh mesh, CpuSkinningEvaluator skinner, float invGlobalScale) { if (!mesh.HasNormals) { return; } // Scale by scene size because the scene will be resized to fit // the unit box but the normals should have a fixed length var scale = invGlobalScale * 0.05f; GL.Begin(BeginMode.Lines); GL.Disable(EnableCap.Lighting); GL.Disable(EnableCap.Texture2D); GL.Enable(EnableCap.ColorMaterial); GL.Color4(new Color4(0.0f, 1.0f, 0.0f, 1.0f)); for (uint i = 0; i < mesh.VertexCount; ++i) { Vector3 v; if (skinner != null && mesh.HasBones) { skinner.GetTransformedVertexPosition(node, meshIndex, i, out v); } else { v = AssimpToOpenTk.FromVector(mesh.Vertices[(int)i]); } Vector3 n; if (skinner != null) { skinner.GetTransformedVertexNormal(node, meshIndex, i, out n); } else { n = AssimpToOpenTk.FromVector(mesh.Normals[(int)i]); } GL.Vertex3(v); GL.Vertex3(v + n * scale); } GL.End(); GL.Disable(EnableCap.ColorMaterial); }
public static void DrawBoundingBox(Node node, int meshIndex, Mesh mesh, CpuSkinningEvaluator skinner) { GL.Disable(EnableCap.Lighting); GL.Disable(EnableCap.Texture2D); GL.Enable(EnableCap.ColorMaterial); GL.Color4(new Color4(1.0f, 0.0f, 0.0f, 1.0f)); var min = new Vector3(1e10f, 1e10f, 1e10f); var max = new Vector3(-1e10f, -1e10f, -1e10f); for (uint i = 0; i < mesh.VertexCount; ++i) { Vector3 tmp; if (skinner != null && mesh.HasBones) { skinner.GetTransformedVertexPosition(node, mesh, i, out tmp); } else { tmp = AssimpToOpenTk.FromVector(mesh.Vertices[(int)i]); } min.X = Math.Min(min.X, tmp.X); min.Y = Math.Min(min.Y, tmp.Y); min.Z = Math.Min(min.Z, tmp.Z); max.X = Math.Max(max.X, tmp.X); max.Y = Math.Max(max.Y, tmp.Y); max.Z = Math.Max(max.Z, tmp.Z); } GL.Begin(BeginMode.LineLoop); GL.Vertex3(min); GL.Vertex3(new Vector3(min.X, max.Y, min.Z)); GL.Vertex3(new Vector3(min.X, max.Y, max.Z)); GL.Vertex3(new Vector3(min.X, min.Y, max.Z)); GL.End(); GL.Begin(BeginMode.LineLoop); GL.Vertex3(new Vector3(max.X, min.Y, min.Z)); GL.Vertex3(new Vector3(max.X, max.Y, min.Z)); GL.Vertex3(new Vector3(max.X, max.Y, max.Z)); GL.Vertex3(new Vector3(max.X, min.Y, max.Z)); GL.End(); GL.Begin(BeginMode.Lines); GL.Vertex3(min); GL.Vertex3(new Vector3(max.X, min.Y, min.Z)); GL.Vertex3(new Vector3(min.X, max.Y, min.Z)); GL.Vertex3(new Vector3(max.X, max.Y, min.Z)); GL.Vertex3(new Vector3(min.X, max.Y, max.Z)); GL.Vertex3(new Vector3(max.X, max.Y, max.Z)); GL.Vertex3(new Vector3(min.X, min.Y, max.Z)); GL.Vertex3(new Vector3(max.X, min.Y, max.Z)); GL.End(); GL.Disable(EnableCap.ColorMaterial); }