-
Notifications
You must be signed in to change notification settings - Fork 0
/
EngineCore.cs
237 lines (195 loc) · 9.53 KB
/
EngineCore.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace WaterSimulation
{
public class EngineCore
{
public static Vector2 Dimensions;
public static Camera gameCamera;
public static int FPS = 60;
public static Skybox skybox;
public static Dictionary<String, FrameBuffer> frameBuffers = new Dictionary<string, FrameBuffer>();
public static Dictionary<String, Entity> gameObjects = new Dictionary<string, Entity>();
public static Dictionary<String, Mesh> meshes = new Dictionary<string, Mesh>();
public static Dictionary<String, Texture> images = new Dictionary<string, Texture>();
public static Dictionary<String, Material> materials = new Dictionary<string, Material>();
public static Dictionary<String, Shader> shaders = new Dictionary<string, Shader>();
public static Entity selected;
public static ShadowMap shadows;
public static void init()
{
shadows = new ShadowMap();
}
public static string AddFrameBuffer(String name, FrameBuffer fb)
{
frameBuffers.Add(name, fb);
return name;
}
public static string AddObject(String name, Entity obj)
{
gameObjects.Add(name, obj);
Console.WriteLine("Added an object called " + name);
return name;
}
public static string AddModel(string path, string name)
{
if (meshes.Keys.Contains(name))
throw new InvalidDataException("A mesh named " + name + " already exists in the engine memory");
CheckFilepath("Assets/Models/" + path);
string[] lines = { };
List<Vector3> vertices = new List<Vector3>();
List<Vector2> textures = new List<Vector2>();
List<Vector3> normals = new List<Vector3>();
List<int> indices = new List<int>();
//After parsing the data, re-sort the info so that all vertices have their data aligned
Vector2[] newTextures = new Vector2[1];
Vector3[] newNormals = new Vector3[1];
//Read in the .obj file
lines = File.ReadAllLines("Assets/Models/" + path);
foreach (string line in lines)
{
string[] currentLine = line.Split(' ');
if (line.StartsWith("v "))
vertices.Add(new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])));
else if (line.StartsWith("vt "))
textures.Add(new Vector2(float.Parse(currentLine[1]), float.Parse(currentLine[2])));
else if (line.StartsWith("vn "))
normals.Add(new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])));
else if (line.StartsWith("f "))
{
if (newTextures.Length == 1)
{
newTextures = new Vector2[vertices.Count];
newNormals = new Vector3[vertices.Count];
}
string[] vertex1 = currentLine[1].Split('/');
string[] vertex2 = currentLine[2].Split('/');
string[] vertex3 = currentLine[3].Split('/');
processVertex(vertex1, indices, textures, normals, newTextures, newNormals);
processVertex(vertex2, indices, textures, normals, newTextures, newNormals);
processVertex(vertex3, indices, textures, normals, newTextures, newNormals);
}
}
meshes.Add(name, new Mesh(vertices, newTextures.ToList(), newNormals.ToList(), indices));
return name;
}
private static void processVertex(string[] vertexData, List<int> indices, List<Vector2> textures, List<Vector3> normals, Vector2[] newTextures, Vector3[] newNormals)
{
int currentVertexPointer = int.Parse(vertexData[0]) - 1;
indices.Add(currentVertexPointer);
Vector2 currentTex = textures[int.Parse(vertexData[1]) - 1];
newTextures[currentVertexPointer] = currentTex;
Vector3 currentNorm = normals[int.Parse(vertexData[2]) - 1];
newNormals[currentVertexPointer] = currentNorm;
}
public static void AddSkybox(string[] textures, string name, int scale)
{
int id = GL.GenTexture();
GL.BindTexture(TextureTarget.TextureCubeMap, id);
for(int i = 0;i<6;i++)
{
AddImage(textures[i], name + i, false, id);
Texture tex = images[name + i];
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX + i, 0, PixelInternalFormat.Rgba, tex.data.Width, tex.data.Height,
0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, tex.data.Scan0);
}
skybox = new Skybox(scale, id);
}
public static string AddImage(string path, string name, bool unique = true, int parentID = -1)
{
CheckFilepath("Assets/Images/" + path);
//Read in bitmap data and process with alpha channels
Bitmap bmp = new Bitmap("Assets/Images/" + path);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Texture tex;
if (unique)
{
//Create a texture object in OpenGL and bind it to read in bitmap data
int id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
tex = new Texture(id, bmp.Width, bmp.Height);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmpData.Width, bmpData.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0);
}
else
{
tex = new Texture(parentID, bmp.Width, bmp.Height);
}
tex.data = bmpData;
images.Add(name, tex);
return name;
}
public static void AddTexture(Texture tex, string name)
{
images.Add(name, tex);
}
public static string AddShader(Shader shader, string name)
{
shaders.Add(name, shader);
return name;
}
public static void Render()
{
foreach (Entity obj in gameObjects.Values)
obj.Render();
skybox.Render();
}
public static void RenderWithout(Entity excludedObj)
{
foreach (Entity obj in gameObjects.Values)
{
if(obj != excludedObj)
obj.Render();
}
skybox.Render();
}
/// <summary>
/// Make sure that no attempts at file IO include invalid file names
/// </summary>
/// <param name="path">The path to the file using Debug as the base directory</param>
/// <exception cref="FileNotFoundException">Thrown when a file path does not exist</exception>
public static void CheckFilepath(String path)
{
if (!File.Exists(path))
throw new FileNotFoundException("File not found at '" + path + "'");
}
/// <summary>
/// Check before a new item is added to a collection if that item was already in that collection
/// </summary>
/// <param name="data">The collection to check</param>
/// <param name="obj">The new term</param>
/// <exception cref="DuplicateNameException">Thrown when another item is added to a collection that already has that item</exception>
public static void CheckDuplicateName(ICollection<string> data, string obj)
{
if (data.Contains(obj))
throw new DuplicateNameException("Another item in this list is already named " + obj);
}
}
#region Custom exceptions to be used in the engine
public class EngineException : Exception
{
public EngineException(string message)
{
Console.Error.WriteLine(message);
Console.Error.WriteLine(Source);
Console.Error.WriteLine(StackTrace);
}
}
public class DuplicateNameException : EngineException { public DuplicateNameException(string message) : base(message) { } }
public class MissingShaderUniformException : EngineException { public MissingShaderUniformException(string message) : base(message) { } }
#endregion
}