Revert "remake for 0910"

This reverts commit db05da4a5c.
master
Unknown 2018-10-16 17:36:59 +02:00
parent db05da4a5c
commit 35e24e9091
4 changed files with 755 additions and 651 deletions

View File

@ -103,39 +103,41 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
public static float noise2(float x, float y)
{
int bx, by, b00, b10, b01, b11;
int bx0, bx1, by0, by1, b00, b10, b01, b11;
float rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v;
int i, j;
t = x + N;
bx0 = ((int)t) & BM;
bx1 = (bx0 + 1) & BM;
rx0 = t - (int)t;
bx = ((int)t) & BM;
i = p[bx];
bx = (bx + 1) & BM;
j = p[bx];
rx1 = rx0 - 1f;
t = y + N;
by0 = ((int)t) & BM;
by1 = (by0 + 1) & BM;
ry0 = t - (int)t;
by = ((int)t) & BM;
b00 = p[i + by];
b10 = p[j + by];
ry1 = ry0 - 1f;
by = (by + 1) & BM;
b01 = p[i + by];
b11 = p[j + by];
i = p[bx0];
j = p[bx1];
b00 = p[i + by0];
b10 = p[j + by0];
b01 = p[i + by1];
b11 = p[j + by1];
sx = s_curve(rx0);
sy = s_curve(ry0);
u = rx0 * g2[b00, 0] + ry0 * g2[b00, 1];
rx1 = rx0 - 1f;
v = rx1 * g2[b10, 0] + ry0 * g2[b10, 1];
a = Utils.Lerp(u, v, sx);
ry1 = ry0 - 1f;
u = rx0 * g2[b01, 0] + ry1 * g2[b01, 1];
v = rx1 * g2[b11, 0] + ry1 * g2[b11, 1];
b = Utils.Lerp(u, v, sx);
sy = s_curve(ry0);
return Utils.Lerp(a, b, sy);
}
@ -200,10 +202,12 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
public static float turbulence1(float x, float freq)
{
float t;
float v;
for (t = 0f; freq >= 1f; freq *= 0.5f)
{
t += noise1(freq * x) / freq;
v = freq * x;
t += noise1(v) / freq;
}
return t;
}
@ -211,20 +215,28 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
public static float turbulence2(float x, float y, float freq)
{
float t;
Vector2 vec;
for (t = 0f; freq >= 1f; freq *= 0.5f)
t += noise2(freq * x, freq * y) / freq;
{
vec.X = freq * x;
vec.Y = freq * y;
t += noise2(vec.X, vec.Y) / freq;
}
return t;
}
public static float turbulence3(float x, float y, float z, float freq)
{
float t;
Vector3 vec;
for (t = 0f; freq >= 1f; freq *= 0.5f)
{
t += noise3(freq * x, freq * y, freq * z) / freq;
vec.X = freq * x;
vec.Y = freq * y;
vec.Z = freq * z;
t += noise3(vec.X, vec.Y, vec.Z) / freq;
}
return t;
}
@ -232,28 +244,23 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
private static void normalize2(float[,] v, int i)
{
float s;
float a = v[i, 0];
float b = v[i, 1];
s = (float)Math.Sqrt(a * a + b * b);
s = (float)Math.Sqrt(v[i, 0] * v[i, 0] + v[i, 1] * v[i, 1]);
s = 1.0f / s;
v[i, 0] = a * s;
v[i, 1] = b * s;
v[i, 0] = v[i, 0] * s;
v[i, 1] = v[i, 1] * s;
}
private static void normalize3(float[,] v, int i)
{
float s;
float a = v[i, 0];
float b = v[i, 1];
float c = v[i, 2];
s = (float)Math.Sqrt(a * a + b * b + c * c);
s = (float)Math.Sqrt(v[i, 0] * v[i, 0] + v[i, 1] * v[i, 1] + v[i, 2] * v[i, 2]);
s = 1.0f / s;
v[i, 0] = a * s;
v[i, 1] = b * s;
v[i, 2] = c * s;
v[i, 0] = v[i, 0] * s;
v[i, 1] = v[i, 1] * s;
v[i, 2] = v[i, 2] * s;
}
private static float s_curve(float t)
@ -261,4 +268,4 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
return t * t * (3f - 2f * t);
}
}
}
}

