Made some functions in cChunk and cNoise inline, this should significantly increase chunk generation speed
git-svn-id: http://mc-server.googlecode.com/svn/trunk@115 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
parent
4c76ca986c
commit
c35db25269
@ -514,6 +514,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\source\AllToLua.pkg" />
|
||||
<None Include="..\source\cNoise.inc" />
|
||||
<None Include="icon.ico" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -1302,6 +1302,9 @@
|
||||
<Filter>LuaBindings</Filter>
|
||||
</None>
|
||||
<None Include="icon.ico" />
|
||||
<None Include="..\source\cNoise.inc">
|
||||
<Filter>cNoise</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="MCServer.rc" />
|
||||
|
@ -448,13 +448,6 @@ void cChunk::CreateBlockEntities()
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int cChunk::MakeIndex(int x, int y, int z )
|
||||
{
|
||||
if( x < 16 && x > -1 && y < 128 && y > -1 && z < 16 && z > -1 )
|
||||
return y + (z * 128) + (x * 128 * 16);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char cChunk::GetLight(char* a_Buffer, int a_BlockIdx)
|
||||
{
|
||||
if( a_BlockIdx > -1 && a_BlockIdx < c_NumBlocks )
|
||||
|
@ -111,7 +111,12 @@ private:
|
||||
|
||||
void CreateBlockEntities();
|
||||
|
||||
unsigned int MakeIndex(int x, int y, int z );
|
||||
inline unsigned int cChunk::MakeIndex(int x, int y, int z )
|
||||
{
|
||||
if( x < 16 && x > -1 && y < 128 && y > -1 && z < 16 && z > -1 )
|
||||
return y + (z * 128) + (x * 128 * 16);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool m_bCalculateLighting;
|
||||
bool m_bCalculateHeightmap;
|
||||
|
@ -52,17 +52,6 @@ void cEntity::Initialize( cWorld* a_World )
|
||||
m_World->AddEntity( this );
|
||||
|
||||
MoveToCorrectChunk(true);
|
||||
|
||||
/*
|
||||
Not needed because it´s covered by the MoveToCorrectChunk function
|
||||
cWorld::BlockToChunk( (int)m_Pos->x, (int)m_Pos->y, (int)m_Pos->z, m_ChunkX, m_ChunkY, m_ChunkZ );
|
||||
cChunk* Chunk = m_World->GetChunk( m_ChunkX, m_ChunkY, m_ChunkZ );
|
||||
if( Chunk )
|
||||
{
|
||||
//LOG("Adding entity %i to chunk %i %i %i", m_UniqueID, Chunk->GetPosX(), Chunk->GetPosY(), Chunk->GetPosZ() );
|
||||
Chunk->AddEntity( *this );
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void cEntity::WrapRotation()
|
||||
@ -73,17 +62,17 @@ void cEntity::WrapRotation()
|
||||
while(m_Rot->y < -180.f) m_Rot->y+=360.f;
|
||||
}
|
||||
|
||||
void cEntity::MoveToCorrectChunk(bool a_IgnoreOldChunk)
|
||||
void cEntity::MoveToCorrectChunk(bool a_bIgnoreOldChunk)
|
||||
{
|
||||
if( !m_World ) return; // Entity needs a world to move to a chunk
|
||||
|
||||
int ChunkX = 0, ChunkY = 0, ChunkZ = 0;
|
||||
cWorld::BlockToChunk( (int)m_Pos->x, (int)m_Pos->y, (int)m_Pos->z, ChunkX, ChunkY, ChunkZ );
|
||||
if(a_IgnoreOldChunk || m_ChunkX != ChunkX || m_ChunkY != ChunkY || m_ChunkZ != ChunkZ)
|
||||
if(a_bIgnoreOldChunk || m_ChunkX != ChunkX || m_ChunkY != ChunkY || m_ChunkZ != ChunkZ)
|
||||
{
|
||||
LOG("From %i %i To %i %i", m_ChunkX, m_ChunkZ, ChunkX, ChunkZ );
|
||||
cChunk* Chunk = 0;
|
||||
if(!a_IgnoreOldChunk)
|
||||
if(!a_bIgnoreOldChunk)
|
||||
Chunk = m_World->GetChunkUnreliable( m_ChunkX, m_ChunkY, m_ChunkZ );
|
||||
|
||||
typedef std::list< cClientHandle* > ClientList;
|
||||
|
@ -86,7 +86,7 @@ public: //tolua_export
|
||||
void WrapRotation();
|
||||
|
||||
protected:
|
||||
void MoveToCorrectChunk(bool a_IgnoreOldChunk = false);
|
||||
void MoveToCorrectChunk(bool a_bIgnoreOldChunk = false);
|
||||
|
||||
friend class cReferenceManager;
|
||||
void AddReference( cEntity*& a_EntityPtr );
|
||||
|
@ -1,7 +1,9 @@
|
||||
#include "cNoise.h"
|
||||
#include <math.h>
|
||||
|
||||
#if NOISE_USE_SSE
|
||||
#include <smmintrin.h> //_mm_mul_epi32
|
||||
#endif
|
||||
|
||||
#define FAST_FLOOR( x ) ( (x) < 0 ? ((int)x)-1 : ((int)x) )
|
||||
|
||||
@ -15,29 +17,7 @@ cNoise::~cNoise()
|
||||
{
|
||||
}
|
||||
|
||||
/****************
|
||||
* Random value generator
|
||||
**/
|
||||
float cNoise::IntNoise( int a_X ) const
|
||||
{
|
||||
int x = ((a_X*m_Seed)<<13) ^ a_X;
|
||||
return ( 1.0f - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f);
|
||||
}
|
||||
|
||||
float cNoise::IntNoise2D( int a_X, int a_Y ) const
|
||||
{
|
||||
int n = a_X + a_Y * 57 + m_Seed*57*57;
|
||||
n = (n<<13) ^ n;
|
||||
return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f);
|
||||
}
|
||||
|
||||
float cNoise::IntNoise3D( int a_X, int a_Y, int a_Z ) const
|
||||
{
|
||||
int n = a_X + a_Y * 57 + a_Z * 57*57 + m_Seed*57*57*57;
|
||||
n = (n<<13) ^ n;
|
||||
return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f);
|
||||
}
|
||||
|
||||
#if NOISE_USE_SSE
|
||||
/****************
|
||||
* SSE Random value generator
|
||||
**/
|
||||
@ -71,6 +51,7 @@ __m128 cNoise::SSE_IntNoise2D( int a_X1, int a_Y1, int a_X2, int a_Y2, int a_X3,
|
||||
|
||||
return Result4;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/***************
|
||||
@ -166,6 +147,7 @@ float cNoise::CubicNoise2D( float a_X, float a_Y ) const
|
||||
return CubicInterpolate( interp1, interp2, interp3, interp4, FracY );
|
||||
}
|
||||
|
||||
#if NOISE_USE_SSE
|
||||
float cNoise::SSE_CubicNoise2D( float a_X, float a_Y ) const
|
||||
{
|
||||
const int BaseX = FAST_FLOOR( a_X );
|
||||
@ -185,6 +167,7 @@ float cNoise::SSE_CubicNoise2D( float a_X, float a_Y ) const
|
||||
const float FracY = (a_Y) - BaseY;
|
||||
return CubicInterpolate( AllInterp.p[0], AllInterp.p[1], AllInterp.p[2], AllInterp.p[3], FracY );
|
||||
}
|
||||
#endif
|
||||
|
||||
/******************
|
||||
* Interpolated (and 1 smoothed) noise in 3-dimensions
|
||||
@ -287,16 +270,8 @@ float cNoise::CubicNoise3D( float a_X, float a_Y, float a_Z ) const
|
||||
/******************
|
||||
* Private
|
||||
**/
|
||||
float cNoise::CubicInterpolate( float a_A, float a_B, float a_C, float a_D, float a_Pct ) const
|
||||
{
|
||||
float P = (a_D - a_C) - (a_A - a_B);
|
||||
float Q = (a_A - a_B) - P;
|
||||
float R = a_C - a_A;
|
||||
float S = a_B;
|
||||
|
||||
return P*(a_Pct*a_Pct*a_Pct) + Q*(a_Pct*a_Pct) + R*a_Pct + S;
|
||||
}
|
||||
|
||||
#if NOISE_USE_SSE
|
||||
__m128 cNoise::CubicInterpolate4( const __m128 & a_A, const __m128 & a_B, const __m128 & a_C, const __m128 & a_D, float a_Pct ) const
|
||||
{
|
||||
const __m128 P = _mm_sub_ps( _mm_sub_ps( a_D, a_C ), _mm_sub_ps( a_A, a_B ) );
|
||||
@ -309,15 +284,8 @@ __m128 cNoise::CubicInterpolate4( const __m128 & a_A, const __m128 & a_B, const
|
||||
|
||||
return _mm_add_ps( _mm_add_ps( _mm_add_ps( _mm_mul_ps(P, Pct3), _mm_mul_ps( Q, Pct2 ) ), _mm_mul_ps( R, Pct ) ), a_B );
|
||||
}
|
||||
#endif
|
||||
|
||||
float cNoise::CosineInterpolate( float a_A, float a_B, float a_Pct ) const
|
||||
{
|
||||
const float ft = a_Pct * 3.1415927f;
|
||||
const float f = (1.f - cosf(ft)) * 0.5f;
|
||||
return a_A*(1-f) + a_B*f;
|
||||
}
|
||||
|
||||
float cNoise::LinearInterpolate( float a_A, float a_B, float a_Pct ) const
|
||||
{
|
||||
return a_A*(1.f-a_Pct) + a_B*a_Pct;
|
||||
}
|
||||
#if NOISE_USE_INLINE
|
||||
# include "cNoise.inc"
|
||||
#endif
|
@ -1,6 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
// Some settings
|
||||
#define NOISE_USE_INLINE 1
|
||||
#define NOISE_USE_SSE 0
|
||||
|
||||
// Do not touch
|
||||
#if NOISE_USE_INLINE
|
||||
# define __NOISE_INLINE__ inline
|
||||
#else
|
||||
# define __NOISE_INLINE__
|
||||
#endif
|
||||
|
||||
#if NOISE_USE_SSE
|
||||
# include <emmintrin.h>
|
||||
#endif
|
||||
|
||||
class cNoise
|
||||
{
|
||||
@ -8,10 +21,13 @@ public:
|
||||
cNoise( unsigned int a_Seed );
|
||||
~cNoise();
|
||||
|
||||
float IntNoise( int a_X ) const;
|
||||
float IntNoise2D( int a_X, int a_Y ) const;
|
||||
#if NOISE_USE_SSE
|
||||
__m128 SSE_IntNoise2D( int a_X1, int a_Y1, int a_X2, int a_Y2, int a_X3, int a_Y3, int a_X4, int a_Y4 ) const;
|
||||
float IntNoise3D( int a_X, int a_Y, int a_Z ) const;
|
||||
#endif
|
||||
|
||||
__NOISE_INLINE__ float IntNoise( int a_X ) const;
|
||||
__NOISE_INLINE__ float IntNoise2D( int a_X, int a_Y ) const;
|
||||
__NOISE_INLINE__ float IntNoise3D( int a_X, int a_Y, int a_Z ) const;
|
||||
|
||||
float LinearNoise1D( float a_X ) const;
|
||||
float CosineNoise1D( float a_X ) const;
|
||||
@ -28,11 +44,17 @@ public:
|
||||
|
||||
void SetSeed( unsigned int a_Seed ) { m_Seed = a_Seed; }
|
||||
private:
|
||||
float CubicInterpolate( float a_A, float a_B, float a_C, float a_D, float a_Pct ) const;
|
||||
__NOISE_INLINE__ float CubicInterpolate( float a_A, float a_B, float a_C, float a_D, float a_Pct ) const;
|
||||
__NOISE_INLINE__ float CosineInterpolate( float a_A, float a_B, float a_Pct ) const;
|
||||
__NOISE_INLINE__ float LinearInterpolate( float a_A, float a_B, float a_Pct ) const;
|
||||
|
||||
#if NOISE_USE_SSE
|
||||
__m128 CubicInterpolate4( const __m128 & a_A, const __m128 & a_B, const __m128 & a_C, const __m128 & a_D, float a_Pct ) const;
|
||||
float CosineInterpolate( float a_A, float a_B, float a_Pct ) const;
|
||||
float LinearInterpolate( float a_A, float a_B, float a_Pct ) const;
|
||||
#endif
|
||||
|
||||
unsigned int m_Seed;
|
||||
};
|
||||
|
||||
#if NOISE_USE_INLINE
|
||||
# include "cNoise.inc"
|
||||
#endif
|
||||
|
54
source/cNoise.inc
Normal file
54
source/cNoise.inc
Normal file
@ -0,0 +1,54 @@
|
||||
#ifndef __C_NOISE_INC__
|
||||
#define __C_NOISE_INC__
|
||||
|
||||
#include <math.h>
|
||||
|
||||
/****************
|
||||
* Random value generator
|
||||
**/
|
||||
float cNoise::IntNoise( int a_X ) const
|
||||
{
|
||||
int x = ((a_X*m_Seed)<<13) ^ a_X;
|
||||
return ( 1.0f - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f);
|
||||
}
|
||||
|
||||
float cNoise::IntNoise2D( int a_X, int a_Y ) const
|
||||
{
|
||||
int n = a_X + a_Y * 57 + m_Seed*57*57;
|
||||
n = (n<<13) ^ n;
|
||||
return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f);
|
||||
}
|
||||
|
||||
float cNoise::IntNoise3D( int a_X, int a_Y, int a_Z ) const
|
||||
{
|
||||
int n = a_X + a_Y * 57 + a_Z * 57*57 + m_Seed*57*57*57;
|
||||
n = (n<<13) ^ n;
|
||||
return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f);
|
||||
}
|
||||
|
||||
/****************
|
||||
* Interpolation functions
|
||||
**/
|
||||
float cNoise::CubicInterpolate( float a_A, float a_B, float a_C, float a_D, float a_Pct ) const
|
||||
{
|
||||
float P = (a_D - a_C) - (a_A - a_B);
|
||||
float Q = (a_A - a_B) - P;
|
||||
float R = a_C - a_A;
|
||||
float S = a_B;
|
||||
|
||||
return P*(a_Pct*a_Pct*a_Pct) + Q*(a_Pct*a_Pct) + R*a_Pct + S;
|
||||
}
|
||||
|
||||
float cNoise::CosineInterpolate( float a_A, float a_B, float a_Pct ) const
|
||||
{
|
||||
const float ft = a_Pct * 3.1415927f;
|
||||
const float f = (1.f - cosf(ft)) * 0.5f;
|
||||
return a_A*(1-f) + a_B*f;
|
||||
}
|
||||
|
||||
float cNoise::LinearInterpolate( float a_A, float a_B, float a_Pct ) const
|
||||
{
|
||||
return a_A*(1.f-a_Pct) + a_B*a_Pct;
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user