-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModel.cs
More file actions
109 lines (86 loc) · 3.74 KB
/
Copy pathModel.cs
File metadata and controls
109 lines (86 loc) · 3.74 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using OpenTK.Graphics.OpenGL4;
using System.Diagnostics;
using OpenTK.Mathematics;
namespace MedViewer
{
class Model
{
public readonly Mesh mesh;
public int vao;
public int vbo_vertex;
public int vbo_index;
public Matrix4 transform = Matrix4.Identity;
public Vector3 color = new Vector3(1.0f, 1.0f, 1.0f);
public PrimitiveType renderType = PrimitiveType.Triangles;
public string name = "";
public bool visible = true;
public float objectID;
public Vector3 centerOfMass = new Vector3();
public Matrix4 frame_transform;
public Matrix4 frame_transform2;
public bool isSelected = false;
public bool isHovering = false;
public Model(Mesh mesh)
{
this.mesh = mesh;
}
public static Model Load(string path)
{
var mesh = ObjLoader.Load(path);
return new Model(mesh);
}
public void ConstructVAO()
{
vao = GL.GenVertexArray();
GL.BindVertexArray(vao);
vbo_vertex = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_vertex);
GL.BufferData(BufferTarget.ArrayBuffer, mesh.numberOfAttributes * sizeof(float), mesh.vertices_attributes, BufferUsageHint.StaticDraw);
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
vbo_index = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo_index);
GL.BufferData(BufferTarget.ElementArrayBuffer, mesh.numberOfIndices * sizeof(uint), mesh.vertexIndices.ToArray(), BufferUsageHint.StaticDraw);
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
}
public void BindToShader(Shader shader)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_vertex);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo_index);
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
var positionLocation = shader.GetAttribLocation("aPos");
GL.EnableVertexAttribArray(positionLocation);
GL.VertexAttribPointer(positionLocation, 3, VertexAttribPointerType.Float, false, mesh.stride * sizeof(float), 0);
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
var normalLocation = shader.GetAttribLocation("aNormal");
GL.EnableVertexAttribArray(normalLocation);
GL.VertexAttribPointer(normalLocation, 3, VertexAttribPointerType.Float, true, mesh.stride * sizeof(float), 3 * sizeof(float));
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
}
public void Draw()
{
if (!visible)
return;
GL.BindVertexArray(vao);
GL.DrawElements(renderType, mesh.numberOfIndices, DrawElementsType.UnsignedInt, 0);
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
GL.BindVertexArray(0);
}
public void Delete()
{
GL.DeleteBuffer(vbo_vertex);
GL.DeleteBuffer(vbo_index);
GL.DeleteVertexArray(vao);
}
public void CalculateCenterOfMass()
{
Vector4 m = new Vector4(this.mesh.centerOfMass, 1.0f);
m *= this.transform;
this.centerOfMass = new Vector3(m.X, m.Y, m.Z);
}
}
}