Game Development Community

Render Mesh Problem

by Jonajoint · in Torque 3D Professional · 04/09/2012 (10:22 am) · 3 replies

Ok, So where to start...

I'm trying to render a mesh generated by delaunay triangulation, however the actual rendering is.. not going so well.
In the editor screen shots below you can see a bounding box where the rendering should happen, and below it a debug line render of the actual delaunay triangles generated, the delaunay side of things seems to be ok.

With less than 8 triangles we see nothing in the bounding box.
i1052.photobucket.com/albums/s454/Jonajoint/d1.jpg
Add a some more and things start to happen.. Although incorrectly
i1052.photobucket.com/albums/s454/Jonajoint/d3.jpg
i1052.photobucket.com/albums/s454/Jonajoint/d5.jpg
Add a whole bunch and we have Mr Messy from the mr men :p
i1052.photobucket.com/albums/s454/Jonajoint/d6.jpg
So obviously i've messed up the point index allocation, but can't figure out how, it all looks in order to me.

I'm hoping someone wouldn't mind browsing through my render code and see where i'm going wrong.

bool DelaunayTest::createGeometry()
{
	mRebuild = false;
	// The triangulation returns a null pointer on an error
	// So we must test for a NULL so we don't try and access it..
	if(&tris == NULL){
		Con::printf("######################## DelaunayTest::createGeometry, tris is NULL");
		return false;
	}
	int numTris = tris.size();
	if(numTris < 1){
		Con::printf("######################## DelaunayTest::createGeometry No Triangles, Returning. tris.size() = %i",numTris);
		return false;
	}
	Con::printf("######################## DelaunayTest creating Geometry");

	// Fill the vertex buffer
	VertexType *pVert = NULL;

	mVertexBuffer.set( GFX, numTris*3 , GFXBufferTypeStatic );
	pVert = mVertexBuffer.lock();
	for (U32 i = 0; i < numTris; i++)
	{
		pVert[i].point = mPnts[tris[i].p1];// * halfSize;
		pVert[i].normal   = mNorms[(i*3)];
		pVert[i].texCoord = mTex[(i*3)];

		pVert[i].point = mPnts[tris[i].p2];// * halfSize;
		pVert[i].normal   = mNorms[(i*3)+1];
		pVert[i].texCoord = mTex[(i*3)+1];

		pVert[i].point = mPnts[tris[i].p3];// * halfSize;
		pVert[i].normal   = mNorms[(i*3)+2];
		pVert[i].texCoord = mTex[(i*3)+2];

//		Con::printf("######################## DelaunayTest::createGeometry Triangle(%i) = (%f,%f,%f) (%f,%f,%f) (%f,%f,%f)",i,mPnts[tris[i].p1].x,mPnts[tris[i].p1].y,mPnts[tris[i].p1].z,mPnts[tris[i].p2].x,mPnts[tris[i].p2].y,mPnts[tris[i].p2].z,mPnts[tris[i].p3].x,mPnts[tris[i].p3].y,mPnts[tris[i].p3].z);
	}
	mVertexBuffer.unlock();
	vertSize = numTris*3;
	primSize = numTris;

	// Fill the primitive buffer
	U16 *pIdx = NULL;

	mPrimitiveBuffer.set( GFX, vertSize, primSize, GFXBufferTypeStatic );

	mPrimitiveBuffer.lock(&pIdx);     
   int idxcount=0;
	for (U16 i = 0; i < primSize; i++){
		pIdx[(i*3)] = tris[i].p1;
		pIdx[(i*3)+1] = tris[i].p2;
		pIdx[(i*3)+2] = tris[i].p3;
		idxcount+=3;
		Con::printf("######################## DelaunayTest::createGeometry Triangle(%i) = (%f,%f,%f) (%f,%f,%f) (%f,%f,%f)",i,mPnts[tris[i].p1].x,mPnts[tris[i].p1].y,mPnts[tris[i].p1].z,mPnts[tris[i].p2].x,mPnts[tris[i].p2].y,mPnts[tris[i].p2].z,mPnts[tris[i].p3].x,mPnts[tris[i].p3].y,mPnts[tris[i].p3].z);
	}
	mPrimitiveBuffer.unlock();

	AssertFatal( idxcount == vertSize, "DelaunayTest, wrote incorrect number of indices in pIdx!" );

   return true;
}

