Hi you all. I am posting again to "show off" the voxel terrain ive been messing around with. thought i would post my code for any possible optimizations. im loading a 64x64 bmp to generate the terrain so there are 4096 blocks being generated, Im currently getting 60fps with exclude based on the distance.
also i am having trouble deleting the vector of cubes it gives me a conversion error any help with that would be cool as well.
Chunk.h
#include "darkgdk.h"
#include "global.h"
#include "Block.h"
#ifndef CHUNK_H
#define CHUNK_H
static const int CHUNK_SIZE = 64;
int CHUNK_DATA[4096] = {0};
class Chunk
{
public:
Chunk(char* heightData)
{
//generate height info from height map
int img = FindFreeObject();
dbLoadImage("Height.bmp",img);
ChunkIterator=0;
DWORD color;
dbOpenToWrite(1,heightData);
while(ChunkIterator<(CHUNK_SIZE*CHUNK_SIZE))
{
dbPasteImage(img,0,0);
dbSync();
for(int x = 0; x < CHUNK_SIZE; x++)
{
for(int y = 0; y < CHUNK_SIZE; y++)
{
dbPasteImage(img,0,0);
color=dbPoint(x,y);
CHUNK_DATA[ChunkIterator] = dbRGBR(color);
dbWriteLong(1,CHUNK_DATA[ChunkIterator]);
ChunkIterator++;
}
}
dbCloseFile(1);
dbWait(10);
}
}
~Chunk()
{
delete[] CHUNK_DATA;
}
void GenerateChunk(int xp, int zp)
{
ChunkIterator = 0;
for(int x = 0; x < CHUNK_SIZE; x++)
{
for(int z = 0; z < CHUNK_SIZE; z++)
{
int y = CHUNK_DATA[ChunkIterator]/16;
int Texture;
if(y<5) Texture = 1;
if(y>=5) Texture = 3;
block.MakeBlock(xp+(x*10),(y)*10,zp+(z*10),Texture);
ChunkIterator++;
}
}
}
void Update()
{
block.Update();
}
protected:
int ChunkIterator;
Block_Manager block;
};
#endif
Block.h
#include "darkgdk.h"
#ifndef BLOCK_H
#define BLOCK_H
class Block
{
public:
Block(int x, int y, int z, int type)
{
ID = FindFreeObject();
if(type==1) dbInstanceObject(ID,BLOCKS);
if(type==2) dbInstanceObject(ID,BLOCKD);
if(type==3) dbInstanceObject(ID,BLOCKG);
dbPositionObject(ID,x,y,z);
}
void Update()
{
int Dist = Distance(ID);
if(Dist<100000)
{
dbExcludeObjectOff(ID);
}
if(Dist>=100000)
{
dbExcludeObjectOn(ID);
}
}
protected:
int ID;
};
class Block_Manager
{
std::vector<Block> blocks;
public:
Block_Manager() {}
~Block_Manager()
{
for(unsigned i = 0; i < blocks.size(); i++)
{
blocks.erase(blocks.begin()+i);
}
}
void MakeBlock(int x, int y, int z, int type)
{
Block block(x,y,z,type);
blocks.push_back(block);
}
void Update()
{
for(unsigned i = 0; i < blocks.size(); i++)
{
blocks[i].Update();
}
}
protected:
};
#endif
main.h
const int BLOCKS = 1;
const int BLOCKD = 2;
const int BLOCKG = 3;
const int SAND = 1;
const int DIRT = 2;
const int GRASS = 3;
const int SKY = 4;
#include "DarkGDK.h"
#include "Chunk.h"
void LoadMedia();
void DarkGDK ( void )
{
dbSyncOn();
dbSyncRate(60);
LoadMedia();
Chunk chunk("test.map");
for(int x = 0; x < 2; x++)
{
for(int y = 0; y < 2; y++)
{
chunk.GenerateChunk(x*(10*64),y*(10*64));
}
}
dbCLS();
dbPositionCamera(0,100,0);
while ( LoopGDK ( ) )
{
dbPrint(dbScreenFPS()*1.0);
dbControlCameraUsingArrowKeys(0,1,1);
chunk.Update();
dbSync();
}
return;
}
void LoadMedia()
{
dbSetCameraRange(1,1000000000);
dbLoadImage("media/Textures/sand.jpg",SAND);
dbLoadImage("media/Textures/dirt.png",DIRT);
dbLoadImage("media/Textures/grass.png",GRASS);
dbLoadImage("media/sky.jpg",SKY);
dbMakeObjectSphere(SKY,-10000000);
dbSetObjectFog(SKY,0);
dbFogOn();
dbFogDistance(300);
dbRotateObject(SKY,180,0,0);
dbTextureObject(SKY,SKY);
dbSetObjectLight(SKY,0);
dbMakeObjectCube(BLOCKS,10);
dbTextureObject(BLOCKS,SAND);
dbMakeObjectCube(BLOCKD,10);
dbTextureObject(BLOCKD,DIRT);
dbMakeObjectCube(BLOCKG,10);
dbTextureObject(BLOCKG,GRASS);
}
and heres a nice little picture XD