Permalink
Browse files

added samplers/shader work

  • Loading branch information...
1 parent 1edd4b9 commit d81aed16e86f3f23d7922b001b89a3550f788580 Joseph Dillon committed May 4, 2012
Showing with 45 additions and 15 deletions.
  1. +9 −2 FieldRenderer.cs
  2. +6 −0 InterleavedFieldManager.cs
  3. +3 −0 Program.cs
  4. +24 −11 ResourceCommons.cs
  5. +3 −2 assets/shaders/fs_simple.glsl
View
@@ -56,7 +56,8 @@ public class Cell
Cell[,] committedCells;
int width;
int height;
- Texture scoreUI;
+ int scoreUI;
+ int scoreUISampler;
Bitmap scoreUIBase;
Rectangle scoreRect = new Rectangle(134, 256, 144, 32);
Rectangle levelRect = new Rectangle(134, 320, 144, 32);
@@ -161,7 +162,7 @@ public void CopyCommit()
protected override void DoLoad()
{
- ResourceCommons.LoadTexture(scoreUIBase = ResourceCommons.PanelBase, out scoreUI);
+ ResourceCommons.LoadTexture(scoreUIBase = ResourceCommons.PanelBase, out scoreUI, out scoreUISampler);
}
protected override void DoUnLoad()
@@ -187,11 +188,17 @@ protected override void DoUpdate(FrameEventArgs e)
protected override void DoDraw(FrameEventArgs e)
{
DrawScoreUI();
+ GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, ResourceCommons.TetrionTexture);
+ GL.BindSampler(0, ResourceCommons.TetrionTextureSampler);
DrawTetrion();
+ GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, ResourceCommons.Block);
+ GL.BindSampler(0, ResourceCommons.BlockSampler);
DrawBlock();
+ GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, ResourceCommons.BlockGhost);
+ GL.BindSampler(0, ResourceCommons.BlockGhostSampler);
DrawGhostBlock();
DrawOther();
}
@@ -89,13 +89,19 @@ protected override void DoDraw(FrameEventArgs e)
{
for (int i = 0; i < (int)players; i++)
renderer[i].DrawScoreUI();
+ GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, ResourceCommons.TetrionTexture);
+ GL.BindSampler(0, ResourceCommons.TetrionTextureSampler);
for (int i = 0; i < (int)players; i++)
renderer[i].DrawTetrion();
+ GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, ResourceCommons.Block);
+ GL.BindSampler(0, ResourceCommons.BlockSampler);
for (int i = 0; i < (int)players; i++)
renderer[i].DrawBlock();
+ GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, ResourceCommons.BlockGhost);
+ GL.BindSampler(0, ResourceCommons.BlockGhostSampler);
for (int i = 0; i < (int)players; i++)
{
renderer[i].DrawGhostBlock();
View
@@ -93,6 +93,7 @@ protected override void OnRenderFrame(FrameEventArgs e)
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.CullFace);
GL.Enable(EnableCap.Blend);
+ //GL.Enable(EnableCap.FramebufferSrgb);
GL.BlendEquation(BlendEquationMode.FuncAdd);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
@@ -109,8 +110,10 @@ protected override void OnRenderFrame(FrameEventArgs e)
GL.UseProgram(ResourceCommons.Simple_Shader);
GL.Uniform3(ResourceCommons.LightPositionUniform, ref lightPos);
GL.Uniform4(ResourceCommons.LightDiffuseUniform, ref lightDiff);
+ GL.Uniform1(ResourceCommons.SamplerUniform, 0);
base.OnRenderFrame(e);
+ //GL.Disable(EnableCap.FramebufferSrgb);
base.SwapBuffers();
}
View
@@ -50,9 +50,12 @@ static class ResourceCommons
const string SHADERS_DIR = "shaders";
const string FONTS_DIR = "fonts";
- public static Texture TetrionTexture;
- public static Texture Block;
- public static Texture BlockGhost;
+ public static int TetrionTexture;
+ public static int Block;
+ public static int BlockGhost;
+ public static int TetrionTextureSampler;
+ public static int BlockSampler;
+ public static int BlockGhostSampler;
public static Dictionary<TetraminoColor, MeshRenderer> Blocks = new Dictionary<TetraminoColor, MeshRenderer>();
public static Dictionary<TetraminoType, Bitmap> BlockOverlays = new Dictionary<TetraminoType, Bitmap>();
public static Dictionary<TetraminoType, Bitmap> BlockOverlaysSmall = new Dictionary<TetraminoType, Bitmap>();
@@ -61,6 +64,7 @@ static class ResourceCommons
public static int Simple_Shader;
public static int LightPositionUniform;
public static int LightDiffuseUniform;
+ public static int SamplerUniform;
public static Bitmap PanelBase;
public static QFont LiberationSans;
@@ -86,9 +90,9 @@ public static void Load()
BlockOverlaysSmall.Add(TetraminoType.Z, new Bitmap(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "Zs.png")));
BlockOverlaysSmall.Add(TetraminoType.T, new Bitmap(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "Ts.png")));
BlockOverlaysSmall.Add(TetraminoType.Null, new Bitmap(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "Ns.png")));
- LoadTexture(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "tetrion.png"), out TetrionTexture);
- LoadTexture(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "block.png"), out Block);
- LoadTexture(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "blockGhost.png"), out BlockGhost);
+ LoadTexture(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "tetrion.png"), out TetrionTexture, out TetrionTextureSampler);
+ LoadTexture(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "block.png"), out Block, out BlockSampler);
+ LoadTexture(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "blockGhost.png"), out BlockGhost, out BlockGhostSampler);
PanelBase = new Bitmap(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), TEXTURES_DIR), "panel.png"));
using (Stream tmp = File.Open(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), MODELS_DIR), "blockCyan.xml"), FileMode.Open))
Blocks.Add(TetraminoColor.Cyan, new MeshRenderer((Mesh)modelSerializer.Deserialize(tmp)));
@@ -113,6 +117,7 @@ public static void Load()
LoadShader(vs.ReadToEnd(), fs.ReadToEnd(), out Simple_vs, out Simple_fs, out Simple_Shader);
LightPositionUniform = GL.GetUniformLocation(Simple_Shader, "lightPosition");
LightDiffuseUniform = GL.GetUniformLocation(Simple_Shader, "lightDiffuse");
+ SamplerUniform = GL.GetUniformLocation(Simple_Shader, "color_texture");
LiberationSans = new QFont(Path.Combine(Path.Combine(Path.Combine(".", RESOURCE_DIR), FONTS_DIR), "LiberationSans.ttf"), 64);
LiberationSans.Options.Colour = new Color4(1.0f, 0.0f, 0.0f, 1.0f);
}
@@ -154,22 +159,30 @@ public static void LoadShader(string vs, string fs, out int vertexObject, out in
GL.LinkProgram(program);
}
- public static void LoadTexture(string filename, out Texture id)
+ public static void LoadTexture(string filename, out Texture id, out int samplerId)
{
if (String.IsNullOrEmpty(filename))
throw new ArgumentException(filename);
- LoadTexture(new Bitmap(filename), out id);
+ LoadTexture(new Bitmap(filename), out id, out samplerId);
}
- public static void LoadTexture(Bitmap bmp, out Texture id)
+ public static void LoadTexture(Bitmap bmp, out Texture id, out int samplerId)
{
+ // Store Texture ID
id = GL.GenTexture();
+ // Bind Texture
GL.BindTexture(TextureTarget.Texture2D, id);
+ // Load Bitmap
BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0, GLPixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);
bmp.UnlockBits(bmp_data);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
+ // Create a Sampler for the Texture
+ GL.GenSamplers(1, out samplerId);
+ // Set the texture access and bondries information for the Sampler
+ GL.SamplerParameter(samplerId, SamplerParameter.TextureMagFilter, (int)TextureMagFilter.Linear);
+ GL.SamplerParameter(samplerId, SamplerParameter.TextureMinFilter, (int)TextureMinFilter.Linear);
+ GL.SamplerParameter(samplerId, SamplerParameter.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
+ GL.SamplerParameter(samplerId, SamplerParameter.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
}
public static void UpdateTexture(Bitmap bmp, Texture id, int x, int y, int w, int h)
@@ -22,6 +22,7 @@ void main()
// Colors
diffuse = gl_Color * lightDiffuse;
-// Frag Color = ( texture Based Color Calc + Ambient );
- gl_FragColor = (texture2D(color_texture, texture_coordinate) * NdotL * diffuse) + (gl_Color * 0.1f);
+
+ // Frag Color = ( texture Based Color Calc + Ambient );
+ gl_FragColor = sqrt((texture2D(color_texture, texture_coordinate) * NdotL * diffuse) + (gl_Color * 0.1f));
}

0 comments on commit d81aed1

Please sign in to comment.