void DelaunayTest::prepRenderImage( SceneRenderState *state )
{
   // Do a little prep work if needed
   if ( mRebuild )//mVertexBuffer.isNull() || 
      if(!createGeometry())
		  return;

   // If we have no material then skip out.
   if ( !mMaterialInst )
      return;

   // If we don't have a material instance after the override then 
   // we can skip rendering all together.
   BaseMatInstance *matInst = state->getOverrideMaterial( mMaterialInst );
   if ( !matInst )
      return;

   // Get a handy pointer to our RenderPassmanager
   RenderPassManager *renderPass = state->getRenderPass();

   // Allocate an MeshRenderInst so that we can submit it to the RenderPassManager
   MeshRenderInst *ri = renderPass->allocInst<MeshRenderInst>();

   // Set our RenderInst as a standard mesh render
   ri->type = RenderPassManager::RIT_Mesh;

   // Calculate our sorting point
   if ( state )
   {
      // Calculate our sort point manually.
      const Box3F& rBox = getRenderWorldBox();
      ri->sortDistSq = rBox.getSqDistanceToPoint( state->getCameraPosition() );      
   } 
   else 
      ri->sortDistSq = 0.0f;

   // Set up our transforms
   MatrixF objectToWorld = getRenderTransform();
   objectToWorld.scale( getScale() );

   ri->objectToWorld = renderPass->allocUniqueXform( objectToWorld );
   ri->worldToCamera = renderPass->allocSharedXform(RenderPassManager::View);
   ri->projection    = renderPass->allocSharedXform(RenderPassManager::Projection);

	// If our material needs lights then fill the RIs 
   // light vector with the best lights.
   if ( matInst->isForwardLit() )
   {
      LightQuery query;
      query.init( getWorldSphere() );
		query.getLights( ri->lights, 8 );
   }

   // Make sure we have an up-to-date backbuffer in case
   // our Material would like to make use of it
   // NOTICE: SFXBB is removed and refraction is disabled!
   //ri->backBuffTex = GFX->getSfxBackBuffer();

   // Set our Material
   ri->matInst = matInst;

   // Set up our vertex buffer and primitive buffer
   ri->vertBuff = &mVertexBuffer;
   ri->primBuff = &mPrimitiveBuffer;

   ri->prim = renderPass->allocPrim();
   ri->prim->type = GFXTriangleList;
   ri->prim->minIndex = 0;
   ri->prim->startIndex = 0;
   ri->prim->numPrimitives = primSize;
   ri->prim->startVertex = 0;
   ri->prim->numVertices = vertSize;

   // We sort by the material then vertex buffer
   ri->defaultKey = matInst->getStateHint();
   ri->defaultKey2 = (U32)ri->vertBuff; // Not 64bit safe!

   // Submit our RenderInst to the RenderPassManager
   state->getRenderPass()->addInst( ri );

	drawDebug(state);
}

Many thanks if you've made it this far :)
John

#1
04/09/2012 (1:02 pm)
Got it sorted

I was passing in the Vertices 3 times DOH! :/

where I put
mVertexBuffer.set( GFX, numTris*3 , GFXBufferTypeStatic );  
    pVert = mVertexBuffer.lock();  
    for (U32 i = 0; i < numTris; i++)  
    {  
        pVert[i].point = mPnts[tris[i].p1];// * halfSize;  
        pVert[i].normal   = mNorms[(i*3)];  
        pVert[i].texCoord = mTex[(i*3)];  
  
        pVert[i].point = mPnts[tris[i].p2];// * halfSize;  
        pVert[i].normal   = mNorms[(i*3)+1];  
        pVert[i].texCoord = mTex[(i*3)+1];  
  
        pVert[i].point = mPnts[tris[i].p3];// * halfSize;  
        pVert[i].normal   = mNorms[(i*3)+2];  
        pVert[i].texCoord = mTex[(i*3)+2];

should have been
mVertexBuffer.set( GFX, mPnts.size() , GFXBufferTypeStatic);
	pVert = mVertexBuffer.lock();
	for (U32 i = 0; i < mPnts.size(); i++)
	{
		pVert[i].point = mPnts[i];// * halfSize;
		pVert[i].normal   = mNorms[i];
		pVert[i].texCoord = mTex[i];
#2
04/09/2012 (2:30 pm)
Wow - looks like one of my bugs.... lol
#3
04/09/2012 (4:16 pm)
lol.. when things look like that I'm confident it's some thing i've done, or not.