Torna al Thread
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Renderay.Core.Textures
{
using Math;
public class ImageMap
{
public color[, ,] pixels;
public enum CubeMapFace
{
XNEG = 0,
XPOS,
YNEG,
YPOS,
ZNEG,
ZPOS
}
public bool FromBitmap(Bitmap img, CubeMapFace face)
{
int h = img.Height;
int w = img.Width;
Rectangle rect = new Rectangle(0, 0, w, h);
ImageLockMode flags = ImageLockMode.ReadOnly;
PixelFormat pixelformat = img.PixelFormat;
BitmapData bmpData =
img.LockBits(rect, flags, pixelformat);
int increment = 0;
int aOffset = 0;
int rOffset = 0;
int gOffset = 0;
int bOffset = 0;
switch (pixelformat)
{
case PixelFormat.Format24bppRgb:
increment = 3;
aOffset = 0;
rOffset = 2;
gOffset = 1;
bOffset = 0;
break;
default:
throw new ArgumentOutOfRangeException("img", "This Image uses an unsupported pixelformat");
}
unsafe
{
byte* data = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
//The pin MUST be done outside the for loops
fixed (color* c = &pixels[(int)face, x, y])
{
c->a = (*(data + aOffset)) / 255.0f;
c->r = (*(data + rOffset)) / 255.0f;
c->g = (*(data + gOffset)) / 255.0f;
c->b = (*(data + bOffset)) / 255.0f;
}
data += increment;
}
}
}
img.UnlockBits(bmpData);
return true;
}
public bool LoadFromFile(string filename)
{
Bitmap bm = new Bitmap(filename);
if ((pixels == null) || (pixels.GetLength(0) != 6))
pixels = new color[1, bm.Width, bm.Height];
return FromBitmap(bm, 0);
}
public bool loadCubemapFaceFromFile(CubeMapFace face, string filename)
{
Bitmap bm = new Bitmap(filename);
if ((pixels == null) || (pixels.GetLength(0) != 6))
pixels = new color[6, bm.Width, bm.Height];
return FromBitmap(bm, face);
}
}
}