Game Development Community

Help: Draw VertexBuffer

by James Laker (BurNinG) · in Torque Game Engine Advanced · 03/12/2007 (7:34 am) · 7 replies

Hi...

Does anyone know how to draw a Vertexbuffer? My game crashes when I call, GFX->drawIndexedPrimitive(GFXTriangleList, 0, mDotsVB->mNumVerts, 0, mDotsVB->mNumVerts);. Can anyone see anything wrong with my code?
Putting Data in Verts (Seems to be working)
GFXVertexPC *Verts = new GFXVertexPC[VertCount];

      ColorF col;
      for(int i=0; i<VertCount; ++i)
      {
         Verts[i].point.x = (F32)((rand() % 10000) / 10000);
         Verts[i].point.y= (F32)((rand() % 10000) / 10000);
         Verts[i].point.z = (F32)((rand() % 10000) / 10000);
     
         col.red = (F32)((rand() % 1000) / 1000);
         col.green = (F32)((rand() % 1000) / 1000);
         col.blue = (F32)((rand() % 1000) / 1000);
         col.alpha = (F32)((rand() % 1000) / 1000);
         Verts[i].color = GFXVertexColor(col);
      }

      mStarsVB.set(GFX, starCount, GFXBufferTypeStatic);
      GFXVertexPCT *vbVerts = mVertsVB.lock();

		//Copy it to vbVerts
      dMemcpy( vbVerts, Verts, VertCount * sizeof(GFXVertexPC) );
		
      mStarsVB.unlock();

  // Clean up
      delete [] Verts;

Render block (Seems to be faulty)
...

		GFX->setBaseRenderState();

		GFX->setAlphaBlendEnable(true);
		GFX->setSrcBlend(GFXBlendSrcAlpha);
		GFX->setDestBlend(GFXBlendDestAlpha);

		GFX->setVertexBuffer(mVertsVB);

		// Draw it all
		[b]GFX->drawIndexedPrimitive(GFXPointList, 0, mVertsVB->mNumVerts, 0, mVertsVB->mNumVerts);[/b]

		GFX->disableShaders();
		GFX->setCullMode(GFXCullNone);
...

Any help would be appreciated.

Regards
James

EDIT: Removed &

#1
03/13/2007 (4:58 am)
In the first code block, why do you have an ampersand in front of Verts[i].point.y and Verts[i].point.z ?

The ampersand in front of the 'col....' lines looks funny too...
#2
03/13/2007 (7:19 am)
You're right... That's not suppose to be in there...
Still not my main problem... I thiiiink I found the problem though... Will test when I get home.

Verts[i].point.x = (F32)((rand() % 10000) / 10000);         
Verts[i].point.y= (F32)((rand() % 10000) / 10000);        
 Verts[i].point.z = (F32)((rand() % 10000) / 10000);

means all the points are always gonna be like 0.xxxxxxxx
Maybe I'm just not seeing them... So I changed em to:

Verts[i].point.x = (F32)((rand() % 10000));         
Verts[i].point.y= (F32)((rand() % 10000));        
 Verts[i].point.z = (F32)((rand() % 10000));

Until tonight then...
#3
03/13/2007 (10:40 am)
You need an index buffer to tell which vertices are used in which triangles. Do a search on the source for setVertexBuffer() and you'll always find a setIndexBuffer() nearby.
#4
03/13/2007 (3:35 pm)
Okay.. even if i'm only drawing points (Pointlist) and not trianglestrips? What I found in code was all Quads and triangles (with textures)...

i'll have a look again
#5
03/13/2007 (5:10 pm)
Now stuffing verts in the buffer is new for me too... but wouldn't it be something more like:

GFX->setVertexBuffer( mVertsVB);
   GFX->drawPrimitive( GFXPointList, 0, mVertsVB->mNumVerts);

EDIT: Since you aren't setting an index to being with that is....
#6
03/14/2007 (10:58 am)
You do not need an index buffer to draw non-indexed polygons. I highly recommend the DirectX docs for DrawIndexedPrimitive and DrawPrimitive method descriptions, for more information on what exactly an 'indexed' primitive is. Obviously the parameters for the function won't be the same, but the concept of indexed geometry vs non-indexed is an API agnostic idea.
#7
04/04/2007 (7:02 am)
Okay after a little break, it's fullsteam ahead. Pat... Thanx... that sorted out the crashing.
I'm getting weird results though:
burningza.googlepages.com/StarVertexBufferBug.jpg
You can see the stars... but they're in a line. I thought it must be one of the axis, but that doesn't seem to be the problem as far as I can tell.

Note! I managed to get it working correctly using the PrimBuilder, but Vertexbuffer seems the correct way of doing it, and it's something new for me.

Thanx for all the help so far.