protected override void OnLoad(EventArgs e) { // Initialize GL State GPUCapabilities.Initialize(); GL.ClearColor(0.25f, 0.25f, 0.25f, 1.0f); // Set up PackageManager PackageManager.BasePath = "../../Assets/"; // Initialize camera. Rest is in OnResize() orthoCamera = new Camera(this); orthoCamera.SetAsCurrent(); // Load shaders ShaderManager.LoadCollection("Shaders/collection.xml"); // Initialize batch and atlas manager spriteBatch = new SpriteBatch(); sam = new SpriteAtlasManager(); // Temporary example until editor gets sprite support // Uncomment to regenerate sprite file //GenerateSpriteFile(); // Get the sprite atlas spaceShips = sam.Get("Sprites/SpaceShips.spr", true); // Get sprite by ID (slow operation, cache result on load) spaceShip = spaceShips["PlayerBlue1"]; }
// This is a temporary solution until editor has support for sprite editing. private void GenerateSpriteFile() { // Calculate sprite UV coordinates in the atlas float left = 211f; float top = 941f; float right = 211f + 99f; float bottom = 941f + 75f; left /= 1024f; top /= 1024f; right /= 1024f; bottom /= 1024f; // All sprites are children of the atlas they belong to spaceShips = new SpriteAtlas() { Sprites = new List<Sprite>() { new Sprite() { Name = "PlayerBlue1", // Sprite ID in this atlas Vertices = new Vector3[] { // A sprite can have more vertices, new Vector3(-0.5f, -0.5f, 0), // useful for fill-rate optimization. new Vector3(0.5f, -0.5f, 0), // vertex position count and uv count new Vector3(-0.5f, 0.5f, 0), // must be equal. 4 vert -> 4 uv new Vector3(0.5f, 0.5f, 0), }, TextureCoordinates = new Vector2[] { new Vector2(left, bottom), new Vector2(right, bottom), new Vector2(left, top), new Vector2(right, top), }, Indices = new uint[] { 0,1,2,2,1,3 }, Size = new Vector2(99f, 75f) } }, // A texture path that will get loaded automatically when the atlas gets loaded TextureName = "Textures/spaceships.tex", // Our texels does not have premultiplied alpha PremultipliedAlpha = false, }; // Insert in temporary location and save to filesystem sam.Insert("tmp", spaceShips); sam.Save("tmp", PackageManager.BasePath + "Sprites/SpaceShips.spr"); sam.Remove("tmp"); }
public void BeginBatch(SpriteAtlas atlas) { current = atlas; if (vao == 0) { program = ShaderManager.Get("sprite.shader"); vao = GL.GenVertexArray(); GL.GenBuffers(2, vbo); GL.BindVertexArray(vao); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]); int vertex = program.GetAttribLocation("vertex"); int uv = program.GetAttribLocation("uv"); int position = program.GetAttribLocation("position"); int size = program.GetAttribLocation("size"); int rotation = program.GetAttribLocation("rotation"); int color = program.GetAttribLocation("color"); int stride = BlittableValueType.StrideOf(new SpriteRenderData()); int offset = 0; GL.EnableVertexAttribArray(vertex); GL.VertexAttribPointer(vertex, 3, VertexAttribPointerType.Float, false, stride, offset); offset += Vector3.SizeInBytes; if(uv != -1) { GL.EnableVertexAttribArray(uv); GL.VertexAttribPointer(uv, 2, VertexAttribPointerType.Float, false, stride, offset); } offset += Vector2.SizeInBytes; if(position != -1) { GL.EnableVertexAttribArray(position); GL.VertexAttribPointer(position, 3, VertexAttribPointerType.Float, false, stride, offset); } offset += Vector3.SizeInBytes; if(size != -1) { GL.EnableVertexAttribArray(size); GL.VertexAttribPointer(size, 2, VertexAttribPointerType.Float, false, stride, offset); } offset += Vector2.SizeInBytes; if(rotation != -1) { GL.EnableVertexAttribArray(rotation); GL.VertexAttribPointer(rotation, 1, VertexAttribPointerType.Float, false, stride, offset); } offset += sizeof(float); if (color != -1) { GL.EnableVertexAttribArray(color); GL.VertexAttribPointer(color, 4, VertexAttribPointerType.Float, false, stride, offset); } offset += Vector4.SizeInBytes; GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo[1]); GL.BindVertexArray(0); } }
// This is a temporary solution until editor has support for sprite editing. private void GenerateSpriteFile() { // One texel unit float unit = 1f / 1024f; // Texture coordinates on the atlas float punkLeft = 0; float punkTop = 0; float punkRight = 128 * unit; float punkBottom = 128 * unit; float punkLeft2 = punkRight; float punkRight2 = punkRight + 128 * unit; var vertices = new Vector3[] { // A sprite can have more vertices, new Vector3(-0.5f, -0.5f, 0), // useful for fill-rate optimization. new Vector3(0.5f, -0.5f, 0), // vertex position count and uv count new Vector3(-0.5f, 0.5f, 0), // must be equal. 4 vert -> 4 uv new Vector3(0.5f, 0.5f, 0), }; // All sprites are children of the atlas they belong to spriteAtlas = new SpriteAtlas() { Sprites = new List<Sprite>() { new Sprite() { Name = "PunkBird.0", // Sprite ID in this atlas Vertices = vertices, TextureCoordinates = new Vector2[] { new Vector2(punkLeft, punkTop), new Vector2(punkRight, punkTop), new Vector2(punkLeft, punkBottom), new Vector2(punkRight, punkBottom), }, Indices = new uint[] { 0,1,2,2,1,3 }, Size = new Vector2(99f, 75f) }, new Sprite() { Name = "PunkBird.1", // Sprite ID in this atlas Vertices = vertices, TextureCoordinates = new Vector2[] { new Vector2(punkLeft2, punkTop), new Vector2(punkRight2, punkTop), new Vector2(punkLeft2, punkBottom), new Vector2(punkRight2, punkBottom), }, Indices = new uint[] { 0,1,2,2,1,3 }, Size = new Vector2(99f, 75f) } }, // A texture path that will get loaded automatically when the atlas gets loaded TextureName = "Textures/SplattyAtlas.tex", // Our texels does not have premultiplied alpha PremultipliedAlpha = false, }; // Insert in temporary location and save to filesystem sam.Insert("tmp", spriteAtlas); sam.Save("tmp", PackageManager.BasePath + "Sprites/SplattySprites.spr"); sam.Remove("tmp"); }
public void EndBatch() { Render(); current = null; }