Archived

This topic is now archived and is closed to further replies.

Vertex Buffers and passing array arguments

This topic is 4943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm using DirectX 8, and I'm trying to create a function that I can pass an array of vertices to and it will create a vertex buffer and render it as a line list. All I'm getting is a single twitchy line. If I create the same array inside the function, everything works fine (unless I dynamically allocate it), but when I pass it as an argument it doesn't. If it helps, here's the function:
HRESULT renderLine(VERTEX_XYZ_ARGB *srcArray, 
				   unsigned int numVerts, 
				   unsigned long col)
{
	HRESULT r = 0;


	for(unsigned int v=0; v<numVerts; v++)
	{
		srcArray[v].colour = col;
	}


	//Create vertex buffer interface.

	IDirect3DVertexBuffer8 *pIVB = 0;
	unsigned char *Verts = 0;

	r = g_pDevice->CreateVertexBuffer(sizeof(VERTEX_XYZ_ARGB)*numVerts,
				  0,
				  (D3DFVF_XYZ | D3DFVF_DIFFUSE),
				  D3DPOOL_DEFAULT,
				  &pIVB);
	if(FAILED(r)) 
	{
		SetError("Failed creating line primitive vertex buffer interface.");
		return r;
	}


	//Lock the vertex buffer

	r = pIVB->Lock(0, sizeof(VERTEX_XYZ_ARGB)*numVerts, &Verts, 0);
	if(FAILED(r)) 
	{
		SetError("Could not lock the line primitive vertex buffer.");
		if(pIVB)
		{
			pIVB->Release();
			pIVB = 0;
		}

		return r;
	}


	//Fill the buffer with the vertices.

	CopyMemory(Verts, &srcArray, sizeof(VERTEX_XYZ_ARGB)*numVerts);

	pIVB->Unlock();


	//Render.

	g_pDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_DIFFUSE);
	g_pDevice->SetStreamSource(0, pIVB, sizeof(VERTEX_XYZ_ARGB));

	g_pDevice->DrawPrimitive(D3DPT_LINELIST, 0, numVerts/2);


	if(pIVB)
	{
		pIVB->Release();
		pIVB = 0;
	}

	return r;
}
[edited by - GrahamZ on May 31, 2004 4:25:04 AM] [edited by - GrahamZ on June 6, 2004 4:22:37 AM]

Share this post


Link to post
Share on other sites
Thought I''d answer my own question in case it''s any use to anyone. I never found out why the above doesn''t work: but the following does:


HRESULT renderLine(VERTEX_XYZ_ARGB *srcArray,
unsigned int numVerts)
{
HRESULT r = 0;


g_pDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_DIFFUSE);

g_pDevice->DrawPrimitiveUP(D3DPT_LINELIST, numVerts/2,
srcArray, sizeof(VERTEX_XYZ_ARGB));

return r;
}

Share this post


Link to post
Share on other sites