void WorldChunk::createChunk()
{
vert_count = 0;
index_count = 0;
index_number = 0;
CUSTOMVERTEX* Vertices;
int* Indices;
cdevice->CreateVertexBuffer( 24 * 16 * 16 * 16 * sizeof( CUSTOMVERTEX ), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &VB, NULL );
cdevice->CreateIndexBuffer(36 * 16 * 16 * 16 *sizeof(int),D3DUSAGE_WRITEONLY,D3DFMT_INDEX32,D3DPOOL_DEFAULT,&IB,NULL);
VB->Lock( 0, 0, ( void** )&Vertices, D3DLOCK_DISCARD);
IB->Lock(0,0,(void **)&Indices, D3DLOCK_DISCARD);
for(int x = 0; x < 16; x++)
{
for(int y = 0; y < 16; y++)
{
for(int z = 0; z < 16; z++)
{
//Ist da kein Block zeichnen wir den nicht...
if(chunk[x][y][z] == 0)
continue;
block_type = chunk[x][y][z];
//Befinden wir uns am linken Rand? Dann haben wir keinen linken Nachbarn ansonsten holen wir den aus dem Chunk-Array
//Dasselbe gilt für alle anderen Richtungen (Hab keine Lust, das für jede Abfrage zu wiederholen ;-) )
if(x > 0)
{
testblock = chunk[x-1][y][z];
}else{
testblock = 0;
}
if(testblock == 0)
{
Vertices[vert_count].position = D3DXVECTOR3(x,y,z+1);
Vertices[vert_count].tu = 0.0f+((float)(block_type-1))*0.25f;
Vertices[vert_count].tv = 1.0f;
ver.push_back(Vertices[vert_count]);
Indices[index_count] = index_number;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+1].position = D3DXVECTOR3(x,y+1,z+1);
Vertices[vert_count+1].tu = 0.0f+((float)(block_type-1))*0.25f;
Vertices[vert_count+1].tv = 0.0f;
ver.push_back(Vertices[vert_count+1]);
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+2].position = D3DXVECTOR3(x,y,z);
Vertices[vert_count+2].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+2].tv = 1.0f;
ver.push_back(Vertices[vert_count+2]);
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+3].position = D3DXVECTOR3(x,y+1,z);
Vertices[vert_count+3].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+3].tv = 0.0f;
ver.push_back(Vertices[vert_count+3]);
Indices[index_count] = index_number+3;
ind.push_back(Indices[index_count]);
index_count++;
index_number += 4;
vert_count += 4; //Erhöhe den Zähler um 6 , weil wir 6 Vertices gezeichnet haben...
}
if(x < 16-1)
{
testblock = chunk[x+1][y][z];
}else{
testblock = 0;
}
if(testblock == 0)
{
Vertices[vert_count].position = D3DXVECTOR3(x+1,y,z+1);
Vertices[vert_count].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count].tv = 1.0f;
ver.push_back(Vertices[vert_count]);
Indices[index_count] = index_number;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+1].position = D3DXVECTOR3(x+1,y+1,z+1);
Vertices[vert_count+1].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count+1].tv = 0.0f;
ver.push_back(Vertices[vert_count+1]);
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+2].position = D3DXVECTOR3(x+1,y,z);
Vertices[vert_count+2].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+2].tv = 1.0f;
ver.push_back(Vertices[vert_count+2]);
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+3].position = D3DXVECTOR3(x+1,y+1,z);
Vertices[vert_count+3].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+3].tv = 0.0f;
ver.push_back(Vertices[vert_count+3]);
Indices[index_count] = index_number+3;
ind.push_back(Indices[index_count]);
index_count++;
index_number += 4;
vert_count += 4; //Erhöhe den Zähler um 6 , weil wir 6 Vertices gezeichnet haben...
}
if(y > 0)
{
testblock = chunk[x][y-1][z];
}else{
testblock = 0;
}
if(testblock == 0)
{
Vertices[vert_count].position = D3DXVECTOR3(x,y,z);
Vertices[vert_count].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count].tv = 1.0f;
ver.push_back(Vertices[vert_count]);
Indices[index_count] = index_number;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+1].position = D3DXVECTOR3(x,y,z+1);
Vertices[vert_count+1].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count+1].tv = 0.0f;
ver.push_back(Vertices[vert_count+1]);
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+2].position = D3DXVECTOR3(x+1,y,z);
Vertices[vert_count+2].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+2].tv = 1.0f;
ver.push_back(Vertices[vert_count+2]);
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+3].position = D3DXVECTOR3(x+1,y,z+1);
Vertices[vert_count+3].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+3].tv = 0.0f;
ver.push_back(Vertices[vert_count+3]);
Indices[index_count] = index_number+3;
ind.push_back(Indices[index_count]);
index_count++;
index_number += 4;
vert_count += 4; //Erhöhe den Zähler um 6 , weil wir 6 Vertices gezeichnet haben...
}
if(y < 16-1)
{
testblock = chunk[x][y+1][z];
}else{
testblock = 0;
}
if(testblock == 0)
{
Vertices[vert_count].position = D3DXVECTOR3(x,y+1,z);
Vertices[vert_count].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count].tv = 1.0f;
ver.push_back(Vertices[vert_count]);
Indices[index_count] = index_number;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+1].position = D3DXVECTOR3(x,y+1,z+1);
Vertices[vert_count+1].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count+1].tv = 0.0f;
ver.push_back(Vertices[vert_count+1]);
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+2].position = D3DXVECTOR3(x+1,y+1,z);
Vertices[vert_count+2].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+2].tv = 1.0f;
ver.push_back(Vertices[vert_count+2]);
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+3].position = D3DXVECTOR3(x+1,y+1,z+1);
Vertices[vert_count+3].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+3].tv = 0.0f;
ver.push_back(Vertices[vert_count+3]);
Indices[index_count] = index_number+3;
ind.push_back(Indices[index_count]);
index_count++;
index_number += 4;
vert_count += 4; //Erhöhe den Zähler um 6 , weil wir 6 Vertices gezeichnet haben...
}
if(z > 0)
{
testblock = chunk[x][y][z-1];
}else{
testblock = 0;
}
if(testblock == 0)
{
Vertices[vert_count].position = D3DXVECTOR3(x,y,z);
Vertices[vert_count].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count].tv = 1.0f;
ver.push_back(Vertices[vert_count]);
Indices[index_count] = index_number;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+1].position = D3DXVECTOR3(x,y+1,z);
Vertices[vert_count+1].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count+1].tv = 0.0f;
ver.push_back(Vertices[vert_count+1]);
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+2].position = D3DXVECTOR3(x+1,y,z);
Vertices[vert_count+2].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+2].tv = 1.0f;
ver.push_back(Vertices[vert_count+2]);
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+3].position = D3DXVECTOR3(x+1,y+1,z);
Vertices[vert_count+3].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+3].tv = 0.0f;
ver.push_back(Vertices[vert_count+3]);
Indices[index_count] = index_number+3;
ind.push_back(Indices[index_count]);
index_count++;
index_number += 4;
vert_count += 4;//Erhöhe den Zähler um 6 , weil wir 6 Vertices gezeichnet haben...
}
if(z < 16-1)
{
testblock = chunk[x][y][z+1];
}else{
testblock = 0;
}
if(testblock == 0)
{
Vertices[vert_count].position = D3DXVECTOR3(x,y,z+1);
Vertices[vert_count].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count].tv = 1.0f;
ver.push_back(Vertices[vert_count]);
Indices[index_count] = index_number;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+1].position = D3DXVECTOR3(x,y+1,z+1);
Vertices[vert_count+1].tu = 0.0f+(block_type-1)*0.25f;
Vertices[vert_count+1].tv = 0.0f;
ver.push_back(Vertices[vert_count+1]);
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+2].position = D3DXVECTOR3(x+1,y,z+1);
Vertices[vert_count+2].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+2].tv = 1.0f;
ver.push_back(Vertices[vert_count+2]);
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+2;
ind.push_back(Indices[index_count]);
index_count++;
Indices[index_count] = index_number+1;
ind.push_back(Indices[index_count]);
index_count++;
Vertices[vert_count+3].position = D3DXVECTOR3(x+1,y+1,z+1);
Vertices[vert_count+3].tu = 0.25f+(block_type-1)*0.25f;
Vertices[vert_count+3].tv = 0.0f;
ver.push_back(Vertices[vert_count+3]);
Indices[index_count] = index_number+3;
ind.push_back(Indices[index_count]);
index_count++;
index_number += 4;
vert_count += 4; //Erhöhe den Zähler um 6 , weil wir 6 Vertices gezeichnet haben...
}
}
}
}
//****************************************************************************************************************
IB->Unlock();
VB->Unlock();
If it's created i use the vectors to "quick fill" the buffers in the next frame (the vertices don't change)
void WorldChunk::QuickFill()
{
void* vv = NULL;
void* ii = NULL;
cdevice->CreateVertexBuffer( ver.size()*sizeof(CUSTOMVERTEX),D3DUSAGE_DYNAMIC, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &VB, NULL );
cdevice->CreateIndexBuffer(ind.size()*sizeof(int),D3DUSAGE_DYNAMIC,D3DFMT_INDEX32,D3DPOOL_DEFAULT,&IB,NULL);
VB->Lock(0,(ver.size()+10)*sizeof(CUSTOMVERTEX),(void**)&vv,D3DLOCK_DISCARD);
memcpy(vv,&ver[0],ver.size()*sizeof(CUSTOMVERTEX));
VB->Unlock();
IB->Lock(0,(ind.size()+10)*sizeof(int),(void**)&ii,D3DLOCK_DISCARD);
memcpy(ii,&(ind[0]),ind.size()*sizeof(int));
IB->Unlock();
}