Does the irrlicht offer a method to create a cylinder??
Posted: Sun Dec 21, 2008 4:26 pm
I am a novice of irrlicht!!
Official forum of the Irrlicht Engine
https://irrlicht.sourceforge.io/forum/
even the greatest noob can ask the most advanced stuff, what he can make out of it is another think...hybrid wrote:So why do you think you can ask advanced stuff then
Code: Select all
scene::SMeshBuffer*
createCylinderMeshBuffer(u32 faces, f32 radius, f32 height, video::SColor color)
{
if (faces < 3)
faces = 3;
else if (65534 < faces)
faces = 65534;
scene::SMeshBuffer* mb =
new scene::SMeshBuffer;
if (!mb)
return 0;
video::S3DVertex vertex;
vertex.Color = color;
u32 n, m = 0;
for (n = 0; n <= faces; ++n)
{
const f32 x = (core::PI * 2.f) * n / faces;
const f32 dx = sinf(x);
const f32 dz = cosf(x);
vertex.Pos.set (dx * radius, height, dz * radius);
vertex.Normal.set (dx, 0, dz);
vertex.TCoords.set(1.f * n / faces, 0.f);
mb->Vertices.push_back(vertex);
vertex.Pos.Y = 0;
vertex.TCoords.Y = 1.f;
mb->Vertices.push_back(vertex);
}
for (u32 n = 0; n < 2 * faces; ++n)
{
mb->Indices.push_back(n);
if (n & 1) {
mb->Indices.push_back(n+2);
mb->Indices.push_back(n+1);
}
else
{
mb->Indices.push_back(n+1);
mb->Indices.push_back(n+2);
}
}
mb->BoundingBox.reset (-radius, 0, -radius);
mb->BoundingBox.addInternalPoint(radius, height, radius);
return mb;
}
scene::SMesh*
createCylinderMesh(u32 faces, f32 radius, f32 height, video::SColor color)
{
scene::SMeshBuffer* mb = createCylinderMeshBuffer(faces, radius, height, color);
if (!mb)
return 0;
scene::SMesh* mesh = new scene::SMesh;
mesh->addMeshBuffer(mb);
mb->drop();
mesh->recalculateBoundingBox();
return mesh;
}
Code: Select all
scene::IMesh* cylinder = createCylinderMesh(6, 10, 10, video::SColor(255, 255, 0, 0));
// make sure cylinder got created
scene::IMeshSceneNode* node = smgr->addMeshSceneNode(mesh);
lizhigang34 wrote:Thanks for your reply!
I just feel accidental..
There is a method addSphereSceneNode() in class ISceneManager for create a sphere , another method addCubeSceneNode() for create a cube..
but,there is no similar method addCylinderSceneNode()..
My English is poor!! and I am a novice of 3D program.
I am a Chinese..
谢谢大家的回复,
我只是感到有点意外而已.
在类ISceneManager有函数addSphereSceneNode()能创建一个球,函数addCubeSceneNode() 创建一个立方体..却没有类似的函数addCylinderSceneNode().
我的英语很差,而且我刚学3D程序设计.
Code: Select all
ISceneNode * cylinder= m_pSceneManager->addMeshSceneNode(
m_pSceneManager->addArrowMesh("arrow",
SColor(255, 255, 0, 0), SColor(255, 0, 255, 0),
4,8,1.f,
0.99f, // 1. arrow's cylinder length
0.01f, // 2. arrow cone's roof
0.5f
), robotRoot);
cylinder->setMaterialFlag(video::EMF_LIGHTING, false);
cylinder->setScale(vector3df(50, 1000, 50)); // 3. scale it
Code: Select all
IMesh* Mesh = SceneManager->getGeometryCreator()->createCylinderMesh();
SceneManager->addMeshSceneNode(Mesh);
I used this code compiled well with our any error but not showing any thing on the irrlicht device window... !vitek wrote:It isn't difficult to create a cylinder. You just have to figure out a little bit of math and have a good understanding of how a mesh buffer describes a series of triangles (i.e., you need to know how a vertex and index buffer work).
I wrote this code some time back, but I believe it works correctly...
You would use it like...Code: Select all
scene::SMeshBuffer* createCylinderMeshBuffer(u32 faces, f32 radius, f32 height, video::SColor color) { if (faces < 3) faces = 3; else if (65534 < faces) faces = 65534; scene::SMeshBuffer* mb = new scene::SMeshBuffer; if (!mb) return 0; video::S3DVertex vertex; vertex.Color = color; u32 n, m = 0; for (n = 0; n <= faces; ++n) { const f32 x = (core::PI * 2.f) * n / faces; const f32 dx = sinf(x); const f32 dz = cosf(x); vertex.Pos.set (dx * radius, height, dz * radius); vertex.Normal.set (dx, 0, dz); vertex.TCoords.set(1.f * n / faces, 0.f); mb->Vertices.push_back(vertex); vertex.Pos.Y = 0; vertex.TCoords.Y = 1.f; mb->Vertices.push_back(vertex); } for (u32 n = 0; n < 2 * faces; ++n) { mb->Indices.push_back(n); if (n & 1) { mb->Indices.push_back(n+2); mb->Indices.push_back(n+1); } else { mb->Indices.push_back(n+1); mb->Indices.push_back(n+2); } } mb->BoundingBox.reset (-radius, 0, -radius); mb->BoundingBox.addInternalPoint(radius, height, radius); return mb; } scene::SMesh* createCylinderMesh(u32 faces, f32 radius, f32 height, video::SColor color) { scene::SMeshBuffer* mb = createCylinderMeshBuffer(faces, radius, height, color); if (!mb) return 0; scene::SMesh* mesh = new scene::SMesh; mesh->addMeshBuffer(mb); mb->drop(); mesh->recalculateBoundingBox(); return mesh; }
TravisCode: Select all
scene::IMesh* cylinder = createCylinderMesh(6, 10, 10, video::SColor(255, 255, 0, 0)); // make sure cylinder got created scene::IMeshSceneNode* node = smgr->addMeshSceneNode(mesh);
Code: Select all
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
Code: Select all
mesh = smgr->getGeometryCreator()->createCylinderMesh(
.1f, //radius
1., //length
50, //tesselation
SColor(), //color
false, //closeTop
0.f //oblique
);
node = smgr->addMeshSceneNode(
mesh,
0, //ISceneNode * parent
-1, //s32 id
vector3df(0, -HEIGHT, 0), //const core::vector3df & position
vector3df(0, 0, 0), //const core::vector3df & rotation
vector3df(1.0f, 2.*HEIGHT, 1.0f) //const core::vector3df & scale
);
node->getMaterial(0).AmbientColor.set(0,0,0,255);
node->getMaterial(0).DiffuseColor.set(0,0,0,255);
node->getMaterial(0).Lighting = true;
Try disabling back culling (and disabling material lighting if there is not any light).cirosantilli wrote:how to turn the cylinder inside out, so that it can be seen and do collision detection if the camera is inside it?