private void load(AtlasNode root) { // heavily based on // http://www.blackpawn.com/texts/lightmaps/default.html // basically it uses a kd-tree to pack the lightmaps // TODO: this shoudln't be hardcoded!! int outputWidth = 512; int outputHeight = 512; _packedTexture = new BitmapSurface(512, 512, null); root.AddToBitmap(_packedTexture); // create the packed rectangles root.UpdateRectList(_packedTextureRects); // now we have the rects, but they need to be converted from pixel coords /* for (int i = 0; i < _packedTextureRects.Length; i++) * { * Rect r = _packedTextureRects[i]; * r.X /= outputWidth; * r.Y /= outputHeight; * r.Width /= outputWidth; * r.Height /= outputHeight; * * _packedTextureRects[i] = r; * }*/ Utils.WriteTga("lightmap.tga", _packedTexture.Pixels, 512, 512); }
public void AddToBitmap(BitmapSurface bitmap) { if (Surface != null) { // top BitmapSurface.Copy((int)Rectangle.X + 1, (int)Rectangle.Y, bitmap, 0, 0, Surface.Width, 1, Surface); // bottom BitmapSurface.Copy((int)Rectangle.X + 1, (int)(Rectangle.Y + Rectangle.Height) - 1, bitmap, 0, Surface.Height - 1, Surface.Width, 1, Surface); //left BitmapSurface.Copy((int)Rectangle.X, (int)Rectangle.Y + 1, bitmap, 0, 0, 1, Surface.Height, Surface); // right BitmapSurface.Copy((int)(Rectangle.X + Rectangle.Width) - 1, (int)Rectangle.Y + 1, bitmap, Surface.Width - 1, 0, 1, Surface.Height, Surface); // center BitmapSurface.Copy((int)Rectangle.X + 1, (int)Rectangle.Y + 1, bitmap, 0, 0, Surface.Width, Surface.Height, Surface); // upper left BitmapSurface.Copy((int)Rectangle.X, (int)Rectangle.Y, bitmap, 0, 0, 1, 1, Surface); // upper right BitmapSurface.Copy((int)(Rectangle.X + Rectangle.Width) - 1, (int)Rectangle.Y, bitmap, Surface.Width - 1, 0, 1, 1, Surface); // lower left BitmapSurface.Copy((int)Rectangle.X, (int)(Rectangle.Y + Rectangle.Height) - 1, bitmap, 0, Surface.Height - 1, 1, 1, Surface); // lower right BitmapSurface.Copy((int)(Rectangle.X + Rectangle.Width) - 1, (int)(Rectangle.Y + Rectangle.Height) - 1, bitmap, Surface.Width - 1, Surface.Height - 1, 1, 1, Surface); } if (Child1 != null) { Child1.AddToBitmap(bitmap); } if (Child2 != null) { Child2.AddToBitmap(bitmap); } }