diff options
| author | jpbruyere <jp.bruyere@hotmail.com> | 2015-10-29 13:56:01 +0100 |
|---|---|---|
| committer | jpbruyere <jp.bruyere@hotmail.com> | 2015-10-29 13:56:01 +0100 |
| commit | 743e3b4692436fe4ab2312c22e4e7c19af0f29c3 (patch) | |
| tree | 1068c980e9ba182f36cd0aba8b128a6704614b23 | |
| parent | f31ffd683210c50b777dce4e76d0d9912ca925a8 (diff) | |
update shader base class and textured shader to reflect GGL class
changes
| -rw-r--r-- | Tests/Tests.csproj | 2 | ||||
| -rw-r--r-- | src/OpenGL/Shader.cs | 191 | ||||
| -rw-r--r-- | src/OpenGL/TexturedShader.cs | 39 | ||||
| -rwxr-xr-x | src/OpenTKGameWindow.cs | 58 |
4 files changed, 165 insertions, 125 deletions
diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index e9eccb48..9d3a3410 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -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> diff --git a/src/OpenGL/Shader.cs b/src/OpenGL/Shader.cs index cb8fe99c..4f92abb3 100644 --- a/src/OpenGL/Shader.cs +++ b/src/OpenGL/Shader.cs @@ -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; + protected int vsId, fsId, gsId, pgmId, + modelViewLocation, + modelLocation, + projectionLocation, + normalLocation, + colorLocation; - Matrix4 projectionMatrix, - modelviewMatrix; + 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); diff --git a/src/OpenGL/TexturedShader.cs b/src/OpenGL/TexturedShader.cs index a410b16e..e62613d1 100644 --- a/src/OpenGL/TexturedShader.cs +++ b/src/OpenGL/TexturedShader.cs @@ -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 (); + + } } } diff --git a/src/OpenTKGameWindow.cs b/src/OpenTKGameWindow.cs index 4e5584c8..c23acbe3 100755 --- a/src/OpenTKGameWindow.cs +++ b/src/OpenTKGameWindow.cs @@ -156,21 +156,21 @@ namespace go public QuadVAO uiQuad, uiQuad2;
go.GLBackend.Shader shader;
- Matrix4 projectionMatrix,
- modelviewMatrix;
int[] viewport = new int[4];
Rectangle dirtyZone = Rectangle.Empty;
void createContext()
{
createOpenGLSurface ();
+
if (uiQuad != null)
uiQuad.Dispose ();
uiQuad = new QuadVAO (0, 0, ClientRectangle.Width, ClientRectangle.Height, 0, 1, 1, -1);
uiQuad2 = new QuadVAO (0, 0, ClientRectangle.Width, ClientRectangle.Height, 0, 0, 1, 1);
- projectionMatrix = Matrix4.CreateOrthographicOffCenter
- (0, ClientRectangle.Width, ClientRectangle.Height, 0, 0, 1);
- modelviewMatrix = Matrix4.Identity;
+
+ shader.ProjectionMatrix = Matrix4.CreateOrthographicOffCenter
+ (0, ClientRectangle.Width, ClientRectangle.Height, 0, 0, 1);
+
redrawClip.AddRectangle (ClientRectangle);
}
void createOpenGLSurface()
@@ -196,18 +196,16 @@ namespace go GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.BindTexture(TextureTarget.Texture2D, 0);
+
+ shader.Texture = texID;
}
void OpenGLDraw()
{
GL.GetInteger (GetPName.Viewport, viewport);
GL.Viewport (0, 0, ClientRectangle.Width, ClientRectangle.Height);
+
shader.Enable ();
- shader.ProjectionMatrix = projectionMatrix;
- shader.ModelViewMatrix = modelviewMatrix;
- shader.Color = new Vector4(1f,1f,1f,1f);
- //if (dirtyZone != Rectangle.Empty) {
- GL.ActiveTexture (TextureUnit.Texture0);
- GL.BindTexture (TextureTarget.Texture2D, texID);
+
GL.TexSubImage2D (TextureTarget.Texture2D, 0,
0, 0, ClientRectangle.Width, ClientRectangle.Height,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp);
@@ -219,23 +217,22 @@ namespace go shader.Disable ();
GL.Viewport (viewport [0], viewport [1], viewport [2], viewport [3]);
}
- public void RenderCustomTextureOnUIQuad(int _customTex)
- {
- GL.GetInteger (GetPName.Viewport, viewport);
- GL.Viewport (0, 0, ClientRectangle.Width, ClientRectangle.Height);
- shader.Enable ();
- shader.ProjectionMatrix = projectionMatrix;
- shader.ModelViewMatrix = modelviewMatrix;
- shader.Color = new Vector4(1f,1f,1f,1f);
- GL.ActiveTexture (TextureUnit.Texture0);
- GL.BindTexture (TextureTarget.Texture2D, _customTex);
- GL.Disable (EnableCap.DepthTest);
- uiQuad2.Render (PrimitiveType.TriangleStrip);
- GL.Enable (EnableCap.DepthTest);
- GL.BindTexture(TextureTarget.Texture2D, 0);
- shader.Disable ();
- GL.Viewport (viewport [0], viewport [1], viewport [2], viewport [3]);
- }
+// public void RenderCustomTextureOnUIQuad(int _customTex)
+// {
+// GL.GetInteger (GetPName.Viewport, viewport);
+// GL.Viewport (0, 0, ClientRectangle.Width, ClientRectangle.Height);
+//
+// shader.Enable ();
+//
+// GL.ActiveTexture (TextureUnit.Texture0);
+// GL.BindTexture (TextureTarget.Texture2D, _customTex);
+// GL.Disable (EnableCap.DepthTest);
+// uiQuad2.Render (PrimitiveType.TriangleStrip);
+// GL.Enable (EnableCap.DepthTest);
+// GL.BindTexture(TextureTarget.Texture2D, 0);
+// shader.Disable ();
+// GL.Viewport (viewport [0], viewport [1], viewport [2], viewport [3]);
+// }
#endregion
@@ -407,11 +404,6 @@ namespace go shader = new go.GLBackend.TexturedShader ();
}
- protected override void OnUnload(EventArgs e)
- {
- if (texID > 0)
- GL.DeleteTexture (texID);
- }
protected override void OnResize(EventArgs e)
{
|