using System;
-using OpenTK.Graphics.OpenGL;
using System.Diagnostics;
+using System.IO;
+using System.Reflection;
using OpenTK;
+using OpenTK.Graphics.OpenGL;
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
}
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()
{
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)
{
}
#region IDisposable implementation
- public void Dispose ()
+ public virtual void Dispose ()
{
if (GL.IsProgram (pgmId))
GL.DeleteProgram (pgmId);
\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
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
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
\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