I am having problem with displaying all the triangles from my mesh. I am fighting with this for many hours without any success, so I would much appreciate your help.
Here is a simplified case of my mesh:
As you see, it is built from four triangles, I've enumerated vertices for convenience. Only X and Y are changing for each vertex, Z is always 0.
Now, I am trying to load this mesh into irrlicht:
Code: Select all
vertices.push_back(vector3df( 9, -4, 0));
vertices.push_back(vector3df( 9, 4, 0));
vertices.push_back(vector3df( 0, 0, 0));
vertices.push_back(vector3df(-9, 4, 0));
vertices.push_back(vector3df(-9, -4, 0));
indices.push_back(0); // 0->2->1 clockwise winding
indices.push_back(2);
indices.push_back(1);
indices.push_back(3); // 3->2->4 clockwise winding
indices.push_back(2);
indices.push_back(4);
indices.push_back(2); // 2->3->1 clockwise winding
indices.push_back(3);
indices.push_back(1);
indices.push_back(4); // 4->2->0 clockwise winding
indices.push_back(2);
indices.push_back(0);
vector3df normal;
for (u32 i = 0; i != indices.size(); i = i + 3) {
color.set(rand() % 255, rand() % 255, rand() % 255, 0);
u16 p1 = indices[i];
u16 p2 = indices[i + 1];
u16 p3 = indices[i + 2];
//Perhaps for smooth lighting I should calculate normal based on each neighbouring vertices.
normal = core::plane3df(vertices[p1], vertices[p2], vertices[p3]).Normal;
meshBuffer->Vertices.push_back(video::S3DVertex(vertices[p1], normal, color, core::vector2df()));
meshBuffer->Vertices.push_back(video::S3DVertex(vertices[p2], normal, color, core::vector2df()));
meshBuffer->Vertices.push_back(video::S3DVertex(vertices[p3], normal, color, core::vector2df()));
meshBuffer->Indices.push_back(p1);
meshBuffer->Indices.push_back(p2);
meshBuffer->Indices.push_back(p3);
}
meshBuffer->drop();
vertices.clear();
indices.clear();
Code: Select all
ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
camera->setPosition(vector3df(0, 0, -110));
Code: Select all
ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
camera->setPosition(vector3df(0, 0, 110));
I do not understand this behaviour, can somone explain to me what is going on? It seems that winding / normal is still not ok. And even more - some triangles are not drawn on any side of my camera.
Maybe I need to sort indices in some other way before putting them to buffer?
Thanks for your help.