/******************************************************************************/
#include "stdafx.h"
/******************************************************************************/
Image ground;
MeshGroup mshg ; // here MeshGroup is used instead of a Mesh (single mesh)
/******************************************************************************/
void InitPre()
{
App.name("Terrain");
App.flag=APP_MS_EXCLUSIVE|APP_FULL_TOGGLE;
IOPath("../data");
Paks.add("engine.pak");
D.full(true).ambPower(0.3).hpRt(true).shdSoft(1);
ViewportFull.range=40;
}
/******************************************************************************/
void CreateMesh(Mesh &mesh)
{
// create random terrain heightmap
Image image(128,128,1,IMAGE_I8,IMAGE_SOFT);
REPD(y,image.y())
REPD(x,image.x())image.pixel(x,y,Random(256)); // software images don't need to be locked/unlocked
image.blur(5,false); // apply gaussian blur to the heightmap with 5 pixel range and no clamping
// create terrain Mesh
mesh.create(1).base(0).createPlane(128,128,VTX_TEX0); // create 2D plane with 128x128 vertexes
mesh.base(0).texScale (Vec2(16)); // scale texture coordinates
mesh.base(0).displaceZ(image,FILTER_BILINEAR); // displace vertexes Z position from heightmap
mesh.transform(Matrix().setPos(Vec(-0.5,-0.6,-0.5)).rotateX(PI_2).scale(Vec(50,8,50))); // transform by matrix
mesh.setNormals(); // recalculate normals
mesh.setMaterial(Materials("mtrl/grass/0.mtrl")); // set material
mesh.setBox(); // set bounding box
}
/******************************************************************************/
Bool Init()
{
Cam.at.y+=2; // move camera up
Sky.atmospheric();
Sun.image=Images("gfx/sky/sun.gfx");
// create mesh
Mesh mesh;
CreateMesh(mesh);
// create terrain MeshGroup from standard Mesh
mshg.create(mesh,VecI(5,1,5)); // create MeshGroup with 5x1x5 (5*1*5=25) splits (Mesh is splitted to 25 parts along axises)
mshg.setRender();
return true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
Bool Update()
{
if(Kb.bp(KB_ESC))return false;
CamHandle(0.01f,10,CAMH_ZOOM|(Ms.b(1) ? CAMH_MOVE : CAMH_ROT));
return true;
}
/******************************************************************************/
void Render()
{
switch(Renderer())
{
case RM_PREPARE:
mshg.draw(MatrixIdentity);
break;
}
}
void Draw()
{
Renderer(Render);
if(Kb.b(KB_SPACE))
{
SetMatrix();
REPA(mshg)mshg.mesh(i).box.draw(); // draw mesh bounding boxes
}
}
/******************************************************************************/