-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMesh.cs
More file actions
66 lines (55 loc) · 2.09 KB
/
Copy pathMesh.cs
File metadata and controls
66 lines (55 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Collections.Generic;
using OpenTK.Mathematics;
namespace MedViewer
{
public class Mesh
{
public readonly float[] vertices_attributes;
public readonly List<Vector3> vertices;
public readonly List<Vector3> normals;
public readonly List<uint> vertexIndices;
public readonly List<uint> normalIndices;
public readonly Vector3 centerOfMass;
public readonly int numberOfVertices;
public readonly int numberOfIndices;
public readonly int numberOfAttributes;
public readonly int stride;
public Mesh(List<Vector3> vertices, List<Vector3> normals,
List<uint> vertexIndices, List<uint> normalIndices)
{
this.vertices = vertices;
this.normals = normals;
this.vertexIndices = vertexIndices;
this.normalIndices = normalIndices;
this.numberOfVertices = vertices.Count;
this.numberOfIndices = vertexIndices.Count;
this.stride = 6;
this.numberOfAttributes = this.numberOfVertices * this.stride;
// per vertex: 3 coordinates, and 3 normal coordinates
this.vertices_attributes = new float[this.numberOfAttributes];
for(int i = 0; i< this.numberOfVertices; i++)
{
this.vertices_attributes[i * this.stride + 0] = this.vertices[i].X;
this.vertices_attributes[i * this.stride + 1] = this.vertices[i].Y;
this.vertices_attributes[i * this.stride + 2] = this.vertices[i].Z;
centerOfMass += this.vertices[i];
}
centerOfMass /= this.numberOfVertices;
Vector3[] normals_averaged = new Vector3[this.numberOfVertices];
int[] normals_averaged_count = new int[this.numberOfVertices];
for (int j = 0; j < this.numberOfIndices; j++)
{
int i = (int)vertexIndices[j];
normals_averaged[i] += this.normals[(int)this.normalIndices[j]];
normals_averaged_count[i]++;
}
for (int i = 0; i < this.numberOfVertices; i++)
{
normals_averaged[i].Normalize();
this.vertices_attributes[i * this.stride + 3] = normals_averaged[i].X;
this.vertices_attributes[i * this.stride + 4] = normals_averaged[i].Y;
this.vertices_attributes[i * this.stride + 5] = normals_averaged[i].Z;
}
}
}
}