diff options
| author | jpbruyere <jp.bruyere@hotmail.com> | 2015-03-16 13:36:36 +0100 |
|---|---|---|
| committer | jpbruyere <jp.bruyere@hotmail.com> | 2015-03-16 13:36:36 +0100 |
| commit | 4338402e29d2c448c24f4efe6a788adb84f81785 (patch) | |
| tree | b22d52562f0dadf4532556cbbd93d8567d50ee23 /src/OpenGL | |
| parent | 871f7388f22f642f913ea955662840ce8da2f7ce (diff) | |
OpenGL indirect drawing for ui quad
(shader and vao)
Diffstat (limited to 'src/OpenGL')
| -rw-r--r-- | src/OpenGL/QuadVAO.cs | 45 | ||||
| -rw-r--r-- | src/OpenGL/Shader.cs | 237 | ||||
| -rw-r--r-- | src/OpenGL/TexturedShader.cs | 65 | ||||
| -rw-r--r-- | src/OpenGL/VertexArrayObject.cs | 94 |
4 files changed, 441 insertions, 0 deletions
diff --git a/src/OpenGL/QuadVAO.cs b/src/OpenGL/QuadVAO.cs new file mode 100644 index 00000000..374f2f71 --- /dev/null +++ b/src/OpenGL/QuadVAO.cs @@ -0,0 +1,45 @@ +using System; +using OpenTK; + +namespace go +{ + public class QuadVAO : VertexArrayObject + { + public QuadVAO (float x, float y, float width, float height):base( + new Vector2[] { + new Vector2 (x, y), + new Vector2 (x, y + height), + new Vector2 (x + width, y), + new Vector2 (x + width, y + height) + }, + new Vector2[] { + new Vector2 (0, 1), + new Vector2 (0, 0), + new Vector2 (1, 1), + new Vector2 (1, 0) + }, + new int[] { 0, 1, 2, 3 }) + { + + } + public QuadVAO (float x, float y, float width, float height, + float texX, float texY, float texW, float texH):base( + new Vector2[] { + new Vector2 (x, y), + new Vector2 (x, y + height), + new Vector2 (x + width, y), + new Vector2 (x + width, y + height) + }, + new Vector2[] { + new Vector2 (texX, texY+texH), + new Vector2 (texX, texY), + new Vector2 (texX+texW, texY+texH), + new Vector2 (texX+texW, texY) + }, + new int[] { 0, 1, 2, 3 }) + { + + } + } +} + diff --git a/src/OpenGL/Shader.cs b/src/OpenGL/Shader.cs new file mode 100644 index 00000000..cb8fe99c --- /dev/null +++ b/src/OpenGL/Shader.cs @@ -0,0 +1,237 @@ +using System; +using OpenTK.Graphics.OpenGL; +using System.Diagnostics; +using OpenTK; + +namespace go.GLBackend +{ + public class Shader : IDisposable + { + #region CTOR + public Shader () + { + Compile (); + } + #endregion + + + #region Sources + protected string _vertSource = @" + #version 130 + + precision highp float; + + uniform mat4 projection_matrix; + uniform mat4 modelview_matrix; + + in vec2 in_position; + + void main(void) + { + gl_Position = projection_matrix * modelview_matrix * vec4(in_position,0, 1); + }"; + + protected string _fragSource = @" + #version 130 + precision highp float; + + uniform vec4 color; + uniform bool stencilTest; + uniform sampler2D stencil; + uniform vec2 resolution; + + 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; + }"; + string _geomSource = ""; + #endregion + + #region Private and protected fields + protected int vsId, fsId, gsId, pgmId, savedPgmId = 0, + modelviewMatrixLocation, + projectionMatrixLocation, + colorLocation,stencilTestLocation,resolutionLocation; + + Matrix4 projectionMatrix, + modelviewMatrix; + #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 Matrix4 ProjectionMatrix{ + set { + projectionMatrix = value; + GL.UniformMatrix4(projectionMatrixLocation, false, ref projectionMatrix); + } + } + public Matrix4 ModelViewMatrix { + set { + modelviewMatrix = value; + GL.UniformMatrix4 (modelviewMatrixLocation, false, ref modelviewMatrix); + } + } + + public Vector4 Color { + set {GL.Uniform4 (colorLocation, value);} + } + + public bool StencilTest { + set { + if (value) + GL.Uniform1 (stencilTestLocation, 1); + else + GL.Uniform1 (stencilTestLocation, 0); + } + } + + public Vector2 Resolution { + set { GL.Uniform2 (resolutionLocation, value); } + } + + #endregion + + #region Public functions + 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 (); + + GL.LinkProgram(pgmId); + GL.ValidateProgram(pgmId); + + string info; + GL.GetProgramInfoLog(pgmId, out info); + Debug.WriteLine(info); + + Enable (); + + GetUniformLocations (); + BindSamplesSlots (); + + Disable (); + } + protected virtual void BindVertexAttributes() + { + GL.BindAttribLocation(pgmId, 0, "in_position"); + } + protected virtual void GetUniformLocations() + { + projectionMatrixLocation = GL.GetUniformLocation(pgmId, "projection_matrix"); + modelviewMatrixLocation = GL.GetUniformLocation(pgmId, "modelview_matrix"); + 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); + } + + public virtual void Enable(){ + GL.GetInteger (GetPName.CurrentProgram, out savedPgmId); + GL.UseProgram (pgmId); + } + public virtual void Disable(){ + GL.UseProgram (savedPgmId); + } + 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 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); + } + } + + #region IDisposable implementation + public 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/src/OpenGL/TexturedShader.cs b/src/OpenGL/TexturedShader.cs new file mode 100644 index 00000000..a410b16e --- /dev/null +++ b/src/OpenGL/TexturedShader.cs @@ -0,0 +1,65 @@ +using System; +using OpenTK.Graphics.OpenGL; + +namespace go.GLBackend +{ + public class TexturedShader : Shader + { + 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; + }"; + + Compile (); + + } + + protected override void BindVertexAttributes () + { + base.BindVertexAttributes (); + GL.BindAttribLocation(pgmId, 1, "in_tex"); + } + protected override void BindSamplesSlots () + { + GL.Uniform1(GL.GetUniformLocation (pgmId, "tex"),0); + GL.Uniform1(GL.GetUniformLocation (pgmId, "stencil"),1); + } + } +} + diff --git a/src/OpenGL/VertexArrayObject.cs b/src/OpenGL/VertexArrayObject.cs new file mode 100644 index 00000000..9b75af29 --- /dev/null +++ b/src/OpenGL/VertexArrayObject.cs @@ -0,0 +1,94 @@ +using System; +using OpenTK; +using OpenTK.Graphics.OpenGL; + +namespace go +{ + public class VertexArrayObject : IDisposable + { + public int vaoHandle, + positionVboHandle, + texVboHandle, + eboHandle; + + Vector2[] positionVboData; + public int[] indicesVboData; + Vector2[] texVboData; + + public VertexArrayObject (Vector2[] _positions, Vector2[] _texCoord, int[] _indices) + { + positionVboData = _positions; + texVboData = _texCoord; + indicesVboData = _indices; + + CreateVBOs (); + CreateVAOs (); + } + + void deleteVAOs() + { + GL.DeleteBuffer (positionVboHandle); + GL.DeleteBuffer (texVboHandle); + GL.DeleteBuffer (eboHandle); + GL.DeleteVertexArray (vaoHandle); + } + + void CreateVBOs() + { + positionVboHandle = GL.GenBuffer(); + GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); + GL.BufferData<Vector2>(BufferTarget.ArrayBuffer, + new IntPtr(positionVboData.Length * Vector2.SizeInBytes), + positionVboData, BufferUsageHint.StaticDraw); + + texVboHandle = GL.GenBuffer(); + GL.BindBuffer(BufferTarget.ArrayBuffer, texVboHandle); + GL.BufferData<Vector2>(BufferTarget.ArrayBuffer, + new IntPtr(texVboData.Length * Vector2.SizeInBytes), + texVboData, BufferUsageHint.StaticDraw); + // + eboHandle = GL.GenBuffer(); + GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle); + GL.BufferData(BufferTarget.ElementArrayBuffer, + new IntPtr(sizeof(uint) * indicesVboData.Length), + indicesVboData, BufferUsageHint.StaticDraw); + + GL.BindBuffer(BufferTarget.ArrayBuffer, 0); + GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); + } + + void CreateVAOs() + { + vaoHandle = GL.GenVertexArray(); + GL.BindVertexArray(vaoHandle); + + GL.EnableVertexAttribArray(0); + GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); + GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, true, Vector2.SizeInBytes, 0); + + GL.EnableVertexAttribArray(1); + GL.BindBuffer(BufferTarget.ArrayBuffer, texVboHandle); + GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, true, Vector2.SizeInBytes, 0); + + GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle); + + GL.BindVertexArray(0); + } + + public void Render(PrimitiveType _primitiveType){ + GL.BindVertexArray(vaoHandle); + GL.DrawElements(_primitiveType, indicesVboData.Length, + DrawElementsType.UnsignedInt, IntPtr.Zero); + GL.BindVertexArray (0); + } + + + #region IDisposable implementation + public void Dispose () + { + deleteVAOs (); + } + #endregion + } +} + |