Initial tiles testing

This commit is contained in:
2017-02-25 00:40:41 -05:00
parent 234d1669dd
commit e473c9fb15
8 changed files with 165 additions and 61 deletions

View File

@@ -7,61 +7,124 @@ using OpenTK.Graphics.OpenGL4;
namespace hexworld namespace hexworld
{ {
public struct Vertex
{
public Vector3 Position;
public Vector2 UV;
public Vector3 Normal;
public Vertex(Vector3 position, Vector2 uv, Vector3 normal)
{
Position = position;
UV = uv;
Normal = normal;
}
}
public struct Tile
{
public Vector3 Position;
public Tile(Vector3 position)
{
Position = position;
}
}
public class HexRender : GameWindow public class HexRender : GameWindow
{ {
private Program pgm; private Program pgm;
private Texture tex1; private Texture grass;
private Texture stone;
private Texture tex2; private Texture tex2;
private Matrix4 view; private Matrix4 view;
private Matrix4 proj; private Matrix4 proj;
private readonly float[] verts = private readonly Vertex[] cubeVerts =
{ {
// +X // +X
+.5f, +.5f, -.5f, 1.0f, 0.5f, new Vertex(new Vector3(+.5f, +.5f, -.5f), new Vector2(1.0f, 0.5f), new Vector3(+1, +0, +0)),
+.5f, +.5f, +.5f, 1.0f, 0.0f, new Vertex(new Vector3(+.5f, +.5f, +.5f), new Vector2(1.0f, 0.0f), new Vector3(+1, +0, +0)),
+.5f, -.5f, +.5f, 0.5f, 0.0f, new Vertex(new Vector3(+.5f, -.5f, +.5f), new Vector2(0.5f, 0.0f), new Vector3(+1, +0, +0)),
+.5f, -.5f, +.5f, 0.5f, 0.0f, new Vertex(new Vector3(+.5f, -.5f, +.5f), new Vector2(0.5f, 0.0f), new Vector3(+1, +0, +0)),
+.5f, -.5f, -.5f, 0.5f, 0.5f, new Vertex(new Vector3(+.5f, -.5f, -.5f), new Vector2(0.5f, 0.5f), new Vector3(+1, +0, +0)),
+.5f, +.5f, -.5f, 1.0f, 0.5f, new Vertex(new Vector3(+.5f, +.5f, -.5f), new Vector2(1.0f, 0.5f), new Vector3(+1, +0, +0)),
// -X // -X
-.5f, +.5f, +.5f, 0.5f, 0.0f, new Vertex(new Vector3(-.5f, +.5f, +.5f), new Vector2(0.5f, 0.0f), new Vector3(-1, +0, +0)),
-.5f, +.5f, -.5f, 0.5f, 0.5f, new Vertex(new Vector3(-.5f, +.5f, -.5f), new Vector2(0.5f, 0.5f), new Vector3(-1, +0, +0)),
-.5f, -.5f, -.5f, 1.0f, 0.5f, new Vertex(new Vector3(-.5f, -.5f, -.5f), new Vector2(1.0f, 0.5f), new Vector3(-1, +0, +0)),
-.5f, -.5f, -.5f, 1.0f, 0.5f, new Vertex(new Vector3(-.5f, -.5f, -.5f), new Vector2(1.0f, 0.5f), new Vector3(-1, +0, +0)),
-.5f, -.5f, +.5f, 1.0f, 0.0f, new Vertex(new Vector3(-.5f, -.5f, +.5f), new Vector2(1.0f, 0.0f), new Vector3(-1, +0, +0)),
-.5f, +.5f, +.5f, 0.5f, 0.0f, new Vertex(new Vector3(-.5f, +.5f, +.5f), new Vector2(0.5f, 0.0f), new Vector3(-1, +0, +0)),
// +Y // +Y
+.5f, +.5f, -.5f, 0.5f, 0.5f, new Vertex(new Vector3(+.5f, +.5f, -.5f), new Vector2(0.5f, 0.5f), new Vector3(+0, +1, +0)),
-.5f, +.5f, -.5f, 1.0f, 0.5f, new Vertex(new Vector3(-.5f, +.5f, -.5f), new Vector2(1.0f, 0.5f), new Vector3(+0, +1, +0)),
-.5f, +.5f, +.5f, 1.0f, 0.0f, new Vertex(new Vector3(-.5f, +.5f, +.5f), new Vector2(1.0f, 0.0f), new Vector3(+0, +1, +0)),
-.5f, +.5f, +.5f, 1.0f, 0.0f, new Vertex(new Vector3(-.5f, +.5f, +.5f), new Vector2(1.0f, 0.0f), new Vector3(+0, +1, +0)),
+.5f, +.5f, +.5f, 0.5f, 0.0f, new Vertex(new Vector3(+.5f, +.5f, +.5f), new Vector2(0.5f, 0.0f), new Vector3(+0, +1, +0)),
+.5f, +.5f, -.5f, 0.5f, 0.5f, new Vertex(new Vector3(+.5f, +.5f, -.5f), new Vector2(0.5f, 0.5f), new Vector3(+0, +1, +0)),
// -Y // -Y
+.5f, -.5f, +.5f, 1.0f, 0.0f, new Vertex(new Vector3(+.5f, -.5f, +.5f), new Vector2(1.0f, 0.0f), new Vector3(+0, -1, +0)),
-.5f, -.5f, +.5f, 0.5f, 0.0f, new Vertex(new Vector3(-.5f, -.5f, +.5f), new Vector2(0.5f, 0.0f), new Vector3(+0, -1, +0)),
-.5f, -.5f, -.5f, 0.5f, 0.5f, new Vertex(new Vector3(-.5f, -.5f, -.5f), new Vector2(0.5f, 0.5f), new Vector3(+0, -1, +0)),
-.5f, -.5f, -.5f, 0.5f, 0.5f, new Vertex(new Vector3(-.5f, -.5f, -.5f), new Vector2(0.5f, 0.5f), new Vector3(+0, -1, +0)),
+.5f, -.5f, -.5f, 1.0f, 0.5f, new Vertex(new Vector3(+.5f, -.5f, -.5f), new Vector2(1.0f, 0.5f), new Vector3(+0, -1, +0)),
+.5f, -.5f, +.5f, 1.0f, 0.0f, new Vertex(new Vector3(+.5f, -.5f, +.5f), new Vector2(1.0f, 0.0f), new Vector3(+0, -1, +0)),
// +Z // +Z
+.5f, +.5f, +.5f, 0.5f, 0.0f, new Vertex(new Vector3(+.5f, +.5f, +.5f), new Vector2(0.5f, 0.0f), new Vector3(+0, +0, +1)),
-.5f, +.5f, +.5f, 0.0f, 0.0f, new Vertex(new Vector3(-.5f, +.5f, +.5f), new Vector2(0.0f, 0.0f), new Vector3(+0, +0, +1)),
-.5f, -.5f, +.5f, 0.0f, 0.5f, new Vertex(new Vector3(-.5f, -.5f, +.5f), new Vector2(0.0f, 0.5f), new Vector3(+0, +0, +1)),
-.5f, -.5f, +.5f, 0.0f, 0.5f, new Vertex(new Vector3(-.5f, -.5f, +.5f), new Vector2(0.0f, 0.5f), new Vector3(+0, +0, +1)),
+.5f, -.5f, +.5f, 0.5f, 0.5f, new Vertex(new Vector3(+.5f, -.5f, +.5f), new Vector2(0.5f, 0.5f), new Vector3(+0, +0, +1)),
+.5f, +.5f, +.5f, 0.5f, 0.0f, new Vertex(new Vector3(+.5f, +.5f, +.5f), new Vector2(0.5f, 0.0f), new Vector3(+0, +0, +1)),
// -Z // -Z
+.5f, +.5f, -.5f, 0.5f, 0.5f, new Vertex(new Vector3(+.5f, +.5f, -.5f), new Vector2(0.5f, 0.5f), new Vector3(+0, +0, -1)),
-.5f, +.5f, -.5f, 0.0f, 0.5f, new Vertex(new Vector3(-.5f, +.5f, -.5f), new Vector2(0.0f, 0.5f), new Vector3(+0, +0, -1)),
-.5f, -.5f, -.5f, 0.0f, 1.0f, new Vertex(new Vector3(-.5f, -.5f, -.5f), new Vector2(0.0f, 1.0f), new Vector3(+0, +0, -1)),
-.5f, -.5f, -.5f, 0.0f, 1.0f, new Vertex(new Vector3(-.5f, -.5f, -.5f), new Vector2(0.0f, 1.0f), new Vector3(+0, +0, -1)),
+.5f, -.5f, -.5f, 0.5f, 1.0f, new Vertex(new Vector3(+.5f, -.5f, -.5f), new Vector2(0.5f, 1.0f), new Vector3(+0, +0, -1)),
+.5f, +.5f, -.5f, 0.5f, 0.5f, new Vertex(new Vector3(+.5f, +.5f, -.5f), new Vector2(0.5f, 0.5f), new Vector3(+0, +0, -1)),
// Plane
new Vertex(new Vector3(+.5f, +.5f, 0.0f), new Vector2(0.5f, 0.0f), new Vector3(+0, +0, +1)),
new Vertex(new Vector3(-.5f, +.5f, 0.0f), new Vector2(0.0f, 0.0f), new Vector3(+0, +0, +1)),
new Vertex(new Vector3(-.5f, -.5f, 0.0f), new Vector2(0.0f, 0.5f), new Vector3(+0, +0, +1)),
new Vertex(new Vector3(-.5f, -.5f, 0.0f), new Vector2(0.0f, 0.5f), new Vector3(+0, +0, +1)),
new Vertex(new Vector3(+.5f, -.5f, 0.0f), new Vector2(0.5f, 0.5f), new Vector3(+0, +0, +1)),
new Vertex(new Vector3(+.5f, +.5f, 0.0f), new Vector2(0.5f, 0.0f), new Vector3(+0, +0, +1)),
}; };
private Tile[] tiles =
{
// Cubes
new Tile(new Vector3(-2, -2, 0)),
new Tile(new Vector3(-2, -1, 0)),
new Tile(new Vector3(-2, +0, 0)),
new Tile(new Vector3(-2, +1, 0)),
new Tile(new Vector3(-2, +2, 0)),
new Tile(new Vector3(+2, -2, 0)),
new Tile(new Vector3(+2, -1, 0)),
new Tile(new Vector3(+2, +0, 0)),
new Tile(new Vector3(+2, +1, 0)),
new Tile(new Vector3(+2, +2, 0)),
new Tile(new Vector3(-1, -2, 0)),
new Tile(new Vector3(-1, +2, 0)),
new Tile(new Vector3(+0, -2, 0)),
new Tile(new Vector3(+0, +2, 0)),
new Tile(new Vector3(+1, -2, 0)),
new Tile(new Vector3(+1, +2, 0)),
// Planes
new Tile(new Vector3(+0, +0, +1)),
new Tile(new Vector3(+0, +1, +1)),
new Tile(new Vector3(+0, -1, +1)),
new Tile(new Vector3(+1, +0, +1)),
new Tile(new Vector3(-1, +0, +1)),
};
private VBO tileVbo;
private VBO cubeVbo;
public HexRender(int width, int height) : base(width, height) public HexRender(int width, int height) : base(width, height)
{ {
Width = width; Width = width;
@@ -70,20 +133,35 @@ namespace hexworld
Y = (DisplayDevice.Default.Height - Height) / 2; Y = (DisplayDevice.Default.Height - Height) / 2;
} }
private Random rand = new Random();
protected override void OnUpdateFrame(FrameEventArgs e) protected override void OnUpdateFrame(FrameEventArgs e)
{ {
base.OnUpdateFrame(e); base.OnUpdateFrame(e);
view = Matrix4.LookAt(10 * Vector3.One, Vector3.Zero, Vector3.UnitZ); view = Matrix4.LookAt(10 * Vector3.One, Vector3.Zero, Vector3.UnitZ);
proj = Matrix4.CreateOrthographic(Width / 100f, Height / 100f, 0, 20); proj = Matrix4.CreateOrthographic(Width / 100f, Height / 100f, 0, 20);
for (var i = 0; i < 16; i++)
{
tiles[i].Position.Z += (float) ((rand.NextDouble() - .5) * e.Time);
}
// tileVbo.Data(tiles, BufferUsageHint.DynamicDraw);
tileVbo.Bind();
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr) (0), (IntPtr) (16 * 3 * sizeof(float)), tiles);
VBO.Unbind();
} }
protected override void OnLoad(EventArgs e) protected override void OnLoad(EventArgs e)
{ {
base.OnLoad(e); base.OnLoad(e);
var vbo = new VBO(); cubeVbo = new VBO();
vbo.Data(verts); cubeVbo.Data(cubeVerts, BufferUsageHint.StaticDraw);
tileVbo = new VBO();
tileVbo.Data(tiles, BufferUsageHint.DynamicDraw);
var vs = new Shader(ShaderType.VertexShader) var vs = new Shader(ShaderType.VertexShader)
{ {
@@ -105,17 +183,34 @@ namespace hexworld
pgm.Link(); pgm.Link();
Console.Out.WriteLine(pgm.Log); Console.Out.WriteLine(pgm.Log);
vbo.Bind(); var pos = pgm.GetAttribute("locpos");
GL.EnableVertexAttribArray(pgm.GetAttribute("pos")); var crd = pgm.GetAttribute("coord");
GL.VertexAttribPointer(pgm.GetAttribute("pos"), 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), var nrm = pgm.GetAttribute("norm");
var tpos = pgm.GetAttribute("glbpos");
GL.EnableVertexAttribArray(pos);
GL.EnableVertexAttribArray(crd);
GL.EnableVertexAttribArray(nrm);
GL.EnableVertexAttribArray(tpos);
cubeVbo.Bind();
GL.VertexAttribPointer(pos, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float),
0); 0);
GL.EnableVertexAttribArray(pgm.GetAttribute("coord")); GL.VertexAttribPointer(crd, 2, VertexAttribPointerType.Float, false, 8 * sizeof(float),
GL.VertexAttribPointer(pgm.GetAttribute("coord"), 2, VertexAttribPointerType.Float, false, 5 * sizeof(float),
3 * sizeof(float)); 3 * sizeof(float));
GL.VertexAttribPointer(nrm, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float),
5 * sizeof(float));
tileVbo.Bind();
GL.VertexAttribPointer(tpos, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.VertexAttribDivisor(tpos, 1);
VBO.Unbind(); VBO.Unbind();
tex1 = Texture.FromBitmap(new Bitmap("tex.png")); grass = Texture.FromBitmap(new Bitmap("grass.png"));
tex2 = Texture.FromBitmap(new Bitmap("tex2.png")); stone = Texture.FromBitmap(new Bitmap("stone.png"));
} }
protected override void OnRenderFrame(FrameEventArgs e) protected override void OnRenderFrame(FrameEventArgs e)
@@ -127,20 +222,22 @@ namespace hexworld
GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.DepthTest);
pgm.Use(); pgm.Use();
tex1.Bind(0);
tex2.Bind(1); grass.Bind(0);
stone.Bind(1);
GL.Uniform1(pgm.GetUniform("tex"), 0); GL.Uniform1(pgm.GetUniform("tex"), 0);
view = Matrix4.CreateTranslation(1,-1,0) * view;
GL.UniformMatrix4(pgm.GetUniform("view"), false, ref view); GL.UniformMatrix4(pgm.GetUniform("view"), false, ref view);
GL.UniformMatrix4(pgm.GetUniform("proj"), false, ref proj); GL.UniformMatrix4(pgm.GetUniform("proj"), false, ref proj);
GL.DrawArrays(PrimitiveType.Triangles, 0, 36); GL.DrawArraysInstancedBaseInstance(PrimitiveType.Triangles, 0, 36, 16, 0);
GL.Uniform1(pgm.GetUniform("tex"), 1); GL.Uniform1(pgm.GetUniform("tex"), 1);
view = Matrix4.CreateTranslation(-2,2,0) * view;
GL.UniformMatrix4(pgm.GetUniform("view"), false, ref view); GL.UniformMatrix4(pgm.GetUniform("view"), false, ref view);
GL.DrawArrays(PrimitiveType.Triangles, 0, 36); GL.UniformMatrix4(pgm.GetUniform("proj"), false, ref proj);
// GL.DrawArraysInstancedBaseInstance(PrimitiveType.Triangles, 36, 6, 5, 16);
GL.DrawArraysInstancedBaseInstance(PrimitiveType.Triangles, 0, 36, 5, 16);
SwapBuffers(); SwapBuffers();
} }