View File

@ -80,12 +80,9 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
/// Note we create a 256x256 dimension texture even if the actual terrain is larger.
/// </remarks>
public static Bitmap Splat(ITerrainChannel terrain, UUID[] textureIDs,
float[] startHeights, float[] heightRanges,
uint regionPositionX, uint regionPositionY,
IAssetService assetService, IJ2KDecoder decoder,
bool textureTerrain, bool averagetextureTerrain,
int twidth, int theight)
public static Bitmap Splat(ITerrainChannel terrain,
UUID[] textureIDs, float[] startHeights, float[] heightRanges,
Vector3d regionPosition, IAssetService assetService, bool textureTerrain)
{
Debug.Assert(textureIDs.Length == 4);
Debug.Assert(startHeights.Length == 4);
@ -93,12 +90,6 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
Bitmap[] detailTexture = new Bitmap[4];
byte[] mapColorsRed = new byte[4];
byte[] mapColorsGreen = new byte[4];
byte[] mapColorsBlue = new byte[4];
bool usecolors = false;
if (textureTerrain)
{
// Swap empty terrain textureIDs with default IDs
@ -114,30 +105,22 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
{
for (int i = 0; i < 4; i++)
{
AssetBase asset = null;
// asset cache indexes are strings
string cacheName = "MAP-Patch" + textureIDs[i].ToString();
AssetBase asset;
UUID cacheID = UUID.Combine(TERRAIN_CACHE_MAGIC, textureIDs[i]);
// Try to fetch a cached copy of the decoded/resized version of this texture
asset = assetService.GetCached(cacheName);
asset = assetService.GetCached(cacheID.ToString());
if (asset != null)
{
try
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(asset.Data))
detailTexture[i] = (Bitmap)Image.FromStream(stream);
if (detailTexture[i].PixelFormat != PixelFormat.Format24bppRgb ||
detailTexture[i].Width != 16 || detailTexture[i].Height != 16)
{
detailTexture[i].Dispose();
detailTexture[i] = null;
}
}
catch (Exception ex)
{
m_log.Warn("Failed to decode cached terrain patch texture" + textureIDs[i] + "): " + ex.Message);
m_log.Warn("Failed to decode cached terrain texture " + cacheID +
" (textureID: " + textureIDs[i] + "): " + ex.Message);
}
}
@ -147,10 +130,10 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
asset = assetService.Get(textureIDs[i].ToString());
if (asset != null)
{
try
{
detailTexture[i] = (Bitmap)decoder.DecodeToImage(asset.Data);
}
// m_log.DebugFormat(
// "[TERRAIN SPLAT]: Got cached original JPEG2000 terrain texture {0} {1}", i, asset.ID);
try { detailTexture[i] = (Bitmap)CSJ2K.J2kImage.FromBytes(asset.Data); }
catch (Exception ex)
{
m_log.Warn("Failed to decode terrain texture " + asset.ID + ": " + ex.Message);
@ -159,10 +142,14 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
if (detailTexture[i] != null)
{
if (detailTexture[i].PixelFormat != PixelFormat.Format24bppRgb ||
detailTexture[i].Width != 16 || detailTexture[i].Height != 16)
// Make sure this texture is the correct size, otherwise resize
if (detailTexture[i].Width != 256 || detailTexture[i].Height != 256)
{
using (Bitmap origBitmap = detailTexture[i])
detailTexture[i] = Util.ResizeImageSolid(origBitmap, 16, 16);
{
detailTexture[i] = ImageUtils.ResizeImage(origBitmap, 256, 256);
}
}
// Save the decoded and resized texture to the cache
byte[] data;
@ -178,8 +165,8 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
Data = data,
Description = "PNG",
Flags = AssetFlags.Collectable,
FullID = UUID.Zero,
ID = cacheName,
FullID = cacheID,
ID = cacheID.ToString(),
Local = true,
Name = String.Empty,
Temporary = true,
@ -193,274 +180,209 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
}
#endregion Texture Fetching
if (averagetextureTerrain)
}
// Fill in any missing textures with a solid color
for (int i = 0; i < 4; i++)
{
if (detailTexture[i] == null)
{
for (int t = 0; t < 4; t++)
m_log.DebugFormat("{0} Missing terrain texture for layer {1}. Filling with solid default color",
LogHeader, i);
// Create a solid color texture for this layer
detailTexture[i] = new Bitmap(256, 256, PixelFormat.Format24bppRgb);
using (Graphics gfx = Graphics.FromImage(detailTexture[i]))
{
usecolors = true;
if (detailTexture[t] == null)
{
mapColorsRed[t] = DEFAULT_TERRAIN_COLOR[t].R;
mapColorsGreen[t] = DEFAULT_TERRAIN_COLOR[t].G;
mapColorsBlue[t] = DEFAULT_TERRAIN_COLOR[t].B;
continue;
}
int npixeis = 0;
int cR = 0;
int cG = 0;
int cB = 0;
BitmapData bmdata = detailTexture[t].LockBits(new Rectangle(0, 0, 16, 16),
ImageLockMode.ReadOnly, detailTexture[t].PixelFormat);
npixeis = bmdata.Height * bmdata.Width;
int ylen = bmdata.Height * bmdata.Stride;
unsafe
{
for (int y = 0; y < ylen; y += bmdata.Stride)
{
byte* ptrc = (byte*)bmdata.Scan0 + y;
for (int x = 0; x < bmdata.Width; ++x)
{
cR += *(ptrc++);
cG += *(ptrc++);
cB += *(ptrc++);
}
}
}
detailTexture[t].UnlockBits(bmdata);
detailTexture[t].Dispose();
mapColorsRed[t] = (byte)Util.Clamp(cR / npixeis, 0, 255);
mapColorsGreen[t] = (byte)Util.Clamp(cG / npixeis, 0, 255);
mapColorsBlue[t] = (byte)Util.Clamp(cB / npixeis, 0, 255);
using (SolidBrush brush = new SolidBrush(DEFAULT_TERRAIN_COLOR[i]))
gfx.FillRectangle(brush, 0, 0, 256, 256);
}
}
else
{
// Fill in any missing textures with a solid color
for (int i = 0; i < 4; i++)
if (detailTexture[i].Width != 256 || detailTexture[i].Height != 256)
{
if (detailTexture[i] == null)
{
m_log.DebugFormat("{0} Missing terrain texture for layer {1}. Filling with solid default color", LogHeader, i);
// Create a solid color texture for this layer
detailTexture[i] = new Bitmap(16, 16, PixelFormat.Format24bppRgb);
using (Graphics gfx = Graphics.FromImage(detailTexture[i]))
{
using (SolidBrush brush = new SolidBrush(DEFAULT_TERRAIN_COLOR[i]))
gfx.FillRectangle(brush, 0, 0, 16, 16);
}
}
else
{
if (detailTexture[i].Width != 16 || detailTexture[i].Height != 16)
{
using (Bitmap origBitmap = detailTexture[i])
detailTexture[i] = Util.ResizeImageSolid(origBitmap, 16, 16);
}
}
detailTexture[i] = ResizeBitmap(detailTexture[i], 256, 256);
}
}
}
else
{
usecolors = true;
for (int t = 0; t < 4; t++)
{
mapColorsRed[t] = DEFAULT_TERRAIN_COLOR[t].R;
mapColorsGreen[t] = DEFAULT_TERRAIN_COLOR[t].G;
mapColorsBlue[t] = DEFAULT_TERRAIN_COLOR[t].B;
}
}
#region Layer Map
float xFactor = terrain.Width / twidth;
float yFactor = terrain.Height / theight;
float[,] layermap = new float[256, 256];
// Scale difference between actual region size and the 256 texture being created
int xFactor = terrain.Width / 256;
int yFactor = terrain.Height / 256;
// Create 'layermap' where each value is the fractional layer number to place
// at that point. For instance, a value of 1.345 gives the blending of
// layer 1 and layer 2 for that point.
for (int y = 0; y < 256; y++)
{
for (int x = 0; x < 256; x++)
{
float height = (float)terrain[x * xFactor, y * yFactor];
float pctX = (float)x / 255f;
float pctY = (float)y / 255f;
// Use bilinear interpolation between the four corners of start height and
// height range to select the current values at this position
float startHeight = ImageUtils.Bilinear(
startHeights[0],
startHeights[2],
startHeights[1],
startHeights[3],
pctX, pctY);
startHeight = Utils.Clamp(startHeight, 0f, 255f);
float heightRange = ImageUtils.Bilinear(
heightRanges[0],
heightRanges[2],
heightRanges[1],
heightRanges[3],
pctX, pctY);
heightRange = Utils.Clamp(heightRange, 0f, 255f);
// Generate two frequencies of perlin noise based on our global position
// The magic values were taken from http://opensimulator.org/wiki/Terrain_Splatting
Vector3 vec = new Vector3
(
((float)regionPosition.X + (x * xFactor)) * 0.20319f,
((float)regionPosition.Y + (y * yFactor)) * 0.20319f,
height * 0.25f
);
float lowFreq = Perlin.noise2(vec.X * 0.222222f, vec.Y * 0.222222f) * 6.5f;
float highFreq = Perlin.turbulence2(vec.X, vec.Y, 2f) * 2.25f;
float noise = (lowFreq + highFreq) * 2f;
// Combine the current height, generated noise, start height, and height range parameters, then scale all of it
float layer = ((height + noise - startHeight) / heightRange) * 4f;
if (Single.IsNaN(layer))
layer = 0f;
layermap[x, y] = Utils.Clamp(layer, 0f, 3f);
}
}
#endregion Layer Map
#region Texture Compositing
Bitmap output = new Bitmap(twidth, theight, PixelFormat.Format24bppRgb);
BitmapData outputData = output.LockBits(new Rectangle(0, 0, twidth, theight), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
Bitmap output = new Bitmap(256, 256, PixelFormat.Format24bppRgb);
BitmapData outputData = output.LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
// Unsafe work as we lock down the source textures for quicker access and access the
// pixel data directly
float invtwitdthMinus1 = 1.0f / (twidth - 1);
float invtheightMinus1 = 1.0f / (theight - 1);
int ty;
int tx;
float pctx;
float pcty;
float height;
float layer;
float layerDiff;
int l0;
int l1;
uint yglobalpos;
if (usecolors)
unsafe
{
float a;
float b;
unsafe
// Get handles to all of the texture data arrays
BitmapData[] datas = new BitmapData[]
{
byte* ptrO;
for (int y = 0; y < theight; ++y)
{
pcty = y * invtheightMinus1;
ptrO = (byte*)outputData.Scan0 + y * outputData.Stride;
ty = (int)(y * yFactor);
yglobalpos = (uint)ty + regionPositionY;
detailTexture[0].LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.ReadOnly, detailTexture[0].PixelFormat),
detailTexture[1].LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.ReadOnly, detailTexture[1].PixelFormat),
detailTexture[2].LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.ReadOnly, detailTexture[2].PixelFormat),
detailTexture[3].LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.ReadOnly, detailTexture[3].PixelFormat)
};
for (int x = 0; x < twidth; ++x)
{
tx = (int)(x * xFactor);
pctx = x * invtwitdthMinus1;
height = (float)terrain[tx, ty];
layer = getLayerTex(height, pctx, pcty,
(uint)tx + regionPositionX, yglobalpos,
startHeights, heightRanges);
// Select two textures
l0 = (int)layer;
l1 = Math.Min(l0 + 1, 3);
layerDiff = layer - l0;
a = mapColorsRed[l0];
b = mapColorsRed[l1];
*(ptrO++) = (byte)(a + layerDiff * (b - a));
a = mapColorsGreen[l0];
b = mapColorsGreen[l1];
*(ptrO++) = (byte)(a + layerDiff * (b - a));
a = mapColorsBlue[l0];
b = mapColorsBlue[l1];
*(ptrO++) = (byte)(a + layerDiff * (b - a));
}
}
}
}
else
{
float aB;
float aG;
float aR;
float bB;
float bG;
float bR;
unsafe
// Compute size of each pixel data (used to address into the pixel data array)
int[] comps = new int[]
{
// Get handles to all of the texture data arrays
BitmapData[] datas = new BitmapData[]
(datas[0].PixelFormat == PixelFormat.Format32bppArgb) ? 4 : 3,
(datas[1].PixelFormat == PixelFormat.Format32bppArgb) ? 4 : 3,
(datas[2].PixelFormat == PixelFormat.Format32bppArgb) ? 4 : 3,
(datas[3].PixelFormat == PixelFormat.Format32bppArgb) ? 4 : 3
};
for (int y = 0; y < 256; y++)
{
for (int x = 0; x < 256; x++)
{
detailTexture[0].LockBits(new Rectangle(0, 0, 16, 16), ImageLockMode.ReadOnly, detailTexture[0].PixelFormat),
detailTexture[1].LockBits(new Rectangle(0, 0, 16, 16), ImageLockMode.ReadOnly, detailTexture[1].PixelFormat),
detailTexture[2].LockBits(new Rectangle(0, 0, 16, 16), ImageLockMode.ReadOnly, detailTexture[2].PixelFormat),
detailTexture[3].LockBits(new Rectangle(0, 0, 16, 16), ImageLockMode.ReadOnly, detailTexture[3].PixelFormat)
};
float layer = layermap[x, y];
byte* ptr;
byte* ptrO;
for (int y = 0; y < theight; y++)
{
pcty = y * invtheightMinus1;
int ypatch = ((int)(y * yFactor) & 0x0f) * datas[0].Stride;
ptrO = (byte*)outputData.Scan0 + y * outputData.Stride;
ty = (int)(y * yFactor);
yglobalpos = (uint)ty + regionPositionY;
// Select two textures
int l0 = (int)Math.Floor(layer);
int l1 = Math.Min(l0 + 1, 3);
for (int x = 0; x < twidth; x++)
{
tx = (int)(x * xFactor);
pctx = x * invtwitdthMinus1;
height = (float)terrain[tx, ty];
layer = getLayerTex(height, pctx, pcty,
(uint)tx + regionPositionX, yglobalpos,
startHeights, heightRanges);
byte* ptrA = (byte*)datas[l0].Scan0 + y * datas[l0].Stride + x * comps[l0];
byte* ptrB = (byte*)datas[l1].Scan0 + y * datas[l1].Stride + x * comps[l1];
byte* ptrO = (byte*)outputData.Scan0 + y * outputData.Stride + x * 3;
// Select two textures
l0 = (int)layer;
layerDiff = layer - l0;
float aB = *(ptrA + 0);
float aG = *(ptrA + 1);
float aR = *(ptrA + 2);
int patchOffset = (tx & 0x0f) * 3 + ypatch;
float bB = *(ptrB + 0);
float bG = *(ptrB + 1);
float bR = *(ptrB + 2);
ptr = (byte*)datas[l0].Scan0 + patchOffset;
aB = *(ptr++);
aG = *(ptr++);
aR = *(ptr);
float layerDiff = layer - l0;
l1 = Math.Min(l0 + 1, 3);
ptr = (byte*)datas[l1].Scan0 + patchOffset;
bB = *(ptr++);
bG = *(ptr++);
bR = *(ptr);
// Interpolate between the two selected textures
*(ptrO++) = (byte)(aB + layerDiff * (bB - aB));
*(ptrO++) = (byte)(aG + layerDiff * (bG - aG));
*(ptrO++) = (byte)(aR + layerDiff * (bR - aR));
}
// Interpolate between the two selected textures
*(ptrO + 0) = (byte)Math.Floor(aB + layerDiff * (bB - aB));
*(ptrO + 1) = (byte)Math.Floor(aG + layerDiff * (bG - aG));
*(ptrO + 2) = (byte)Math.Floor(aR + layerDiff * (bR - aR));
}
for (int i = 0; i < detailTexture.Length; i++)
detailTexture[i].UnlockBits(datas[i]);
}
for (int i = 0; i < detailTexture.Length; i++)
if (detailTexture[i] != null)
detailTexture[i].Dispose();
detailTexture[i].UnlockBits(datas[i]);
}
for (int i = 0; i < detailTexture.Length; i++)
if (detailTexture[i] != null)
detailTexture[i].Dispose();
output.UnlockBits(outputData);
//output.Save("terr.png",ImageFormat.Png);
// We generated the texture upside down, so flip it
output.RotateFlip(RotateFlipType.RotateNoneFlipY);
#endregion Texture Compositing
return output;
}
private static float getLayerTex(float height, float pctX, float pctY, uint X, uint Y,
float[] startHeights, float[] heightRanges)
public static Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
// Use bilinear interpolation between the four corners of start height and
// height range to select the current values at this position
float startHeight = ImageUtils.Bilinear(
startHeights[0], startHeights[2],
startHeights[1], startHeights[3],
pctX, pctY);
startHeight = Utils.Clamp(startHeight, 0f, 255f);
m_log.DebugFormat("{0} ResizeBitmap. From <{1},{2}> to <{3},{4}>",
LogHeader, b.Width, b.Height, nWidth, nHeight);
Bitmap result = new Bitmap(nWidth, nHeight);
using (Graphics g = Graphics.FromImage(result))
g.DrawImage(b, 0, 0, nWidth, nHeight);
b.Dispose();
return result;
}
public static Bitmap SplatSimple(float[] heightmap)
{
const float BASE_HSV_H = 93f / 360f;
const float BASE_HSV_S = 44f / 100f;
const float BASE_HSV_V = 34f / 100f;
float heightRange = ImageUtils.Bilinear(
heightRanges[0], heightRanges[2],
heightRanges[1], heightRanges[3],
pctX, pctY);
heightRange = Utils.Clamp(heightRange, 0f, 255f);
if (heightRange == 0f)
return 0;
Bitmap img = new Bitmap(256, 256);
BitmapData bitmapData = img.LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
// Generate two frequencies of perlin noise based on our global position
// The magic values were taken from http://opensimulator.org/wiki/Terrain_Splatting
float sX = X * 0.20319f;
float sY = Y * 0.20319f;
unsafe
{
for (int y = 255; y >= 0; y--)
{
for (int x = 0; x < 256; x++)
{
float normHeight = heightmap[y * 256 + x] / 255f;
normHeight = Utils.Clamp(normHeight, BASE_HSV_V, 1.0f);
float noise = Perlin.noise2(sX * 0.222222f, sY * 0.222222f) * 13.0f;
noise += Perlin.turbulence2(sX, sY, 2f) * 4.5f;
Color4 color = Color4.FromHSV(BASE_HSV_H, BASE_HSV_S, normHeight);
// Combine the current height, generated noise, start height, and height range parameters, then scale all of it
float layer = ((height + noise - startHeight) / heightRange) * 4f;
return Utils.Clamp(layer, 0f, 3f);
byte* ptr = (byte*)bitmapData.Scan0 + y * bitmapData.Stride + x * 3;
*(ptr + 0) = (byte)(color.B * 255f);
*(ptr + 1) = (byte)(color.G * 255f);
*(ptr + 2) = (byte)(color.R * 255f);
}
}
}
img.UnlockBits(bitmapData);
return img;
}
}
}
}

