2015-10-06 20:15:48 +11:00
|
|
|
|
using System;
|
|
|
|
|
using ClassicalSharp.GraphicsAPI;
|
|
|
|
|
using ClassicalSharp.Model;
|
2015-10-08 16:47:53 +11:00
|
|
|
|
using OpenTK;
|
2015-10-06 20:15:48 +11:00
|
|
|
|
|
|
|
|
|
namespace ClassicalSharp {
|
|
|
|
|
|
|
|
|
|
public static class IsometricBlockDrawer {
|
|
|
|
|
|
|
|
|
|
static BlockInfo info;
|
|
|
|
|
static ModelCache cache;
|
|
|
|
|
static TerrainAtlas2D atlas;
|
|
|
|
|
static float blockHeight;
|
|
|
|
|
static int index;
|
|
|
|
|
static float scale;
|
|
|
|
|
|
2015-10-07 16:16:39 +11:00
|
|
|
|
static FastColour colNormal, colXSide, colZSide, colYBottom;
|
2015-10-08 16:47:53 +11:00
|
|
|
|
static float cosX, sinX, cosY, sinY;
|
2015-10-07 16:16:39 +11:00
|
|
|
|
static IsometricBlockDrawer() {
|
|
|
|
|
colNormal = FastColour.White;
|
|
|
|
|
FastColour.GetShaded( colNormal, ref colXSide, ref colZSide, ref colYBottom );
|
2015-10-08 16:47:53 +11:00
|
|
|
|
|
|
|
|
|
cosX = (float)Math.Cos( 26.565f * Utils.Deg2Rad );
|
|
|
|
|
sinX = (float)Math.Sin( 26.565f * Utils.Deg2Rad );
|
|
|
|
|
cosY = (float)Math.Cos( -45f * Utils.Deg2Rad );
|
|
|
|
|
sinY = (float)Math.Sin( -45f * Utils.Deg2Rad );
|
2015-10-07 16:16:39 +11:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-06 20:15:48 +11:00
|
|
|
|
public static void Draw( Game game, byte block, float size, float x, float y ) {
|
|
|
|
|
info = game.BlockInfo;
|
|
|
|
|
cache = game.ModelCache;
|
|
|
|
|
atlas = game.TerrainAtlas;
|
2015-12-23 17:43:45 +11:00
|
|
|
|
blockHeight = info.MaxBB[block].Y;
|
|
|
|
|
if( game.BlockInfo.IsSprite[block] )
|
|
|
|
|
blockHeight = 1;
|
2015-10-06 20:15:48 +11:00
|
|
|
|
index = 0;
|
2015-11-09 19:53:24 +11:00
|
|
|
|
// isometric coords size: cosY * -scale - sinY * scale
|
2015-11-30 08:08:32 +11:00
|
|
|
|
// we need to divide by (2 * cosY), as the calling function expects size to be in pixels.
|
2015-11-09 19:53:24 +11:00
|
|
|
|
scale = size / (2 * cosY);
|
2015-10-07 19:39:20 +11:00
|
|
|
|
|
2015-10-08 16:47:53 +11:00
|
|
|
|
// screen to isometric coords (cos(-x) = cos(x), sin(-x) = -sin(x))
|
|
|
|
|
pos.X = x; pos.Y = y; pos.Z = 0;
|
|
|
|
|
pos = Utils.RotateY( Utils.RotateX( pos, cosX, -sinX ), cosY, -sinY );
|
2015-10-06 20:15:48 +11:00
|
|
|
|
|
|
|
|
|
if( info.IsSprite[block] ) {
|
2015-10-29 06:28:23 +11:00
|
|
|
|
XQuad( block, 0f, TileSide.Right );
|
|
|
|
|
ZQuad( block, 0f, TileSide.Back );
|
2015-10-06 20:15:48 +11:00
|
|
|
|
} else {
|
2015-10-29 06:28:23 +11:00
|
|
|
|
XQuad( block, scale, TileSide.Left );
|
|
|
|
|
ZQuad( block, -scale, TileSide.Back );
|
|
|
|
|
YQuad( block, scale * blockHeight, TileSide.Top );
|
2015-10-06 20:15:48 +11:00
|
|
|
|
}
|
2015-10-07 19:39:20 +11:00
|
|
|
|
|
|
|
|
|
for( int i = 0; i < index; i++ )
|
|
|
|
|
TransformVertex( ref cache.vertices[i] );
|
2015-11-08 16:32:08 +11:00
|
|
|
|
game.Graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, cache.vb,
|
2015-10-06 20:15:48 +11:00
|
|
|
|
cache.vertices, index, index * 6 / 4 );
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 19:39:20 +11:00
|
|
|
|
static void TransformVertex( ref VertexPos3fTex2fCol4b vertex ) {
|
2015-10-11 20:22:20 +11:00
|
|
|
|
Vector3 p = new Vector3( vertex.X, vertex.Y, vertex.Z );
|
|
|
|
|
//p = Utils.RotateY( p - pos, time ) + pos;
|
2015-10-07 19:39:20 +11:00
|
|
|
|
// See comment in IGraphicsApi.Draw2DTexture()
|
2015-10-11 20:22:20 +11:00
|
|
|
|
p.X -= 0.5f; p.Y -= 0.5f;
|
|
|
|
|
p = Utils.RotateX( Utils.RotateY( p, cosY, sinY ), cosX, sinX );
|
|
|
|
|
vertex.X = p.X; vertex.Y = p.Y; vertex.Z = p.Z;
|
2015-10-07 19:39:20 +11:00
|
|
|
|
}
|
2015-10-06 20:15:48 +11:00
|
|
|
|
|
2015-10-07 19:39:20 +11:00
|
|
|
|
static Vector3 pos = Vector3.Zero;
|
2015-10-29 06:28:23 +11:00
|
|
|
|
static void YQuad( byte block, float y, int side ) {
|
2015-10-06 20:15:48 +11:00
|
|
|
|
int texId = info.GetTextureLoc( block, side );
|
2015-10-08 19:39:27 +11:00
|
|
|
|
TextureRec rec = atlas.GetTexRec( texId );
|
2015-10-07 16:16:39 +11:00
|
|
|
|
FastColour col = colNormal;
|
2015-10-06 20:15:48 +11:00
|
|
|
|
|
2015-10-07 19:39:20 +11:00
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - scale, pos.Y - y,
|
2015-10-07 18:14:59 +11:00
|
|
|
|
pos.Z - scale, rec.U2, rec.V2, col );
|
2015-10-07 19:39:20 +11:00
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - scale, pos.Y - y,
|
2015-10-07 18:14:59 +11:00
|
|
|
|
pos.Z + scale, rec.U1, rec.V2, col );
|
2015-10-07 19:39:20 +11:00
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + scale, pos.Y - y,
|
2015-10-07 18:14:59 +11:00
|
|
|
|
pos.Z + scale, rec.U1, rec.V1, col );
|
2015-10-07 19:39:20 +11:00
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + scale, pos.Y - y,
|
2015-10-07 18:14:59 +11:00
|
|
|
|
pos.Z - scale, rec.U2, rec.V1, col );
|
2015-10-06 20:15:48 +11:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 06:28:23 +11:00
|
|
|
|
static void ZQuad( byte block, float z, int side ) {
|
2015-10-06 20:15:48 +11:00
|
|
|
|
int texId = info.GetTextureLoc( block, side );
|
2015-10-08 19:39:27 +11:00
|
|
|
|
TextureRec rec = atlas.GetTexRec( texId );
|
2015-10-06 20:15:48 +11:00
|
|
|
|
if( blockHeight != 1 )
|
|
|
|
|
rec.V2 = rec.V1 + blockHeight * TerrainAtlas2D.invElementSize;
|
2015-10-07 16:16:39 +11:00
|
|
|
|
FastColour col = colZSide;
|
2015-10-06 20:15:48 +11:00
|
|
|
|
|
2015-10-07 19:39:20 +11:00
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - scale, pos.Y + scale * blockHeight,
|
|
|
|
|
pos.Z - z, rec.U1, rec.V2, col );
|
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - scale, pos.Y - scale * blockHeight,
|
|
|
|
|
pos.Z - z, rec.U1, rec.V1, col );
|
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + scale, pos.Y - scale * blockHeight,
|
|
|
|
|
pos.Z - z, rec.U2, rec.V1, col );
|
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + scale, pos.Y + scale * blockHeight,
|
|
|
|
|
pos.Z - z, rec.U2, rec.V2, col );
|
2015-10-06 20:15:48 +11:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 06:28:23 +11:00
|
|
|
|
static void XQuad( byte block, float x, int side ) {
|
2015-10-06 20:15:48 +11:00
|
|
|
|
int texId = info.GetTextureLoc( block, side );
|
2015-10-08 19:39:27 +11:00
|
|
|
|
TextureRec rec = atlas.GetTexRec( texId );
|
2015-10-06 20:15:48 +11:00
|
|
|
|
if( blockHeight != 1 )
|
|
|
|
|
rec.V2 = rec.V1 + blockHeight * TerrainAtlas2D.invElementSize;
|
2015-10-07 16:16:39 +11:00
|
|
|
|
FastColour col = colXSide;
|
2015-10-06 20:15:48 +11:00
|
|
|
|
|
2015-10-07 19:39:20 +11:00
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - x, pos.Y + scale * blockHeight,
|
2015-11-09 19:53:24 +11:00
|
|
|
|
pos.Z - scale , rec.U1, rec.V2, col );
|
2015-10-07 19:39:20 +11:00
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - x, pos.Y - scale * blockHeight,
|
2015-10-20 12:57:18 +11:00
|
|
|
|
pos.Z - scale, rec.U1, rec.V1, col );
|
2015-10-07 19:39:20 +11:00
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - x, pos.Y - scale * blockHeight,
|
2015-10-20 12:57:18 +11:00
|
|
|
|
pos.Z + scale, rec.U2, rec.V1, col );
|
2015-10-07 19:39:20 +11:00
|
|
|
|
cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - x, pos.Y + scale * blockHeight,
|
2015-10-20 12:57:18 +11:00
|
|
|
|
pos.Z + scale, rec.U2, rec.V2, col );
|
2015-10-06 20:15:48 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|