BIN
hexworld/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 B

View File

@@ -74,12 +74,10 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="tex.png"> <Content Include="grass.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> <Content Include="stone.png">
<ItemGroup>
<Content Include="tex2.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>

View File

@@ -1,10 +1,12 @@
#version 440 #version 440
in vec2 vcoord; in vec2 vcoord;
in float light;
uniform sampler2D tex; uniform sampler2D tex;
void main () void main ()
{ {
gl_FragColor = texture(tex, vcoord); vec4 color = light * texture(tex, vcoord);
gl_FragColor = clamp(color, 0, 1);
} }

View File

@@ -1,15 +1,22 @@
#version 440 #version 440
in vec3 pos; in vec3 locpos;
in vec3 glbpos;
in vec2 coord; in vec2 coord;
in vec3 norm;
out vec2 vcoord; out vec2 vcoord;
out float light;
uniform mat4 view; uniform mat4 view;
uniform mat4 proj; uniform mat4 proj;
void main () void main ()
{ {
gl_Position = proj*view*vec4(pos, 1); mat4 pv = proj * view;
vec3 position = glbpos + locpos;
gl_Position = pv * vec4(position, 1);
vcoord = coord; vcoord = coord;
light = dot(vec3(0, 0, 1), norm) / 2 + .5;
} }

BIN
hexworld/stone.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 596 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 718 B