Skip to main content
GameDev.net gamedev.net
🔒 Locked

dynamic polygon creation?

Started by argonaut Mar 31, 2007 at 9:38 PM 1 replies 650+ views
Original Post
argonaut
argonaut
I'm trying to dynamically allocate some polys, but I haven't been able to figure out how just yet. This code is in my render loop, so far. Here is my code:

	static int startCount = GetTickCount();
	static float cx = 0;
	// calculate the size of the new vertex 
	int objCount = 1 + (GetTickCount() - startCount) / 1000;
	// create a new vertex data set
	stD3DVertex *objData = new stD3DVertex[objCount];
	// loop through to create the new vertex data set
	for(int i = 0; i < objCount; i++)
	{
		cx = i + 0.5f - (float)(rand() % 1000) / 1000.0f;
		// objectargs must be delivered independently

		stD3DVertex objArg[] = {   {-2.5f - cx, -0.5f, -6.0f, D3DCOLOR_ARGB(255,255,255,255), 0.0f, 1.0f},
			{2.5f, -0.5f,  -6.0f, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 1.0f},
			{2.5f, -0.5f, 6.0f, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 0.0f},

			{2.5f, -0.5f, 6.0f, D3DCOLOR_ARGB(100,255,255,255), 1.0f, 0.0f},
			{-2.5f - cx, -0.5f, 6.0f, D3DCOLOR_ARGB(100,255,255,255), 0.0f, 0.0f},
			{-2.5f - cx, -0.5f, -6.0f, D3DCOLOR_ARGB(100,255,255,255), 0.0f, 1.0f}
		};
	// deliver the data set to the vertex buffer
	objData = *objArg;
	}
   // Create the vertex buffer.
   if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
             D3DFVF_VERTEX, D3DPOOL_DEFAULT,
             &g_VertexBuffer, NULL))) return;
   
   // Fill the vertex buffer.
   void *ptr;
   if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
      (void**)&ptr, 0))) return ;
   memcpy(ptr, objData, sizeof(objData));

   g_VertexBuffer->Unlock();





The problem lies in sending the objArg pointer to the objData array. Wow, I'm sure i'm going to get flamed for this, but salvo away!! [Edited by - argonaut on March 31, 2007 10:54:31 PM]
~Argonaut________Why "~Argonaut"? It's all just a mathematical expression denoting a close approximation of "Argonaut", which is irrational and can't be precisely defined.
Mastaba
Mastaba
I see a couple of errors, both semantic. One is, you allocate an array of objcount stD3DVertex objects, then for in the loop, from 0 to objcount , you create six that you want to copy to the array (thus your array needs to allocate space for 6*objcount stD3DVertex objects). Next why not simply create the objects in the vertex array to begin with rather than to a temporary array that that you then copy to the other array?

Something like this:

static float cx = 0;	// calculate the size of the new vertex 	int objCount = 1 + (GetTickCount() - startCount) / 1000;  // Create the vertex buffer.   if(FAILED(g_D3DDevice->CreateVertexBuffer(6*objCount*sizeof(stD3DVertex), 0,             D3DFVF_VERTEX, D3DPOOL_DEFAULT,             &g_VertexBuffer, NULL))) return;      // Fill the vertex buffer.   stD3DVertex *vertex;   if(FAILED(g_VertexBuffer->Lock(0, 0,      (void**)&vertex, 0))) return ;	// loop through to create the new vertex data set	for(int i = 0; i < objCount; i++)	{		cx = i + 0.5f - (float)(rand() % 1000) / 1000.0f;		// objectargs must be delivered independently		*vertex++ = stD3DVertex(-2.5f - cx, -0.5f, -6.0f, D3DCOLOR_ARGB(255,255,255,255), 0.0f, 1.0f);		*vertex++ = stD3DVertex(2.5f, -0.5f,  -6.0f, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 1.0f);		*vertex++ = stD3DVertex(2.5f, -0.5f, 6.0f, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 0.0f);		*vertex++ = stD3DVertex(2.5f, -0.5f, 6.0f, D3DCOLOR_ARGB(100,255,255,255), 1.0f, 0.0f);		*vertex++ = stD3DVertex(-2.5f - cx, -0.5f, 6.0f, D3DCOLOR_ARGB(100,255,255,255), 0.0f, 0.0f);		*vertex++ = stD3DVertex(-2.5f - cx, -0.5f, -6.0f, D3DCOLOR_ARGB(100,255,255,255), 0.0f, 1.0f);	}    g_VertexBuffer->Unlock();


Though you'll need the relevant stD3DVertex constructor, which I assume is available. Also sizeof objData is always the size of a pointer, not the array it points to since it is defined as a pointer rather than an array. You also have a potential memory leak in the code but I'm assuming this is just for illustrative purposes and not actual code so I'll overlook that.
argonaut
argonaut
Wow! Interesting technique. I'll have to give it a try! Thanks for the response :)
~Argonaut________Why "~Argonaut"? It's all just a mathematical expression denoting a close approximation of "Argonaut", which is irrational and can't be precisely defined.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.