165
src/Viewport.cs Normal file
View File

@ -0,0 +1,165 @@
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Drawing;
using OpenMetaverse;
namespace OpenSim.Region.CoreModules.World.Warp3DMap
{
public class Viewport
{
private const float DEG_TO_RAD = (float)Math.PI / 180f;
private static readonly Vector3 UP_DIRECTION = Vector3.UnitZ;
public Vector3 Position;
public Vector3 LookDirection;
public float FieldOfView;
public float NearPlaneDistance;
public float FarPlaneDistance;
public int Width;
public int Height;
public bool Orthographic;
public float OrthoWindowWidth;
public float OrthoWindowHeight;
public Viewport(Vector3 position, Vector3 lookDirection, float fieldOfView, float farPlaneDist, float nearPlaneDist, int width, int height)
{
// Perspective projection mode
Position = position;
LookDirection = lookDirection;
FieldOfView = fieldOfView;
FarPlaneDistance = farPlaneDist;
NearPlaneDistance = nearPlaneDist;
Width = width;
Height = height;
}
public Viewport(Vector3 position, Vector3 lookDirection, float farPlaneDist, float nearPlaneDist, int width, int height, float orthoWindowWidth, float orthoWindowHeight)
{
// Orthographic projection mode
Position = position;
LookDirection = lookDirection;
FarPlaneDistance = farPlaneDist;
NearPlaneDistance = nearPlaneDist;
Width = width;
Height = height;
OrthoWindowWidth = orthoWindowWidth;
OrthoWindowHeight = orthoWindowHeight;
Orthographic = true;
}
public Point VectorToScreen(Vector3 v)
{
Matrix4 m = GetWorldToViewportMatrix();
Vector3 screenPoint = v * m;
return new Point((int)screenPoint.X, (int)screenPoint.Y);
}
public Matrix4 GetWorldToViewportMatrix()
{
Matrix4 result = GetViewMatrix();
result *= GetPerspectiveProjectionMatrix();
result *= GetViewportMatrix();
return result;
}
public Matrix4 GetViewMatrix()
{
Vector3 zAxis = -LookDirection;
zAxis.Normalize();
Vector3 xAxis = Vector3.Cross(UP_DIRECTION, zAxis);
xAxis.Normalize();
Vector3 yAxis = Vector3.Cross(zAxis, xAxis);
Vector3 position = Position;
float offsetX = -Vector3.Dot(xAxis, position);
float offsetY = -Vector3.Dot(yAxis, position);
float offsetZ = -Vector3.Dot(zAxis, position);
return new Matrix4(
xAxis.X, yAxis.X, zAxis.X, 0f,
xAxis.Y, yAxis.Y, zAxis.Y, 0f,
xAxis.Z, yAxis.Z, zAxis.Z, 0f,
offsetX, offsetY, offsetZ, 1f);
}
public Matrix4 GetPerspectiveProjectionMatrix()
{
float aspectRatio = (float)Width / (float)Height;
float hFoV = FieldOfView * DEG_TO_RAD;
float zn = NearPlaneDistance;
float zf = FarPlaneDistance;
float xScale = 1f / (float)Math.Tan(hFoV / 2f);
float yScale = aspectRatio * xScale;
float m33 = (zf == double.PositiveInfinity) ? -1 : (zf / (zn - zf));
float m43 = zn * m33;
return new Matrix4(
xScale, 0f, 0f, 0f,
0f, yScale, 0f, 0f,
0f, 0f, m33, -1f,
0f, 0f, m43, 0f);
}
public Matrix4 GetOrthographicProjectionMatrix(float aspectRatio)
{
float w = Width;
float h = Height;
float zn = NearPlaneDistance;
float zf = FarPlaneDistance;
float m33 = 1 / (zn - zf);
float m43 = zn * m33;
return new Matrix4(
2f / w, 0f, 0f, 0f,
0f, 2f / h, 0f, 0f,
0f, 0f, m33, 0f,
0f, 0f, m43, 1f);
}
public Matrix4 GetViewportMatrix()
{
float scaleX = (float)Width * 0.5f;
float scaleY = (float)Height * 0.5f;
float offsetX = 0f + scaleX;
float offsetY = 0f + scaleY;
return new Matrix4(
scaleX, 0f, 0f, 0f,
0f, -scaleY, 0f, 0f,
0f, 0f, 1f, 0f,
offsetX, offsetY, 0f, 1f);
}
}
}

File diff suppressed because it is too large Load Diff