From: jpbruyere Date: Thu, 21 Jul 2016 04:43:51 +0000 (+0200) Subject: put OpenTKCrowWin in game win X-Git-Tag: v0.4~51 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=140b573eb396082414e7d572f95ee56afeb2890e;p=jp%2Fcrow.git put OpenTKCrowWin in game win --- diff --git a/Crow.csproj b/Crow.csproj index 722764a1..2933f844 100644 --- a/Crow.csproj +++ b/Crow.csproj @@ -142,15 +142,19 @@ $(SolutionDir)packages\gtk-sharp.Linux.3.14.3.14.7\lib\net40\cairo-sharp.dll + gtk-sharp-3.0 $(SolutionDir)packages\gtk-sharp.Linux.3.14.3.14.7\lib\net40\gdk-sharp.dll + gtk-sharp-2.0 $(SolutionDir)packages\gtk-sharp.Linux.3.14.3.14.7\lib\net40\gio-sharp.dll + gio-sharp-3.0 $(SolutionDir)packages\gtk-sharp.Linux.3.14.3.14.7\lib\net40\glib-sharp.dll + glib-sharp-3.0 @@ -247,6 +251,8 @@ + + diff --git a/Crow.sln b/Crow.sln index d24e6d4f..81c03617 100644 --- a/Crow.sln +++ b/Crow.sln @@ -5,18 +5,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Crow", "Crow.csproj", "{C29 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{74289092-9F70-4941-AFCB-DFD7BE2140B6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OTKCrow", "OTKCrow\OTKCrow.csproj", "{350D4F49-9901-4998-9903-BCA7D48DA58C}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {350D4F49-9901-4998-9903-BCA7D48DA58C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {350D4F49-9901-4998-9903-BCA7D48DA58C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {350D4F49-9901-4998-9903-BCA7D48DA58C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {350D4F49-9901-4998-9903-BCA7D48DA58C}.Release|Any CPU.Build.0 = Release|Any CPU {74289092-9F70-4941-AFCB-DFD7BE2140B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {74289092-9F70-4941-AFCB-DFD7BE2140B6}.Debug|Any CPU.Build.0 = Debug|Any CPU {74289092-9F70-4941-AFCB-DFD7BE2140B6}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/Images/Icons/crow.png b/Images/Icons/crow.png new file mode 100644 index 00000000..7b737d04 Binary files /dev/null and b/Images/Icons/crow.png differ diff --git a/Images/Icons/crow.svg b/Images/Icons/crow.svg new file mode 100644 index 00000000..4904e68e --- /dev/null +++ b/Images/Icons/crow.svg @@ -0,0 +1,108 @@ + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + C.R.O.W. + + diff --git a/OTKCrow/OTKCrow.csproj b/OTKCrow/OTKCrow.csproj deleted file mode 100644 index 6a557706..00000000 --- a/OTKCrow/OTKCrow.csproj +++ /dev/null @@ -1,60 +0,0 @@ - - - - Debug - AnyCPU - {350D4F49-9901-4998-9903-BCA7D48DA58C} - Library - OTKCrow - OTKCrow - v4.5 - 8.0.30703 - 2.0 - 0.4 - $(SolutionDir)/build/$(Configuration) - $(SolutionDir)/build/obj/$(Configuration) - - - true - full - false - DEBUG;MEASURE_TIME - prompt - 4 - false - - - full - true - prompt - 4 - false - - - - - - - - - ..\packages\gtk-sharp.Linux.3.14.3.14.7\lib\net40\cairo-sharp.dll - - - ..\packages\OpenTK.Next.1.2.2336.6514-pre\lib\net20\OpenTK.dll - - - - - - - - - - {C2980F9B-4798-4C05-99E2-E174810F7C7B} - Crow - - - - - - diff --git a/OTKCrow/OpenGL/Shader.cs b/OTKCrow/OpenGL/Shader.cs deleted file mode 100644 index 54f9140a..00000000 --- a/OTKCrow/OpenGL/Shader.cs +++ /dev/null @@ -1,317 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Reflection; -using OpenTK; -using OpenTK.Graphics.OpenGL; -using System.Collections.Generic; - -namespace Tetra -{ - public class Shader : IDisposable - { - #region CTOR - public Shader () - { - Init (); - } - public Shader (string vertResPath, string fragResPath = null, string geomResPath = null) - { - VertSourcePath = vertResPath; - FragSourcePath = fragResPath; - GeomSourcePath = geomResPath; - - loadSourcesFiles (); - - Init (); - } - #endregion - - public string VertSourcePath, - FragSourcePath, - GeomSourcePath; - #region Sources - protected string _vertSource = @" - #version 330 - precision lowp float; - - uniform mat4 mvp; - - layout(location = 0) in vec3 in_position; - layout(location = 1) in vec2 in_tex; - - out vec2 texCoord; - - void main(void) - { - texCoord = in_tex; - gl_Position = mvp * vec4(in_position, 1.0); - }"; - - protected string _fragSource = @" - #version 330 - precision lowp float; - - uniform sampler2D tex; - - in vec2 texCoord; - out vec4 out_frag_color; - - void main(void) - { - out_frag_color = texture( tex, texCoord); - }"; - string _geomSource = @""; -// #version 330 -// layout(triangles) in; -// layout(triangle_strip, max_vertices=3) out; -// void main() -// { -// for(int i=0; i<3; i++) -// { -// gl_Position = gl_in[i].gl_Position; -// EmitVertex(); -// } -// EndPrimitive(); -// }"; - #endregion - - #region Private and protected fields - public int vsId, fsId, gsId, pgmId, mvpLocation; - - Matrix4 mvp = Matrix4.Identity; - #endregion - - - #region Public properties - public virtual string vertSource - { - get { return _vertSource;} - set { _vertSource = value; } - } - public virtual string fragSource - { - get { return _fragSource;} - set { _fragSource = value; } - } - public virtual string geomSource - { - get { return _geomSource; } - set { _geomSource = value; } - } - - public virtual Matrix4 MVP{ - set { mvp = value; } - get { return mvp; } - } - #endregion - - #region Public functions - /// - /// configure sources and compile - /// - public virtual void Init() - { - Compile (); - } - public void Reload(){ - loadSourcesFiles (); - Compile (); - } - public void SetSource(ShaderType shaderType, string _source){ - switch (shaderType) { - case ShaderType.FragmentShader: - fragSource = _source; - return; - case ShaderType.VertexShader: - vertSource = _source; - return; - case ShaderType.GeometryShader: - geomSource = _source; - return; - } - } - public string GetSource(ShaderType shaderType){ - switch (shaderType) { - case ShaderType.FragmentShader: - return fragSource; - case ShaderType.VertexShader: - return vertSource; - case ShaderType.GeometryShader: - return geomSource; - } - return ""; - } - public string GetSourcePath(ShaderType shaderType){ - switch (shaderType) { - case ShaderType.FragmentShader: - return FragSourcePath; - case ShaderType.VertexShader: - return VertSourcePath; - case ShaderType.GeometryShader: - return GeomSourcePath; - } - return ""; - } - public virtual void Compile() - { - Dispose (); - - pgmId = GL.CreateProgram(); - - if (!string.IsNullOrEmpty(vertSource)) - { - vsId = GL.CreateShader(ShaderType.VertexShader); - compileShader(vsId, vertSource); - } - if (!string.IsNullOrEmpty(fragSource)) - { - fsId = GL.CreateShader(ShaderType.FragmentShader); - compileShader(fsId, fragSource); - - } - if (!string.IsNullOrEmpty(geomSource)) - { - gsId = GL.CreateShader(ShaderType.GeometryShader); - compileShader(gsId,geomSource); - } - - if (vsId != 0) - GL.AttachShader(pgmId, vsId); - if (fsId != 0) - GL.AttachShader(pgmId, fsId); - if (gsId != 0) - GL.AttachShader(pgmId, gsId); - - BindVertexAttributes (); - - string info; - GL.LinkProgram(pgmId); - GL.GetProgramInfoLog(pgmId, out info); - - if (!string.IsNullOrEmpty (info)) { - Debug.WriteLine ("Linkage:"); - Debug.WriteLine (info); - } - - info = null; - - GL.ValidateProgram(pgmId); - GL.GetProgramInfoLog(pgmId, out info); - if (!string.IsNullOrEmpty (info)) { - Debug.WriteLine ("Validation:"); - Debug.WriteLine (info); - } - - GL.UseProgram (pgmId); - - GetUniformLocations (); - BindSamplesSlots (); - - Disable (); - } - - protected virtual void BindVertexAttributes() - { - GL.BindAttribLocation(pgmId, 0, "in_position"); - GL.BindAttribLocation(pgmId, 1, "in_tex"); - } - protected virtual void GetUniformLocations() - { - mvpLocation = GL.GetUniformLocation(pgmId, "mvp"); - } - protected virtual void BindSamplesSlots(){ - GL.Uniform1(GL.GetUniformLocation (pgmId, "tex"), 0); - } - public void SetMVP(Matrix4 _mvp){ - GL.UniformMatrix4(mvpLocation, false, ref _mvp); - } - public virtual void Enable(){ - GL.UseProgram (pgmId); - } - public virtual void Disable(){ - GL.UseProgram (0); - } - public static void Enable(Shader s) - { - if (s == null) - return; - s.Enable (); - } - public static void Disable(Shader s) - { - if (s == null) - return; - s.Disable (); - } - #endregion - - void loadSourcesFiles(){ - Stream s; - - if (!string.IsNullOrEmpty (VertSourcePath)) { - s = Crow.Interface.GetStreamFromPath (VertSourcePath); - if (s != null) { - using (StreamReader sr = new StreamReader (s)) { - vertSource = sr.ReadToEnd (); - } - } - } - - if (!string.IsNullOrEmpty (FragSourcePath)) { - s = Crow.Interface.GetStreamFromPath (FragSourcePath); - if (s != null) { - using (StreamReader sr = new StreamReader (s)) { - fragSource = sr.ReadToEnd (); - } - } - } - - if (!string.IsNullOrEmpty (GeomSourcePath)) { - s = Crow.Interface.GetStreamFromPath (GeomSourcePath); - if (s != null) { - using (StreamReader sr = new StreamReader (s)) { - geomSource = sr.ReadToEnd (); - } - } - } - } - void compileShader(int shader, string source) - { - GL.ShaderSource(shader, source); - GL.CompileShader(shader); - - string info; - GL.GetShaderInfoLog(shader, out info); - Debug.WriteLine(info); - - int compileResult; - GL.GetShader(shader, ShaderParameter.CompileStatus, out compileResult); - if (compileResult != 1) - { - Debug.WriteLine("Compile Error!"); - Debug.WriteLine(source); - } - } - public override string ToString () - { - return string.Format ("{0} {1} {2}", VertSourcePath, FragSourcePath, GeomSourcePath); - } - - #region IDisposable implementation - public virtual void Dispose () - { - if (GL.IsProgram (pgmId)) - GL.DeleteProgram (pgmId); - - if (GL.IsShader (vsId)) - GL.DeleteShader (vsId); - if (GL.IsShader (fsId)) - GL.DeleteShader (fsId); - if (GL.IsShader (gsId)) - GL.DeleteShader (gsId); - } - #endregion - } -} - diff --git a/OTKCrow/OpenGL/vaoMesh.cs b/OTKCrow/OpenGL/vaoMesh.cs deleted file mode 100644 index b076362c..00000000 --- a/OTKCrow/OpenGL/vaoMesh.cs +++ /dev/null @@ -1,145 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using System.Collections.Generic; -using OpenTK; -using OpenTK.Graphics.OpenGL; -//using System.Drawing; -using System.Drawing.Imaging; -using System.IO; -using System.Threading; -using System.Diagnostics; -using System.Linq; - -namespace Crow -{ - public class vaoMesh : IDisposable - { - public int vaoHandle, - positionVboHandle, - texVboHandle, - eboHandle; - - public Vector3[] positions; - public Vector2[] texCoords; - public int[] indices; - - public vaoMesh() - { - } - - public vaoMesh (Vector3[] _positions, Vector2[] _texCoord, int[] _indices) - { - positions = _positions; - texCoords = _texCoord; - indices = _indices; - - CreateVBOs (); - CreateVAOs (); - } - - public vaoMesh (float x, float y, float z, float width, float height, float TileX = 1f, float TileY = 1f) - { - positions = - new Vector3[] { - new Vector3 (x - width / 2, y + height / 2, z), - new Vector3 (x - width / 2, y - height / 2, z), - new Vector3 (x + width / 2, y + height / 2, z), - new Vector3 (x + width / 2, y - height / 2, z) - }; - texCoords = new Vector2[] { - new Vector2 (0, TileY), - new Vector2 (0, 0), - new Vector2 (TileX, TileY), - new Vector2 (TileX, 0) - }; - indices = new int[] { 0, 1, 2, 3 }; - - CreateVBOs (); - CreateVAOs (); - } - public void CreateBuffers(){ - CreateVBOs (); - CreateVAOs (); - } - protected void CreateVBOs() - { - positionVboHandle = GL.GenBuffer(); - GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); - GL.BufferData(BufferTarget.ArrayBuffer, - new IntPtr(positions.Length * Vector3.SizeInBytes), - positions, BufferUsageHint.StaticDraw); - - if (texCoords != null) { - texVboHandle = GL.GenBuffer (); - GL.BindBuffer (BufferTarget.ArrayBuffer, texVboHandle); - GL.BufferData (BufferTarget.ArrayBuffer, - new IntPtr (texCoords.Length * Vector2.SizeInBytes), - texCoords, BufferUsageHint.StaticDraw); - } - - GL.BindBuffer(BufferTarget.ArrayBuffer, 0); - - if (indices != null) { - eboHandle = GL.GenBuffer (); - GL.BindBuffer (BufferTarget.ElementArrayBuffer, eboHandle); - GL.BufferData (BufferTarget.ElementArrayBuffer, - new IntPtr (sizeof(uint) * indices.Length), - indices, BufferUsageHint.StaticDraw); - GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); - } - } - protected void CreateVAOs() - { - vaoHandle = GL.GenVertexArray(); - GL.BindVertexArray(vaoHandle); - - GL.EnableVertexAttribArray(0); - GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); - GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); - - if (texCoords != null) { - GL.EnableVertexAttribArray (1); - GL.BindBuffer (BufferTarget.ArrayBuffer, texVboHandle); - GL.VertexAttribPointer (1, 2, VertexAttribPointerType.Float, true, Vector2.SizeInBytes, 0); - } - if (indices != null) - GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle); - - GL.BindVertexArray(0); - } - - public void Render(BeginMode _primitiveType){ - GL.BindVertexArray(vaoHandle); - if (indices == null) - GL.DrawArrays (_primitiveType, 0, positions.Length); - else - GL.DrawElements(_primitiveType, indices.Length, - DrawElementsType.UnsignedInt, IntPtr.Zero); - GL.BindVertexArray (0); - } - public void Render(BeginMode _primitiveType, int[] _customIndices){ - GL.BindVertexArray(vaoHandle); - GL.DrawElements(_primitiveType, _customIndices.Length, - DrawElementsType.UnsignedInt, _customIndices); - GL.BindVertexArray (0); - } - public void Render(BeginMode _primitiveType, int instances){ - - GL.BindVertexArray(vaoHandle); - GL.DrawElementsInstanced(_primitiveType, indices.Length, - DrawElementsType.UnsignedInt, IntPtr.Zero, instances); - GL.BindVertexArray (0); - } - - #region IDisposable implementation - public void Dispose () - { - GL.DeleteBuffer (positionVboHandle); - GL.DeleteBuffer (texVboHandle); - GL.DeleteBuffer (eboHandle); - GL.DeleteVertexArray (vaoHandle); - } - #endregion - - } -} \ No newline at end of file diff --git a/OTKCrow/OpenTKGameWindow.cs b/OTKCrow/OpenTKGameWindow.cs deleted file mode 100644 index 1dcd6066..00000000 --- a/OTKCrow/OpenTKGameWindow.cs +++ /dev/null @@ -1,331 +0,0 @@ -// -// OpenTKGameWindow.cs -// -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2016 jp -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Threading; -using System.Xml; -using Cairo; -using OpenTK; -using OpenTK.Graphics.OpenGL; - -namespace Crow -{ - public class OpenTKGameWindow : GameWindow, IValueChange - { - #region IValueChange implementation - public event EventHandler ValueChanged; - public virtual void NotifyValueChanged(string MemberName, object _value) - { - if (ValueChanged != null) - ValueChanged.Invoke(this, new ValueChangeEventArgs(MemberName, _value)); - } - #endregion - - public Interface CrowInterface; - - #region FPS - int frameCpt = 0; - int _fps = 0; - - public int fps { - get { return _fps; } - set { - if (_fps == value) - return; - - _fps = value; - - if (_fps > fpsMax) { - fpsMax = _fps; - ValueChanged.Raise(this, new ValueChangeEventArgs ("fpsMax", fpsMax)); - } else if (_fps < fpsMin) { - fpsMin = _fps; - ValueChanged.Raise(this, new ValueChangeEventArgs ("fpsMin", fpsMin)); - } - - ValueChanged.Raise(this, new ValueChangeEventArgs ("fps", _fps)); - #if MEASURE_TIME - ValueChanged.Raise (this, new ValueChangeEventArgs ("update", - this.CrowInterface.clippingTime.ElapsedTicks.ToString () + " ticks")); - ValueChanged.Raise (this, new ValueChangeEventArgs ("layouting", - this.CrowInterface.layoutTime.ElapsedTicks.ToString () + " ticks")); - ValueChanged.Raise (this, new ValueChangeEventArgs ("drawing", - this.CrowInterface.drawingTime.ElapsedTicks.ToString () + " ticks")); - #endif - } - } - - public int fpsMin = int.MaxValue; - public int fpsMax = 0; - - void resetFps () - { - fpsMin = int.MaxValue; - fpsMax = 0; - _fps = 0; - } - public string update = ""; - public string drawing = ""; - public string layouting = ""; - #endregion - - #region ctor - public OpenTKGameWindow(int _width = 800, int _height = 600, string _title="Crow", - int colors = 32, int depth = 24, int stencil = 0, int samples = 1, - int major=3, int minor=3) - : this(_width, _height, new OpenTK.Graphics.GraphicsMode(colors, depth, stencil, samples), - _title,GameWindowFlags.Default,DisplayDevice.Default, - major,minor,OpenTK.Graphics.GraphicsContextFlags.Default) - { - } - public OpenTKGameWindow (int width, int height, OpenTK.Graphics.GraphicsMode mode, string title, GameWindowFlags options, DisplayDevice device, int major, int minor, OpenTK.Graphics.GraphicsContextFlags flags) - : base(width,height,mode,title,options,device,major,minor,flags) - { - CrowInterface = new Interface (); - - Thread t = new Thread (interfaceThread); - t.IsBackground = true; - t.Start (); - } - - #endregion - - void interfaceThread() - { - CrowInterface.Quit += Quit; - CrowInterface.MouseCursorChanged += CrowInterface_MouseCursorChanged; - while (CrowInterface.ClientRectangle.Size.Width == 0) - Thread.Sleep (5); - - while (true) { - CrowInterface.Update (); - Thread.Sleep (1); - } - } - - public void Quit (object sender, EventArgs e) - { - this.Exit (); - } - void CrowInterface_MouseCursorChanged (object sender, MouseCursorChangedEventArgs e) - { - this.Cursor = new MouseCursor( - (int)e.NewCursor.Xhot, - (int)e.NewCursor.Yhot, - (int)e.NewCursor.Width, - (int)e.NewCursor.Height, - e.NewCursor.data); - } - - #region Events - //those events are raised only if mouse isn't in a graphic object - public event EventHandler MouseWheelChanged; - public event EventHandler MouseButtonUp; - public event EventHandler MouseButtonDown; - public event EventHandler MouseClick; - public event EventHandler MouseMove; - public event EventHandler KeyboardKeyDown; - public event EventHandler KeyboardKeyUp; - - #endregion - - #region graphic context - int texID; - Tetra.Shader shader; - public static Crow.vaoMesh quad; - - void createContext() - { - #region Create texture - if (GL.IsTexture(texID)) - GL.DeleteTexture (texID); - GL.GenTextures(1, out texID); - GL.ActiveTexture (TextureUnit.Texture0); - GL.BindTexture(TextureTarget.Texture2D, texID); - - GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, - ClientRectangle.Width, ClientRectangle.Height, 0, - OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, CrowInterface.bmp); - - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); - - GL.BindTexture(TextureTarget.Texture2D, 0); - #endregion - } - void OpenGLDraw() - { - bool blend, depthTest; - GL.GetBoolean (GetPName.Blend, out blend); - GL.GetBoolean (GetPName.DepthTest, out depthTest); - GL.Enable (EnableCap.Blend); - GL.Disable (EnableCap.DepthTest); - - shader.Enable (); - GL.ActiveTexture (TextureUnit.Texture0); - GL.BindTexture (TextureTarget.Texture2D, texID); - lock (CrowInterface.RenderMutex) { - if (CrowInterface.IsDirty) { - GL.TexSubImage2D (TextureTarget.Texture2D, 0, - CrowInterface.DirtyRect.Left, CrowInterface.DirtyRect.Top, - CrowInterface.DirtyRect.Width, CrowInterface.DirtyRect.Height, - OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, CrowInterface.dirtyBmp); - CrowInterface.IsDirty = false; - } - } - quad.Render (BeginMode.TriangleStrip); - GL.BindTexture(TextureTarget.Texture2D, 0); - shader.Disable (); - if (!blend) - GL.Disable (EnableCap.Blend); - if (depthTest) - GL.Enable (EnableCap.DepthTest); - } - #endregion - - public virtual void OnRender(FrameEventArgs e) - { - } - public virtual void GLClear() - { - GL.Clear (ClearBufferMask.ColorBufferBit); - } - - #region Game win overrides - protected override void OnLoad(EventArgs e) - { - base.OnLoad(e); - - this.KeyPress += new EventHandler(OpenTKGameWindow_KeyPress); - Keyboard.KeyDown += new EventHandler(Keyboard_KeyDown); - Keyboard.KeyUp += new EventHandler(Keyboard_KeyUp); - Mouse.WheelChanged += new EventHandler(Mouse_WheelChanged); - Mouse.ButtonDown += new EventHandler(Mouse_ButtonDown); - Mouse.ButtonUp += new EventHandler(Mouse_ButtonUp); - Mouse.Move += new EventHandler(Mouse_Move); - - GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); - - Console.WriteLine("\n\n*************************************"); - Console.WriteLine("GL version: " + GL.GetString (StringName.Version)); - Console.WriteLine("GL vendor: " + GL.GetString (StringName.Vendor)); - Console.WriteLine("GLSL version: " + GL.GetString (StringName.ShadingLanguageVersion)); - Console.WriteLine("*************************************\n"); - - shader = new Tetra.Shader (); - shader.Enable(); - shader.SetMVP(OpenTK.Matrix4.CreateOrthographicOffCenter (-0.5f, 0.5f, -0.5f, 0.5f, 1, -1)); - GL.UseProgram(0); - quad = new Crow.vaoMesh (0, 0, 0, 1, 1, 1, -1); - } - - protected override void OnUpdateFrame(FrameEventArgs e) - { - base.OnUpdateFrame(e); - fps = (int)RenderFrequency; - - - if (frameCpt > 50) { - resetFps (); - frameCpt = 0; - GC.Collect(); - GC.WaitForPendingFinalizers(); - NotifyValueChanged("memory", GC.GetTotalMemory (false).ToString()); - } - frameCpt++; - //CrowInterface.Update (); - } - protected override void OnRenderFrame(FrameEventArgs e) - { - GLClear (); - - base.OnRenderFrame(e); - - OnRender (e); - OpenGLDraw (); - - SwapBuffers (); - } - - protected override void OnResize(EventArgs e) - { - base.OnResize (e); - CrowInterface.ProcessResize( - new Rectangle( - 0, - 0, - this.ClientRectangle.Width, - this.ClientRectangle.Height)); - createContext (); - GL.Viewport (0, 0, ClientRectangle.Width, ClientRectangle.Height); - } - #endregion - - #region Mouse Handling - void update_mouseButtonStates(ref MouseState e, OpenTK.Input.MouseState otk_e){ - for (int i = 0; i < MouseState.MaxButtons; i++) { - if (otk_e.IsButtonDown ((OpenTK.Input.MouseButton)i)) - e.EnableBit (i); - } - } - void Mouse_Move(object sender, OpenTK.Input.MouseMoveEventArgs otk_e) - { - if (!CrowInterface.ProcessMouseMove (otk_e.X, otk_e.Y)) - MouseMove.Raise (sender, otk_e); - } - void Mouse_ButtonUp(object sender, OpenTK.Input.MouseButtonEventArgs otk_e) - { - if (!CrowInterface.ProcessMouseButtonUp ((int)otk_e.Button)) - MouseButtonUp.Raise (sender, otk_e); - } - void Mouse_ButtonDown(object sender, OpenTK.Input.MouseButtonEventArgs otk_e) - { - if (!CrowInterface.ProcessMouseButtonDown ((int)otk_e.Button)) - MouseButtonDown.Raise (sender, otk_e); - } - void Mouse_WheelChanged(object sender, OpenTK.Input.MouseWheelEventArgs otk_e) - { - if (!CrowInterface.ProcessMouseWheelChanged (otk_e.DeltaPrecise)) - MouseWheelChanged.Raise (sender, otk_e); - } - #endregion - - #region keyboard Handling - void Keyboard_KeyDown(object sender, OpenTK.Input.KeyboardKeyEventArgs otk_e) - { - //if (!CrowInterface.ProcessKeyDown((int)otk_e.Key)) - KeyboardKeyDown.Raise (this, otk_e); - } - void Keyboard_KeyUp(object sender, OpenTK.Input.KeyboardKeyEventArgs otk_e) - { - //if (!CrowInterface.ProcessKeyUp((int)otk_e.Key)) - KeyboardKeyUp.Raise (this, otk_e); - } - void OpenTKGameWindow_KeyPress (object sender, OpenTK.KeyPressEventArgs e) - { - CrowInterface.ProcessKeyPress (e.KeyChar); - } - #endregion - } -} diff --git a/Tests/GLCrow.cs b/Tests/GLCrow.cs deleted file mode 100644 index 1ede0268..00000000 --- a/Tests/GLCrow.cs +++ /dev/null @@ -1,152 +0,0 @@ -// -// GLCrow.cs -// -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2016 jp -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -using System; -using GLC; -using Pencil.Gaming; -using System.Collections.Generic; -using Crow; -using System.Linq; -using System.IO; - -namespace Tests -{ - public class GLCrow : GLC.Window - { - int idx = 0; - string [] testFiles; - - public int intValue = 25; - - public int IntValue { - get { - return intValue; - } - set { - intValue = value; - NotifyValueChanged ("IntValue", intValue); - } - } - void onSpinnerValueChange(object sender, ValueChangeEventArgs e){ - if (e.MemberName != "Value") - return; - intValue = Convert.ToInt32(e.NewValue); - } - void change_alignment(object sender, EventArgs e){ - RadioButton rb = sender as RadioButton; - if (rb == null) - return; - NotifyValueChanged ("alignment", Enum.Parse(typeof(Alignment), rb.Caption)); - } - public IList List2 = new List(new string[] - { - "string1", - "string2" - //"string3", - //"string4", - //"string5", - //"string6", - //"string7", - //"string8", - //"string8", - //"string8", - //"string8", - //"string8", - //"string8", - //"string9" - } - ); - IList testList = Color.ColorDic.ToList(); - public IList TestList { - set{ - testList = value; - NotifyValueChanged ("TestList", testList); - } - get { return testList; } - } - - void OnClear (object sender, MouseButtonEventArgs e) => TestList = null; - - void OnLoadList (object sender, MouseButtonEventArgs e) => TestList = Color.ColorDic.ToList(); - - - [STAThread] - public static void Main (string [] args) - { - using (GLCrow w = new GLCrow ()) { - w.Run (30); - } - } - public object data = "datas"; - public object Datas { - get { return data; } - } - void onSetDataToNull (object sender, MouseButtonEventArgs e) { - data = null; - NotifyValueChanged ("Datas", null); - } - public override void OnLoad () - { - //testFiles = new string [] { @"Interfaces/Divers/testBind0.crow" }; - testFiles = new string [] { @"Interfaces/Divers/testCombobox.crow" }; - testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/GraphicObject", "*.crow")).ToArray (); - //testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/basicTests", "*.crow")).ToArray (); - testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Container", "*.crow")).ToArray (); - testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Group", "*.crow")).ToArray (); - testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Stack", "*.crow")).ToArray (); - testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Splitter", "*.crow")).ToArray (); - testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Expandable", "*.crow")).ToArray (); - testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Divers", "*.crow")).ToArray (); - - //testFiles = Directory.GetFiles(@"Interfaces", "*.crow").Concat(testFiles).ToArray(); - this.Title = testFiles [idx]; - CrowInterface.LoadInterface(testFiles[idx]).DataSource = this; - - //CrowInterface.LoadInterface ("#Tests.ui.fps.crow").DataSource = this; - } - public GLCrow(int _width = 800, int _height=600) - :base(_width, _height, 32, 24, 0, 8, "TestWin") - { - - } - - protected override void OnKeyEvent (GlfwWindowPtr wnd, Pencil.Gaming.Key key, int scanCode, KeyAction action, Pencil.Gaming.KeyModifiers mods) - { - base.OnKeyEvent (wnd, key, scanCode, action, mods); - switch (action) { - case KeyAction.Release: - - break; - case KeyAction.Press: - if (key == Pencil.Gaming.Key.Space) { - CrowInterface.ClearInterface (); - idx++; - if (idx == testFiles.Length) - idx = 0; - this.Title = testFiles [idx]; - CrowInterface.LoadInterface(testFiles[idx]).DataSource = this; - - } - break; - } - } - } -} - diff --git a/Tests/GOLIBTests.cs b/Tests/GOLIBTests.cs deleted file mode 100644 index 53f36590..00000000 --- a/Tests/GOLIBTests.cs +++ /dev/null @@ -1,277 +0,0 @@ -#define MONO_CAIRO_DEBUG_DISPOSE - - -using System; -using System.Runtime.InteropServices; -using OpenTK; -using OpenTK.Graphics.OpenGL; - -using System.Diagnostics; - -//using GGL; -using Crow; -using System.Threading; -using System.Collections.Generic; -using System.Linq; - - -namespace test -{ - class GOLIBTests : OpenTKGameWindow, IValueChange - { - #region IValueChange implementation - public event EventHandler ValueChanged; - public virtual void NotifyValueChanged(string MemberName, object _value) - { - ValueChanged.Raise(this, new ValueChangeEventArgs(MemberName, _value)); - } - #endregion - - public GOLIBTests () - : base(800, 600,"test: press spacebar to toogle test files") - { - //VSync = VSyncMode.Off; - Interface.CurrentInterface = CrowInterface; - GraphicObject obj = CrowInterface.LoadInterface("Interfaces/" + testFiles[idx]); - obj.DataSource = this; - - } - - int frameCpt = 0; - int idx = 0; - string[] testFiles = { - "testMsgBox.goml", - "testCombobox.goml", - "testExpandable.goml", - "test_Listbox.goml", - "6.crow", - "testGroupBox.goml", - "1.crow", - "5.crow", - "testCheckbox.goml", - "testWindow.goml", - "fps.goml", - "testTabView.crow", - "0.crow", - "testImage.crow", - "testOutOfClipUpdate.crow", -// "test_Listbox.goml", -// "testTreeView.crow", - "1.crow", - "clip4.crow", - "clip3.crow", - "clip2.crow", - "clip0.crow", - "clip1.crow", - "testPopper.goml", - "testTextBox.crow", - "testColorList.crow", -// "5.crow", -// "testCombobox.goml", -// "testPopper.goml", - "testTextBox.crow", -// "testColorList.crow", - "4.crow", - "testSpinner.goml", - "testScrollbar.goml", - "testGrid.goml", - "testButton.crow", - "testBorder.goml", -// "testButton2.crow", - "test2WayBinding.crow", - "test4.goml", - "2.crow", - "test1.goml", - "testWindow2.goml", - - "testWindow3.goml", - "testLabel.goml", - "testAll.goml", -// "testSpinner.goml", -// "testRadioButton2.goml", - "testContainer.goml", - "testRadioButton.goml", - -// "testMeter.goml", - }; - - #region FPS - int _fps = 0; - - public int fps { - get { return _fps; } - set { - if (_fps == value) - return; - - _fps = value; - - if (_fps > fpsMax) { - fpsMax = _fps; - ValueChanged.Raise(this, new ValueChangeEventArgs ("fpsMax", fpsMax)); - } else if (_fps < fpsMin) { - fpsMin = _fps; - ValueChanged.Raise(this, new ValueChangeEventArgs ("fpsMin", fpsMin)); - } - - ValueChanged.Raise(this, new ValueChangeEventArgs ("fps", _fps)); - #if MEASURE_TIME - ValueChanged.Raise (this, new ValueChangeEventArgs ("update", - this.CrowInterface.updateTime.ElapsedTicks.ToString () + " ticks")); - ValueChanged.Raise (this, new ValueChangeEventArgs ("layouting", - this.CrowInterface.layoutTime.ElapsedTicks.ToString () + " ticks")); - ValueChanged.Raise (this, new ValueChangeEventArgs ("drawing", - this.CrowInterface.drawingTime.ElapsedTicks.ToString () + " ticks")); - #endif - } - } - - public int fpsMin = int.MaxValue; - public int fpsMax = 0; - - void resetFps () - { - fpsMin = int.MaxValue; - fpsMax = 0; - _fps = 0; - } - public string update = ""; - public string drawing = ""; - public string layouting = ""; - public Alignment alignment = Alignment.Left; - #endregion - - public int intValue = 25; - - public int IntValue { - get { - return intValue; - } - set { - intValue = value; - NotifyValueChanged ("IntValue", intValue); - } - } - void onSpinnerValueChange(object sender, ValueChangeEventArgs e){ - if (e.MemberName != "Value") - return; - intValue = Convert.ToInt32(e.NewValue); - } - void change_alignment(object sender, EventArgs e){ - RadioButton rb = sender as RadioButton; - if (rb == null) - return; - NotifyValueChanged ("alignment", Enum.Parse(typeof(Alignment), rb.Caption)); - } - public IList List2 = new List(new string[] - { - "string1", - "string2", - "string3", - "string4", - "string5", - "string6", - "string7", - "string8", - "string8", - "string8", - "string8", - "string8", - "string8", - "string9" - } - ); - IList testList = Color.ColorDic.ToList(); - public IList TestList { - set{ - testList = value; - NotifyValueChanged ("TestList", testList); - } - get { return testList; } - } - void OnClear (object sender, MouseButtonEventArgs e) => TestList = null; - - void OnLoadList (object sender, MouseButtonEventArgs e) { - TestList = Color.ColorDic.ToList(); - } - - void GOLIBTests_KeyboardKeyDown1 (object sender, OpenTK.Input.KeyboardKeyEventArgs e) - { - if (e.Key == OpenTK.Input.Key.Escape) { - Quit (null, null); - return; - } else if (e.Key == OpenTK.Input.Key.L) { - TestList.Add ("new string"); - NotifyValueChanged ("TestList", TestList); - return; - } else if (e.Key == OpenTK.Input.Key.W) { - GraphicObject w = CrowInterface.LoadInterface("Interfaces/testWindow.goml"); - w.DataSource = this; - return; - } - CrowInterface.ClearInterface (); - idx++; - if (idx == testFiles.Length) - idx = 0; - this.Title = testFiles [idx]; - GraphicObject obj = CrowInterface.LoadInterface("Interfaces/" + testFiles[idx]); - obj.DataSource = this; - } - -// protected override void OnUpdateFrame (FrameEventArgs e) -// { -// //if (frameCpt % 8 == 0) -// base.OnUpdateFrame (e); -// -// fps = (int)RenderFrequency; -// -// -// if (frameCpt > 50) { -// resetFps (); -// frameCpt = 0; -// GC.Collect(); -// GC.WaitForPendingFinalizers(); -// NotifyValueChanged("memory", GC.GetTotalMemory (false).ToString()); -// } -// frameCpt++; -// } - void onButClick(object send, MouseButtonEventArgs e) - { - Console.WriteLine ("button clicked:" + send.ToString()); - } - void onAddTabButClick(object sender, MouseButtonEventArgs e){ - - TabView tv = CrowInterface.FindByName("tabview1") as TabView; - if (tv == null) - return; - tv.AddChild (new TabItem () { Caption = "NewTab" }); - } - [STAThread] - static void Main () - { - Console.WriteLine ("starting example"); - Gtk.Application.Init (); - GOLIBTests win = new GOLIBTests (); - win.KeyPressEvent += win.Win_KeyPressEvent; - - Gtk.Application.Run (); - } - void onMsgBoxOk(object sender, EventArgs e){ - Debug.WriteLine ("OK"); - } - void onMsgBoxCancel(object sender, EventArgs e) - { - Debug.WriteLine ("cancel"); - } - void Win_KeyPressEvent (object o, Gtk.KeyPressEventArgs args) - { - CrowInterface.ClearInterface (); - idx++; - if (idx == testFiles.Length) - idx = 0; - this.Title = testFiles [idx]; - GraphicObject obj = CrowInterface.LoadInterface("Interfaces/" + testFiles[idx]); - obj.DataSource = this; - } - } -} diff --git a/Tests/OpenGL/Shader.cs b/Tests/OpenGL/Shader.cs new file mode 100644 index 00000000..54f9140a --- /dev/null +++ b/Tests/OpenGL/Shader.cs @@ -0,0 +1,317 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using OpenTK; +using OpenTK.Graphics.OpenGL; +using System.Collections.Generic; + +namespace Tetra +{ + public class Shader : IDisposable + { + #region CTOR + public Shader () + { + Init (); + } + public Shader (string vertResPath, string fragResPath = null, string geomResPath = null) + { + VertSourcePath = vertResPath; + FragSourcePath = fragResPath; + GeomSourcePath = geomResPath; + + loadSourcesFiles (); + + Init (); + } + #endregion + + public string VertSourcePath, + FragSourcePath, + GeomSourcePath; + #region Sources + protected string _vertSource = @" + #version 330 + precision lowp float; + + uniform mat4 mvp; + + layout(location = 0) in vec3 in_position; + layout(location = 1) in vec2 in_tex; + + out vec2 texCoord; + + void main(void) + { + texCoord = in_tex; + gl_Position = mvp * vec4(in_position, 1.0); + }"; + + protected string _fragSource = @" + #version 330 + precision lowp float; + + uniform sampler2D tex; + + in vec2 texCoord; + out vec4 out_frag_color; + + void main(void) + { + out_frag_color = texture( tex, texCoord); + }"; + string _geomSource = @""; +// #version 330 +// layout(triangles) in; +// layout(triangle_strip, max_vertices=3) out; +// void main() +// { +// for(int i=0; i<3; i++) +// { +// gl_Position = gl_in[i].gl_Position; +// EmitVertex(); +// } +// EndPrimitive(); +// }"; + #endregion + + #region Private and protected fields + public int vsId, fsId, gsId, pgmId, mvpLocation; + + Matrix4 mvp = Matrix4.Identity; + #endregion + + + #region Public properties + public virtual string vertSource + { + get { return _vertSource;} + set { _vertSource = value; } + } + public virtual string fragSource + { + get { return _fragSource;} + set { _fragSource = value; } + } + public virtual string geomSource + { + get { return _geomSource; } + set { _geomSource = value; } + } + + public virtual Matrix4 MVP{ + set { mvp = value; } + get { return mvp; } + } + #endregion + + #region Public functions + /// + /// configure sources and compile + /// + public virtual void Init() + { + Compile (); + } + public void Reload(){ + loadSourcesFiles (); + Compile (); + } + public void SetSource(ShaderType shaderType, string _source){ + switch (shaderType) { + case ShaderType.FragmentShader: + fragSource = _source; + return; + case ShaderType.VertexShader: + vertSource = _source; + return; + case ShaderType.GeometryShader: + geomSource = _source; + return; + } + } + public string GetSource(ShaderType shaderType){ + switch (shaderType) { + case ShaderType.FragmentShader: + return fragSource; + case ShaderType.VertexShader: + return vertSource; + case ShaderType.GeometryShader: + return geomSource; + } + return ""; + } + public string GetSourcePath(ShaderType shaderType){ + switch (shaderType) { + case ShaderType.FragmentShader: + return FragSourcePath; + case ShaderType.VertexShader: + return VertSourcePath; + case ShaderType.GeometryShader: + return GeomSourcePath; + } + return ""; + } + public virtual void Compile() + { + Dispose (); + + pgmId = GL.CreateProgram(); + + if (!string.IsNullOrEmpty(vertSource)) + { + vsId = GL.CreateShader(ShaderType.VertexShader); + compileShader(vsId, vertSource); + } + if (!string.IsNullOrEmpty(fragSource)) + { + fsId = GL.CreateShader(ShaderType.FragmentShader); + compileShader(fsId, fragSource); + + } + if (!string.IsNullOrEmpty(geomSource)) + { + gsId = GL.CreateShader(ShaderType.GeometryShader); + compileShader(gsId,geomSource); + } + + if (vsId != 0) + GL.AttachShader(pgmId, vsId); + if (fsId != 0) + GL.AttachShader(pgmId, fsId); + if (gsId != 0) + GL.AttachShader(pgmId, gsId); + + BindVertexAttributes (); + + string info; + GL.LinkProgram(pgmId); + GL.GetProgramInfoLog(pgmId, out info); + + if (!string.IsNullOrEmpty (info)) { + Debug.WriteLine ("Linkage:"); + Debug.WriteLine (info); + } + + info = null; + + GL.ValidateProgram(pgmId); + GL.GetProgramInfoLog(pgmId, out info); + if (!string.IsNullOrEmpty (info)) { + Debug.WriteLine ("Validation:"); + Debug.WriteLine (info); + } + + GL.UseProgram (pgmId); + + GetUniformLocations (); + BindSamplesSlots (); + + Disable (); + } + + protected virtual void BindVertexAttributes() + { + GL.BindAttribLocation(pgmId, 0, "in_position"); + GL.BindAttribLocation(pgmId, 1, "in_tex"); + } + protected virtual void GetUniformLocations() + { + mvpLocation = GL.GetUniformLocation(pgmId, "mvp"); + } + protected virtual void BindSamplesSlots(){ + GL.Uniform1(GL.GetUniformLocation (pgmId, "tex"), 0); + } + public void SetMVP(Matrix4 _mvp){ + GL.UniformMatrix4(mvpLocation, false, ref _mvp); + } + public virtual void Enable(){ + GL.UseProgram (pgmId); + } + public virtual void Disable(){ + GL.UseProgram (0); + } + public static void Enable(Shader s) + { + if (s == null) + return; + s.Enable (); + } + public static void Disable(Shader s) + { + if (s == null) + return; + s.Disable (); + } + #endregion + + void loadSourcesFiles(){ + Stream s; + + if (!string.IsNullOrEmpty (VertSourcePath)) { + s = Crow.Interface.GetStreamFromPath (VertSourcePath); + if (s != null) { + using (StreamReader sr = new StreamReader (s)) { + vertSource = sr.ReadToEnd (); + } + } + } + + if (!string.IsNullOrEmpty (FragSourcePath)) { + s = Crow.Interface.GetStreamFromPath (FragSourcePath); + if (s != null) { + using (StreamReader sr = new StreamReader (s)) { + fragSource = sr.ReadToEnd (); + } + } + } + + if (!string.IsNullOrEmpty (GeomSourcePath)) { + s = Crow.Interface.GetStreamFromPath (GeomSourcePath); + if (s != null) { + using (StreamReader sr = new StreamReader (s)) { + geomSource = sr.ReadToEnd (); + } + } + } + } + void compileShader(int shader, string source) + { + GL.ShaderSource(shader, source); + GL.CompileShader(shader); + + string info; + GL.GetShaderInfoLog(shader, out info); + Debug.WriteLine(info); + + int compileResult; + GL.GetShader(shader, ShaderParameter.CompileStatus, out compileResult); + if (compileResult != 1) + { + Debug.WriteLine("Compile Error!"); + Debug.WriteLine(source); + } + } + public override string ToString () + { + return string.Format ("{0} {1} {2}", VertSourcePath, FragSourcePath, GeomSourcePath); + } + + #region IDisposable implementation + public virtual void Dispose () + { + if (GL.IsProgram (pgmId)) + GL.DeleteProgram (pgmId); + + if (GL.IsShader (vsId)) + GL.DeleteShader (vsId); + if (GL.IsShader (fsId)) + GL.DeleteShader (fsId); + if (GL.IsShader (gsId)) + GL.DeleteShader (gsId); + } + #endregion + } +} + diff --git a/Tests/OpenGL/vaoMesh.cs b/Tests/OpenGL/vaoMesh.cs new file mode 100644 index 00000000..b076362c --- /dev/null +++ b/Tests/OpenGL/vaoMesh.cs @@ -0,0 +1,145 @@ +using System; +using System.Runtime.InteropServices; +using System.Collections.Generic; +using OpenTK; +using OpenTK.Graphics.OpenGL; +//using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Threading; +using System.Diagnostics; +using System.Linq; + +namespace Crow +{ + public class vaoMesh : IDisposable + { + public int vaoHandle, + positionVboHandle, + texVboHandle, + eboHandle; + + public Vector3[] positions; + public Vector2[] texCoords; + public int[] indices; + + public vaoMesh() + { + } + + public vaoMesh (Vector3[] _positions, Vector2[] _texCoord, int[] _indices) + { + positions = _positions; + texCoords = _texCoord; + indices = _indices; + + CreateVBOs (); + CreateVAOs (); + } + + public vaoMesh (float x, float y, float z, float width, float height, float TileX = 1f, float TileY = 1f) + { + positions = + new Vector3[] { + new Vector3 (x - width / 2, y + height / 2, z), + new Vector3 (x - width / 2, y - height / 2, z), + new Vector3 (x + width / 2, y + height / 2, z), + new Vector3 (x + width / 2, y - height / 2, z) + }; + texCoords = new Vector2[] { + new Vector2 (0, TileY), + new Vector2 (0, 0), + new Vector2 (TileX, TileY), + new Vector2 (TileX, 0) + }; + indices = new int[] { 0, 1, 2, 3 }; + + CreateVBOs (); + CreateVAOs (); + } + public void CreateBuffers(){ + CreateVBOs (); + CreateVAOs (); + } + protected void CreateVBOs() + { + positionVboHandle = GL.GenBuffer(); + GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); + GL.BufferData(BufferTarget.ArrayBuffer, + new IntPtr(positions.Length * Vector3.SizeInBytes), + positions, BufferUsageHint.StaticDraw); + + if (texCoords != null) { + texVboHandle = GL.GenBuffer (); + GL.BindBuffer (BufferTarget.ArrayBuffer, texVboHandle); + GL.BufferData (BufferTarget.ArrayBuffer, + new IntPtr (texCoords.Length * Vector2.SizeInBytes), + texCoords, BufferUsageHint.StaticDraw); + } + + GL.BindBuffer(BufferTarget.ArrayBuffer, 0); + + if (indices != null) { + eboHandle = GL.GenBuffer (); + GL.BindBuffer (BufferTarget.ElementArrayBuffer, eboHandle); + GL.BufferData (BufferTarget.ElementArrayBuffer, + new IntPtr (sizeof(uint) * indices.Length), + indices, BufferUsageHint.StaticDraw); + GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); + } + } + protected void CreateVAOs() + { + vaoHandle = GL.GenVertexArray(); + GL.BindVertexArray(vaoHandle); + + GL.EnableVertexAttribArray(0); + GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); + GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); + + if (texCoords != null) { + GL.EnableVertexAttribArray (1); + GL.BindBuffer (BufferTarget.ArrayBuffer, texVboHandle); + GL.VertexAttribPointer (1, 2, VertexAttribPointerType.Float, true, Vector2.SizeInBytes, 0); + } + if (indices != null) + GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle); + + GL.BindVertexArray(0); + } + + public void Render(BeginMode _primitiveType){ + GL.BindVertexArray(vaoHandle); + if (indices == null) + GL.DrawArrays (_primitiveType, 0, positions.Length); + else + GL.DrawElements(_primitiveType, indices.Length, + DrawElementsType.UnsignedInt, IntPtr.Zero); + GL.BindVertexArray (0); + } + public void Render(BeginMode _primitiveType, int[] _customIndices){ + GL.BindVertexArray(vaoHandle); + GL.DrawElements(_primitiveType, _customIndices.Length, + DrawElementsType.UnsignedInt, _customIndices); + GL.BindVertexArray (0); + } + public void Render(BeginMode _primitiveType, int instances){ + + GL.BindVertexArray(vaoHandle); + GL.DrawElementsInstanced(_primitiveType, indices.Length, + DrawElementsType.UnsignedInt, IntPtr.Zero, instances); + GL.BindVertexArray (0); + } + + #region IDisposable implementation + public void Dispose () + { + GL.DeleteBuffer (positionVboHandle); + GL.DeleteBuffer (texVboHandle); + GL.DeleteBuffer (eboHandle); + GL.DeleteVertexArray (vaoHandle); + } + #endregion + + } +} \ No newline at end of file diff --git a/Tests/OpenTK.dll.config b/Tests/OpenTK.dll.config deleted file mode 100644 index 23689f65..00000000 --- a/Tests/OpenTK.dll.config +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/Tests/OpenTKGameWindow.cs b/Tests/OpenTKGameWindow.cs new file mode 100644 index 00000000..1dcd6066 --- /dev/null +++ b/Tests/OpenTKGameWindow.cs @@ -0,0 +1,331 @@ +// +// OpenTKGameWindow.cs +// +// Author: +// Jean-Philippe Bruyère +// +// Copyright (c) 2016 jp +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Threading; +using System.Xml; +using Cairo; +using OpenTK; +using OpenTK.Graphics.OpenGL; + +namespace Crow +{ + public class OpenTKGameWindow : GameWindow, IValueChange + { + #region IValueChange implementation + public event EventHandler ValueChanged; + public virtual void NotifyValueChanged(string MemberName, object _value) + { + if (ValueChanged != null) + ValueChanged.Invoke(this, new ValueChangeEventArgs(MemberName, _value)); + } + #endregion + + public Interface CrowInterface; + + #region FPS + int frameCpt = 0; + int _fps = 0; + + public int fps { + get { return _fps; } + set { + if (_fps == value) + return; + + _fps = value; + + if (_fps > fpsMax) { + fpsMax = _fps; + ValueChanged.Raise(this, new ValueChangeEventArgs ("fpsMax", fpsMax)); + } else if (_fps < fpsMin) { + fpsMin = _fps; + ValueChanged.Raise(this, new ValueChangeEventArgs ("fpsMin", fpsMin)); + } + + ValueChanged.Raise(this, new ValueChangeEventArgs ("fps", _fps)); + #if MEASURE_TIME + ValueChanged.Raise (this, new ValueChangeEventArgs ("update", + this.CrowInterface.clippingTime.ElapsedTicks.ToString () + " ticks")); + ValueChanged.Raise (this, new ValueChangeEventArgs ("layouting", + this.CrowInterface.layoutTime.ElapsedTicks.ToString () + " ticks")); + ValueChanged.Raise (this, new ValueChangeEventArgs ("drawing", + this.CrowInterface.drawingTime.ElapsedTicks.ToString () + " ticks")); + #endif + } + } + + public int fpsMin = int.MaxValue; + public int fpsMax = 0; + + void resetFps () + { + fpsMin = int.MaxValue; + fpsMax = 0; + _fps = 0; + } + public string update = ""; + public string drawing = ""; + public string layouting = ""; + #endregion + + #region ctor + public OpenTKGameWindow(int _width = 800, int _height = 600, string _title="Crow", + int colors = 32, int depth = 24, int stencil = 0, int samples = 1, + int major=3, int minor=3) + : this(_width, _height, new OpenTK.Graphics.GraphicsMode(colors, depth, stencil, samples), + _title,GameWindowFlags.Default,DisplayDevice.Default, + major,minor,OpenTK.Graphics.GraphicsContextFlags.Default) + { + } + public OpenTKGameWindow (int width, int height, OpenTK.Graphics.GraphicsMode mode, string title, GameWindowFlags options, DisplayDevice device, int major, int minor, OpenTK.Graphics.GraphicsContextFlags flags) + : base(width,height,mode,title,options,device,major,minor,flags) + { + CrowInterface = new Interface (); + + Thread t = new Thread (interfaceThread); + t.IsBackground = true; + t.Start (); + } + + #endregion + + void interfaceThread() + { + CrowInterface.Quit += Quit; + CrowInterface.MouseCursorChanged += CrowInterface_MouseCursorChanged; + while (CrowInterface.ClientRectangle.Size.Width == 0) + Thread.Sleep (5); + + while (true) { + CrowInterface.Update (); + Thread.Sleep (1); + } + } + + public void Quit (object sender, EventArgs e) + { + this.Exit (); + } + void CrowInterface_MouseCursorChanged (object sender, MouseCursorChangedEventArgs e) + { + this.Cursor = new MouseCursor( + (int)e.NewCursor.Xhot, + (int)e.NewCursor.Yhot, + (int)e.NewCursor.Width, + (int)e.NewCursor.Height, + e.NewCursor.data); + } + + #region Events + //those events are raised only if mouse isn't in a graphic object + public event EventHandler MouseWheelChanged; + public event EventHandler MouseButtonUp; + public event EventHandler MouseButtonDown; + public event EventHandler MouseClick; + public event EventHandler MouseMove; + public event EventHandler KeyboardKeyDown; + public event EventHandler KeyboardKeyUp; + + #endregion + + #region graphic context + int texID; + Tetra.Shader shader; + public static Crow.vaoMesh quad; + + void createContext() + { + #region Create texture + if (GL.IsTexture(texID)) + GL.DeleteTexture (texID); + GL.GenTextures(1, out texID); + GL.ActiveTexture (TextureUnit.Texture0); + GL.BindTexture(TextureTarget.Texture2D, texID); + + GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, + ClientRectangle.Width, ClientRectangle.Height, 0, + OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, CrowInterface.bmp); + + GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); + GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); + + GL.BindTexture(TextureTarget.Texture2D, 0); + #endregion + } + void OpenGLDraw() + { + bool blend, depthTest; + GL.GetBoolean (GetPName.Blend, out blend); + GL.GetBoolean (GetPName.DepthTest, out depthTest); + GL.Enable (EnableCap.Blend); + GL.Disable (EnableCap.DepthTest); + + shader.Enable (); + GL.ActiveTexture (TextureUnit.Texture0); + GL.BindTexture (TextureTarget.Texture2D, texID); + lock (CrowInterface.RenderMutex) { + if (CrowInterface.IsDirty) { + GL.TexSubImage2D (TextureTarget.Texture2D, 0, + CrowInterface.DirtyRect.Left, CrowInterface.DirtyRect.Top, + CrowInterface.DirtyRect.Width, CrowInterface.DirtyRect.Height, + OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, CrowInterface.dirtyBmp); + CrowInterface.IsDirty = false; + } + } + quad.Render (BeginMode.TriangleStrip); + GL.BindTexture(TextureTarget.Texture2D, 0); + shader.Disable (); + if (!blend) + GL.Disable (EnableCap.Blend); + if (depthTest) + GL.Enable (EnableCap.DepthTest); + } + #endregion + + public virtual void OnRender(FrameEventArgs e) + { + } + public virtual void GLClear() + { + GL.Clear (ClearBufferMask.ColorBufferBit); + } + + #region Game win overrides + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + this.KeyPress += new EventHandler(OpenTKGameWindow_KeyPress); + Keyboard.KeyDown += new EventHandler(Keyboard_KeyDown); + Keyboard.KeyUp += new EventHandler(Keyboard_KeyUp); + Mouse.WheelChanged += new EventHandler(Mouse_WheelChanged); + Mouse.ButtonDown += new EventHandler(Mouse_ButtonDown); + Mouse.ButtonUp += new EventHandler(Mouse_ButtonUp); + Mouse.Move += new EventHandler(Mouse_Move); + + GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); + + Console.WriteLine("\n\n*************************************"); + Console.WriteLine("GL version: " + GL.GetString (StringName.Version)); + Console.WriteLine("GL vendor: " + GL.GetString (StringName.Vendor)); + Console.WriteLine("GLSL version: " + GL.GetString (StringName.ShadingLanguageVersion)); + Console.WriteLine("*************************************\n"); + + shader = new Tetra.Shader (); + shader.Enable(); + shader.SetMVP(OpenTK.Matrix4.CreateOrthographicOffCenter (-0.5f, 0.5f, -0.5f, 0.5f, 1, -1)); + GL.UseProgram(0); + quad = new Crow.vaoMesh (0, 0, 0, 1, 1, 1, -1); + } + + protected override void OnUpdateFrame(FrameEventArgs e) + { + base.OnUpdateFrame(e); + fps = (int)RenderFrequency; + + + if (frameCpt > 50) { + resetFps (); + frameCpt = 0; + GC.Collect(); + GC.WaitForPendingFinalizers(); + NotifyValueChanged("memory", GC.GetTotalMemory (false).ToString()); + } + frameCpt++; + //CrowInterface.Update (); + } + protected override void OnRenderFrame(FrameEventArgs e) + { + GLClear (); + + base.OnRenderFrame(e); + + OnRender (e); + OpenGLDraw (); + + SwapBuffers (); + } + + protected override void OnResize(EventArgs e) + { + base.OnResize (e); + CrowInterface.ProcessResize( + new Rectangle( + 0, + 0, + this.ClientRectangle.Width, + this.ClientRectangle.Height)); + createContext (); + GL.Viewport (0, 0, ClientRectangle.Width, ClientRectangle.Height); + } + #endregion + + #region Mouse Handling + void update_mouseButtonStates(ref MouseState e, OpenTK.Input.MouseState otk_e){ + for (int i = 0; i < MouseState.MaxButtons; i++) { + if (otk_e.IsButtonDown ((OpenTK.Input.MouseButton)i)) + e.EnableBit (i); + } + } + void Mouse_Move(object sender, OpenTK.Input.MouseMoveEventArgs otk_e) + { + if (!CrowInterface.ProcessMouseMove (otk_e.X, otk_e.Y)) + MouseMove.Raise (sender, otk_e); + } + void Mouse_ButtonUp(object sender, OpenTK.Input.MouseButtonEventArgs otk_e) + { + if (!CrowInterface.ProcessMouseButtonUp ((int)otk_e.Button)) + MouseButtonUp.Raise (sender, otk_e); + } + void Mouse_ButtonDown(object sender, OpenTK.Input.MouseButtonEventArgs otk_e) + { + if (!CrowInterface.ProcessMouseButtonDown ((int)otk_e.Button)) + MouseButtonDown.Raise (sender, otk_e); + } + void Mouse_WheelChanged(object sender, OpenTK.Input.MouseWheelEventArgs otk_e) + { + if (!CrowInterface.ProcessMouseWheelChanged (otk_e.DeltaPrecise)) + MouseWheelChanged.Raise (sender, otk_e); + } + #endregion + + #region keyboard Handling + void Keyboard_KeyDown(object sender, OpenTK.Input.KeyboardKeyEventArgs otk_e) + { + //if (!CrowInterface.ProcessKeyDown((int)otk_e.Key)) + KeyboardKeyDown.Raise (this, otk_e); + } + void Keyboard_KeyUp(object sender, OpenTK.Input.KeyboardKeyEventArgs otk_e) + { + //if (!CrowInterface.ProcessKeyUp((int)otk_e.Key)) + KeyboardKeyUp.Raise (this, otk_e); + } + void OpenTKGameWindow_KeyPress (object sender, OpenTK.KeyPressEventArgs e) + { + CrowInterface.ProcessKeyPress (e.KeyChar); + } + #endregion + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index fe7570b3..ff4e3c78 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -48,6 +48,9 @@ + + + @@ -68,7 +71,6 @@ PreserveNewest - PreserveNewest @@ -328,8 +330,6 @@ PreserveNewest - - @@ -362,9 +362,5 @@ {C2980F9B-4798-4C05-99E2-E174810F7C7B} Crow - - {350D4F49-9901-4998-9903-BCA7D48DA58C} - OTKCrow - diff --git a/src/GraphicObjects/Window.cs b/src/GraphicObjects/Window.cs index 05acf526..a36019e3 100644 --- a/src/GraphicObjects/Window.cs +++ b/src/GraphicObjects/Window.cs @@ -62,7 +62,7 @@ namespace Crow NotifyValueChanged ("Title", _title); } } - [XmlAttributeAttribute()][DefaultValue("#Crow.Images.Icons.tetra.png")] + [XmlAttributeAttribute()][DefaultValue("#Crow.Images.Icons.crow.png")] public string Icon { get { return _icon; } set {