I don't know what problem is exist in my source code.
Code: Select all
// IAdvencedTerrainSceneNode.h
#ifndef __I_ADVENCEDTERRAIN_SCENE_NODE_H__
#define __I_ADVENCEDTERRAIN_SCENE_NODE_H__
#include <irrlicht.h>
#include "Mapdata.h"
#include "SMaterial.h"
#define TILE_SIZE 1.0f
namespace irr
{
using namespace video;
namespace io
{
class IReadFile;
} // end namespace io
namespace scene
{
class IMesh;
class IAdvencedTerrainSceneNode : public IMeshSceneNode
{
public:
//! Constructor
IAdvencedTerrainSceneNode(
ISceneNode* parent,
ISceneManager* mgr,
s32 id,
CMapdata * pMap);
~IAdvencedTerrainSceneNode();
IMesh * CreateMesh();
virtual void render();
virtual const core::aabbox3d<f32>& getBoundingBox() const
{
return m_pMesh->getBoundingBox();
}
virtual u32 getMaterialCount() const
{
return 0;
}
virtual video::SMaterial& getMaterial(u32 i)
{
return m_Material;
}
// IMeshSceneNode
virtual void setMesh(IMesh* mesh) {};
virtual IMesh* getMesh(void) { return m_pMesh; }
virtual void setReadOnlyMaterials(bool readonly) {}
virtual bool isReadOnlyMaterials() const { return false; }
protected:
IMesh * m_pMesh;
CMapdata * m_pMap;
IDynamicMeshBuffer * m_RenderBuffer;
SMaterial m_Material;
};
} // end namespace scene
} // end namespace irr
#endif // __I_TERRAIN_SCENE_NODE_H__
Code: Select all
// IAdvencedTerrainSceneNode.cpp
#include "IAdvencedTerrainSceneNode.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
IAdvencedTerrainSceneNode::IAdvencedTerrainSceneNode(
ISceneNode* parent,
ISceneManager* mgr,
s32 id,
CMapdata * pMap)
: IMeshSceneNode(parent,mgr,id)
{
m_pMesh = NULL;
//m_pMaterial = NULL;
m_pMap = pMap;
if (m_pMesh)
m_pMesh->drop();
m_pMesh = CreateMesh();
m_Material.Wireframe = false;
m_Material.Lighting = false;
//if (m_pMaterial)
// delete m_pMaterial;
//m_pMaterial = CreateMaterial();
}
IAdvencedTerrainSceneNode::~IAdvencedTerrainSceneNode()
{
if (m_pMesh)
m_pMesh->drop();
//if (m_pMaterial)
// delete m_pMaterial;
}
IMesh* IAdvencedTerrainSceneNode::CreateMesh()
{
int i,j, iWidth=m_pMap->m_iMapwidth, iLength=m_pMap->m_iMaplength;
SMeshBuffer* pBuffer = new SMeshBuffer();
S3DVertex vertex;
vertex.Color = SColor(255,255,255,255);
for (i=0; i<iWidth; i++)
{
for(j=0; j<iLength; j++)
{
vertex.Pos.X = i*TILE_SIZE;
vertex.Pos.Z = j*TILE_SIZE;
vertex.Pos.Y = 0.0f;
vertex.Normal = vertex.Pos;
vertex.TCoords.X = 0.0f;
vertex.TCoords.Y = 0.0f;
pBuffer->Vertices.push_back(vertex);
vertex.Pos.X = (i+1)*TILE_SIZE;
vertex.Pos.Z = j*TILE_SIZE;
vertex.Pos.Y = 0.0f;
vertex.Normal = vertex.Pos;
vertex.TCoords.X = 1.0f;
vertex.TCoords.Y = 0.0f;
pBuffer->Vertices.push_back(vertex);
vertex.Pos.X = i*TILE_SIZE;
vertex.Pos.Z = (j+1)*TILE_SIZE;
vertex.Pos.Y = 0.0f;
vertex.Normal = vertex.Pos;
vertex.TCoords.X = 0.0f;
vertex.TCoords.Y = 1.0f;
pBuffer->Vertices.push_back(vertex);
vertex.Pos.X = (i+1)*TILE_SIZE;
vertex.Pos.Z = (j+1)*TILE_SIZE;
vertex.Pos.Y = 0.0f;
vertex.Normal = vertex.Pos;
vertex.TCoords.X = 1.0f;
vertex.TCoords.Y = 1.0f;
pBuffer->Vertices.push_back(vertex);
}
}
int _WL = iWidth * iLength;
for (i=0; i<_WL; i++)
{
pBuffer->Indices.push_back(4*i + 1);
pBuffer->Indices.push_back(4*i + 2);
pBuffer->Indices.push_back(4*i);
pBuffer->Indices.push_back(4*i + 1);
pBuffer->Indices.push_back(4*i + 3);
pBuffer->Indices.push_back(4*i + 2);
/*pBuffer->Indices.push_back(4*i);
pBuffer->Indices.push_back(4*i + 2);
pBuffer->Indices.push_back(4*i + 1);
pBuffer->Indices.push_back(4*i + 2);
pBuffer->Indices.push_back(4*i + 3);
pBuffer->Indices.push_back(4*i + 1);*/
}
pBuffer->recalculateBoundingBox();
SMesh* mesh = new irr::scene::SMesh();
mesh->addMeshBuffer(pBuffer);
mesh->recalculateBoundingBox();
pBuffer->drop();
return mesh;
}
void IAdvencedTerrainSceneNode::render()
{
IVideoDriver *driver = SceneManager->getVideoDriver();
driver->setMaterial(getMaterial(0));
driver->setTransform(irr::video::ETS_WORLD,AbsoluteTransformation);
driver->drawMeshBuffer(m_pMesh->getMeshBuffer(0));
if(DebugDataVisible)
{
driver->draw3DBox(m_pMesh->getBoundingBox(), irr::video::SColor(255,255,0,0));
}
}