Пример #1
0
void Chunk::Init() {
	int width = g_BoxWidth;
	int depth = g_BoxDepth;
	int height = g_BoxHeight;

	int nCount = 0;

	float xLimit = 0;
	float yLimit = 0;
	float zLimit = 0;
	
	CPerformanceCounter counter;
	counter.Start();

	m_ChunkPrim.Init();

	m_pPhysContainer = new PhysContainer;
	m_GameObjects = new GameObject[g_BoxWidth * g_BoxDepth * g_BoxHeight];
	m_pBoxGrid = NULL;//new Prim_t*[g_BoxWidth * g_BoxDepth * g_BoxHeight];

	//m_pPhysContainer->halfwidths.Set(width * 4.0f + 0.01f, height * 4.0f + 0.01f, depth * 4.0f + 0.01f);
	//m_pPhysContainer->center.Set( );
	
	Vector3d transPos;
	transPos.Set( m_Pos );//-128.0f, -10.0f, -128.0f);

	for ( int h = 0; h < height; ++h ){
		for ( int i = 0; i < depth; ++i ){
			for ( int j = 0; j < width; ++j ){
				int idx = (h*(depth*width)) + (i*width) + j;
				//printf("idx: %d\n", idx);
				
				int r = rand() % 100;
				
				if( r > 65 ) {
					float x = transPos.x + (j*8);
					float y = transPos.y + (h*8);
					float z = transPos.z + (i*8);
					
					float xFabs = fabs(x);
					float yFabs = fabs(y);
					float zFabs = fabs(z);
					
					if( xFabs > xLimit) xLimit = xFabs;
					if( yFabs > yLimit) yLimit = yFabs;
					if( zFabs > zLimit) zLimit = zFabs;

					/*
					if( r > 75) {
						m_pBoxGrid[ idx ] = CreateBox( "dirt.raw", 4 );
					} else {
						m_pBoxGrid[ idx ] = CreateBox( "test.raw", 4 );
					}
					*/
					//m_pBoxGrid[ idx ] = CreateBox( "test.raw", 4 );
					
					//m_pBoxGrid[ idx ]->vPos.Set( x, y, z );
					m_GameObjects[ idx ].renderable = NULL;
					
					Vector3d boxPos;
					boxPos.Set( transPos.x * -1.0f, transPos.y * -1.0f, transPos.z * -1.0f );
					boxPos.x += x;
					boxPos.y += y;
					boxPos.z += z;
					#if CHUNK_OPTIMIZATIONS
					m_ChunkPrim.AddBoxOpt(boxPos);
					#else
					m_ChunkPrim.AddBox(boxPos);
					#endif
					
					//if(m_pBoxGrid[ idx ] == NULL) {
						//printf("Box call failed\n");
					//}
					
					//printf("Box Pos: %f %f %f\n", g_pBoxGrid[ idx ]->vPos.x, g_pBoxGrid[ idx ]->vPos.y, g_pBoxGrid[ idx ]->vPos.z);
		
					//m_GameObjects[ idx ].renderable = m_pBoxGrid[ idx ];
					m_GameObjects[ idx ].phys = new PhysObject;
		
					//center point
					m_GameObjects[ idx ].phys->center.Set(transPos.x + (j*8), (transPos.y *0.5f) + (h*8), transPos.z + (i*8));
					m_GameObjects[ idx ].phys->halfwidths.Set(4.0f,4.0f,4.0f);

					m_pPhysContainer->AddObject( m_GameObjects[ idx ].phys );
					
					nCount++;
				} else {
					m_GameObjects[ idx ].renderable = NULL;
					m_GameObjects[ idx ].phys = NULL;
					//printf("renderable set to NULL\n");
				}
	
			}
		}
	}

	m_pPhysContainer->halfwidths.Set(width * 4.0f + 0.01f, height * 4.0f + 0.01f, depth * 4.0f + 0.01f);
	m_pPhysContainer->center.Set( transPos.x + ((width-1)*4.0f) , (height * 4.0f) + transPos.y, transPos.z + ((depth-1.0f)*4.0f));
	PhysicsManager::GetInstance().AddContainer( m_pPhysContainer );

	//m_pPhysContainer->SetDebug( true );
	//Vector3d boxPos;
	//m_ChunkPrim.AddBox(boxPos);
	
	#if CHUNK_OPTIMIZATIONS
	m_ChunkPrim.AddFaces();
	#endif
	m_ChunkPrim.Bake();
	m_ChunkPrim.m_Prim.vPos.Set( transPos );

	//printf("%d boxes created\n", nCount);
	
	//m_ChunkPrim.PrintFaces();
	counter.Stop();
	
	printf("Chunk generated in %f seconds.\n", counter.TimeInMilliseconds());
}