]> O.S.I.I.S - jp/crow.git/commitdiff
update shader base class and textured shader to reflect GGL class
authorjpbruyere <jp.bruyere@hotmail.com>
Thu, 29 Oct 2015 12:56:01 +0000 (13:56 +0100)
committerjpbruyere <jp.bruyere@hotmail.com>
Thu, 29 Oct 2015 12:56:01 +0000 (13:56 +0100)
changes

Tests/Tests.csproj
src/OpenGL/Shader.cs
src/OpenGL/TexturedShader.cs
src/OpenTKGameWindow.cs

index e9eccb48ecc0139711f51acb9f4b463fb9611463..9d3a3410a87d4b2b41df7e837a350d1eb9ec620d 100644 (file)
@@ -8,7 +8,7 @@
     <OutputType>Exe</OutputType>
     <RootNamespace>Tests</RootNamespace>
     <AssemblyName>Tests</AssemblyName>
-    <StartupObject>test2.GOLIBTest_DirViewer</StartupObject>
+    <StartupObject>test.GOLIBTests</StartupObject>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <OutputPath>..\bin\$(configuration)</OutputPath>
     <IntermediateOutputPath>obj\$(configuration)</IntermediateOutputPath>
index cb8fe99c8371686c0df96f4b72494bbdd33a4f5b..4f92abb3e54577fb4bad8d23db3717080cc0c3d4 100644 (file)
@@ -1,7 +1,9 @@
 using System;
-using OpenTK.Graphics.OpenGL;
 using System.Diagnostics;
+using System.IO;
+using System.Reflection;
 using OpenTK;
+using OpenTK.Graphics.OpenGL;
 
 namespace go.GLBackend
 {
@@ -12,58 +14,103 @@ namespace go.GLBackend
                {
                        Compile ();
                }
-               #endregion
+               public Shader (string vertResId, string fragResId)
+               {
+
+                       Stream s = tryGetStreamForResource (vertResId);
+                       if (s != null) {
+                               using (StreamReader sr = new StreamReader (s)) {                                
+                                       vertSource = sr.ReadToEnd ();
+                               }
+                       }
+
+                       s = tryGetStreamForResource (fragResId);
+                       if (s != null) {
+                               using (StreamReader sr = new StreamReader (s)) {                                
+                                       fragSource = sr.ReadToEnd ();
+                               }
+                       }
 
+                       Compile ();
+               }
+               Stream tryGetStreamForResource(string resId){
+                       if (string.IsNullOrEmpty (resId))
+                               return null;
+                       
+                       Stream s = Assembly.GetEntryAssembly ().
+                               GetManifestResourceStream (resId);
+                       return s == null ?
+                               Assembly.GetExecutingAssembly ().
+                                       GetManifestResourceStream (resId) :
+                               s;
+               }
+               #endregion
 
                #region Sources
                protected string _vertSource = @"
-                       #version 130
+                       #version 330
 
                        precision highp float;
 
-                       uniform mat4 projection_matrix;
-                       uniform mat4 modelview_matrix;
+                       uniform mat4 Projection;
+                       uniform mat4 ModelView;
+                       uniform mat4 Model;
+                       uniform mat4 Normal;
 
                        in vec2 in_position;
+                       in vec2 in_tex;
+
+                       out vec2 texCoord;
+                       
 
                        void main(void)
                        {
-                               gl_Position = projection_matrix * modelview_matrix * vec4(in_position,0, 1);
+                               texCoord = in_tex;
+                               gl_Position = Projection * ModelView * Model * vec4(in_position, 0, 1);
                        }";
 
                protected string _fragSource = @"
-                       #version 130
+                       #version 330
                        precision highp float;
 
                        uniform vec4 color;
-                       uniform bool stencilTest;
-                       uniform sampler2D stencil;
-                       uniform vec2 resolution;
+                       uniform sampler2D tex;
 
+                       in vec2 texCoord;
                        out vec4 out_frag_color;
 
                        void main(void)
                        {
-                               if (stencilTest)
-                               {
-                                       vec2 uv = gl_FragCoord.xy/resolution;
-                                       vec4 s = texture( stencil, uv);                                 
-                                       if (s.r == 0.0)
-                                               discard;
-                               }
-                               out_frag_color = color;
+                               out_frag_color = texture( tex, texCoord);
                        }";
-               string _geomSource = "";
+               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
-               protected int vsId, fsId, gsId, pgmId, savedPgmId = 0,
-                                               modelviewMatrixLocation,
-                                               projectionMatrixLocation,
-                                               colorLocation,stencilTestLocation,resolutionLocation;
-
-               Matrix4 projectionMatrix, 
-                               modelviewMatrix;
+               protected int vsId, fsId, gsId, pgmId, 
+                                               modelViewLocation,
+                                               modelLocation,
+                                               projectionLocation,
+                                               normalLocation, 
+                                               colorLocation;
+
+               Matrix4 projectionMat = Matrix4.Identity, 
+                               modelMat = Matrix4.Identity,
+                               modelViewMat = Matrix4.Identity;
+               Vector4 color = new Vector4(1,1,1,1);
+               int texture;
                #endregion
 
 
@@ -85,37 +132,36 @@ namespace go.GLBackend
                }
 
                public Matrix4 ProjectionMatrix{
-                       set { 
-                               projectionMatrix = value;
-                               GL.UniformMatrix4(projectionMatrixLocation, false, ref projectionMatrix);  
-                       }
+                       set { projectionMat = value; }
+                       get { return projectionMat; }
                }
                public Matrix4 ModelViewMatrix {
-                       set { 
-                               modelviewMatrix = value;
-                               GL.UniformMatrix4 (modelviewMatrixLocation, false, ref modelviewMatrix); 
-                       }
+                       set { modelViewMat = value; }
+                       get { return modelViewMat; }
                }
