[2.1] creating manual objects
Posted: Thu Nov 22, 2018 2:22 am
I am trying to create manual objects in 2.1 on Linux. My test case is two cubes, one inside the other. Each cube a separate mesh. If I only render one cube, it renders correctly, no issues. The problem is when trying to render both cubes, the first cube (mesh) renders but the second cube isn't rendering correctly, rather, it's drawing lines within the first cube as if it's part of the first cube, which it is not. The cubes are created/exported via another app which I'm certain are correct. Also, some of the points, when trying to render both cubes, extend to 0,0,0 which is not a coordinate of any of my cubes vertices.
I was hoping someone could look over what I've done and see if anything sticks out, or have thoughts on how else I might troubleshoot this problem. I've printed the values of the arrays being passed in to createVertexBuffer() and createIndexBuffer(), and they appear perfectly ok.
I was hoping someone could look over what I've done and see if anything sticks out, or have thoughts on how else I might troubleshoot this problem. I've printed the values of the arrays being passed in to createVertexBuffer() and createIndexBuffer(), and they appear perfectly ok.
Code: Select all
Point* AGS::fill_vertices(const MInfo &mi)
{
PointGroup pg = points[mi.get_group_id()];
Point *vertices = reinterpret_cast<Point*>
(OGRE_MALLOC_SIMD(sizeof(Point) * mi.get_total_points(),
Ogre::MEMCATEGORY_GEOMETRY));
for (auto& p : pg)
vertices[i++] = p.second;
return vertices;
}
Ogre::uint16* AGS::fill_indices(const MInfo &mi)
{
ElementGroup egrp = mesh.at(mi.get_group_id());
Ogre::uint16 *indices =
reinterpret_cast<Ogre::uint16*>(OGRE_MALLOC_SIMD(
sizeof(Ogre::uint16) * (mm.get_total_elements() * 3),
Ogre::MEMCATEGORY_GEOMETRY));
auto i { 0 };
for (auto& epnts : egrp) {
for (auto& pnt : epnts->get_points())
indices[i] = pnt.first;
}
return indices;
}
Ogre::IndexBufferPacked* AGS::createIndexBuffer(
const MInfo &mi)
{
Ogre::IndexBufferPacked *index_buffer = nullptr;
Ogre::uint16 *tri_indices = fill_indices(mi);
Ogre::RenderSystem *renderSystem = root->getRenderSystem();
Ogre::VaoManager *vaoManager = renderSystem->getVaoManager();
try {
index_buffer = vaoManager->createIndexBuffer(
Ogre::IndexBufferPacked::IT_16BIT, mi.get_total_elements() * 3,
Ogre::BT_IMMUTABLE, tri_indices, true);
}
catch (Ogre::Exception &e) {
OGRE_FREE_SIMD(index_buffer, Ogre::MEMCATEGORY_GEOMETRY);
index_buffer = nullptr;
throw e;
}
return index_buffer;
}
Ogre::MeshPtr AGS::createStaticMesh(const MInfo &mi)
{
Ogre::RenderSystem *renderSystem = root->getRenderSystem();
Ogre::VaoManager *vaoManager = renderSystem->getVaoManager();
/* Create the mesh. */
Ogre::MeshPtr mesh =
Ogre::MeshManager::getSingleton().createManual(mi.get_label(),
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
/* Create a submesh. */
Ogre::SubMesh *subMesh = mesh->createSubMesh();
/* Vertex declaration. */
Ogre::VertexElement2Vec vertexElements;
vertexElements.push_back(Ogre::VertexElement2(Ogre::VET_FLOAT3,
Ogre::VES_POSITION));
vertexElements.push_back(Ogre::VertexElement2(Ogre::VET_FLOAT3,
Ogre::VES_NORMAL));
/* Fill the data. */
Point *tri_vertices = fill_vertices(mm);
Ogre::VertexBufferPacked *vertexBuffer = nullptr;
try {
/* Create the actual vertex buffer. */
vertexBuffer = vaoManager->createVertexBuffer(vertexElements,
mi.get_total_points(), Ogre::BT_IMMUTABLE, tri_vertices, true);
}
catch (Ogre::Exception &e) {
OGRE_FREE_SIMD(vertexBuffer, Ogre::MEMCATEGORY_GEOMETRY);
vertexBuffer = nullptr;
throw e;
}
Ogre::VertexBufferPackedVec vertexBuffers;
vertexBuffers.push_back(vertexBuffer);
Ogre::IndexBufferPacked *indexBuffer = createIndexBuffer(mm);
Ogre::VertexArrayObject *vao = vaoManager->createVertexArrayObject(
vertexBuffers, indexBuffer, Ogre::OT_TRIANGLE_LIST);
subMesh->mVao[Ogre::VpNormal].push_back(vao);
subMesh->mVao[Ogre::VpShadow].push_back(vao);
mesh->_setBounds(Ogre::Aabb(Ogre::Vector3::ZERO,
Ogre::Vector3::UNIT_SCALE), false);
mesh->_setBoundingSphereRadius(1.732f);
return mesh;
}
// Here is the iteration of the two cubes from my createScene() ...
for (auto& mi : mesh_info) {
Ogre::MeshPtr mp = createStaticMesh(mi);
Ogre::Item *item =
sceneManager->createItem(mp, Ogre::SCENE_DYNAMIC);
item->setDatablock(unlitBlock);
Ogre::SceneNode *scene_node =
sceneManager->getRootSceneNode(Ogre::SCENE_DYNAMIC)->
createChildSceneNode(Ogre::SCENE_DYNAMIC);
scene_node->attachObject(item);
scene_node->setPosition(Ogre::Vector3(0.0f, 0.0f, 0.0f));
scene_node->setScale(Ogre::Vector3(3.0f, 3.0f, 3.0f));
}