public static void DrawTexture(Texture2D texture, Vector2 position, Color4 color, float rotation, Vector2 origin, Vector2 scale) { DrawTexture(texture, new RectangleF(position.X, position.Y, texture.Width*scale.X, texture.Height*scale.Y), new RectangleF(0, 0, 1, 1), color, rotation, origin); }
public TextureAtlas(string name, Bitmap tileMap, int tileWidth, int tileHeight) { AtlasSize = tileMap.Size; Textures = new Dictionary<string, Texture2D>(); TextureID = GL.GenTexture(); Name = name; GL.BindTexture(TextureTarget.Texture2D, TextureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); BitmapData bmpData = tileMap.LockBits(new Rectangle(0, 0, tileMap.Width, tileMap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmpData.Width, bmpData.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0); tileMap.UnlockBits(bmpData); int index = 0; for (int y=0 ; y <= (tileMap.Height - tileHeight) ; y += tileHeight) { for (int x = 0; x <= (tileMap.Width - tileWidth); x += tileWidth) { Rectangle pixelBounds = new Rectangle(x, y, tileWidth, tileHeight); Texture2D texture = new Texture2D(MakeUnitRectangle(pixelBounds, tileMap.Size), TextureID); texture.Name = index.ToString(); texture.Width = tileWidth; texture.Height = tileHeight; Textures.Add(texture.Name, texture); index++; } } IsPacked = true; }
/// <summary> /// The base DrawTexture function. /// </summary> /// <param name="texture">The texture to draw</param> /// <param name="destRectangle">The destination rectangle, in pixel space, where the texture is drawn</param> /// <param name="sourceRectangle">The source within the texture, values must be between [0-1]</param> /// <param name="color">The color to be applied</param> /// <param name="rotation">The rotation, in degrees to be applied</param> /// <param name="origin">The origin of the rotation, this value is in pixel space referenced from the top left of destRectangle</param> public static void DrawTexture(Texture2D texture, RectangleF destRectangle, RectangleF sourceRectangle, Color4 color, float rotation, Vector2 origin) { RectangleF atlasRect = texture.TextureCoordRect; RectangleF sourceInAtlasCoords = new RectangleF(atlasRect.Left + atlasRect.Width * sourceRectangle.Left, atlasRect.Top + atlasRect.Height * sourceRectangle.Top, atlasRect.Width * sourceRectangle.Width, atlasRect.Height * sourceRectangle.Height); GL.Enable(EnableCap.Texture2D); GL.PushMatrix(); GL.Translate(destRectangle.Left + origin.X, destRectangle.Top + origin.Y, 0); GL.Rotate(rotation, 0.0f, 0.0f, 1.0f); GL.Translate(-origin.X, -origin.Y, 0); GL.BindTexture(TextureTarget.Texture2D, texture.ID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); float width = destRectangle.Width; float height = destRectangle.Height; GL.Color4(color); GL.Begin(PrimitiveType.Quads); GL.TexCoord2(sourceInAtlasCoords.Left, sourceInAtlasCoords.Top); GL.Vertex2(0.0f, 0.0f); GL.TexCoord2(sourceInAtlasCoords.Right, sourceInAtlasCoords.Top); GL.Vertex2(width, 0.0f); GL.TexCoord2(sourceInAtlasCoords.Right, sourceInAtlasCoords.Bottom); GL.Vertex2(width, height); GL.TexCoord2(sourceInAtlasCoords.Left, sourceInAtlasCoords.Bottom); GL.Vertex2(0.0f, height); GL.End(); GL.PopMatrix(); GL.Disable(EnableCap.Texture2D); }
public void PackTextures(int maxWidth = 2048, int maxHeight = 2048) { // Calculate packing List<KeyValuePair<string, Size>> sizeList = new List<KeyValuePair<string, Size>>(); foreach (KeyValuePair<string, Bitmap> kvp in unpackedImages) { sizeList.Add(new KeyValuePair<string, Size>(kvp.Key, kvp.Value.Size)); } RectanglePack packedRectangles = RectanglePacker.PackRectangles(sizeList, 2, maxWidth, maxHeight); // Build bitmaps Bitmap atlasBitmap = new Bitmap(packedRectangles.BoundingSize.Width, packedRectangles.BoundingSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(atlasBitmap); graphics.Clear(Color.Transparent); TextureID = GL.GenTexture(); foreach (KeyValuePair<string, Rectangle> packedRectangle in packedRectangles.PackedRectangles) { Bitmap bitmap = null; if (unpackedImages.TryGetValue(packedRectangle.Key, out bitmap)) { graphics.DrawImage(bitmap, new Rectangle(packedRectangle.Value.Left, packedRectangle.Value.Top, bitmap.Width, bitmap.Height)); Texture2D texture = new Texture2D(MakeUnitRectangle(packedRectangle.Value, packedRectangles.BoundingSize), TextureID); texture.Name = packedRectangle.Key.ToLower(); texture.Width = bitmap.Width; texture.Height = bitmap.Height; Textures.Add(texture.Name, texture); } } graphics.Dispose(); GL.BindTexture(TextureTarget.Texture2D, TextureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); BitmapData bmpData = atlasBitmap.LockBits(new Rectangle(0, 0, atlasBitmap.Width, atlasBitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmpData.Width, bmpData.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0); atlasBitmap.UnlockBits(bmpData); atlasBitmap.Save("test.png"); atlasBitmap.Dispose(); }
public static void DrawTexture(Texture2D texture, RectangleF destRectangle, Color4 color) { DrawTexture(texture, destRectangle, new RectangleF(0, 0, 1, 1), color, 0f, Vector2.Zero); }
public static void DrawTexture(Texture2D texture, RectangleF destRectangle, RectangleF sourceRectangle, Color4 color) { DrawTexture(texture, destRectangle, sourceRectangle, color, 0f, Vector2.Zero); }
public static void DrawTexture(Texture2D texture, Vector2 position, Color4 color) { DrawTexture(texture, new RectangleF(position.X, position.Y, texture.Width, texture.Height), new RectangleF(0, 0, 1, 1), color, 0f, Vector2.Zero); }
public void DrawTexture(Texture2D texture, Vector2 position) { DrawTexture( texture, new RectangleF(position.X, position.Y, texture.Width, texture.Height), Color4.White, 0.0f); }
public void SetTexture(Texture2D texture, RectangleF destinationRectangle) { this.Texture = texture; this.DestinationRectangle = destinationRectangle; CalculateRotation(); }
public void DrawTexture(Texture2D texture, Vector2 position, Color4 color, float rotation, Vector2 scale) { DrawTexture( texture, position, color, rotation, scale, Vector2.Zero); }
public void DrawTexture(Texture2D texture, Vector2 position, Color4 color, float rotation, Vector2 scale, Vector2 origin) { float x = position.X + (texture.Width * origin.X) * (1 - scale.X); float y = position.Y + (texture.Height * origin.Y) * (1 - scale.Y); float width = texture.Width * scale.X; float height = texture.Height * scale.Y; DrawTexture( texture, new RectangleF(x, y, width, height), color, rotation); }
public void DrawTexture(Texture2D texture, RectangleF destRectangle, Color4 color, float rotation) { AddDrawCommand(texture, destRectangle, color, rotation); }
private void AddDrawCommand(Texture2D texture, RectangleF destRectangle, Color4 color, float rotation) { if (bufferPosition >= renderBuffer.Length) { //TODO: Flush current buffer bufferPosition = 0; } RenderBatchObject batchObject = renderBuffer[bufferPosition]; batchObject.SetTexture(texture, destRectangle); batchObject.Color = color.ToAbgr(); batchObject.Rotation = rotation; bufferPosition++; }
private void BuildFont() { Font font = new Font(FontFamily, PointSize); Bitmap FontBitmap = BuildGlyphImage(font); BitmapData bmpData = FontBitmap.LockBits(new Rectangle(0, 0, FontBitmap.Width, FontBitmap.Height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); byte[] pixelData = new byte[bmpData.Stride * bmpData.Height]; Marshal.Copy(bmpData.Scan0, pixelData, 0, bmpData.Stride * bmpData.Height); for (int r = 0; r < bmpData.Height; ++r) { int rowOffset = r * bmpData.Stride; for (int c = 0; c < bmpData.Width; c += 1) { int offset = rowOffset + c * 4; byte grayValue = (byte)(pixelData[offset] * 0.11 + pixelData[offset + 1] * 0.59 + pixelData[offset + 2] * 0.3); pixelData[offset] = 255; pixelData[offset + 1] = 255; pixelData[offset + 2] = 255; pixelData[offset + 3] = (byte)(255 - grayValue); } } Marshal.Copy(pixelData, 0, bmpData.Scan0, pixelData.Length); int textureID = GL.GenTexture(); FontTexture = new Texture2D(new RectangleF(0.0f, 0.0f, 1f, 1f), textureID); FontTexture.Width = bmpData.Width; FontTexture.Height = bmpData.Height; GL.BindTexture(TextureTarget.Texture2D, FontTexture.ID); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmpData.Width, bmpData.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0); FontBitmap.UnlockBits(bmpData); FontBitmap.Dispose(); }