BlazePhoenix

Sprite For GLK Renderig

Jun 27th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. //Based off of http://www.raywenderlich.com/9743/how-to-create-a-simple-2d-iphone-game-with-opengl-es-2-0-and-glkit-part-1
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using MonoTouch.Foundation;
  8. using MonoTouch.GLKit;
  9. using System.Drawing;
  10. using OpenTK.Graphics.ES20;
  11. using System.Runtime.InteropServices;
  12.  
  13. namespace XamarinSpriteAnimation
  14. {
  15.     public struct TexturedVertex
  16.     {
  17.         public PointF geomertryVertex;
  18.         public PointF textureVertex;
  19.     }
  20.  
  21.     public struct TexturedQuad
  22.     {
  23.         public TexturedVertex bl;
  24.         public TexturedVertex br;
  25.         public TexturedVertex tl;
  26.         public TexturedVertex tr;
  27.     }
  28.  
  29.     public class Sprite : NSObject
  30.     {
  31.         public string FileName { get; set; }
  32.         GLKBaseEffect Effect { get; set; }
  33.         GLKTextureInfo TextureInfo { get; set; }
  34.         TexturedQuad Quad;
  35.  
  36.         public Sprite(string f, GLKBaseEffect e)
  37.         {
  38.            
  39.             FileName = f;
  40.             Effect = e;
  41.  
  42.             NSDictionary options = NSDictionary.FromObjectAndKey(NSNumber.FromBoolean(true), GLKTextureLoader.OriginBottomLeft);
  43.             string path = NSBundle.MainBundle.PathForResource(FileName, null);
  44.             NSError error;
  45.             TextureInfo = GLKTextureLoader.FromFile(path, options, out error);
  46.  
  47.             // To Do: Set up textured Quad
  48.             TexturedQuad someQuad = new TexturedQuad();
  49.             someQuad.bl.geomertryVertex = new PointF(0, 0);
  50.             someQuad.br.geomertryVertex = new PointF(TextureInfo.Width, 0);
  51.             someQuad.tl.geomertryVertex = new PointF(0, TextureInfo.Height);
  52.             someQuad.tr.geomertryVertex = new PointF(TextureInfo.Width, TextureInfo.Height);
  53.  
  54.             someQuad.bl.textureVertex = new PointF(0, 0);
  55.             someQuad.br.textureVertex = new PointF(1, 0);
  56.             someQuad.tl.textureVertex = new PointF(0, 1);
  57.             someQuad.tr.textureVertex = new PointF(1, 1);
  58.  
  59.             Quad = someQuad;
  60.         }
  61.  
  62.         public void Render()
  63.         {
  64.             Effect.Texture2d0.GLName = TextureInfo.Name;
  65.             Effect.Texture2d0.Enabled = true;
  66.  
  67.             Effect.PrepareToDraw();
  68.  
  69.             GL.EnableVertexAttribArray((int)GLKVertexAttrib.Position);
  70.             GL.EnableVertexAttribArray((int)GLKVertexAttrib.TexCoord0);
  71.  
  72.             IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(Quad));
  73.             Marshal.StructureToPtr(Quad, ptr, false);
  74.             int offset = (int)ptr;
  75.  
  76.             GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "geomertryVertex"));
  77.             GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "textureVertex"));
  78.  
  79.             GL.DrawArrays(BeginMode.TriangleStrip, 0, 4);
  80.  
  81.             Marshal.FreeHGlobal(ptr);
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment