Sign in to follow this  

Squares not showing

This topic is 3333 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

I've been following www.directxtutorial.com and all has been fine so far, but I've gotten to the tutorial about drawing quads and for some reason my square isn't showing:
const int FVF_FORMAT = (D3DFVF_XYZ | D3DFVF_DIFFUSE);
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
	D3DDevice->CreateVertexBuffer(4 * sizeof(Vertex),
				      0,
				      FVF_FORMAT,
				      D3DPOOL_MANAGED,
				      &vertexBuffer,
				      NULL);
	Vertex square[] = {
            { -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 0), },
            { 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(200, 200, 200), },
            { -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 0), },
            { 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(200, 200, 200), },
        };

	VOID* pVoid;
	vertexBuffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, square, sizeof(square));
	vertexBuffer->Unlock();
        
        /* Loop stuff */
		D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
		D3DDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
		D3DDevice->BeginScene();
			D3DDevice->SetFVF(FVF_FORMAT);

			D3DXMatrixLookAtLH(&View, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0F, 1.0f, 0.0f));
			D3DDevice->SetTransform(D3DTS_VIEW, &View);
			D3DXMatrixPerspectiveFovLH(&Projection, D3DXToRadian(45), 800.0f / 600.0f, 1.0f, 100.0f);
			D3DDevice->SetTransform(D3DTS_PROJECTION, &Projection);

			D3DDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(Vertex));
			D3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
		D3DDevice->EndScene();
		D3DDevice->Present(NULL, NULL, NULL, NULL);



[Edited by - MrPickle on October 26, 2008 5:07:30 AM]

Share this post


Link to post
Share on other sites
-A common mistake in this scenario is for the camera to look "back" at the quad, resulting the back-face culling to take away the triangles before they get drawn. To test whether this is your problem (I haven't verified this), set the culling render state to not cull anything. In case this is the actual problem, you can properly fix it by changing the vertex order of the geometry from counterclockwise to clockwise.

-If you're supplying the diffuse color in your vertices, you should disable lighting (another renderstate).

-You should set the world transform to at least the identity matrix. I've seen some drivers that do not do this by default.

-In debugging this type of behavior, it is helpful to clear the background to some other color than [0,0,0]. Most geometry will still get rasterized to some color even though the pipeline is misconfigured, and often the color involved in this is black.

-Finally, you don't need two clear calls; just combine the D3DCLEAR_TARGET and D3DCLEAR_ZBUFFER with the | (bitwise or) operator. This will result in better performance.

Share this post


Link to post
Share on other sites
I have tried all you have suggested but nothing has worked :( Here's the updated code:

Here is how I am initializing:
LPDIRECT3D9 InitializeD3D(HWND hWnd, LPDIRECT3DDEVICE9& D3DDevice) {
LPDIRECT3D9 D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(!D3D) {
MessageBox(NULL, L"Unable to create D3D9 Object", L"ERROR", MB_OK | MB_ICONERROR);
return 0;
}

D3DPRESENT_PARAMETERS D3Dpp;
ZeroMemory(&D3Dpp, sizeof(D3Dpp));
D3Dpp.Windowed = TRUE;
D3Dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3Dpp.hDeviceWindow = hWnd;
D3Dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
D3Dpp.BackBufferWidth = 800;
D3Dpp.BackBufferHeight = 600;
D3Dpp.EnableAutoDepthStencil = TRUE;
D3Dpp.AutoDepthStencilFormat = D3DFMT_D16;

D3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&D3Dpp,
&D3DDevice);

D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
D3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

return D3D;
}


const int FVF_FORMAT = (D3DFVF_XYZ | D3DFVF_DIFFUSE);
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
D3DDevice->CreateVertexBuffer(4 * sizeof(Vertex),
0,
FVF_FORMAT,
D3DPOOL_MANAGED,
&vertexBuffer,
NULL);

Vertex square[] = {
{ -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 0), },
{ 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(200, 200, 200), },
{ -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 0), },
{ 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(200, 200, 200), },
};

VOID* pVoid;
vertexBuffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, square, sizeof(square));
vertexBuffer->Unlock();

/* Loop stuff */
D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(10, 128, 255), 1.0f, 0);
D3DDevice->BeginScene();
D3DDevice->SetFVF(FVF_FORMAT);

D3DXMatrixIdentity(&Identity);
D3DDevice->SetTransform(D3DTS_WORLD, &Identity);
D3DXMatrixLookAtLH(&View, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0F, 1.0f, 0.0f));
D3DDevice->SetTransform(D3DTS_VIEW, &View);
D3DXMatrixPerspectiveFovLH(&Projection, D3DXToRadian(45), 800.0f / 600.0f, 1.0f, 100.0f);
D3DDevice->SetTransform(D3DTS_PROJECTION, &Projection);

D3DDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(Vertex));
D3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
D3DDevice->EndScene();
D3DDevice->Present(NULL, NULL, NULL, NULL);

Share this post


Link to post
Share on other sites
Your camera is at exactly 1.0 units distance from the geometry. Your near plane is (also) exactly 1.0 units from your camera. As the geometry lies exactly in the near plane, it tends to be clipped away.

Try to move your camera back a bit, by setting the eye vector to [0,0,2], for example.

Share this post


Link to post
Share on other sites

This topic is 3333 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.

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