Updates to Level. More functionality, but also very unstable.

This commit is contained in:
2017-02-27 23:11:14 -05:00
parent 8566105158
commit 1824de6842
10 changed files with 756 additions and 429 deletions

View File

@@ -13,7 +13,7 @@ namespace Diamond
/// <summary>
/// Logger for this class
/// </summary>
protected Logger Log { get; private set; }
protected Logger Log { get; }
/// <summary>
/// The name of this object

View File

@@ -12,8 +12,10 @@ using OpenTK.Graphics.OpenGL4;
namespace Diamond.Level
{
public class Level
public class Level : IDisposable
{
public Dictionary<string, Program> Programs { get; private set; }
private TileData[] _allTiles;
private ObjVertex[] _allVertices;
@@ -39,9 +41,9 @@ namespace Diamond.Level
var dir = Path.GetDirectoryName(file);
// this is horrendous, but not as bad as trying to directly deserialize it.
var meshes = levelData["models"]
.Select(path => Mesh.FromObj(Path.Combine(dir, (string) path)))
.Select(path => Mesh.FromObj(Path.Combine(dir, (string) path), false))
.SelectMany(objects => objects)
.ToArray();
@@ -50,20 +52,32 @@ namespace Diamond.Level
var allVertices = Mesh.Join(meshes);
var programs = levelData["shaders"]
.Select(shader => new
{
name = (string) shader["name"],
program = Program.FromFiles(
shader["files"]
.Select(path => Path.Combine(dir, (string) path))
.ToArray())
})
.ToDictionary(s => s.name, s => s.program);
var tilegroups = levelData["tiles"]
.Select(tile => new
{
info = new
{
mesh = meshDict[(string) tile["mesh"]]
mesh = meshDict[(string) tile["mesh"]],
shader = programs[(string) tile["shader"]]
},
pos = tile["pos"].ToObject<Vector3>()
})
.GroupBy(tile => tile.info)
.Select(group => new TileGroup(new SubArray<TileData>(
.Select(group => new TileGroup(group.Key.mesh, group.Key.shader,
new SubArray<TileData>(
group.Select(data => new TileData(data.pos))
.ToArray()),
group.Key.mesh))
.ToArray())))
.ToArray();
var tileArrays = tilegroups
@@ -76,7 +90,8 @@ namespace Diamond.Level
_allTiles = allTiles,
_allVertices = allVertices,
_meshes = meshes,
_tileGroups = tilegroups
_tileGroups = tilegroups,
Programs = programs
};
level.InitializeBuffers();
@@ -86,16 +101,30 @@ namespace Diamond.Level
public void Draw()
{
if (Program.Current == null)
throw new Exception("cant render without a shader.");
Program.Current.SetAttribPointers(_vertexBuffer);
Program.Current.SetAttribPointers(_tileBuffer);
foreach (var tileGroup in _tileGroups)
{
var pgm = tileGroup.Program;
pgm.Use();
pgm.SetAttribPointers(_vertexBuffer);
pgm.SetAttribPointers(_tileBuffer);
tileGroup.Mesh.DrawInstanced(tileGroup.Tiles);
}
}
public void Dispose()
{
_tileBuffer?.Dispose();
_vertexBuffer?.Dispose();
foreach (var program in Programs.Values)
program?.Dispose();
GC.SuppressFinalize(this);
}
~Level()
{
Dispose();
}
}
}

View File

@@ -1,4 +1,5 @@
using Diamond.Buffers;
using Diamond.Shaders;
namespace Diamond.Level
{
@@ -6,11 +7,13 @@ namespace Diamond.Level
{
public SubArray<TileData> Tiles;
public Mesh<ObjVertex> Mesh;
public Program Program;
public TileGroup(SubArray<TileData> tiles, Mesh<ObjVertex> mesh)
public TileGroup(Mesh<ObjVertex> mesh, Program program, SubArray<TileData> tiles)
{
Tiles = tiles;
Mesh = mesh;
Program = program;
Tiles = tiles;
}
}
}

View File

