Works on AMD now

This commit is contained in:
2017-02-24 23:08:21 -05:00
parent ece5b0d30f
commit 234d1669dd
9 changed files with 167 additions and 185 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

148
hexworld/HexRender.cs Normal file
View File

@@ -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();
}
}
}

View File

@@ -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);

View File

@@ -8,7 +8,7 @@
<OutputType>Exe</OutputType>
<RootNamespace>hexworld</RootNamespace>
<AssemblyName>hexworld</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
@@ -51,8 +51,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Game.cs" />
<Compile Include="Driver.cs" />
<Compile Include="HexRender.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util\Extensions.cs" />
<Compile Include="Util\GLObject.cs" />

View File

@@ -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);
}

View File

@@ -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;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 490 B

After

Width:  |  Height:  |  Size: 718 B