-
-               public Vector4 Color {
-                       set {GL.Uniform4 (colorLocation, value);}
+               public Matrix4 ModelMatrix {
+                       set { modelMat = value; }
+                       get { return modelMat; }
                }
-
-               public bool StencilTest {
-                       set {
-                               if (value)
-                                       GL.Uniform1 (stencilTestLocation, 1);
-                               else
-                                       GL.Uniform1 (stencilTestLocation, 0);
-                       }
+               public Vector4 Color {
+                       set { color = value; }
+                       get { return color; }
                }
 
-               public Vector2 Resolution {
-                       set { GL.Uniform2 (resolutionLocation, value); }
+               public int Texture {
+                       get { return texture; }
+                       set { texture = value; }
                }
 
                #endregion
 
+               void updateNormalMatrix()
+               {
+                       Matrix4 normalMat = (modelViewMat).Inverted();
+                       normalMat.Transpose ();
+                       GL.UniformMatrix4 (normalLocation, false, ref normalMat);
+               }
+
                #region Public functions
                public virtual void Compile()
                {
@@ -149,42 +195,67 @@ namespace go.GLBackend
 
                        BindVertexAttributes ();
 
-                       GL.LinkProgram(pgmId);
-                       GL.ValidateProgram(pgmId);
-
                        string info;
+                       GL.LinkProgram(pgmId);
                        GL.GetProgramInfoLog(pgmId, out info);
-                       Debug.WriteLine(info);
 
-                       Enable ();
+                       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, 0, "in_position");                                         
+                       GL.BindAttribLocation(pgmId, 1, "in_tex");
                }
                protected virtual void GetUniformLocations()
                {
-                       projectionMatrixLocation = GL.GetUniformLocation(pgmId, "projection_matrix");
-                       modelviewMatrixLocation = GL.GetUniformLocation(pgmId, "modelview_matrix");
+                       projectionLocation = GL.GetUniformLocation(pgmId, "Projection");
+                       modelViewLocation = GL.GetUniformLocation(pgmId, "ModelView");
+                       modelLocation = GL.GetUniformLocation(pgmId, "Model");
+                       normalLocation = GL.GetUniformLocation(pgmId, "Normal");
                        colorLocation = GL.GetUniformLocation (pgmId, "color");
-                       stencilTestLocation = GL.GetUniformLocation (pgmId, "stencilTest");
-                       resolutionLocation = GL.GetUniformLocation (pgmId, "resolution");
+
                }
                protected virtual void BindSamplesSlots(){
-                       GL.Uniform1(GL.GetUniformLocation (pgmId, "stencil"),0);
+                       GL.Uniform1(GL.GetUniformLocation (pgmId, "tex"), 0);
                }
 
                public virtual void Enable(){
-                       GL.GetInteger (GetPName.CurrentProgram, out savedPgmId);
                        GL.UseProgram (pgmId);
+
+                       GL.UniformMatrix4(projectionLocation, false, ref projectionMat);
+                       GL.UniformMatrix4 (modelLocation, false, ref modelMat); 
+                       GL.UniformMatrix4 (modelViewLocation, false, ref modelViewMat);
+                       updateNormalMatrix ();
+                       GL.Uniform4 (colorLocation, color);
+
+                       if (texture < 0)
+                               return;
+
+                       GL.ActiveTexture (TextureUnit.Texture0);
+                       GL.BindTexture(TextureTarget.Texture2D, texture);
                }
                public virtual void Disable(){
-                       GL.UseProgram (savedPgmId);
+                       GL.UseProgram (0);
                }
                public static void Enable(Shader s)
                {
@@ -219,7 +290,7 @@ namespace go.GLBackend
                }                       
                        
                #region IDisposable implementation
-               public void Dispose ()
+               public virtual void Dispose ()
                {
                        if (GL.IsProgram (pgmId))
                                GL.DeleteProgram (pgmId);
index a410b16e052a0af03b7bd07648ea360f518fe001..e62613d1b6d19229d55dbeec449e468e848bd09d 100644 (file)
@@ -7,59 +7,36 @@ namespace go.GLBackend
        {
                public TexturedShader ()
                {
-                       vertSource = @"
-                               #version 130
-
-                               precision highp float;
-
-                               uniform mat4 projection_matrix;
-                               uniform mat4 modelview_matrix;
-
-                               in vec2 in_position;
-                               in vec2 in_tex;
-                               out vec2 texCoord;
-
-
-                               void main(void)
-                               {
-                                       texCoord = in_tex;
-                                       gl_Position = projection_matrix * modelview_matrix * vec4(in_position,0, 1);
-                               }";
 
                        fragSource = @"
                                #version 130
                                precision highp float;
 
-                               uniform vec4 color;
                                uniform sampler2D tex;
-                               uniform sampler2D stencil;
 
                                in vec2 texCoord;
                                out vec4 out_frag_color;
 
                                void main(void)
                                {
-//                                     vec4 s = texture( stencil, texCoord);
-//                                     if (s.r == 0)
-//                                             discard;
-                                       vec4 t = texture( tex, texCoord);
-                                       out_frag_color = t;
+                                       out_frag_color = texture( tex, texCoord);
                                }";
 
                        Compile ();
 
                }
 
-               protected override void BindVertexAttributes ()
-               {
-                       base.BindVertexAttributes ();
-                       GL.BindAttribLocation(pgmId, 1, "in_tex");
-               }
                protected override void BindSamplesSlots ()
                {
-                       GL.Uniform1(GL.GetUniformLocation (pgmId, "tex"),0);
+                       base.BindSamplesSlots ();
+
                        GL.Uniform1(GL.GetUniformLocation (pgmId, "stencil"),1);
                }
+               public override void Enable ()
+               {
+                       base.Enable ();
+
+               }
        }
 }
 
index 4e5584c89a60c83da1b5317b484bbb94f8740193..c23acbe30b45c48420c548b023fc386fc96cfb93 100755 (executable)
@@ -156,21 +156,21 @@ namespace go
 \r
                public QuadVAO uiQuad, uiQuad2;\r
                go.GLBackend.Shader shader;\r
-               Matrix4 projectionMatrix, \r
-                               modelviewMatrix;\r
                int[] viewport = new int[4];\r
 \r
                Rectangle dirtyZone = Rectangle.Empty;\r
                void createContext()\r
                {                       \r
                        createOpenGLSurface ();\r
+\r
                        if (uiQuad != null)\r
                                uiQuad.Dispose ();\r
                        uiQuad = new QuadVAO (0, 0, ClientRectangle.Width, ClientRectangle.Height, 0, 1, 1, -1);\r
                        uiQuad2 = new QuadVAO (0, 0, ClientRectangle.Width, ClientRectangle.Height, 0, 0, 1, 1);\r
-                       projectionMatrix = Matrix4.CreateOrthographicOffCenter \r
-                               (0, ClientRectangle.Width, ClientRectangle.Height, 0, 0, 1);\r
-                       modelviewMatrix = Matrix4.Identity;\r
+\r
+                       shader.ProjectionMatrix = Matrix4.CreateOrthographicOffCenter \r
+                               (0, ClientRectangle.Width, ClientRectangle.Height, 0, 0, 1);                    \r
+\r
                        redrawClip.AddRectangle (ClientRectangle);\r
                }\r
                void createOpenGLSurface()\r
@@ -196,18 +196,16 @@ namespace go
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);\r
 \r
                        GL.BindTexture(TextureTarget.Texture2D, 0);\r
+\r
+                       shader.Texture = texID;\r
                }\r
                void OpenGLDraw()\r
                {\r
                        GL.GetInteger (GetPName.Viewport, viewport);\r
                        GL.Viewport (0, 0, ClientRectangle.Width, ClientRectangle.Height);\r
+\r
                        shader.Enable ();\r
-                       shader.ProjectionMatrix = projectionMatrix;\r
-                       shader.ModelViewMatrix = modelviewMatrix;\r
-                       shader.Color = new Vector4(1f,1f,1f,1f);\r
-                       //if (dirtyZone != Rectangle.Empty) {\r
-                       GL.ActiveTexture (TextureUnit.Texture0);\r
-                       GL.BindTexture (TextureTarget.Texture2D, texID);\r
+\r
                        GL.TexSubImage2D (TextureTarget.Texture2D, 0,\r
                                0, 0, ClientRectangle.Width, ClientRectangle.Height,\r
                                OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp);\r
@@ -219,23 +217,22 @@ namespace go
                        shader.Disable ();\r
                        GL.Viewport (viewport [0], viewport [1], viewport [2], viewport [3]);\r
                }\r
