Integrate ChunkMeshBuilderTex2Col4 and ChunkMeshBuilder into one class.

This commit is contained in:
UnknownShadow200 2015-05-02 06:53:48 +10:00
parent afd70c38c2
commit 3994af123b
3 changed files with 40 additions and 92 deletions

View File

@ -3,12 +3,12 @@ using ClassicalSharp.GraphicsAPI;
namespace ClassicalSharp {
public abstract class ChunkMeshBuilder {
public partial class ChunkMeshBuilder {
protected int X, Y, Z;
protected byte tile;
int X, Y, Z;
byte tile;
public BlockInfo BlockInfo;
protected Map map;
Map map;
public Game Window;
public IGraphicsApi Graphics;
const int chunkSize = 16, extChunkSize = 18;
@ -19,12 +19,13 @@ namespace ClassicalSharp {
Window = window;
Graphics = window.Graphics;
BlockInfo = window.BlockInfo;
Window.TerrainAtlasChanged += TerrainAtlasChanged;
}
protected int width, length, height;
protected int maxX, maxY, maxZ;
protected byte[] counts = new byte[chunkSize3 * 6];
protected byte[] chunk = new byte[extChunkSize3];
int width, length, height;
int maxX, maxY, maxZ;
byte[] counts = new byte[chunkSize3 * 6];
byte[] chunk = new byte[extChunkSize3];
bool BuildChunk( int x1, int y1, int z1 ) {
PreStretchTiles( x1, y1, z1 );
@ -94,15 +95,6 @@ namespace ClassicalSharp {
public ChunkDrawInfo GetDrawInfo( int x, int y, int z ) {
return BuildChunk( x, y, z ) ? null : GetChunkInfo( x, y, z );
}
protected virtual void PreStretchTiles( int x1, int y1, int z1 ) {
}
protected virtual void PostStretchTiles( int x1, int y1, int z1 ) {
}
protected virtual void PreRenderTile() {
}
public void RenderTile( int chunkIndex, int xx, int yy, int zz, int x, int y, int z ) {
tile = chunk[chunkIndex];
@ -110,7 +102,7 @@ namespace ClassicalSharp {
X = x;
Y = y;
Z = z;
PreRenderTile();
blockHeight = -1;
int index = ( ( yy << 8 ) + ( zz << 4 ) + xx ) * 6;
int count = 0;
@ -146,8 +138,7 @@ namespace ClassicalSharp {
if( count != 0 ) {
DrawTopFace( count );
}
}
}
void Stretch( int x1, int y1, int z1 ) {
for( int i = 0; i < counts.Length; i++ ) {
@ -186,13 +177,7 @@ namespace ClassicalSharp {
}
}
protected virtual void AddSpriteVertices( byte tile, int count ) {
}
protected virtual void AddVertices( byte tile, int count, int face ) {
}
protected int startX, startY, startZ;
int startX, startY, startZ;
void DoStretchTerrain( int xx, int yy, int zz, int x, int y, int z, int index, byte tile, int chunkIndex ) {
startX = x;
startY = y;
@ -275,12 +260,7 @@ namespace ClassicalSharp {
}
}
protected virtual bool CanStretch( byte initialTile, int chunkIndex, int x, int y, int z, int face ) {
byte tile = chunk[chunkIndex];
return tile == initialTile && !BlockInfo.IsFaceHidden( tile, GetNeighbour( chunkIndex, face ), face );
}
protected byte GetNeighbour( int chunkIndex, int face ) {
byte GetNeighbour( int chunkIndex, int face ) {
switch( face ) {
case TileSide.Left:
return chunk[chunkIndex - 1]; // x - 1
@ -308,7 +288,7 @@ namespace ClassicalSharp {
x++;
chunkIndex++;
countIndex += 6;
int max = 16 - xx;
int max = chunkSize - xx;
while( count < max && x < width && CanStretch( tile, chunkIndex, x, y, z, face ) ) {
count++;
counts[countIndex + face] = 0;
@ -322,31 +302,23 @@ namespace ClassicalSharp {
int StretchZ( int zz, int countIndex, int x, int y, int z, int chunkIndex, byte tile, int face ) {
int count = 1;
z++;
chunkIndex += 18;
countIndex += 16 * 6;
int max = 16 - zz;
chunkIndex += extChunkSize;
countIndex += chunkSize * 6;
int max = chunkSize - zz;
while( count < max && z < length && CanStretch( tile, chunkIndex, x, y, z, face ) ) {
count++;
counts[countIndex + face] = 0;
z++;
chunkIndex += 18;
countIndex += 16 * 6;
chunkIndex += extChunkSize;
countIndex += chunkSize * 6;
}
return count;
}
public abstract void BeginRender();
public abstract void Render( ChunkPartInfo drawInfo );
public abstract void Render2( ChunkPartInfo drawInfo );
public abstract void EndRender();
public virtual void OnNewMap() {
public void OnNewMap() {
}
public virtual void OnNewMapLoaded() {
public void OnNewMapLoaded() {
map = Window.Map;
width = map.Width;
height = map.Height;
@ -355,22 +327,6 @@ namespace ClassicalSharp {
maxY = height - 1;
maxZ = length - 1;
}
protected abstract ChunkDrawInfo GetChunkInfo( int x, int y, int z );
protected abstract void DrawTopFace( int count );
protected abstract void DrawBottomFace( int count );
protected abstract void DrawBackFace( int count );
protected abstract void DrawFrontFace( int count );
protected abstract void DrawLeftFace( int count );
protected abstract void DrawRightFace( int count );
protected abstract void DrawSprite( int count );
}
public class ChunkDrawInfo {

View File

@ -3,16 +3,12 @@ using ClassicalSharp.GraphicsAPI;
namespace ClassicalSharp {
public class ChunkMeshBuilderTex2Col4 : ChunkMeshBuilder {
public partial class ChunkMeshBuilder {
DrawInfo1D[] drawInfoBuffer;
TextureAtlas1D atlas;
int arraysCount = 0;
const int maxIndices = 65536;
public ChunkMeshBuilderTex2Col4( Game window ) : base( window ) {
Window.TerrainAtlasChanged += TerrainAtlasChanged;
}
void TerrainAtlasChanged( object sender, EventArgs e ) {
int newArraysCount = Window.TerrainAtlas1DTexIds.Length;
@ -74,7 +70,7 @@ namespace ClassicalSharp {
}
}
protected override bool CanStretch( byte initialTile, int chunkIndex, int x, int y, int z, int face ) {
bool CanStretch( byte initialTile, int chunkIndex, int x, int y, int z, int face ) {
byte tile = chunk[chunkIndex];
return tile == initialTile && !BlockInfo.IsFaceHidden( tile, GetNeighbour( chunkIndex, face ), face ) &&
( IsLit( startX, startY, startZ, face ) == IsLit( x, y, z, face ) );
@ -131,7 +127,7 @@ namespace ClassicalSharp {
( BlockInfo.BlockHeight( map.GetBlock( x, y, z ) ) == 1 ? y : y - 1 );
}
protected override ChunkDrawInfo GetChunkInfo( int x, int y, int z ) {
ChunkDrawInfo GetChunkInfo( int x, int y, int z ) {
ChunkDrawInfo info = new ChunkDrawInfo( arraysCount );
for( int i = 0; i < arraysCount; i++ ) {
DrawInfo1D drawInfo = drawInfoBuffer[i];
@ -158,11 +154,8 @@ namespace ClassicalSharp {
bool isTranslucent;
float blockHeight;
float invVerElementSize;
protected override void PreRenderTile() {
blockHeight = -1;
}
protected override void PreStretchTiles( int x1, int y1, int z1 ) {
void PreStretchTiles( int x1, int y1, int z1 ) {
invVerElementSize = Window.TerrainAtlas1D.invElementSize;
arraysCount = Window.TerrainAtlas1DTexIds.Length;
atlas = Window.TerrainAtlas1D;
@ -182,7 +175,7 @@ namespace ClassicalSharp {
}
}
protected override void PostStretchTiles( int x1, int y1, int z1 ) {
void PostStretchTiles( int x1, int y1, int z1 ) {
for( int i = 0; i < drawInfoBuffer.Length; i++ ) {
DrawInfo1D info = drawInfoBuffer[i];
info.Solid.ExpandToCapacity();
@ -191,28 +184,28 @@ namespace ClassicalSharp {
}
}
public override void BeginRender() {
public void BeginRender() {
Graphics.BeginIndexedVbBatch();
}
public override void Render( ChunkPartInfo info ) {
public void Render( ChunkPartInfo info ) {
Graphics.DrawIndexedVbBatch( DrawMode.Triangles, info.VbId, info.IndicesCount );
}
public override void Render2( ChunkPartInfo info ) {
public void Render2( ChunkPartInfo info ) {
Graphics.DrawIndexedVbBatch( DrawMode.Triangles, info.VbId2, info.IndicesCount2 );
}
public override void EndRender() {
public void EndRender() {
Graphics.EndIndexedVbBatch();
}
protected override void AddSpriteVertices( byte tile, int count ) {
void AddSpriteVertices( byte tile, int count ) {
int i = atlas.Get1DIndex( BlockInfo.GetOptimTextureLoc( tile, TileSide.Left ) );
drawInfoBuffer[i].Sprite.iCount += 6 + 6 * count;
}
protected override void AddVertices( byte tile, int count, int face ) {
void AddVertices( byte tile, int count, int face ) {
int i = atlas.Get1DIndex( BlockInfo.GetOptimTextureLoc( tile, face ) );
if( BlockInfo.IsTranslucent( tile ) ) {
drawInfoBuffer[i].Translucent.iCount += 6;
@ -221,7 +214,7 @@ namespace ClassicalSharp {
}
}
protected override void DrawTopFace( int count ) {
void DrawTopFace( int count ) {
int texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Top );
int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out drawInfoIndex );
@ -241,7 +234,7 @@ namespace ClassicalSharp {
part.vertices[part.vIndex++] = new VertexPos3fTex2fCol4b( X + count, Y + blockHeight, Z + 1, rec.U2, rec.V2, col );
}
protected override void DrawBottomFace( int count ) {
void DrawBottomFace( int count ) {
int texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Bottom );
int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out drawInfoIndex );
@ -261,7 +254,7 @@ namespace ClassicalSharp {
part.vertices[part.vIndex++] = new VertexPos3fTex2fCol4b( X + count, Y, Z, rec.U2, rec.V1, col );
}
protected override void DrawBackFace( int count ) {
void DrawBackFace( int count ) {
int texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Back );
int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out drawInfoIndex );
@ -284,7 +277,7 @@ namespace ClassicalSharp {
part.vertices[part.vIndex++] = new VertexPos3fTex2fCol4b( X + count, Y, Z + 1, rec.U2, rec.V2, col );
}
protected override void DrawFrontFace( int count ) {
void DrawFrontFace( int count ) {
int texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Front );
int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out drawInfoIndex );
@ -307,7 +300,7 @@ namespace ClassicalSharp {
part.vertices[part.vIndex++] = new VertexPos3fTex2fCol4b( X + count, Y + blockHeight, Z, rec.U1, rec.V1, col );
}
protected override void DrawLeftFace( int count ) {
void DrawLeftFace( int count ) {
int texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Left );
int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out drawInfoIndex );
@ -330,7 +323,7 @@ namespace ClassicalSharp {
part.vertices[part.vIndex++] = new VertexPos3fTex2fCol4b( X, Y, Z + count, rec.U2, rec.V2, col );
}
protected override void DrawRightFace( int count ) {
void DrawRightFace( int count ) {
int texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Right );
int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out drawInfoIndex );
@ -353,7 +346,7 @@ namespace ClassicalSharp {
part.vertices[part.vIndex++] = new VertexPos3fTex2fCol4b( X + 1, Y, Z, rec.U2, rec.V2, col );
}
protected override void DrawSprite( int count ) {
void DrawSprite( int count ) {
int texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Right );
int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out drawInfoIndex );

View File

@ -51,7 +51,7 @@ namespace ClassicalSharp {
public MapRenderer( Game window ) {
Window = window;
_1Dcount = window.TerrainAtlas1DTexIds.Length;
builder = new ChunkMeshBuilderTex2Col4( window );
builder = new ChunkMeshBuilder( window );
Graphics = window.Graphics;
elementsPerBitmap = window.TerrainAtlas1D.elementsPerBitmap;
Window.TerrainAtlasChanged += TerrainAtlasChanged;
@ -119,7 +119,6 @@ namespace ClassicalSharp {
void ClearChunkCache() {
if( chunks == null ) return;
for( int i = 0; i < chunks.Length; i++ ) {
ChunkInfo info = chunks[i];
DeleteChunk( chunks[i] );
}
}