colored quad not rendering, only a black triangle

Started by
6 comments, last by fireking 20 years, 6 months ago
ok my problem is, ive got this code and it renders a single black triangle, but its supposed to render a colored quad (only the second triangle is being draw out of both triangles in the quad, and its being colored black, but its supposed to be several colors...)

//vertex info
struct sVertex{
  FLOAT x, y, z;
  D3DCOLOR diffuse;
};
#define VertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)

//initialization code
bool Init()
{
	d3d=Direct3DCreate8(D3D_SDK_VERSION);
	D3DDISPLAYMODE d3ddm;
	ZeroMemory(&d3ddm,sizeof(D3DDISPLAYMODE));
	d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
	d3dpp.Windowed=true;
	d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat=d3ddm.Format;
	d3dpp.EnableAutoDepthStencil=true;
	d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
	d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,myapp.GetHwnd(),D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&dev);

	dev->SetRenderState(D3DRS_ZENABLE,true);

	dev->CreateVertexBuffer(sizeof(sVertex)*4,0,VertexFVF,D3DPOOL_DEFAULT,&vertexbuffer);
	
	BYTE *Ptr;

	sVertex Verts[4]=
	{
		{-1.0f, 1.0f, 0.0f,D3DCOLOR_RGBA(255,0,0,255)},
		{ 1.0f, 1.0f, 0.0f,D3DCOLOR_RGBA(0,255,0,255)},
		{ 1.0f,-1.0f, 0.0f,D3DCOLOR_RGBA(0,0,255,255)},
		{-1.0f,-1.0f, 0.0f,D3DCOLOR_RGBA(255,0,255,255)}
	};

	vertexbuffer->Lock(0,0,(BYTE**)&Ptr,0);
	memcpy(Ptr,Verts,sizeof(Verts));
	vertexbuffer->Unlock();

D3DXMATRIX matrix;
D3DXMatrixIdentity(&matrix);
dev->SetTransform(D3DTS_WORLD, &matrix);

D3DXMatrixLookAtLH(&matrix,&D3DXVECTOR3(0.0f,2.0f,-10.0f),&D3DXVECTOR3(0.0f,0.0f, 0.0f),&D3DXVECTOR3(0.0f,1.0f,0.0f));
dev->SetTransform(D3DTS_VIEW, &matrix);

D3DXMatrixPerspectiveFovLH(&matrix,D3DX_PI/4,4.0f/3.0f,1.0f,100.0f);
dev->SetTransform(D3DTS_PROJECTION, &matrix);


	return true;
}

//frame code
bool Frame()
{
	dev->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_RGBA(200,200,200,255),1.0f,0);
	if(SUCCEEDED(dev->BeginScene()))
	{
		dev->SetStreamSource(0,vertexbuffer,sizeof(sVertex));
		dev->SetVertexShader(VertexFVF);
		dev->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
		dev->EndScene();
	}
	dev->Present(NULL,NULL,NULL,NULL);

	return true;
}
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
Advertisement
This caught my attention:

you wrote:
memcpy(Ptr,Verts,sizeof(Verts));

you should pass memcpy the address to the vertex data, as such:
memcpy(Ptr,&Verts[0],sizeof(Verts));


I'll let you know if i notice something else.

-Nik

EDIT: Changed word "pointer" to "address" for clarity.

[edited by - Nik02 on October 16, 2003 8:29:05 AM]

Niko Suni

here''s some code from a sample that comes with the dxsdk

if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB ) ) )
{
return E_FAIL;
}

// Fill the vertex buffer.
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (BYTE**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pVB->Unlock();

return S_OK;


using the address & makes no difference

well i turned lighting off and turned cull mode off, im getting a little bit of something, but my verts aren''t in the right order? what order does it draw?
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios


[edited by - fireking on October 16, 2003 8:38:55 AM]
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
quote:Original post by Nik02
This caught my attention:

you wrote:
memcpy(Ptr,Verts,sizeof(Verts));

you should pass memcpy the address to the vertex data, as such:
memcpy(Ptr,&Verts[0],sizeof(Verts));


I''ll let you know if i notice something else.

-Nik

EDIT: Changed word "pointer" to "address" for clarity.

Nah, I think that''s a red herring. One of the peculiarities of C and C++ is that an array evaluates to a pointer to its first element -- hence, "Verts" and "&Verts[0]" evaluate to the same thing. To keep this in mind, think about char* -- you declare a char array (myArray[20]), but when passing it to other functions you simply use its name (myArray) rather than &myArray[0] .

So it''s probably not that...
Oh yes, i stand corrected

However, try to render with D3DPT_TRIANGLEFAN.

Niko Suni

Turn off lighting
bleh, that didnt work...

so i decided to draw two triangles

thanks for the help though
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios

This topic is closed to new replies.

Advertisement