@@ -61,7 +61,7 @@ namespace Diamond
public static class Mesh
{
public static Mesh<ObjVertex>[] FromObj(string file)
public static Mesh<ObjVertex>[] FromObj(string file, bool join = true)
{
var lines = File.ReadAllLines(file);
@@ -125,7 +125,8 @@ namespace Diamond
if (faces.Count > 0)
meshes.Add(new Mesh<ObjVertex>(faces.ToArray()) {Name = name});
Join(meshes);
if (join)
Join(meshes);
return meshes.ToArray();
}

View File

@@ -15,11 +15,10 @@ namespace hexworld
#region GLObjects
private Program _objPgm;
private Texture _grass;
private Texture _stone;
private Texture _gray;
private Texture _door;
private Level _level;
@@ -27,13 +26,12 @@ namespace hexworld
{
base.OnClosed(e);
_objPgm?.Dispose();
_grass?.Dispose();
_stone?.Dispose();
_gray?.Dispose();
_door?.Dispose();
// _level?.Dispose();
_level?.Dispose();
}
#endregion
@@ -47,7 +45,7 @@ namespace hexworld
public HexRender(int width, int height)
: base(width, height, new GraphicsMode(32, 24, 0, 8))
: base(width, height, new GraphicsMode(32, 24, 0, 0))
{
Width = width;
Height = Height;
@@ -59,13 +57,12 @@ namespace hexworld
{
base.OnLoad(e);
_objPgm = Program.FromFiles(@"res\obj.vs.glsl", @"res\obj.fs.glsl");
_level = Level.LoadLevel(@"res\level.json");
_grass = Texture.FromBitmap(new Bitmap(@"res\grass.png"));
_stone = Texture.FromBitmap(new Bitmap(@"res\stone.png"));
_gray = Texture.FromBitmap(new Bitmap(@"res\gray.png"));
_door = Texture.FromBitmap(new Bitmap(@"res\door.png"));
}
protected override void OnUpdateFrame(FrameEventArgs e)
@@ -92,23 +89,19 @@ namespace hexworld
GL.DepthFunc(DepthFunction.Lequal);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
if (_objPgm.Linked)
{
_objPgm.Use();
_grass.Bind(0);
_stone.Bind(1);
_gray.Bind(2);
_door.Bind(3);
_grass.Bind(0);
_stone.Bind(1);
_gray.Bind(2);
GL.Uniform1(_objPgm.GetUniform("tex"), 2);
GL.UniformMatrix4(_objPgm.GetUniform("view"), false, ref _view);
GL.UniformMatrix4(_objPgm.GetUniform("proj"), false, ref _proj);
_level.Draw();
}
var pgm = _level.Programs["textured"];
pgm.Use();
GL.Uniform1(pgm.GetUniform("tex"), 0);
GL.UniformMatrix4(pgm.GetUniform("view"), false, ref _view);
GL.UniformMatrix4(pgm.GetUniform("proj"), false, ref _proj);
_level.Draw();
SwapBuffers();
}

View File

@@ -59,6 +59,9 @@
<None Include="App.config" />
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
<None Include="res\cube.obj">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="res\door.obj">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
@@ -77,6 +80,9 @@
</None>
</ItemGroup>
<ItemGroup>
<Content Include="res\door.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="res\grass.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

52
hexworld/res/cube.obj Normal file
View File

@@ -0,0 +1,52 @@
# Blender v2.78 (sub 0) OBJ File: ''
# www.blender.org
o Cube
v 0.500050 -0.500050 -0.500050
v 0.500050 -0.500050 0.500050
v -0.500050 -0.500050 -0.500050
v -0.500050 -0.500050 0.500050
v 0.500050 0.500050 -0.500050
v 0.500050 0.500050 0.500050
v -0.500050 0.500050 -0.500050
v -0.500050 0.500050 0.500050
vt 1.0000 1.0000
vt 0.5000 0.5000
vt 1.0000 0.5000
vt 1.0000 1.0000
vt 0.5000 0.5000
vt 1.0000 0.5000
vt 1.0000 1.0000
vt 0.5000 0.5000
vt 1.0000 0.5000
vt 1.0000 1.0000
vt 0.5000 0.5000
vt 1.0000 0.5000
vt 0.0000 -0.0000
vt 0.5000 0.0000
vt 0.5000 1.0000
vt -0.0000 0.5000
vt 0.5000 0.5000
vt 0.5000 1.0000
vt 0.5000 1.0000
vt 0.5000 1.0000
vt -0.0000 0.5000
vt -0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 0.0000 0.0000 1.0000
s off
f 2/1/1 3/2/1 1/3/1
f 4/4/2 7/5/2 3/6/2
f 8/7/3 5/8/3 7/9/3
f 6/10/4 1/11/4 5/12/4
f 7/5/5 1/13/5 3/14/5
f 4/15/6 6/16/6 8/17/6
f 2/1/1 4/15/1 3/2/1
f 4/4/2 8/18/2 7/5/2
f 8/7/3 6/19/3 5/8/3
f 6/10/4 2/20/4 1/11/4
f 7/5/5 5/21/5 1/13/5
f 4/15/6 2/22/6 6/16/6

File diff suppressed because it is too large Load Diff

BIN
hexworld/res/door.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 B

View File

@@ -1,17 +1,52 @@
{
"models": [
"door.obj"
"door.obj",
"cube.obj"
],
"shaders": [
{
"name": "textured",
"files": [
"obj.fs.glsl",
"obj.vs.glsl",
]
}
],
"tiles": [
{ "mesh": "BaseLeft", "pos": { "x": -1, "y": 0, "z": 0 } },
{ "mesh": "BaseRight", "pos": { "x": 1, "y": 0, "z": 0 } },
{ "mesh": "Stairs", "pos": { "x": 0, "y": 0, "z": 0 } },
{ "mesh": "ColLeft", "pos": { "x": -1, "y": 0, "z": 1 } },
{ "mesh": "ColRight", "pos": { "x": 1, "y": 0, "z": 1 } },
{ "mesh": "ColLeft", "pos": { "x": -1, "y": 0, "z": 2 } },
{ "mesh": "ColRight", "pos": { "x": 1, "y": 0, "z": 2 } },
{ "mesh": "CapLeft", "pos": { "x": -1, "y": 0, "z": 3 } },
{ "mesh": "Lintel", "pos": { "x": 0, "y": 0, "z": 3 } },
{ "mesh": "CapRight", "pos": { "x": 1, "y": 0, "z": 3 } }
{ "mesh": "BaseLeft", "shader": "textured", "pos": { "x": -1, "y": 0, "z": 0 } },
{ "mesh": "BaseRight", "shader": "textured", "pos": { "x": 1, "y": 0, "z": 0 } },
{ "mesh": "Stairs", "shader": "textured", "pos": { "x": 0, "y": 0, "z": 0 } },
{ "mesh": "ColLeft", "shader": "textured", "pos": { "x": -1, "y": 0, "z": 1 } },
{ "mesh": "ColRight", "shader": "textured", "pos": { "x": 1, "y": 0, "z": 1 } },
{ "mesh": "ColLeft", "shader": "textured", "pos": { "x": -1, "y": 0, "z": 2 } },
{ "mesh": "ColRight", "shader": "textured", "pos": { "x": 1, "y": 0, "z": 2 } },
{ "mesh": "CapLeft", "shader": "textured", "pos": { "x": -1, "y": 0, "z": 3 } },
{ "mesh": "Lintel", "shader": "textured", "pos": { "x": 0, "y": 0, "z": 3 } },
{ "mesh": "CapRight", "shader": "textured", "pos": { "x": 1, "y": 0, "z": 3 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -2, "y": -2, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -2, "y": -1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -2, "y": 0, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -2, "y": 1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -2, "y": 2, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -1, "y": -2, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -1, "y": -1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -1, "y": 0, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -1, "y": 1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": -1, "y": 2, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 0, "y": -2, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 0, "y": -1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 0, "y": 0, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 0, "y": 1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 0, "y": 2, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 1, "y": -2, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 1, "y": -1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 1, "y": 0, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 1, "y": 1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 1, "y": 2, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 2, "y": -2, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 2, "y": -1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 2, "y": 0, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 2, "y": 1, "z": -1 } },
{ "mesh": "Cube", "shader": "textured", "pos": { "x": 2, "y": 2, "z": -1 } },
]
}