Looking for Information about low level mesh access

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
uzik
Posts: 24
Joined: Wed Sep 22, 2004 4:56 pm

Looking for Information about low level mesh access

Post by uzik »

Good mroning all,

I've just started learning IrrLicht and I'm looking to get low level information
about a mesh I've loaded. Specifically I'm trying to rotate a mesh until the
face nearest the viewer is perpendicular to the camera. The best way
seemed to be to iterate through the faces and examine the distance
from the camera and the normal vectors of each (my mesh is simple
enough this will always work). I can then calculate the rotation needed
to get the normal vector perpendicular to the camera vector.

Does anyone have an example code or pointers on where to start
looking?

Thanks!
Guest

Post by Guest »

Take a look at http://irrlicht.sourceforge.net/tut-ode.html
"Converting Irrlicht Meshes to ODE TriMesh"
there you'll find code that traverses an IMesh structure. To get the faces you must sequencially take 3 indices and get the corresponding vertecis, these vertecis build the face.
uzik
Posts: 24
Joined: Wed Sep 22, 2004 4:56 pm

Post by uzik »

Thanks :D

I've been very favorably impressed with the library so far. The folks on the forums seem
to be a great bunch too.
uzik
Posts: 24
Joined: Wed Sep 22, 2004 4:56 pm

finding the poly facing the camera

Post by uzik »

I looked through the code to calculate the number of polys in a mesh
and that gave me what I needed to iterate through the polys and find
the one most nearly facing the camera. Here's what I came up with:

Code: Select all

vector3df Roller::FindNearestPoly( scene::IMesh* mesh )
  {
    vector3df vR;

	  if ( mesh )
      {
	      signed int bcount = mesh->getMeshBufferCount();
	      for ( signed int b = 0; b < bcount; ++b )
	        {
		        IMeshBuffer* buffer = mesh->getMeshBuffer( b );
		        //signed int vtxcnt = buffer->getVertexCount();
		        signed int idxcnt = buffer->getIndexCount();
		        u16* idx = buffer->getIndices();

		        switch ( buffer->getVertexType() )
		          {
		            case video::EVT_STANDARD:
			            {
                    // get pointer to vertex data
				            video::S3DVertex* v = (S3DVertex*)buffer->getVertices();

				            plane3df pClosest;
                    float fClosest = 1.0;
                    for ( signed int i = 0; i < idxcnt; i += 3 )
				              {
                        // look at polygon created by vertices
                        plane3df p( v[idx[i+0]].Pos, v[idx[i+1]].Pos, v[idx[i+2]].Pos );

                        float f = p.Normal.dotProduct( vCameraLook );
                        // is the poly front facing?
                        if ( f <= 0.0f )
                          {
                            // which poly is most nearly aimed at camera?
                            if ( fClosest >= f )
                              {
                                fClosest = f;
                                pClosest = p;
                              }
                          }
				              }

                    // We now know the visible poly most nearly aimed at the camera.
                    // The camera view vector -
                    // the poly normal vector should get
                    // the vector to rotate through so poly faces the camera
                    vR = -vCameraLook.normalize();
                    vR -= pClosest.Normal;
			            }
			            break;
		            case video::EVT_2TCOORDS:
			            {
			            break;
		            }
	          }
      }
		return vR;
  }
Post Reply