diff --git a/GLCamera.cs b/GLCamera.cs deleted file mode 100644 index fae761f..0000000 --- a/GLCamera.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System; - -using OpenTK.Graphics.OpenGL; -using OpenTK.Graphics; -using OpenTK; - -namespace nhengine -{ - public class GLCamera - { - Matrix4 position, - projection, - rotation, - lookat; - - Matrix4 result; - - float arc_z, - arc_x; - float distance; - - public GLCamera() - { - lookat = Matrix4.CreateTranslation(1600, 1600, 0); - - distance = 2500.0f; - - projection = Matrix4.CreatePerspectiveFieldOfView( - MathHelper.PiOver4, - 1.6f, - 0.5f, - 100000.0f - ); - rotation = Matrix4.CreateScale(1.0f); - - setup(); - } - - public void setViewport(int width,int height){ - projection = Matrix4.CreatePerspectiveFieldOfView( - MathHelper.PiOver4, - width / (float)height, - 0.5f, - 100000.0f - ); - } - - public Matrix4 Rotation { - get { return this.rotation; } - } - - public float ArcZ - { - get { return this.arc_z; } - set { this.arc_z = value; } - } - - public float ArcX - { - get { return this.arc_x; } - set { this.arc_x = value; } - } - - public float Distance { - get { return this.distance; } - set { this.distance = value; } - } - - public void setup(){ - rotation = Matrix4.CreateRotationZ(this.arc_z) * Matrix4.CreateRotationX(this.arc_x); - position = Matrix4.CreateTranslation(0, 0, distance); - - result = Matrix4.Identity; - result *= lookat.Inverted(); - result *= rotation; -// result *= Matrix4.CreateRotationZ(this.arc_z); -// result *= Matrix4.CreateRotationX(this.arc_x); - result *= position.Inverted(); - result *= projection; - } - - public Matrix4 Matrix { - get { return this.result; } - } - } -} diff --git a/BootStrap.cs b/NHEngine/BootStrap.cs similarity index 71% rename from BootStrap.cs rename to NHEngine/BootStrap.cs index 9128882..c86e044 100644 --- a/BootStrap.cs +++ b/NHEngine/BootStrap.cs @@ -3,6 +3,11 @@ using OpenTK.Graphics.OpenGL4; using OpenTK; +using OpenTK.Audio.OpenAL; +using OpenTK.Audio; + +using org.nhengine.graphics; + namespace nhengine { public class BootStrap @@ -14,15 +19,13 @@ namespace nhengine } OpenGLWindow glWindow; - Graphics graphics; SquaredMap map; public static void Main(string[] args){ - _instance = new BootStrap(); - - _instance.run(); + _instance = new BootStrap(); + _instance.run(); } public BootStrap() @@ -37,7 +40,14 @@ namespace nhengine public void bootGraphics(){ glWindow = new OpenGLWindow(); - graphics = new Graphics(); + + GlobalDefaults.instance(); + + //graphics = new Graphics(); + } + + public void bootAudio(){ + AL.Enable(ALCapability.Invalid); } public void bootMap(){ diff --git a/Graphics.cs b/NHEngine/Graphics.cs similarity index 100% rename from Graphics.cs rename to NHEngine/Graphics.cs diff --git a/OpenGLWindow.cs b/NHEngine/OpenGLWindow.cs similarity index 54% rename from OpenGLWindow.cs rename to NHEngine/OpenGLWindow.cs index 34ef2c6..f0ef597 100644 --- a/OpenGLWindow.cs +++ b/NHEngine/OpenGLWindow.cs @@ -5,6 +5,9 @@ using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; using OpenTK; +using org.nhengine.graphics; +using org.nhengine.graphics.primitives; + namespace nhengine { @@ -17,6 +20,16 @@ namespace nhengine private Point mouseCapturePosition; private bool mouseCaptured; + private float arcUpDown, + arcLeftRight; + + private Cube cube, + c1, c2, c3; + + GraphicsMode gmode = new GraphicsMode(); + + + public OpenGLWindow() :base(800, 600, GraphicsMode.Default, @@ -27,8 +40,9 @@ namespace nhengine GraphicsContextFlags.ForwardCompatible) { VSync = VSyncMode.On; - camera = new GLCamera(); - camera.ArcX = -MathHelper.PiOver3; + camera = GlobalDefaults.instance().ActiveCamera; + camera.Distance = 1000; + camera.Position = new Vector3(1600, 1600, 1000); } protected override void OnLoad(EventArgs e) @@ -36,6 +50,13 @@ namespace nhengine base.OnLoad(e); GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f); GL.Enable(EnableCap.DepthTest); + GL.Enable(EnableCap.Normalize); + + cube = new Cube(new Vector3(0,0,512), 128.0f); + c1 = new Cube(new Vector3(512,0,512), 128.0f); + c2 = new Cube(new Vector3(0,512,512), 128.0f); + c3 = new Cube(new Vector3(512,512,512), 128.0f); + } protected override void OnResize(EventArgs e) @@ -54,9 +75,14 @@ namespace nhengine GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); - camera.setup(); + cube.paint(camera); + c1.paint(camera); + c2.paint(camera); + c3.paint(camera); + boot.SquaredMap.paint(camera); + //triangle.paint(camera); SwapBuffers(); } @@ -82,19 +108,8 @@ namespace nhengine } protected virtual void onMouseCapturedMove(Point delta){ - camera.ArcZ += ((float)delta.X) / 100.0f; - camera.ArcX += ((float)delta.Y) / 100.0f; - - if (camera.ArcX > 0.0f){ - camera.ArcX = 0.0f; - } - - if (camera.ArcX < -MathHelper.PiOver2){ - camera.ArcX = -MathHelper.PiOver2; - } - - - + rotateLeftRight(((float)delta.X) / 10.0f); + rotateUpDown(((float)delta.Y) / 10.0f); } protected override void OnMouseDown(OpenTK.Input.MouseButtonEventArgs e) @@ -116,6 +131,54 @@ namespace nhengine protected override void OnMouseWheel(OpenTK.Input.MouseWheelEventArgs e) { camera.Distance += e.DeltaPrecise * 16.0f; + if (camera.Distance < 0) + camera.Distance = 0; + + applyRotation(); } + + protected override void OnKeyPress(KeyPressEventArgs e) + { + } + + protected override void OnKeyDown(OpenTK.Input.KeyboardKeyEventArgs e) + { + switch (e.Key){ + case OpenTK.Input.Key.Up: + rotateUpDown(MathHelper.Pi / 90.0f); + break; + case OpenTK.Input.Key.Down: + rotateUpDown(-MathHelper.Pi / 90.0f); + break; + case OpenTK.Input.Key.Left: + rotateLeftRight(MathHelper.Pi / 90.0f); + break; + case OpenTK.Input.Key.Right: + rotateLeftRight(-MathHelper.Pi / 90.0f); + break; + } + } + + public void rotateUpDown(float arc) + { + arcUpDown += arc; + applyRotation(); + } + public void rotateLeftRight(float arc) + { + arcLeftRight += arc; + applyRotation(); + } + + private void applyRotation(){ + Console.WriteLine("Rotation: {0} {1}", arcLeftRight, arcUpDown); + camera.MRotation = Matrix4.CreateRotationZ(arcLeftRight) * Matrix4.CreateRotationX(arcUpDown); + + } + + protected override void OnKeyUp(OpenTK.Input.KeyboardKeyEventArgs e) + { + } + } } diff --git a/OpenTK.dll.config b/NHEngine/OpenTK.dll.config similarity index 100% rename from OpenTK.dll.config rename to NHEngine/OpenTK.dll.config diff --git a/NHEngine/Properties/AssemblyInfo.cs b/NHEngine/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..65f7a89 --- /dev/null +++ b/NHEngine/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("nhengine")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("${AuthorCopyright}")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/NHEngine/SquaredMap.cs b/NHEngine/SquaredMap.cs new file mode 100644 index 0000000..2612aba --- /dev/null +++ b/NHEngine/SquaredMap.cs @@ -0,0 +1,127 @@ +using System; +using System.IO; + +using OpenTK.Graphics.OpenGL4; +using OpenTK; + +using org.nhengine.graphics; + +namespace nhengine +{ + public class SquaredMap : GLObject + { + private int width, + height; + + private float[] heightMap; + + public SquaredMap(int width,int height) + :base( 12 * width * height ) + { + this.width = width; + this.height = height; + + this.heightMap = new float[ (width + 1) * (height + 1) ]; + + normalsEnabled = true; + + generateHeightMap(); + + computeGL(); + updateGL(); + } + + public void generateHeightMap(){ + Random rand = new Random(); + + for (int y = 0; y <= height; y++) + { + for (int x = 0; x <= width; x++) + { + float h = (float)rand.NextDouble(); + heightMap[x + (y * width)] = (float)(256 * (h * h)); + } + } + } + + public void computeGL(){ + float left, + right, + bottom, + top, + vcenter, + hcenter; + + + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + int i = x + ((width + 1) * y); + int j = x + ((width + 1) * (y + 1)); + + float[] h = new float[4]; + h[0] = heightMap[i]; + h[1] = heightMap[i+1]; + h[2] = heightMap[j]; + h[3] = heightMap[j+1]; + + float ah = (h[0] + h[1] + h[2] + h[3]) / 4.0f; + + left = 128 * x; + right = 128 * (x + 1); + bottom = 128 * y; + top = 128 * (y + 1); + vcenter = (top + bottom) / 2.0f; + hcenter = (left + right) / 2.0f; + + int b = 4 * (x + (width * y)); + + setTriangle( + b, + new Vector3(left, top, h[2]), + new Vector3(right, top, h[3]), + new Vector3(hcenter,vcenter,ah) + ); + setTriangle( + b + 1, + new Vector3(right, top, h[3]), + new Vector3(right, bottom, h[1]), + new Vector3(hcenter, vcenter, ah) + ); + setTriangle( + b + 2, + new Vector3(right, bottom, h[1]), + new Vector3(left, bottom, h[0]), + new Vector3(hcenter, vcenter, ah) + ); + setTriangle( + b + 3, + new Vector3(left, bottom, h[0]), + new Vector3(left, top, h[2]), + new Vector3(hcenter, vcenter, ah) + ); + + setColor(new Vector4(1.0f, 1.0f, 0.5f, 1.0f)); + + for (int n = 0; n < vertices.Length / 3;n++){ + float _h = vertices[(3 * n) + 2] / 256.0f; + setColor( + n, + new Vector4( + 0.3f + (0.7f * _h), + 0.2f + (0.8f * _h), + 0.4f + (0.6f * _h), + 1.0f + ) + ); + } + + } + } + + } + + + } +} diff --git a/TextureManager.cs b/NHEngine/TextureManager.cs similarity index 100% rename from TextureManager.cs rename to NHEngine/TextureManager.cs diff --git a/nhengine.csproj b/NHEngine/nhengine.csproj similarity index 59% rename from nhengine.csproj rename to NHEngine/nhengine.csproj index 5fe58d3..c81e846 100644 --- a/nhengine.csproj +++ b/NHEngine/nhengine.csproj @@ -2,14 +2,14 @@ Debug - x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC} - Exe + AnyCPU + {42BCFEF7-3F24-469A-BD46-E0C9C2C20D21} + WinExe nhengine nhengine - v4.5.1 + v4.5 - + true full false @@ -17,51 +17,43 @@ DEBUG; prompt 4 - x86 + false true - + true bin\Release prompt 4 - x86 + false true - - Project - bin\Debug - false - false - - - - - - - - - - - - - - - - - PreserveNewest - - - PreserveNewest - - - - + - packages\OpenTK.2.0.0\lib\net20\OpenTK.dll + ..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll + + + + + + + + + + + + + + + + + {3E812F66-D5F3-4599-8360-97F355B6CC1B} + org.nhengine + + \ No newline at end of file diff --git a/NHEngine/nhengine.sln b/NHEngine/nhengine.sln new file mode 100644 index 0000000..ce08228 --- /dev/null +++ b/NHEngine/nhengine.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nhengine", "nhengine.csproj", "{48BC2215-027E-435F-B8DB-336BD9F678FC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pencil.Gaming", "..\Pencil.Gaming\Pencil.Gaming\Pencil.Gaming.csproj", "{DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + Compatibility-GLFW2|Any CPU = Compatibility-GLFW2|Any CPU + Core-GLFW2|Any CPU = Core-GLFW2|Any CPU + Compatibility-GLFW3|Any CPU = Compatibility-GLFW3|Any CPU + Core-GLFW3|Any CPU = Core-GLFW3|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Debug|x86.ActiveCfg = Debug|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Debug|x86.Build.0 = Debug|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Release|x86.ActiveCfg = Release|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Release|x86.Build.0 = Release|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Compatibility-GLFW2|Any CPU.ActiveCfg = Debug|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Compatibility-GLFW2|Any CPU.Build.0 = Debug|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Core-GLFW2|Any CPU.ActiveCfg = Debug|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Core-GLFW2|Any CPU.Build.0 = Debug|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Compatibility-GLFW3|Any CPU.ActiveCfg = Debug|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Compatibility-GLFW3|Any CPU.Build.0 = Debug|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Core-GLFW3|Any CPU.ActiveCfg = Debug|x86 + {48BC2215-027E-435F-B8DB-336BD9F678FC}.Core-GLFW3|Any CPU.Build.0 = Debug|x86 + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Debug|x86.ActiveCfg = Debug|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Debug|x86.Build.0 = Debug|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Release|x86.ActiveCfg = Release|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Release|x86.Build.0 = Release|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Compatibility-GLFW2|Any CPU.ActiveCfg = Compatibility-GLFW2|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Compatibility-GLFW2|Any CPU.Build.0 = Compatibility-GLFW2|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Core-GLFW2|Any CPU.ActiveCfg = Core-GLFW2|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Core-GLFW2|Any CPU.Build.0 = Core-GLFW2|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Compatibility-GLFW3|Any CPU.ActiveCfg = Compatibility-GLFW3|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Compatibility-GLFW3|Any CPU.Build.0 = Compatibility-GLFW3|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Core-GLFW3|Any CPU.ActiveCfg = Core-GLFW3|Any CPU + {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Core-GLFW3|Any CPU.Build.0 = Core-GLFW3|Any CPU + EndGlobalSection +EndGlobal diff --git a/NHEngine/packages.config b/NHEngine/packages.config new file mode 100644 index 0000000..b371766 --- /dev/null +++ b/NHEngine/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/shader/simple_fragment.shader b/NHEngine/shader/simple_fragment.shader similarity index 100% rename from shader/simple_fragment.shader rename to NHEngine/shader/simple_fragment.shader diff --git a/shader/simple_vertex.shader b/NHEngine/shader/simple_vertex.shader similarity index 100% rename from shader/simple_vertex.shader rename to NHEngine/shader/simple_vertex.shader diff --git a/ShaderManager.cs b/ShaderManager.cs deleted file mode 100644 index a2ca320..0000000 --- a/ShaderManager.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System; -using System.IO; -using OpenTK.Graphics.OpenGL; -using System.Collections.Generic; - -namespace nhengine { - - public class ShaderManager { - private static ShaderManager _instance; - - private List shaders; - - public ShaderManager(){ - if (_instance == null) - { - _instance = this; - } - - this.shaders = new List(); - } - - void registerShader(Shader shader){ - this.shaders.Add(shader); - } - - public class Shader - { - - private int id; - private ShaderType shaderType; - private String source; - - public Shader(ShaderType shaderType, String source) - { - this.id = GL.CreateShader(shaderType); - - this.shaderType = shaderType; - this.source = source; - - _instance.registerShader(this); - - compile(); - } - - public int ID { - get { return this.id; } - } - - public Shader(ShaderType shaderType,FileStream file){ - this.id = GL.CreateShader(shaderType); - this.shaderType = shaderType; - - StreamReader sr = new StreamReader(file); - this.source = sr.ReadToEnd(); - - _instance.registerShader(this); - - compile(); - } - - private void compile(){ - GL.ShaderSource(this.id, source); - GL.CompileShader(this.id); - - string msg = GL.GetShaderInfoLog((int)this.id); - - Console.WriteLine("Supported Shader Version: {0}", GL.GetString(StringName.ShadingLanguageVersion)); - Console.WriteLine("Shader Compiled: {0}", msg); - - } - - public ShaderType ShaderType { - get { return this.shaderType; } - } - } - - public class ShaderProgram { - - Shader vertexShader; - Shader fragmentShader; - - int id; - - public ShaderProgram(Shader vertexShader, Shader fragmentShader){ - - this.vertexShader = vertexShader; - this.fragmentShader = fragmentShader; - - this.id = GL.CreateProgram(); - - GL.AttachShader(this.id, vertexShader.ID); - GL.AttachShader(this.id, fragmentShader.ID); - GL.LinkProgram(this.id); - Console.WriteLine("Shader Program Result:D {0}", GL.GetProgramInfoLog(this.id)); - } - - public int ID { - get { return this.id; } - } - } - } - -} diff --git a/ShaderProgram.cs b/ShaderProgram.cs deleted file mode 100644 index 99d77d5..0000000 --- a/ShaderProgram.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.IO; - -using OpenTK.Graphics.OpenGL; - -namespace nhengine -{ - public class ShaderProgram - { - - Shader vertexShader; - Shader fragmentShader; - - int id; - - public ShaderProgram(Shader vertexShader, Shader fragmentShader) - { - init(vertexShader, fragmentShader); - } - - public ShaderProgram(String vertexShader, String fragmentShader) - { - init( - new Shader(ShaderType.VertexShader, vertexShader), - new Shader(ShaderType.FragmentShader, fragmentShader) - ); - } - - public ShaderProgram(Stream vertexShader, Stream fragmentShader) - { - init( - new Shader(ShaderType.VertexShader, new StreamReader(vertexShader).ReadToEnd()), - new Shader(ShaderType.FragmentShader, new StreamReader(fragmentShader).ReadToEnd()) - ); - } - - - private void init(Shader vertexShader, Shader fragmentShader){ - this.vertexShader = vertexShader; - this.fragmentShader = fragmentShader; - - this.id = GL.CreateProgram(); - - GL.AttachShader(this.id, vertexShader.ID); - GL.AttachShader(this.id, fragmentShader.ID); - - GL.BindAttribLocation(this.id, 0, "position"); - GL.BindAttribLocation(this.id, 1, "v_color"); - - - GL.LinkProgram(this.id); - Console.WriteLine("Shader Program Result: {0}", GL.GetProgramInfoLog(this.id)); - } - - public int ID - { - get { return this.id; } - } - } -} diff --git a/SquaredMap.cs b/SquaredMap.cs deleted file mode 100644 index 29f7266..0000000 --- a/SquaredMap.cs +++ /dev/null @@ -1,205 +0,0 @@ -using System; -using System.IO; - -using OpenTK.Graphics.OpenGL4; -using OpenTK; - -namespace nhengine -{ - public class SquaredMap - { - private int width, - height; - - private double[] heightMap; - private int[] tileTypes; - - /* GL shared resources */ - private int vao, cao, nao, ebo; - - private int bufVertices; - private float[] - vertices; - private float[] - colors, - normals; - - private int[] elements; - - private ShaderProgram - shaderProgram; - - public SquaredMap(int width,int height) - { - this.width = width; - this.height = height; - - this.heightMap = new double[getVertexIndex(width,height) + 1]; - this.tileTypes = new int[getTileIndex(width,height) + 1]; - - shaderProgram = new ShaderProgram( - new FileStream("shader/simple_vertex.shader",FileMode.Open), - new FileStream("shader/simple_fragment.shader",FileMode.Open) - ); - - prepareGL(); - computeGL(); - - dumpGL(); - } - - private void prepareGL(){ - vao = GL.GenVertexArray(); - cao = GL.GenBuffer(); - nao = GL.GenBuffer(); - ebo = GL.GenBuffer(); - bufVertices = GL.GenBuffer(); - } - - public void computeGL(){ - int b; - - if (vertices == null){ - vertices = new float[heightMap.Length * 3]; - } - - if (elements == null){ - elements = new int[width * height * 6]; - } - - if (colors == null){ - colors = new float[heightMap.Length * 3]; - normals = new float[colors.Length]; - } - - Random rand = new Random(); - - for (int x = 0; x <= width;x++) - { - for (int y = 0; y <= height;y++){ - b = 3 * getVertexIndex(x, y); - - vertices[b + 0] = 128 * x; - vertices[b + 1] = 128 * y; - - float h = (float)rand.NextDouble(); - vertices[b + 2] = (float)(256 * (h * h)); - - colors[b + 0] = 0.3f + 0.7f * (float)vertices[b + 2] / 256.0f; - colors[b + 1] = 0.2f + 0.8f * (float)vertices[b + 2] / 256.0f; - colors[b + 2] = 0.4f + 0.6f * (float)vertices[b + 2] / 256.0f; - } - } - - for (int x = 0; x < width;x++) - { - for (int y = 0; y < height;y++){ - b = 6 * getTileIndex(x, y); - - elements[b + 0] = x + (y * (width + 1)); - elements[b + 1] = x + (y * (width + 1)) + 1; - elements[b + 2] = x + ((y + 1) * (width + 1)); - - elements[b + 3] = x + ((y + 1) * (width + 1)); - elements[b + 4] = x + (y * (width + 1)) + 1; - elements[b + 5] = x + ((y + 1) * (width + 1)) + 1; - } - } - - GL.BindVertexArray(vao); - - GL.BindBuffer(BufferTarget.ArrayBuffer, bufVertices); - GL.BufferData(BufferTarget.ArrayBuffer, 4 * vertices.Length, vertices, BufferUsageHint.StaticDraw); - - GL.VertexAttribPointer(0, - 3, - VertexAttribPointerType.Float, - false, - 0, - 0); - - GL.BindBuffer(BufferTarget.ArrayBuffer, cao); - GL.BufferData(BufferTarget.ArrayBuffer, 4 * colors.Length, colors, BufferUsageHint.StaticDraw); - - GL.VertexAttribPointer(1, - 3, - VertexAttribPointerType.Float, - false, - 0, - 0); - - GL.EnableVertexAttribArray(0); - GL.EnableVertexAttribArray(1); - - - GL.BindBuffer(BufferTarget.ElementArrayBuffer,ebo); - GL.BufferData(BufferTarget.ElementArrayBuffer, 4 * elements.Length, elements, BufferUsageHint.StaticDraw); - - GL.BindVertexArray(0); - } - - void dumpGL(){ - Console.WriteLine("Vertices:"); - for (int x = 0; x < 8;x++){ - Console.WriteLine("{0}", vertices[x]); - } - } - - private int getTileIndex(int x, int y) - { - return x + (y * this.width); - } - private int getVertexIndex(int x,int y){ - return x + (y * (this.width + 1)); - } - - private int getTileType(int x, int y) - { - int ind = getTileIndex(x, y); - if ((ind < 0) || (ind > tileTypes.Length)) - { - return 0; - } - - return this.tileTypes[ind]; - } - - private int getTileType(int ind) - { - if ((ind < 0) || (ind > tileTypes.Length)) - { - return 0; - } - return this.tileTypes[ind]; - } - - private double getHeight(int ind) - { - if ((ind < 0) || (ind > heightMap.Length)) - { - return 0; - } - - return this.heightMap[ind]; - } - - public void paint(GLCamera camera){ - - - Matrix4 pmat = camera.Matrix; - - GL.UseProgram(shaderProgram.ID); - - int pm = GL.GetUniformLocation(shaderProgram.ID, "proj_matrix"); - GL.UniformMatrix4(pm, false, ref pmat); - - GL.BindVertexArray(vao); - - GL.DrawElements(BeginMode.Triangles, elements.Length, DrawElementsType.UnsignedInt, 0); - - GL.BindVertexArray(0); - - } - - } -} diff --git a/nhengine.sln b/nhengine.sln index ce08228..5332283 100644 --- a/nhengine.sln +++ b/nhengine.sln @@ -1,43 +1,23 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nhengine", "nhengine.csproj", "{48BC2215-027E-435F-B8DB-336BD9F678FC}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nhengine", "NHEngine\nhengine.csproj", "{42BCFEF7-3F24-469A-BD46-E0C9C2C20D21}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pencil.Gaming", "..\Pencil.Gaming\Pencil.Gaming\Pencil.Gaming.csproj", "{DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "org.nhengine", "org.nhengine\org.nhengine.csproj", "{3E812F66-D5F3-4599-8360-97F355B6CC1B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x86 = Debug|x86 - Release|x86 = Release|x86 - Compatibility-GLFW2|Any CPU = Compatibility-GLFW2|Any CPU - Core-GLFW2|Any CPU = Core-GLFW2|Any CPU - Compatibility-GLFW3|Any CPU = Compatibility-GLFW3|Any CPU - Core-GLFW3|Any CPU = Core-GLFW3|Any CPU + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Debug|x86.ActiveCfg = Debug|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Debug|x86.Build.0 = Debug|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Release|x86.ActiveCfg = Release|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Release|x86.Build.0 = Release|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Compatibility-GLFW2|Any CPU.ActiveCfg = Debug|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Compatibility-GLFW2|Any CPU.Build.0 = Debug|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Core-GLFW2|Any CPU.ActiveCfg = Debug|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Core-GLFW2|Any CPU.Build.0 = Debug|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Compatibility-GLFW3|Any CPU.ActiveCfg = Debug|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Compatibility-GLFW3|Any CPU.Build.0 = Debug|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Core-GLFW3|Any CPU.ActiveCfg = Debug|x86 - {48BC2215-027E-435F-B8DB-336BD9F678FC}.Core-GLFW3|Any CPU.Build.0 = Debug|x86 - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Debug|x86.ActiveCfg = Debug|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Debug|x86.Build.0 = Debug|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Release|x86.ActiveCfg = Release|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Release|x86.Build.0 = Release|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Compatibility-GLFW2|Any CPU.ActiveCfg = Compatibility-GLFW2|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Compatibility-GLFW2|Any CPU.Build.0 = Compatibility-GLFW2|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Core-GLFW2|Any CPU.ActiveCfg = Core-GLFW2|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Core-GLFW2|Any CPU.Build.0 = Core-GLFW2|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Compatibility-GLFW3|Any CPU.ActiveCfg = Compatibility-GLFW3|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Compatibility-GLFW3|Any CPU.Build.0 = Compatibility-GLFW3|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Core-GLFW3|Any CPU.ActiveCfg = Core-GLFW3|Any CPU - {DDB6DB6D-E5DE-4BDB-8AC8-26DF800E9FF0}.Core-GLFW3|Any CPU.Build.0 = Core-GLFW3|Any CPU + {42BCFEF7-3F24-469A-BD46-E0C9C2C20D21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {42BCFEF7-3F24-469A-BD46-E0C9C2C20D21}.Debug|Any CPU.Build.0 = Debug|Any CPU + {42BCFEF7-3F24-469A-BD46-E0C9C2C20D21}.Release|Any CPU.ActiveCfg = Release|Any CPU + {42BCFEF7-3F24-469A-BD46-E0C9C2C20D21}.Release|Any CPU.Build.0 = Release|Any CPU + {3E812F66-D5F3-4599-8360-97F355B6CC1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3E812F66-D5F3-4599-8360-97F355B6CC1B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3E812F66-D5F3-4599-8360-97F355B6CC1B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3E812F66-D5F3-4599-8360-97F355B6CC1B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/org.nhengine/OpenTK.dll.config b/org.nhengine/OpenTK.dll.config new file mode 100644 index 0000000..7098d39 --- /dev/null +++ b/org.nhengine/OpenTK.dll.config @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.nhengine/Properties/AssemblyInfo.cs b/org.nhengine/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2b80d1e --- /dev/null +++ b/org.nhengine/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("org.nhengine")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("${AuthorCopyright}")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/org.nhengine/graphics/GLCamera.cs b/org.nhengine/graphics/GLCamera.cs new file mode 100644 index 0000000..8dbcba3 --- /dev/null +++ b/org.nhengine/graphics/GLCamera.cs @@ -0,0 +1,129 @@ +using System; + +using OpenTK.Graphics.OpenGL; +using OpenTK.Graphics; +using OpenTK; + +namespace org.nhengine.graphics +{ + public class GLCamera + { + Vector3 vPosition; // Position der Kamera + Vector3 vDirection; // Blickrichtung der Kamera + + float fDistance; // Zusätzliche Distanz der Kamera entgegen der Blickrichtung + + Matrix4 mTranslation; // Matrix für Translation + Matrix4 mRotation; // Matrix für Rotation + Matrix4 mDistance; // Matrix für Rückzug entgegen der Blickrichtung + Matrix4 mProjection; // Projektionsmatrix + + public GLCamera() + { + vPosition = new Vector3(0,0,0); + vDirection = Vector3.UnitZ; + fDistance = 0.0f; + + mProjection = Matrix4.CreatePerspectiveFieldOfView( + MathHelper.PiOver4, + 1.0f, + 0.1f, + 100000.0f + ); + + mRotation = Matrix4.Identity; + + glPrepare(); + } + + public void setViewport(int width, int height) + { + mProjection = Matrix4.CreatePerspectiveFieldOfView( + MathHelper.PiOver4, + width / (float)height, + 1.0f, + 1000000.0f + ); + } + + private void glPrepare(){ + this.mTranslation = Matrix4.CreateTranslation(vPosition * -1.0f); + this.mDistance = mRotation * Matrix4.CreateTranslation(0, 0, -fDistance); + + // Console.WriteLine("View Direction: {0}", vDirection); + // Console.WriteLine("Rotation Matrix: {0}", mRotation); + + } + + private void glUpdate(){ + + } + + public void use(ShaderProgram sp){ + glUpdate(); + + } + + + public Matrix4 MTranslation + { + get + { + return mTranslation; + } + } + + public Matrix4 MRotation + { + get + { + return mRotation; + } + set { + this.mRotation = value; + } + } + + public Matrix4 MDistance + { + get + { + return mDistance; + } + } + + public Matrix4 MProjection + { + get + { + return mProjection; + } + } + + public float Distance + { + get + { + return fDistance; + } + + set + { + fDistance = value; + glPrepare(); + } + } +/* + public Vector3 Direction { + get { return this.vDirection; } + set { this.vDirection = value; glPrepare(); } + } +*/ + public Vector3 Position { + get { return this.vPosition; } + set { this.vPosition = value; glPrepare(); } + } + + + } +} diff --git a/org.nhengine/graphics/GLObject.cs b/org.nhengine/graphics/GLObject.cs new file mode 100644 index 0000000..7052c7a --- /dev/null +++ b/org.nhengine/graphics/GLObject.cs @@ -0,0 +1,218 @@ +using System; + +using OpenTK.Graphics.OpenGL; +using OpenTK.Graphics; +using OpenTK; + + +namespace org.nhengine.graphics +{ + public class GLObject + { + ShaderProgram shaderProgram; + + protected float[] vertices, + colors, + normals; + + protected int[] elements; + + int vao, // Vertex Array Object + ebo, // Element Buffer + vbo, // Vertex Buffer + cbo, // Color Buffer + nbo; // Normals Buffer + + Vector3 position; + + protected bool colorEnabled, + normalsEnabled, + uvEnabled; + + public GLObject(int vertexes) + { + shaderProgram = GlobalDefaults.instance().DefaultShaderProgram; + + this.vertices = new float[3 * vertexes]; + this.colors = new float[4 * vertexes]; + this.normals = new float[3 * vertexes]; + this.elements = null; // Element Arrays können im Moment nicht verwendet werden + + colorEnabled = true; + normalsEnabled = false; + uvEnabled = false; + + prepareGL(); + } + + protected void prepareGL() + { + vao = GL.GenVertexArray(); + ebo = GL.GenBuffer(); + vbo = GL.GenBuffer(); + cbo = GL.GenBuffer(); + nbo = GL.GenBuffer(); + + GL.BindVertexArray(vao); + + GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); + GL.VertexAttribPointer(0, + 3, + VertexAttribPointerType.Float, + false, + 0, + 0); + + GL.BindBuffer(BufferTarget.ArrayBuffer, cbo); + GL.VertexAttribPointer(1, + 4, + VertexAttribPointerType.Float, + false, + 0, + 0); + + GL.BindBuffer(BufferTarget.ArrayBuffer, nbo); + GL.VertexAttribPointer(2, + 3, + VertexAttribPointerType.Float, + false, + 0, + 0); + + GL.BindVertexArray(0); + } + + protected void updateGL() + { + GL.BindVertexArray(vao); + + GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); + GL.BufferData(BufferTarget.ArrayBuffer, 4 * this.vertices.Length, this.vertices, BufferUsageHint.StaticDraw); + + GL.EnableVertexAttribArray(0); + + if (colorEnabled) + { + GL.BindBuffer(BufferTarget.ArrayBuffer, cbo); + GL.BufferData(BufferTarget.ArrayBuffer, 4 * this.colors.Length, this.colors, BufferUsageHint.StaticDraw); + + GL.EnableVertexAttribArray(1); + } + else + { + GL.DisableVertexAttribArray(1); + } + + if (normalsEnabled) + { + GL.BindBuffer(BufferTarget.ArrayBuffer, nbo); + GL.BufferData(BufferTarget.ArrayBuffer, 4 * this.normals.Length, this.normals, BufferUsageHint.StaticDraw); + + GL.EnableVertexAttribArray(2); + } + else + { + GL.DisableVertexAttribArray(2); + } + + if (uvEnabled) + { + GL.EnableVertexAttribArray(3); + } + else + { + GL.DisableVertexAttribArray(3); + } + + GL.BindVertexArray(0); + } + + protected void setVertex(int n, Vector3 p) + { + vertices[(3 * n)] = p.X; + vertices[(3 * n) + 1] = p.Y; + vertices[(3 * n) + 2] = p.Z; + } + + protected void setNormal(int n, Vector3 norm){ + normals[(3 * n)] = norm.X; + normals[(3 * n)+1] = norm.Y; + normals[(3 * n)+2] = norm.Z; + } + + protected void setColor(int n, Vector4 p) + { + colors[(4 * n)] = p.X; + colors[(4 * n) + 1] = p.Y; + colors[(4 * n) + 2] = p.Z; + colors[(4 * n) + 3] = p.W; + } + + protected void setTriangle(int n, Vector3 a, Vector3 b, Vector3 c) + { + setVertex((3 * n), a); + setVertex((3 * n) + 1, b); + setVertex((3 * n) + 2, c); + + Vector3 normal = Vector3.Cross((b - a),(c - a)); + normal.Normalize(); + + setNormal((3 * n), normal); + setNormal((3 * n) + 1, normal); + setNormal((3 * n) + 2, normal); + + Console.WriteLine("setTriangle(): "); + Console.WriteLine("a: {0}", a); + Console.WriteLine("b: {0}", b); + Console.WriteLine("c: {0}", c); + Console.WriteLine("n: {0}", normal); + + + } + + protected void setColor(Vector4 c) + { + for (int n = 0; n < colors.Length / 4; n++) + { + setColor(n, c); + } + } + + protected Vector4 getColor(int n){ + return new Vector4( + colors[(4 * n)], + colors[(4 * n) + 1], + colors[(4 * n) + 2], + colors[(4 * n) + 3]); + } + + public void paint() + { + paint(GlobalDefaults.instance().ActiveCamera); + } + + public void paint(GLCamera camera) + { + shaderProgram.use(camera); + + GL.BindVertexArray(this.vao); + + GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length); + + GL.BindVertexArray(0); + } + + + + public ShaderProgram ShaderProgram + { + get { return this.shaderProgram; } + set { this.shaderProgram = value; } + } + + public Vector3 Position{ + get { return this.position; } + set { this.position = value; } + } + } +} diff --git a/org.nhengine/graphics/GLSceneOrientation.cs b/org.nhengine/graphics/GLSceneOrientation.cs new file mode 100644 index 0000000..798de06 --- /dev/null +++ b/org.nhengine/graphics/GLSceneOrientation.cs @@ -0,0 +1,16 @@ +using System; +namespace org.nhengine +{ + public class GLSceneOrientation + { + + + + public GLSceneOrientation() + { + } + + + + } +} diff --git a/org.nhengine/graphics/GlobalDefaults.cs b/org.nhengine/graphics/GlobalDefaults.cs new file mode 100644 index 0000000..3b99069 --- /dev/null +++ b/org.nhengine/graphics/GlobalDefaults.cs @@ -0,0 +1,110 @@ +using System; + +using OpenTK.Graphics.OpenGL; + +namespace org.nhengine.graphics +{ + public class GlobalDefaults + { + private static GlobalDefaults _instance = new GlobalDefaults(); + public static GlobalDefaults instance(){ + return _instance; + } + + private Shader defVertexShader, + defFragmentShader; + private ShaderProgram + defShaderProgram; + + private GLCamera activeCamera; + + private GlobalDefaults(){ + + Console.WriteLine("GlobalDefaults: Shader Version: {0}", GL.GetString(StringName.ShadingLanguageVersion)); + + defVertexShader = new Shader(OpenTK.Graphics.OpenGL.ShaderType.VertexShader, defVertexShaderSource); + defFragmentShader = new Shader(OpenTK.Graphics.OpenGL.ShaderType.FragmentShader, defFragmentShaderSource); + + defShaderProgram = new ShaderProgram(defVertexShader, defFragmentShader); + + + activeCamera = new GLCamera(); + } + + public ShaderProgram DefaultShaderProgram { + get { return this.defShaderProgram;} + } + + public Shader DefaultVertexShader + { + get { return defVertexShader; } + } + + public Shader DefaultFragmentShader + { + get { return defFragmentShader; } + } + + public GLCamera ActiveCamera { + get { return this.activeCamera; } + set { this.activeCamera = value; } + } + + +private string defVertexShaderSource = @"#version 330 + +in vec3 iv_position; +in vec4 iv_color; +in vec3 iv_normal; +in vec2 iv_uv; + +uniform mat4 mTranslation; +uniform mat4 mRotation; +uniform mat4 mWorld; +uniform mat4 mDistance; +uniform mat4 mCamera; +uniform mat4 mProjection; +uniform mat4 mComplete; + +out vec4 color; +out vec3 norm; +out vec2 uv; + +void main() +{ + gl_Position = mComplete * vec4( iv_position, 1 ); + + norm = normalize( ( mRotation * vec4( iv_normal, 1 ) ).xyz ); + vec3 camray = normalize( -gl_Position.xyz ); + + float cosTheta = dot( norm , camray ); + + float visibility = 1.0; + if (cosTheta < 0){ + visibility = 1.0; + } + color = vec4( iv_color.xyz * (0.5 + (0.5 * cosTheta * cosTheta)), 0.5f ); +} + +"; + +private string defFragmentShaderSource = @"#version 330 + +in vec4 color; +in vec3 norm; +in vec2 uv; + +out vec4 _color; + +void main() +{ + _color = color; + gl_FragDepth = gl_FragCoord.z; +} + +"; + + + } + +} diff --git a/Shader.cs b/org.nhengine/graphics/Shader.cs similarity index 75% rename from Shader.cs rename to org.nhengine/graphics/Shader.cs index 289a9b3..72a1a2f 100644 --- a/Shader.cs +++ b/org.nhengine/graphics/Shader.cs @@ -3,7 +3,7 @@ using System.IO; using OpenTK.Graphics.OpenGL; -namespace nhengine +namespace org.nhengine.graphics { public class Shader { @@ -41,13 +41,18 @@ namespace nhengine private void compile() { + int result; + GL.ShaderSource(this.id, source); GL.CompileShader(this.id); string msg = GL.GetShaderInfoLog((int)this.id); + GL.GetShader(this.id, ShaderParameter.CompileStatus, out result); - Console.WriteLine("Supported Shader Version: {0}", GL.GetString(StringName.ShadingLanguageVersion)); - Console.WriteLine("Shader Compiled: {0}", msg); + if (result != 1){ + Console.WriteLine("Shader compile failed: {0}", msg); + throw new Exception(String.Format("OpenGL: Shader did not compile. ({0})",msg)); + } } diff --git a/org.nhengine/graphics/ShaderProgram.cs b/org.nhengine/graphics/ShaderProgram.cs new file mode 100644 index 0000000..3d90204 --- /dev/null +++ b/org.nhengine/graphics/ShaderProgram.cs @@ -0,0 +1,205 @@ +using System; +using System.IO; + +using OpenTK.Graphics.OpenGL; +using OpenTK; + +/** + * ShaderProgram Class + * + * Defines a shading program to use for drawing operations + * + * The coordinate space used to describe the world is called "WorldSpace" + * + * Each object to be drawn/used in the "World" is based on its individual coordinate space "ObjectSpace" + * Each object provides a Transformation Matrix "mObject" which translates ObjectSpace to WorldSpace. + * + * WorldSpace = mObject * ObjectSpace + * + * To render the scene another coordinate space is used, which is called "CameraSpace" + * The transformation matrix used to translate from WorldSpace to CameraSpace is called "mCamera" + * + * + * + * + * + * As convention the following data will be given to shaders: + * + * Uniform: + * + * mTranslation World Coordinate Translation + * mRotation Rotation to camera view direction + * mWorld Transformation Matrix to world coordinate system + * + * mDistance Camera Distance translation (Z) + * mCamera Transformation to camera space + * mProjection Projection Matrix + * + * mComplete Result of all Matrices + * + * Stream Data: + * + * iv_position Vertex coordinate + * iv_color Vertex Color + * iv_normal Vertex normal + * iv_uv Vertex texture coordinate + * + * + **/ + + +namespace org.nhengine.graphics +{ + public class ShaderProgram + { + + Shader vertexShader; + Shader fragmentShader; + + int id; + + int nm_Translation, + nm_Rotation, + nm_World, + nm_Distance, + nm_Camera, + nm_Projection, + nm_Complete; + + public ShaderProgram(Shader vertexShader, Shader fragmentShader) + { + init(vertexShader, fragmentShader); + } + + public ShaderProgram(String vertexShader, String fragmentShader) + { + init( + new Shader(ShaderType.VertexShader, vertexShader), + new Shader(ShaderType.FragmentShader, fragmentShader) + ); + } + + public ShaderProgram(Stream vertexShader, Stream fragmentShader) + { + init( + new Shader(ShaderType.VertexShader, new StreamReader(vertexShader).ReadToEnd()), + new Shader(ShaderType.FragmentShader, new StreamReader(fragmentShader).ReadToEnd()) + ); + } + + + private void init(Shader vertexShader, Shader fragmentShader){ + this.vertexShader = vertexShader; + this.fragmentShader = fragmentShader; + + this.id = GL.CreateProgram(); + + GL.AttachShader(this.id, this.vertexShader.ID); + GL.AttachShader(this.id, this.fragmentShader.ID); + + GL.BindAttribLocation(this.id, 0, "iv_position"); + GL.BindAttribLocation(this.id, 1, "iv_color"); + GL.BindAttribLocation(this.id, 2, "iv_normal"); + GL.BindAttribLocation(this.id, 3, "iv_uv"); + + GL.LinkProgram(this.id); + Console.WriteLine("Shader Program Result: {0}", GL.GetProgramInfoLog(this.id)); + + nm_Translation = GL.GetUniformLocation(this.ID, "mTranslation"); + nm_Rotation = GL.GetUniformLocation(this.ID, "mRotation"); + nm_World = GL.GetUniformLocation(this.ID, "mWorld"); + nm_Distance = GL.GetUniformLocation(this.ID, "mDistance"); + nm_Camera = GL.GetUniformLocation(this.ID, "mCamera"); + nm_Projection = GL.GetUniformLocation(this.ID, "mProjection"); + nm_Complete = GL.GetUniformLocation(this.ID, "mComplete"); + } + + public int ID + { + get { return this.id; } + } + + public int nmTranslation + { + get + { + return nm_Translation; + } + } + + public int nmRotation + { + get + { + return nm_Rotation; + } + } + + public int nmWorld + { + get + { + return nm_World; + } + } + + public int nmDistance + { + get + { + return nm_Distance; + } + } + + public int nmCamera + { + get + { + return nm_Camera; + } + } + + public int nmProjection + { + get + { + return nm_Projection; + } + } + + public int nmComplete + { + get { return this.nm_Complete; } + } + + public void use(GLCamera camera){ + GL.UseProgram(this.id); + + Matrix4 + mTranslate, + mRotation, + mWorld, + mDistance, + mCamera, + mProjection, + mComplete; + + mTranslate = camera.MTranslation; + mRotation = camera.MRotation; + mWorld = mTranslate * mRotation; + mDistance = camera.MDistance; + mCamera = mWorld * mDistance; + mProjection = camera.MProjection; + mComplete = mCamera * mProjection; + + GL.UniformMatrix4(nm_Translation, false, ref mTranslate); + GL.UniformMatrix4(nm_Rotation, false, ref mRotation); + GL.UniformMatrix4(nm_World, false, ref mWorld); + GL.UniformMatrix4(nm_Distance, false, ref mDistance); + GL.UniformMatrix4(nm_Camera, false, ref mCamera); + GL.UniformMatrix4(nm_Projection, false, ref mProjection); + GL.UniformMatrix4(nm_Complete, false, ref mComplete); + + } + } +} diff --git a/org.nhengine/graphics/primitives/Cube.cs b/org.nhengine/graphics/primitives/Cube.cs new file mode 100644 index 0000000..fd56be7 --- /dev/null +++ b/org.nhengine/graphics/primitives/Cube.cs @@ -0,0 +1,48 @@ +using System; + +using OpenTK; + +namespace org.nhengine.graphics.primitives +{ + public class Cube : GLObject + { + public Cube(Vector3 position, float width) + :base(36) + { + Vector3 dx, dy, dz; + Vector3 a, b, c, d, e, f, g, h; + + dx = (Vector3.UnitX * width / 2); + dy = (Vector3.UnitY * width / 2); + dz = (Vector3.UnitZ * width / 2); + + a = position - dx - dy - dz; + b = position + dx - dy - dz; + c = position + dx + dy - dz; + d = position - dx + dy - dz; + e = position - dx - dy + dz; + f = position + dx - dy + dz; + g = position + dx + dy + dz; + h = position - dx + dy + dz; + + normalsEnabled = true; + + setTriangle(0, a, b, c); + setTriangle(1, c, d, a); + setTriangle(2, f, e, h); + setTriangle(3, h, g, f); + setTriangle(4, e, a, d); + setTriangle(5, d, h, e); + setTriangle(6, b, f, g); + setTriangle(7, g, c, b); + setTriangle(8, a, e, f); + setTriangle(9, f, b, a); + setTriangle(10, g, c, d); + setTriangle(11, d, h, g); + + setColor(new Vector4(0.8f, 0.3f, 0.5f, 1.0f)); + + updateGL(); + } + } +} diff --git a/org.nhengine/graphics/primitives/Triangle.cs b/org.nhengine/graphics/primitives/Triangle.cs new file mode 100644 index 0000000..e02f989 --- /dev/null +++ b/org.nhengine/graphics/primitives/Triangle.cs @@ -0,0 +1,23 @@ +using System; + +using OpenTK; +using org.nhengine.graphics; + +namespace org.nhengine.graphics.primitives +{ + public class Triangle : GLObject + { + public Triangle(Vector3 a,Vector3 b,Vector3 c) + :base(3) + { + normalsEnabled = true; + + setTriangle(0, a, b, c); + setColor(new Vector4(0, 1, 0, 1)); + + updateGL(); + + + } + } +} diff --git a/org.nhengine/org.nhengine.csproj b/org.nhengine/org.nhengine.csproj new file mode 100644 index 0000000..6954ce9 --- /dev/null +++ b/org.nhengine/org.nhengine.csproj @@ -0,0 +1,55 @@ + + + + Debug + AnyCPU + {3E812F66-D5F3-4599-8360-97F355B6CC1B} + Library + org.nhengine + org.nhengine + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + true + bin\Release + prompt + 4 + false + + + + + ..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/org.nhengine/packages.config b/org.nhengine/packages.config new file mode 100644 index 0000000..b371766 --- /dev/null +++ b/org.nhengine/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/packages.config b/packages.config deleted file mode 100644 index 30c9bc0..0000000 --- a/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file