diff --git a/hexworld/App.config b/hexworld/App.config index 8d23437..bae5d6d 100644 --- a/hexworld/App.config +++ b/hexworld/App.config @@ -1,6 +1,6 @@ - + diff --git a/hexworld/Driver.cs b/hexworld/Driver.cs index a6db25f..966bbd3 100644 --- a/hexworld/Driver.cs +++ b/hexworld/Driver.cs @@ -11,10 +11,7 @@ namespace hexworld { public static void Main(string[] args) { - using (var gw = new HexWindow(1280, 720)) - { - gw.Run(); - } + using (var gw = new HexRender(1280, 720)) gw.Run(); } } } \ No newline at end of file diff --git a/hexworld/Game.cs b/hexworld/Game.cs deleted file mode 100644 index c000e0e..0000000 --- a/hexworld/Game.cs +++ /dev/null @@ -1,168 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Imaging; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using hexworld.Util; -using OpenTK; -using OpenTK.Graphics; -using OpenTK.Graphics.OpenGL4; -using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat; - -namespace hexworld -{ - public struct Vert - { - public static readonly int SizeInBytes = sizeof(float) * 5; - - public Vector3 Point; - public Vector2 TexCoord; - - public Vert(Vector3 point, Vector2 texCoord) - { - Point = point; - TexCoord = texCoord; - } - - public Vert(float px, float py, float pz, float tx, float ty) - : this(new Vector3(px, py, pz), new Vector2(tx, ty)) - { - } - } - - public class HexWindow : GameWindow - { - private readonly Vert[] _verts = - { - // +X - new Vert(+.5f, +.5f, -.5f, 1.0f, 0.5f), new Vert(+.5f, +.5f, +.5f, 1.0f, 0.0f), - new Vert(+.5f, -.5f, +.5f, 0.5f, 0.0f), - new Vert(+.5f, -.5f, +.5f, 0.5f, 0.0f), new Vert(+.5f, -.5f, -.5f, 0.5f, 0.5f), - new Vert(+.5f, +.5f, -.5f, 1.0f, 0.5f), - // -X - new Vert(-.5f, +.5f, +.5f, 0.5f, 0.0f), new Vert(-.5f, +.5f, -.5f, 0.5f, 0.5f), - new Vert(-.5f, -.5f, -.5f, 1.0f, 0.5f), - new Vert(-.5f, -.5f, -.5f, 1.0f, 0.5f), new Vert(-.5f, -.5f, +.5f, 1.0f, 0.0f), - new Vert(-.5f, +.5f, +.5f, 0.5f, 0.0f), - // +Y - new Vert(+.5f, +.5f, -.5f, 0.5f, 0.5f), new Vert(-.5f, +.5f, -.5f, 1.0f, 0.5f), - new Vert(-.5f, +.5f, +.5f, 1.0f, 0.0f), - new Vert(-.5f, +.5f, +.5f, 1.0f, 0.0f), new Vert(+.5f, +.5f, +.5f, 0.5f, 0.0f), - new Vert(+.5f, +.5f, -.5f, 0.5f, 0.5f), - // -Y - new Vert(+.5f, -.5f, +.5f, 1.0f, 0.0f), new Vert(-.5f, -.5f, +.5f, 0.5f, 0.0f), - new Vert(-.5f, -.5f, -.5f, 0.5f, 0.5f), - new Vert(-.5f, -.5f, -.5f, 0.5f, 0.5f), new Vert(+.5f, -.5f, -.5f, 1.0f, 0.5f), - new Vert(+.5f, -.5f, +.5f, 1.0f, 0.0f), - // +Z - new Vert(+.5f, +.5f, +.5f, 0.5f, 0.0f), new Vert(-.5f, +.5f, +.5f, 0.0f, 0.0f), - new Vert(-.5f, -.5f, +.5f, 0.0f, 0.5f), - new Vert(-.5f, -.5f, +.5f, 0.0f, 0.5f), new Vert(+.5f, -.5f, +.5f, 0.5f, 0.5f), - new Vert(+.5f, +.5f, +.5f, 0.5f, 0.0f), - // -Z - new Vert(+.5f, +.5f, -.5f, 0.5f, 0.5f), new Vert(-.5f, +.5f, -.5f, 0.0f, 0.5f), - new Vert(-.5f, -.5f, -.5f, 0.0f, 1.0f), - new Vert(-.5f, -.5f, -.5f, 0.0f, 1.0f), new Vert(+.5f, -.5f, -.5f, 0.5f, 1.0f), - new Vert(+.5f, +.5f, -.5f, 0.5f, 0.5f), - }; - - private Matrix4 _view = Matrix4.Identity; - private Matrix4 _proj = Matrix4.Identity; - - private Program _pgm; - - private Texture _tex1; - private Texture _tex2; - - public HexWindow(int width, int height) - : base( - width, height, GraphicsMode.Default, "Hexworld", GameWindowFlags.Default, DisplayDevice.Default, 4, 4, - GraphicsContextFlags.Debug) - { - GL.GetInteger(GetPName.MajorVersion, out int major); - GL.GetInteger(GetPName.MinorVersion, out int minor); - Console.Out.WriteLine($"GL {major}.{minor}"); - - Width = width; - Height = height; - X = (DisplayDevice.Default.Width - Width) / 2; - Y = (DisplayDevice.Default.Height - Height) / 2; - } - - - protected override void OnUpdateFrame(FrameEventArgs e) - { - base.OnUpdateFrame(e); - - const float s = 200f; - _view = Matrix4.LookAt(10 * Vector3.One, Vector3.Zero, Vector3.UnitZ); - _proj = Matrix4.CreateOrthographic(Width / s, Height / s, 0, 100); - } - - protected override void OnLoad(EventArgs e) - { - base.OnLoad(e); - - var vbo = new VBO(); - vbo.Data(_verts); - - var vs = new Shader(ShaderType.VertexShader) {Source = File.ReadAllText("s.vs.glsl")}; - if (!vs.Compile()) - Console.Out.WriteLine($"vs: {vs.Log}"); - - var fs = new Shader(ShaderType.FragmentShader) {Source = File.ReadAllText("s.fs.glsl")}; - if (!fs.Compile()) - Console.Out.WriteLine($"fs: {fs.Log}"); - - _pgm = new Program(); - _pgm.Attach(vs); - _pgm.Attach(fs); - if (!_pgm.Link()) - Console.Out.WriteLine($"pgm: {_pgm.Log}"); - - vbo.Bind(); - GL.EnableVertexAttribArray(0); - GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vert.SizeInBytes, 0); - GL.EnableVertexAttribArray(1); - GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, Vert.SizeInBytes, Vector3.SizeInBytes); - VBO.Unbind(); - - _tex1 = Texture.FromBitmap(new Bitmap("tex.png")); - - _tex2 = Texture.FromBitmap(new Bitmap("tex2.png")); - } - - protected override void OnRenderFrame(FrameEventArgs e) - { - base.OnRenderFrame(e); - - GL.Viewport(ClientRectangle); - GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); - - GL.Enable(EnableCap.DepthTest); - - _pgm.Use(); - - _tex1.Bind(0); - _tex2.Bind(1); - - GL.Uniform1(_pgm.GetUniform("tex"), 0); - - GL.UniformMatrix4(_pgm.GetUniform("view"), false, ref _view); - GL.UniformMatrix4(_pgm.GetUniform("proj"), false, ref _proj); - - GL.DrawArrays(PrimitiveType.Triangles, 0, _verts.Length); - - GL.Uniform1(_pgm.GetUniform("tex"), 1); - _view = Matrix4.CreateTranslation(0, 2, 0) * _view; - GL.UniformMatrix4(_pgm.GetUniform("view"), false, ref _view); - - GL.DrawArrays(PrimitiveType.Triangles, 0, _verts.Length); - - SwapBuffers(); - } - } -} \ No newline at end of file diff --git a/hexworld/HexRender.cs b/hexworld/HexRender.cs new file mode 100644 index 0000000..2bcdba2 --- /dev/null +++ b/hexworld/HexRender.cs @@ -0,0 +1,148 @@ +using System; +using System.Drawing; +using System.IO; +using hexworld.Util; +using OpenTK; +using OpenTK.Graphics.OpenGL4; + +namespace hexworld +{ + public class HexRender : GameWindow + { + private Program pgm; + private Texture tex1; + private Texture tex2; + + private Matrix4 view; + private Matrix4 proj; + + private readonly float[] verts = + { + // +X + +.5f, +.5f, -.5f, 1.0f, 0.5f, + +.5f, +.5f, +.5f, 1.0f, 0.0f, + +.5f, -.5f, +.5f, 0.5f, 0.0f, + +.5f, -.5f, +.5f, 0.5f, 0.0f, + +.5f, -.5f, -.5f, 0.5f, 0.5f, + +.5f, +.5f, -.5f, 1.0f, 0.5f, + // -X + -.5f, +.5f, +.5f, 0.5f, 0.0f, + -.5f, +.5f, -.5f, 0.5f, 0.5f, + -.5f, -.5f, -.5f, 1.0f, 0.5f, + -.5f, -.5f, -.5f, 1.0f, 0.5f, + -.5f, -.5f, +.5f, 1.0f, 0.0f, + -.5f, +.5f, +.5f, 0.5f, 0.0f, + // +Y + +.5f, +.5f, -.5f, 0.5f, 0.5f, + -.5f, +.5f, -.5f, 1.0f, 0.5f, + -.5f, +.5f, +.5f, 1.0f, 0.0f, + -.5f, +.5f, +.5f, 1.0f, 0.0f, + +.5f, +.5f, +.5f, 0.5f, 0.0f, + +.5f, +.5f, -.5f, 0.5f, 0.5f, + // -Y + +.5f, -.5f, +.5f, 1.0f, 0.0f, + -.5f, -.5f, +.5f, 0.5f, 0.0f, + -.5f, -.5f, -.5f, 0.5f, 0.5f, + -.5f, -.5f, -.5f, 0.5f, 0.5f, + +.5f, -.5f, -.5f, 1.0f, 0.5f, + +.5f, -.5f, +.5f, 1.0f, 0.0f, + // +Z + +.5f, +.5f, +.5f, 0.5f, 0.0f, + -.5f, +.5f, +.5f, 0.0f, 0.0f, + -.5f, -.5f, +.5f, 0.0f, 0.5f, + -.5f, -.5f, +.5f, 0.0f, 0.5f, + +.5f, -.5f, +.5f, 0.5f, 0.5f, + +.5f, +.5f, +.5f, 0.5f, 0.0f, + // -Z + +.5f, +.5f, -.5f, 0.5f, 0.5f, + -.5f, +.5f, -.5f, 0.0f, 0.5f, + -.5f, -.5f, -.5f, 0.0f, 1.0f, + -.5f, -.5f, -.5f, 0.0f, 1.0f, + +.5f, -.5f, -.5f, 0.5f, 1.0f, + +.5f, +.5f, -.5f, 0.5f, 0.5f, + }; + + public HexRender(int width, int height) : base(width, height) + { + Width = width; + Height = Height; + X = (DisplayDevice.Default.Width - Width) / 2; + Y = (DisplayDevice.Default.Height - Height) / 2; + } + + protected override void OnUpdateFrame(FrameEventArgs e) + { + base.OnUpdateFrame(e); + + view = Matrix4.LookAt(10 * Vector3.One, Vector3.Zero, Vector3.UnitZ); + proj = Matrix4.CreateOrthographic(Width / 100f, Height / 100f, 0, 20); + } + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + var vbo = new VBO(); + vbo.Data(verts); + + var vs = new Shader(ShaderType.VertexShader) + { + Source = File.ReadAllText("s.vs.glsl") + }; + vs.Compile(); + Console.Out.WriteLine(vs.Log); + + var fs = new Shader(ShaderType.FragmentShader) + { + Source = File.ReadAllText("s.fs.glsl") + }; + fs.Compile(); + Console.Out.WriteLine(fs.Log); + + pgm = new Program(); + pgm.Attach(vs); + pgm.Attach(fs); + pgm.Link(); + Console.Out.WriteLine(pgm.Log); + + vbo.Bind(); + GL.EnableVertexAttribArray(pgm.GetAttribute("pos")); + GL.VertexAttribPointer(pgm.GetAttribute("pos"), 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), + 0); + GL.EnableVertexAttribArray(pgm.GetAttribute("coord")); + GL.VertexAttribPointer(pgm.GetAttribute("coord"), 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), + 3 * sizeof(float)); + VBO.Unbind(); + + tex1 = Texture.FromBitmap(new Bitmap("tex.png")); + tex2 = Texture.FromBitmap(new Bitmap("tex2.png")); + } + + protected override void OnRenderFrame(FrameEventArgs e) + { + base.OnRenderFrame(e); + + GL.Viewport(ClientRectangle); + GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); + GL.Enable(EnableCap.DepthTest); + + pgm.Use(); + tex1.Bind(0); + tex2.Bind(1); + + 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("proj"), false, ref proj); + + GL.DrawArrays(PrimitiveType.Triangles, 0, 36); + + GL.Uniform1(pgm.GetUniform("tex"), 1); + view = Matrix4.CreateTranslation(-2,2,0) * view; + GL.UniformMatrix4(pgm.GetUniform("view"), false, ref view); + GL.DrawArrays(PrimitiveType.Triangles, 0, 36); + + SwapBuffers(); + } + } +} \ No newline at end of file diff --git a/hexworld/Util/Program.cs b/hexworld/Util/Program.cs index 74dda95..3119c58 100644 --- a/hexworld/Util/Program.cs +++ b/hexworld/Util/Program.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; -using System.Threading.Tasks; using OpenTK.Graphics.OpenGL4; namespace hexworld.Util @@ -53,7 +51,14 @@ namespace hexworld.Util { if (!uniforms.TryGetValue(name, out int id)) throw new ShaderException($"Shader Program {Id} does not contain uniform '{name}'"); - else return id; + return id; + } + + public int GetAttribute(string name) + { + if (!attributes.TryGetValue(name, out int id)) + throw new ShaderException($"Shader Program {Id} does not contain attribute '{name}'"); + return id; } public void Use() => GL.UseProgram(Id); diff --git a/hexworld/hexworld.csproj b/hexworld/hexworld.csproj index 076b8d0..439710c 100644 --- a/hexworld/hexworld.csproj +++ b/hexworld/hexworld.csproj @@ -8,7 +8,7 @@ Exe hexworld hexworld - v4.6.2 + v4.6.1 512 true @@ -51,8 +51,8 @@ - + diff --git a/hexworld/s.fs.glsl b/hexworld/s.fs.glsl index 8039d90..eedf250 100644 --- a/hexworld/s.fs.glsl +++ b/hexworld/s.fs.glsl @@ -1,10 +1,10 @@ #version 440 -in vec2 vTexCoord; +in vec2 vcoord; uniform sampler2D tex; void main () { - gl_FragColor = texture(tex, vTexCoord); + gl_FragColor = texture(tex, vcoord); } \ No newline at end of file diff --git a/hexworld/s.vs.glsl b/hexworld/s.vs.glsl index 39134a3..8c6ac6e 100644 --- a/hexworld/s.vs.glsl +++ b/hexworld/s.vs.glsl @@ -1,15 +1,15 @@ #version 440 in vec3 pos; -in vec2 texCoord; +in vec2 coord; -out vec2 vTexCoord; +out vec2 vcoord; -layout (location=1) uniform mat4 view; -layout (location=2) uniform mat4 proj; +uniform mat4 view; +uniform mat4 proj; void main () { gl_Position = proj*view*vec4(pos, 1); - vTexCoord = texCoord; + vcoord = coord; } \ No newline at end of file diff --git a/hexworld/tex2.png b/hexworld/tex2.png index 2f7e1ae..2d3ddb5 100644 Binary files a/hexworld/tex2.png and b/hexworld/tex2.png differ