internal TextureResource(string name, BitmapSurface surface) : base(name, true) { _width = surface.Width; _height = surface.Height; _pixels = surface.Pixels; }
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 Resource.Resource Load(string name, Resource.ResourceManager content) { try { if (name.IndexOf('.') < 0) { name += ".BMP"; } System.IO.Stream stream = FileSystem.Open(name); BitmapSurface surface = new BitmapSurface(stream, BitmapSurface.SourceType.Unknown, true); Resource.Resource resource = RendererManager.CurrentRenderer.CreateTexture(name, surface, true, true); stream.Close(); return(resource); } catch (System.IO.FileNotFoundException) { Logger.WriteError("Unable to find texture: {0}", name); return(RendererManager.CurrentRenderer.ErrorTexture); } }
protected void loadFace(Stream stream, out byte[] pixels, out int width, out int height) { BitmapSurface face = new BitmapSurface(stream); pixels = face.Pixels; width = face.Width; height = face.Height; }
internal static void AddSun(Math.Vector3 direction, Math.Vector3 color, float radius, BitmapSurface front, BitmapSurface back, BitmapSurface left, BitmapSurface right, BitmapSurface up, bool memory) { // process each surface of the skybox and figure out how much "sun" is in that texel addSunToSurface(direction, color, radius, new Math.Vector3(1.0f, 0, 0), Math.Vector3.Up, front, memory); addSunToSurface(direction, color, radius, new Math.Vector3(-1.0f, 0, 0), Math.Vector3.Up, back, memory); addSunToSurface(direction, color, radius, new Math.Vector3(0, 0, 1.0f), Math.Vector3.Up, right, memory); addSunToSurface(direction, color, radius, new Math.Vector3(0, 0, -1.0f), Math.Vector3.Up, left, memory); addSunToSurface(direction, color, radius, new Math.Vector3(0, 1.0f, 0), Math.Vector3.Forward, up, memory); }
public static void Copy(int destX, int destY, BitmapSurface destSurface, int sourceX, int sourceY, int sourceWidth, int sourceHeight, BitmapSurface srcSurface) { for (int y = 0; y < sourceHeight; y++) { int destinationIndex = ((destY + y) * destSurface.Width + destX) * 4; Array.Copy(srcSurface.Pixels, ((sourceY + y) * srcSurface.Width + sourceX) * 4, destSurface.Pixels, destinationIndex, sourceWidth * 4); } }
private static Graphics.SkyBox loadSkybox(Game.ScnResource scn, bool addSun) { if (string.IsNullOrEmpty(scn.SkyboxLeft) == false && string.IsNullOrEmpty(scn.SkyboxRight) == false && string.IsNullOrEmpty(scn.SkyboxFront) == false && string.IsNullOrEmpty(scn.SkyboxBack) == false && string.IsNullOrEmpty(scn.SkyboxUp) == false && string.IsNullOrEmpty(scn.SkyboxDown) == false) { System.IO.Stream frontStream = FileSystem.Open(scn.SkyboxFront + ".BMP"); System.IO.Stream backStream = FileSystem.Open(scn.SkyboxBack + ".BMP"); System.IO.Stream leftStream = FileSystem.Open(scn.SkyboxLeft + ".BMP"); System.IO.Stream rightStream = FileSystem.Open(scn.SkyboxRight + ".BMP"); System.IO.Stream upStream = FileSystem.Open(scn.SkyboxUp + ".BMP"); Graphics.BitmapSurface fronts = new Graphics.BitmapSurface(frontStream); Graphics.BitmapSurface backs = new Graphics.BitmapSurface(backStream); Graphics.BitmapSurface lefts = new Graphics.BitmapSurface(leftStream); Graphics.BitmapSurface rights = new Graphics.BitmapSurface(rightStream); Graphics.BitmapSurface ups = new Graphics.BitmapSurface(upStream); Graphics.BitmapSurface downs = null; try { System.IO.Stream downStream = FileSystem.Open(scn.SkyboxDown + ".BMP"); downs = new Graphics.BitmapSurface(downStream); downStream.Close(); } catch (System.IO.FileNotFoundException) { downs = lefts; } frontStream.Close(); backStream.Close(); leftStream.Close(); rightStream.Close(); upStream.Close(); Gk3Main.Graphics.SkyBox sb = new Gk3Main.Graphics.SkyBox(scn.Name + "_skybox", fronts, backs, lefts, rights, ups, downs, Utils.DegreesToRadians(scn.SkyboxAzimuth)); if (addSun) { Math.Vector3 dir = new Math.Vector3(0.4873306f, -0.8727542f, 0.02844055f); //Gk3Main.Graphics.SkyBox.AddSun(dir, new Math.Vector3(1.0f, 0, 0), 0.025f, fronts, backs, lefts, rights, ups, false); sb.AddSun(dir, new Math.Vector3(1.0f, 0, 0), false); } return(sb); } return(null); }
public void MergeAlpha(BitmapSurface alphaSurface) { if (alphaSurface == null || alphaSurface.Width != _width || alphaSurface.Height != _height) { throw new ArgumentException(); } // merge color and alpha info for (int i = 0; i < _width * _height; i++) { _pixels[i * 4 + 3] = alphaSurface.Pixels[i * 4 + 0]; } }
private int testFit(BitmapSurface surface) { int width = surface.Width + Padding * 2; int height = surface.Height + Padding * 2; if (width > Rectangle.Width || height > Rectangle.Height) { return(1); // too big } if (width == Rectangle.Width && height == Rectangle.Height) { return(0); // just right } return(-1); // texture fits with space left over }
public LightmapResource(string name, System.IO.Stream stream) : base(name, true) { System.IO.BinaryReader reader = new System.IO.BinaryReader(stream); uint header = reader.ReadUInt32(); uint numMaps = reader.ReadUInt32(); _maps = new BitmapSurface[numMaps]; for (int i = 0; i < numMaps; i++) { _maps[i] = new BitmapSurface(stream, BitmapSurface.SourceType.Unknown, false); } GenTextureAtlas(); }
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); } }
public SkyBox(string name, BitmapSurface front, BitmapSurface back, BitmapSurface left, BitmapSurface right, BitmapSurface up, BitmapSurface down, float azimuth) { _cubeMap = Graphics.RendererManager.CurrentRenderer.CreateCubeMap(name, front, back, left, right, up, down); _azimuth = azimuth; }
private static void addSunToSurface(Math.Vector3 direction, Math.Vector3 color, float radius, Math.Vector3 faceNormal, Math.Vector3 faceUp, BitmapSurface faceSurface, bool memory) { Math.Vector3 faceRight = faceNormal.Cross(faceUp); float t = -0.5f / faceNormal.Dot(-direction); if (t > 0) { return; // the sun never hits this plane } Math.Vector3 sunP = -direction * t; for (int y = 0; y < faceSurface.Height; y++) { for (int x = 0; x < faceSurface.Width; x++) { // calc the distance from this pixel to the sun float u = (float)x / faceSurface.Width - 0.5f; float v = (float)y / faceSurface.Height - 0.5f; Math.Vector3 texelP = faceUp * v + faceRight * u + -faceNormal * 0.5f; float distance = Math.Vector3.Distance(texelP, sunP); if (distance < radius) { if (!memory) { faceSurface.Pixels[(y * faceSurface.Width + x) * 4 + 0] = 255; faceSurface.Pixels[(y * faceSurface.Width + x) * 4 + 1] = 0; faceSurface.Pixels[(y * faceSurface.Width + x) * 4 + 2] = 0; } else { Color c = faceSurface.ReadColorAt(x, y); uint ptr = (uint)c.R | ((uint)c.G << 8) | ((uint)c.B << 16) | ((uint)c.A << 24); UIntPtr ptr2 = (UIntPtr)ptr; unsafe { float *f = (float *)ptr2.ToPointer(); f[0] = color.X; f[1] = color.Y; f[2] = color.Z; } } } } } }
public AtlasNode Insert(BitmapSurface surface, int index) { AtlasNode newNode = null; if (Child1 != null && Child2 != null) { // not on a leaf... newNode = Child1.Insert(surface, index); if (newNode != null) { return(newNode); } // no room? try the other child... return(Child2.Insert(surface, index)); } else { // we're on a leaf! if (Surface != null) { return(null); } int fit = testFit(surface); if (fit > 0) { return(null); // too big } if (fit == 0) { Surface = surface; Index = index; return(this); } // guess we need to split this node Child1 = new AtlasNode(); Child2 = new AtlasNode(); int paddedWidth = surface.Width + Padding * 2; int paddedHeight = surface.Height + Padding * 2; float dw = Rectangle.Width - paddedWidth; float dh = Rectangle.Height - paddedHeight; if (dw > dh) { Child1.Rectangle = new Rect(Rectangle.X, Rectangle.Y, paddedWidth, Rectangle.Height); Child2.Rectangle = new Rect(Rectangle.X + paddedWidth + 1, Rectangle.Y, Rectangle.Width - paddedWidth - 1, Rectangle.Height); } else { Child1.Rectangle = new Rect(Rectangle.X, Rectangle.Y, Rectangle.Width, paddedHeight); Child2.Rectangle = new Rect(Rectangle.X, Rectangle.Y + paddedHeight + 1, Rectangle.Width, Rectangle.Height - paddedHeight - 1); } return(Child1.Insert(surface, index)); } }
public static void CalculateLightmaps(LightmapSpecs specs) { if (_currentRoom != null && _currentLightmaps != null) { Radiosity.Init(new Radiosity.RenderDelegate(renderRadiosityCallback)); _calculatingRadiosity = true; CurrentFilterMode = TextureFilterMode.None; LightmapsEnabled = true; CurrentShadeMode = ShadeMode.Flat; DoubleLightmapValues = false; Graphics.Camera originalCamera = CurrentCamera; Graphics.Viewport originalViewport = Graphics.RendererManager.CurrentRenderer.Viewport; RadiosityMaps radiosityMaps = _currentRoom.GenerateMemoryTextures(specs); // generate the omni lights _omniLightInfo = new List <OmniInfo>(); foreach (LightmapSpecs.OmniLight light in specs.OmniLights) { OmniInfo o; o.Info = light; Radiosity.GenerateOmniLight((int)light.Radius, light.Color, out o.MemTex, out o.AlphaMask); _omniLightInfo.Add(o); } Graphics.RendererManager.CurrentRenderer.CullMode = Graphics.CullMode.None; Graphics.TextureResource skyboxTop = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z); Graphics.TextureResource skyboxFront = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z); Graphics.TextureResource skyboxBack = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z); Graphics.TextureResource skyboxLeft = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z); Graphics.TextureResource skyboxRight = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z); Graphics.TextureResource skyboxBottom = Radiosity.GenerateMemoryTexture(16, 16, 0, 0, 0); Graphics.BitmapSurface skyboxTopPixels = new Graphics.BitmapSurface(skyboxTop); Graphics.BitmapSurface skyboxFrontPixels = new Graphics.BitmapSurface(skyboxFront); Graphics.BitmapSurface skyboxBackPixels = new Graphics.BitmapSurface(skyboxBack); Graphics.BitmapSurface skyboxLeftPixels = new Graphics.BitmapSurface(skyboxLeft); Graphics.BitmapSurface skyboxRightPixels = new Graphics.BitmapSurface(skyboxRight); Graphics.BitmapSurface skyboxBottomPixels = new Graphics.BitmapSurface(skyboxBottom); // Graphics.SkyBox.AddSun(specs.SunDirection, specs.SunColor, 0.125f, skyboxFrontPixels, skyboxBackPixels, skyboxLeftPixels, skyboxRightPixels, skyboxTopPixels, true); Graphics.SkyBox originalSkybox = _currentSkybox; if (_currentSkybox != null) { _currentSkybox = new Graphics.SkyBox("box", skyboxFrontPixels, skyboxBackPixels, skyboxLeftPixels, skyboxRightPixels, skyboxTopPixels, skyboxBottomPixels, 0); _currentSkybox.AddSun(specs.SunDirection, specs.SunColor, true); } Graphics.LightmapResource oldLightmaps = _currentLightmaps; _currentLightmaps = radiosityMaps.CreateBigMemoryTexture(_currentLightmaps.Name); _currentRoom.CalcRadiosityPass(_currentLightmaps, radiosityMaps); Radiosity.Shutdown(); _currentRoom.FinalizeVertices(_currentLightmaps, true); _currentLightmaps = radiosityMaps.ConvertToLightmap(_currentLightmaps.Name, 0.02f); Graphics.RendererManager.CurrentRenderer.CullMode = Graphics.CullMode.CounterClockwise; Graphics.RendererManager.CurrentRenderer.Viewport = originalViewport; CurrentCamera = originalCamera; _currentSkybox = originalSkybox; _calculatingRadiosity = false; } }
internal LightmapResource(string name, int numMaps) : base(name, true) { _maps = new BitmapSurface[numMaps]; }