forked from NCDyson/StudioCCS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.cs
155 lines (128 loc) · 4.27 KB
/
Grid.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
/*
* Created by SharpDevelop.
* User: NCDyson
* Date: 7/31/2017
* Time: 10:18 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace StudioCCS
{
/// <summary>
/// Description of Grid.
/// </summary>
public static class Grid
{
public static bool WasInit = false;
public static Vector3[] Vertices;
public static int VertexCount = 0;
public static int ProgramID = -1;
public static int AttribPosition = -1;
public static int UniformMatrix = -1;
public static int UniformColor = -1;
public static int UniformScale = -1;
public static int ArrayID = -1;
public static int BufferID = -1;
public static int GridSize = 0;
public static float GridSpacing = 1.0f;
public static bool Init()
{
if(WasInit) return true;
//Init Vertices based on default sizing/spacing
InitGridVerts(16);
ProgramID = Scene.LoadProgram("Grid");
if(ProgramID == -1)
{
Logger.LogError("Error loading Grid shader program");
return false;
}
AttribPosition = GL.GetAttribLocation(ProgramID, "Position");
UniformMatrix = GL.GetUniformLocation(ProgramID, "Matrix");
UniformColor = GL.GetUniformLocation(ProgramID, "Color");
UniformScale = GL.GetUniformLocation(ProgramID, "Scale");
if(AttribPosition == -1 || UniformMatrix == -1 || UniformColor == -1 || UniformScale == -1)
{
Logger.LogError("Error getting Grid Shader Attribute/Uniform Locations:\n");
Logger.LogError(string.Format("\tPosition: {0}, Matrix: {1}, Color: {2}, Scale: {3}", AttribPosition, UniformMatrix, UniformColor, UniformScale));
return false;
}
ArrayID = GL.GenVertexArray();
GL.BindVertexArray(ArrayID);
BufferID = GL.GenBuffer();
UpdateGridVerts(false);
Type vertexType = Vertices[0].GetType();
int vertexSize = Marshal.SizeOf(vertexType);
GL.EnableVertexAttribArray(AttribPosition);
GL.VertexAttribPointer(AttribPosition, 3, VertexAttribPointerType.Float, false, vertexSize, IntPtr.Zero);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
WasInit = true;
return true;
}
private static void InitGridVerts(int _gridSize)
{
//Grid Size = how many on each side of axis
//A Grid Size of 0 is just the center lines
int gridLines = (_gridSize * 2) + 1;
GridSize = _gridSize;
VertexCount = gridLines * 4;
Vertices = new Vector3[VertexCount];
Debug.WriteLine("Grid Initialized with {0} vertices...", VertexCount);
float startY = -GridSize;
float startX = -GridSize;
int vCount = 0;
//Do Horizontal Lines
for(int y = 0; y < gridLines; y++)
{
float vertY = startY + y;
Vertices[vCount] = new Vector3(startX, 0.0f, vertY);
Vertices[vCount + 1] = new Vector3(-startX, 0.0f, vertY);
vCount += 2;
}
//Do Vertical Lines
for(int x = 0; x < gridLines; x++)
{
float vertX = startX + x;
Vertices[vCount] = new Vector3(vertX, 0.0f, startY);
Vertices[vCount + 1] = new Vector3(vertX, 0.0f, -startY);
vCount += 2;
}
}
//TODO: Grid::UpdateGridVerts(): Better Naming
private static void UpdateGridVerts(bool unbindAfter = true)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, BufferID);
Type vertexType = Vertices[0].GetType();
int vertexSize = Marshal.SizeOf(vertexType);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertexSize * VertexCount), Vertices, BufferUsageHint.StaticDraw);
if(unbindAfter) GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
}
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;
GL.UseProgram(ProgramID);
GL.BindVertexArray(ArrayID);
GL.Uniform1(UniformScale, scale);
GL.Uniform4(UniformColor, 0.5f, 0.5f, 0.5f, 1.0f);
GL.UniformMatrix4(UniformMatrix, false, ref matrix);
GL.DrawArrays(PrimitiveType.Lines, 0, VertexCount);
GL.BindVertexArray(0);
GL.UseProgram(0);
}
}
}