-
Notifications
You must be signed in to change notification settings - Fork 1
/
Map.cs
88 lines (76 loc) · 2.56 KB
/
Map.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace HLMapFileLoader
{
public class Map
{
private static Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>();
public static List<Brush> Load(string FileName, ContentManager contentManager)
{
List<Brush> brushes = FileParser.LoadBrushes(FileName);
SortVerticesCW(brushes);
LoadTextures(brushes, contentManager);
CalculateTextureCoordinates(brushes);
Inverse(brushes);
return brushes;
}
private static void Inverse(List<Brush> brushes)
{
foreach (Brush brush in brushes)
{
foreach (Polygon poly in brush.polygons)
{
for (int i = 0; i < poly.Vertices.Count; i++)
{
poly.Vertices[i] = new Vector3(poly.Vertices[i].X * -1, poly.Vertices[i].Y, poly.Vertices[i].Z);
}
}
}
}
private static void CalculateTextureCoordinates(List<Brush> brushes)
{
foreach (Brush brush in brushes)
{
foreach (Polygon poly in brush.polygons)
{
GeometryMath.CalculateTextureCoordinates(poly);
}
}
}
private static void SortVerticesCW(List<Brush> brushes)
{
foreach (Brush brush in brushes)
{
foreach (Polygon poly in brush.polygons)
{
GeometryMath.SortVerticesCW(poly);
}
}
}
private static void LoadTextures(List<Brush> brushes, ContentManager contentManager)
{
foreach (Brush brush in brushes)
{
foreach (Polygon poly in brush.polygons)
{
poly.Texture = GetTexture(poly.Face.TextureName, contentManager);
}
}
}
private static Texture2D GetTexture(string textureName, ContentManager contentManager)
{
if (textures.ContainsKey(textureName))
{
return textures[textureName];
}
else
{
Texture2D texture = contentManager.Load<Texture2D>(textureName);
textures.Add(textureName, texture);
return texture;
}
}
}
}