Advertisement

Remove Triangles from ogre mesh

Started by January 16, 2018 06:37 PM
5 comments, last by CrazyCdn 7 years ago

I'm having a ray to intersection and getting an intersection point, then I compare a condition. I would like to remove that particular triangle from the mesh.

In the RayCasting function, when I get hit, I save three variables u1,u2,u3 which are the indices of the triangle that has a hit.

Ogre::Ray ray;
                ray.setOrigin(Ogre::Vector3( ray_pos.x, ray_pos.y, ray_pos.z));
                ray.setDirection(Ogre::Vector3(ray_dir.x, ray_dir.y, ray_dir.z));
                Ogre::Vector3 result;

                RaycastFromPoint(ray.getOrigin(), ray.getDirection(), result, u1, u2, u3);
                float pointZ = out.pointlist[(3*i)+2];

                if(result.z < pointZ)
                {
                    std::cout << "Remove edge "<< u1 << " "<< u2 << " "<< u3 << std::endl;
                    Utility::DebugPrimitives::drawSphere( result, 0.3f , "RayMesh"+std::to_string(counter), "SimpleColors/SolidGreen" );

                    cntEdges++;
                    indices = static_cast<uint16_t *>(indexBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD));

                    for (int i = 0; i<out.numberofedges; i++)
                    {
                        if(indices[i] == u1 || indices[i] == u2 || indices[i] == u3)
                        {
                            continue;
                        }

                        out.edgelist[i] = indices[i];


                    }
                    indexBuffer->unlock();


                    indices = static_cast<uint16_t *>(indexBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD));

                    for (int i = 0; i<numEdges - cntEdges; i++)
                    {
                        indices[i] = out.edgelist[i];
                    }

                    indexBuffer->unlock();

                }
Game Programming is the process of converting dead pictures to live ones .

You just need to erase those indices (u1, u2, and u3) from your indices list and resubmit/update it on the GPU.  You need to leave the vertices as they're still used by other indices no doubt, though you can verify this if you want to save that tiny bit of memory.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Advertisement

@Mike2343 Can you show me c++ code to remove those vertices from indices array ?

Game Programming is the process of converting dead pictures to live ones .

I don't know Ogre or your code.  You don't show enough for me to deduce what you're doing.  Is indices a std::vector of unsigned int's?  What is the type of the u1, u2 and u3 variables?

If indices is a vector, look up std::vector::erase.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

@Mike2343 Indices is an array of unsigned int, unsigned int * indices.  u1,u2,u2 is unsigned int referring to the triangle indices u1,u2,u3

Game Programming is the process of converting dead pictures to live ones .

I'm not sure why you're using a dynamically allocated C array when you're using C++ which has a perfectly fine working and easier to work with array class (std::vector).  I would move your code over to this and it will become a whole lot easier.  Otherwise here is some code (the accepted answer has the green check mark next to it) on one way to remove your elements: https://stackoverflow.com/questions/9246165/how-to-remove-elements-from-dynamically-allocated-array?noredirect=1&lq=1

Though, again, I would just translate your existing code to use std::vector<uint32_t> indices;  If something needs access to the raw array pass in &indices[0].  And erase elements with indices.erase( indices.begin() + u1 );, etc.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

This topic is closed to new replies.

Advertisement