Code: Select all
#include <irrlicht.h>
#pragma comment(lib,"irrlicht.lib")
using namespace irr;
IrrlichtDevice* device;
video::IVideoDriver* driver;
scene::ISceneManager* smgr;
class CCustomSceneNode:public scene::ISceneNode
{
core::aabbox3d<f32> Box;
video::SMaterial Material;
video::S3DVertex vtx[11];
u16 indices1[30];
scene::SMeshBuffer* buffer;
scene::SMesh* mesh;
scene::SAnimatedMesh* animatedMesh;
public:
CCustomSceneNode(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id):scene::ISceneNode(parent, mgr, id)
{
Material.Wireframe = true;
Material.Lighting = false;
buffer = new scene::SMeshBuffer();
vtx->Color.set(255,255,0,0);
float step = core::PI * 2 / 10;
vtx[0].Pos.X = 0;
vtx[0].Pos.Y = -50;
vtx[0].Pos.Z = 0;
buffer->Vertices.push_back(vtx[0]);
animatedMesh = new scene::SAnimatedMesh();
float r = 250;
int i;
for (i = 1 ; i <= 10; i ++)
{
vtx[i].Pos.X = r * cosf(i * step);
vtx[i].Pos.Y = vtx[0].Pos.Y;
vtx[i].Pos.Z = r * sinf(i * step);
buffer->Vertices.push_back(vtx[i]);
}
int j = 0;
for (i = 0,j = 0; i < 27; i= i + 3,j ++)
{
indices1[i] = j+2;
indices1[i+1] = j + 1;
indices1[i+2] = 0;
buffer->Indices.push_back(j+2);
buffer->Indices.push_back(j + 1);
buffer->Indices.push_back(0);
}
indices1[i] = 1;
indices1[i+1] = j +1;
indices1[i+2] = 0;
buffer->Indices.push_back(1);
buffer->Indices.push_back(j + 1);
buffer->Indices.push_back(0);
buffer->recalculateBoundingBox();
mesh = new scene::SMesh();
mesh->addMeshBuffer(buffer);
mesh->recalculateBoundingBox();
buffer->drop();
}
virtual void OnRegisterSceneNode()
{
if (IsVisible)
SceneManager->registerNodeForRendering(this);
ISceneNode::OnRegisterSceneNode();
}
virtual void render()
{
driver->setMaterial(Material);
driver->setTransform(video::ETS_WORLD,AbsoluteTransformation);
driver->drawIndexedTriangleList(vtx,30,indices1,10);
//section to add a round mesh and add water effect to it
if (!animatedMesh)
{
mesh->drop();
return ;
}
animatedMesh->addMesh(mesh);
mesh->drop();
if (!mesh)
return;
smgr->getMeshCache()->addMesh("round",animatedMesh);
animatedMesh->drop();
scene::ISceneNode* node = smgr->addWaterSurfaceSceneNode(animatedMesh, 3.0f, 300.0f, 30.0f);
node->setPosition(core::vector3df(0,-10,0));
node->setMaterialTexture(0, driver->getTexture("stones.jpg"));
node->setMaterialTexture(1, driver->getTexture("water.jpg"));
node->setMaterialType(video::EMT_REFLECTION_2_LAYER);
node = smgr->addLightSceneNode(0, core::vector3df(0,80,0),
video::SColorf(1.0f, 0.6f, 0.7f, 1.0f), 800.0f);
}
virtual const core::aabbox3d<f32>& getBoundingBox() const
{
return Box;
}
virtual video::SMaterial& getMaterial(u32 i)
{
return Material;
}
};
int main()
{
device = createDevice(video::EDT_OPENGL,core::dimension2d<s32>(640,480),16,false);
device->setWindowCaption(L"custom scene node - irrlicht engine demo");
driver = device->getVideoDriver();
smgr = device->getSceneManager();
smgr->addCameraSceneNodeFPS();
CCustomSceneNode* myNode = new CCustomSceneNode(smgr->getRootSceneNode(),smgr,666);
myNode->drop();
while(device->run())
{
driver->beginScene(true, true, video::SColor(0,100,100,100));
smgr->drawAll();
driver->endScene();
}
device->drop();
return 0;
}
i create a round mesh,and add water effect on it ,but when the program is implementing,the memory grows continously,why?
the round mesh has ten points.