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

View File

@ -3,17 +3,13 @@ using ClassicalSharp.GraphicsAPI;
namespace ClassicalSharp { namespace ClassicalSharp {
public class ChunkMeshBuilderTex2Col4 : ChunkMeshBuilder { public partial class ChunkMeshBuilder {
DrawInfo1D[] drawInfoBuffer; DrawInfo1D[] drawInfoBuffer;
TextureAtlas1D atlas; TextureAtlas1D atlas;
int arraysCount = 0; int arraysCount = 0;
const int maxIndices = 65536; const int maxIndices = 65536;
public ChunkMeshBuilderTex2Col4( Game window ) : base( window ) {
Window.TerrainAtlasChanged += TerrainAtlasChanged;
}
void TerrainAtlasChanged( object sender, EventArgs e ) { void TerrainAtlasChanged( object sender, EventArgs e ) {
int newArraysCount = Window.TerrainAtlas1DTexIds.Length; int newArraysCount = Window.TerrainAtlas1DTexIds.Length;
if( arraysCount > 0 && arraysCount != newArraysCount ) { if( arraysCount > 0 && arraysCount != newArraysCount ) {
@ -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]; byte tile = chunk[chunkIndex];
return tile == initialTile && !BlockInfo.IsFaceHidden( tile, GetNeighbour( chunkIndex, face ), face ) && return tile == initialTile && !BlockInfo.IsFaceHidden( tile, GetNeighbour( chunkIndex, face ), face ) &&
( IsLit( startX, startY, startZ, face ) == IsLit( x, y, z, 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 ); ( 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 ); ChunkDrawInfo info = new ChunkDrawInfo( arraysCount );
for( int i = 0; i < arraysCount; i++ ) { for( int i = 0; i < arraysCount; i++ ) {
DrawInfo1D drawInfo = drawInfoBuffer[i]; DrawInfo1D drawInfo = drawInfoBuffer[i];
@ -158,11 +154,8 @@ namespace ClassicalSharp {
bool isTranslucent; bool isTranslucent;
float blockHeight; float blockHeight;
float invVerElementSize; 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; invVerElementSize = Window.TerrainAtlas1D.invElementSize;
arraysCount = Window.TerrainAtlas1DTexIds.Length; arraysCount = Window.TerrainAtlas1DTexIds.Length;
atlas = Window.TerrainAtlas1D; 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++ ) { for( int i = 0; i < drawInfoBuffer.Length; i++ ) {
DrawInfo1D info = drawInfoBuffer[i]; DrawInfo1D info = drawInfoBuffer[i];
info.Solid.ExpandToCapacity(); info.Solid.ExpandToCapacity();
@ -191,28 +184,28 @@ namespace ClassicalSharp {
} }
} }
public override void BeginRender() { public void BeginRender() {
Graphics.BeginIndexedVbBatch(); Graphics.BeginIndexedVbBatch();
} }
public override void Render( ChunkPartInfo info ) { public void Render( ChunkPartInfo info ) {
Graphics.DrawIndexedVbBatch( DrawMode.Triangles, info.VbId, info.IndicesCount ); 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 ); Graphics.DrawIndexedVbBatch( DrawMode.Triangles, info.VbId2, info.IndicesCount2 );
} }
public override void EndRender() { public void EndRender() {
Graphics.EndIndexedVbBatch(); 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 ) ); int i = atlas.Get1DIndex( BlockInfo.GetOptimTextureLoc( tile, TileSide.Left ) );
drawInfoBuffer[i].Sprite.iCount += 6 + 6 * count; 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 ) ); int i = atlas.Get1DIndex( BlockInfo.GetOptimTextureLoc( tile, face ) );
if( BlockInfo.IsTranslucent( tile ) ) { if( BlockInfo.IsTranslucent( tile ) ) {
drawInfoBuffer[i].Translucent.iCount += 6; 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 texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Top );
int drawInfoIndex; int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out 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 ); 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 texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Bottom );
int drawInfoIndex; int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out 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 ); 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 texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Back );
int drawInfoIndex; int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out 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 ); 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 texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Front );
int drawInfoIndex; int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out 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 ); 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 texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Left );
int drawInfoIndex; int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out 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 ); 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 texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Right );
int drawInfoIndex; int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out 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 ); 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 texId = BlockInfo.GetOptimTextureLoc( tile, TileSide.Right );
int drawInfoIndex; int drawInfoIndex;
TextureRectangle rec = atlas.GetTexRec( texId, out drawInfoIndex ); TextureRectangle rec = atlas.GetTexRec( texId, out drawInfoIndex );

View File

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