-               public void RenderCustomTextureOnUIQuad(int _customTex)\r
-               {\r
-                       GL.GetInteger (GetPName.Viewport, viewport);\r
-                       GL.Viewport (0, 0, ClientRectangle.Width, ClientRectangle.Height);\r
-                       shader.Enable ();\r
-                       shader.ProjectionMatrix = projectionMatrix;\r
-                       shader.ModelViewMatrix = modelviewMatrix;\r
-                       shader.Color = new Vector4(1f,1f,1f,1f);\r
-                       GL.ActiveTexture (TextureUnit.Texture0);\r
-                       GL.BindTexture (TextureTarget.Texture2D, _customTex);\r
-                       GL.Disable (EnableCap.DepthTest);\r
-                       uiQuad2.Render (PrimitiveType.TriangleStrip);\r
-                       GL.Enable (EnableCap.DepthTest);\r
-                       GL.BindTexture(TextureTarget.Texture2D, 0);\r
-                       shader.Disable ();\r
-                       GL.Viewport (viewport [0], viewport [1], viewport [2], viewport [3]);                   \r
-               }\r
+//             public void RenderCustomTextureOnUIQuad(int _customTex)\r
+//             {\r
+//                     GL.GetInteger (GetPName.Viewport, viewport);\r
+//                     GL.Viewport (0, 0, ClientRectangle.Width, ClientRectangle.Height);\r
+//\r
+//                     shader.Enable ();\r
+//\r
+//                     GL.ActiveTexture (TextureUnit.Texture0);\r
+//                     GL.BindTexture (TextureTarget.Texture2D, _customTex);\r
+//                     GL.Disable (EnableCap.DepthTest);\r
+//                     uiQuad2.Render (PrimitiveType.TriangleStrip);\r
+//                     GL.Enable (EnableCap.DepthTest);\r
+//                     GL.BindTexture(TextureTarget.Texture2D, 0);\r
+//                     shader.Disable ();\r
+//                     GL.Viewport (viewport [0], viewport [1], viewport [2], viewport [3]);                   \r
+//             }\r
                        \r
                #endregion\r
 \r
@@ -407,11 +404,6 @@ namespace go
 \r
                        shader = new go.GLBackend.TexturedShader ();\r
                }\r
-               protected override void OnUnload(EventArgs e)\r
-               {\r
-                       if (texID > 0)\r
-                               GL.DeleteTexture (texID);\r
-               }\r
 \r
                protected override void OnResize(EventArgs e)\r
                {\r