-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAxisMarker.cs
179 lines (145 loc) · 4.76 KB
/
AxisMarker.cs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Created by SharpDevelop.
* User: NCDyson
* Date: 7/30/2017
* Time: 4:48 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Runtime.InteropServices;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Diagnostics;
namespace StudioCCS
{
/// <summary>
/// Description of AxisMarker.
/// </summary>
public static class AxisMarker
{
[StructLayout(LayoutKind.Sequential)]
public struct AxisVertex
{
public Vector3 Position;
public uint Color;
}
private static AxisVertex[] Vertices = null;
private static int VertexCount = 0;
private static bool WasInit = false;
private static int ArrayID = -1;
private static int BufferID = -1;
private static int ProgramID = -1;
private static int AttribPosition = -1;
private static int AttribColor = -1;
private static int UniMatrix = -1;
private static int UniScale = -1;
public static bool Init()
{
if(WasInit) return true;
//Load Vertices
ReadBin();
//Load Shader Program
ProgramID = Scene.LoadProgram("AxisMarker");
if(ProgramID == -1)
{
Logger.LogError("Error loading AxisMarker shader program");
return false;
}
AttribPosition = GL.GetAttribLocation(ProgramID, "Position");
AttribColor = GL.GetAttribLocation(ProgramID, "Color");
UniMatrix = GL.GetUniformLocation(ProgramID, "Matrix");
UniScale = GL.GetUniformLocation(ProgramID, "Scale");
if(AttribPosition == -1 || AttribColor == -1 || UniMatrix == -1 || UniScale == -1)
{
Logger.LogError("Error getting AxisVertex Shader Attribute/Uniform Locations:\n");
Logger.LogError(string.Format("\tPosition: {0}, Color: {1}, Matrix: {2}, Scale: {3}\n", AttribPosition, AttribColor, UniMatrix, UniScale));
return false;
}
//Deb
ArrayID = GL.GenVertexArray();
GL.BindVertexArray(ArrayID);
BufferID = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, BufferID);
Type vertexType = Vertices[0].GetType();
int vertexSize = Marshal.SizeOf(vertexType);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(VertexCount * vertexSize), Vertices, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(AttribPosition);
GL.VertexAttribPointer(AttribPosition, 3, VertexAttribPointerType.Float, false, vertexSize, Marshal.OffsetOf(vertexType, "Position"));
GL.EnableVertexAttribArray(AttribColor);
GL.VertexAttribPointer(AttribColor, 4, VertexAttribPointerType.UnsignedByte, true, vertexSize, Marshal.OffsetOf(vertexType, "Color"));
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
WasInit = true;
return WasInit;
}
private static void ReadBin()
{
using(var fs = new FileStream("data/bin/AxisMarker.bin", FileMode.Open))
{
using(var bs = new BinaryReader(fs))
{
VertexCount = bs.ReadInt32();
bs.ReadInt32();
bs.ReadInt32();
bs.ReadInt32();
if(Vertices == null || Vertices.Length != VertexCount)
{
Vertices = new AxisVertex[VertexCount];
}
for(int i = 0; i < VertexCount; i++)
{
float x, y, z;
uint col;
x = bs.ReadSingle();
y = bs.ReadSingle();
z = bs.ReadSingle();
col = bs.ReadUInt32();
Vertices[i] = new AxisVertex()
{
Position = new Vector3(x, y, z),
Color = col
};
}
}
}
}
public static void Reload()
{
ReadBin();
GL.BindBuffer(BufferTarget.ArrayBuffer, BufferID);
Type vertexType = Vertices[0].GetType();
int vertexSize = Marshal.SizeOf(vertexType);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(VertexCount * vertexSize), Vertices, BufferUsageHint.StaticDraw);
}
public static void DeInit()
{
if(ProgramID != -1) GL.DeleteProgram(ProgramID);
ProgramID = -1;
if(ArrayID != -1) GL.DeleteVertexArray(ArrayID);
ArrayID = -1;
if(BufferID != -1) GL.DeleteBuffer(BufferID);
BufferID = -1;
}
public static void Render(Matrix4 matrix, float scale)
{
if(!WasInit || ProgramID == -1) return;
var currentMode = (PolygonMode)GL.GetInteger(GetPName.PolygonMode);
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
//Matrix4 FinalMtx = matrix * Matrix4.Identity;
GL.UseProgram(ProgramID);
GL.BindVertexArray(ArrayID);
GL.BindBuffer(BufferTarget.ArrayBuffer, BufferID);
GL.UniformMatrix4(UniMatrix, false, ref matrix);
GL.Uniform1(UniScale, scale);
//GL.EnableVertexAttribArray(AttribPosition);
GL.DrawArrays(PrimitiveType.Triangles, 0, VertexCount);
//GL.DisableVertexAttribArray(AttribPosition);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.UseProgram(0);
GL.PolygonMode(MaterialFace.FrontAndBack, currentMode);
}
}
}