Sign in to follow this  
CDProp

Weird rendering artifacts (D3D)

Recommended Posts

Hello there. I'm just trying to get acquainted with Direct3D, and so I thought I'd start by rendering a single triangle. The ambient is turned all the way up, lighting turned off, backface culling off, and z-buffer is enabled. Each vertex has a different color. For some reason, alternate columns of pixels are rendering black, as well as a pattern of random black pixels that is different every time I run the program: (click to enlarge) I can provide any source code that people need, but it's somewhat complex because I was loosely following a tutorial so that I could keep things in different classes as I pleased. I'll post what I think are the relevant bits. There is a Renderer class that has an InitScene() function that is called once. It sets the states, creates the flexible vertex format, creates the vertex buffer, and then copies vertices into it (of course, the Renderer InitScene() function will not be handling the vertex stuff once I get a mesh class going).
bool Renderer::InitScene()
{
	for(int i = 0; i < 8; i++)
	{
		m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_ANISOTROPIC);
		m_pDevice->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, 0);
	}

	m_pDevice->SetRenderState(D3DRS_AMBIENT, RGB(255,255,255));
	m_pDevice->SetRenderState(D3DRS_LIGHTING, false);
	m_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	m_pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

	m_pDevice->SetFVF(D3DFVF_CUSTOMVERTEX);

	D3DVERTEX aTriangle[]	= { {-2.0f,1.0f,10.0f,0xffff0000},
								{-3.0f,-1.0f,10.0f,0xff00ff00},
								{-1.0f,-1.0f,10.0f,0xff0000ff} };

	m_pDevice->CreateVertexBuffer(sizeof( aTriangle ), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &g_pTriangle, NULL);

	g_pTriangle->Lock(0, sizeof(g_pData), &g_pData, 0);
	memcpy(g_pData, aTriangle, sizeof(aTriangle) );
	g_pTriangle->Unlock();

	return true;
}

Here are the lines of code related to the flexible vertex format and some globals. The define is at the top of the source file, as are the globals, and the struct is found in the header, but I'll put them all together here since those details don't really matter:
#define D3DFVF_CUSTOMVERTEX	(D3DFVF_XYZ | D3DFVF_DIFFUSE)

struct D3DVERTEX
{
	float	fX,
			fY,
			fZ;
	DWORD	dwColor;
};

Renderer g_Renderer;
LPDIRECT3DVERTEXBUFFER9 g_pTriangle;
void	*g_pData;

And finally, the render function:
bool Renderer::Render()
{
	if(!CheckDevice())
		return false;

	// Set Projection Matrix
	if(FAILED( m_pDevice->SetTransform(D3DTS_PROJECTION, &g_Camera.GetProjMat()) ))
		return false;

	m_pDevice->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0, 0);

	m_pDevice->BeginScene();

	m_pDevice->SetStreamSource(0, g_pTriangle, 0, sizeof(D3DVERTEX));
	m_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

	m_pDevice->EndScene();					

	m_pDevice->Present(NULL, NULL, NULL, NULL);

	return true;
}

I was conflicted as to whether this should be posted here, since its such a beginner question, or in the DirectX forum. Let me know if I should have it moved.

Share this post


Link to post
Share on other sites
Thanks; I feel stupid, as I should have thought of that. I didn't bother looking at the tutorial's call to Clear() because I already was clearing the render target and it didn't hit me that the same function was used to clear the z buffer.

Thanks again!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this