Archived

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

chaser01

Trouble Getting Direct3D Working

Recommended Posts

chaser01    122
I didn’t know whether to put this in the beginners or DirectX forum, so sorry if I’ve gone for the wrong one. I’ve been trying a bit of games programming after work as a bit of a hobby. I did some 2D stuff with DirectDraw7 and later the ID3DXSprite class, and that all went nicely. Just over two weeks ago I decided to try something in 3D, so I wrote some Direct3D set up code and a bit of code to display a single point on the screen and a single line. Unfortunately when I ran it I just got a black screen. Two weeks down the line I’ve still just got a black screen and still can’t work out what’s going wrong. D3DXSprite pictures will still display fine, but I just can’t get a point, line, face or anything 3D to display. I’ve been looking at it every evening but can’t find what’s missing. I’d be extremely grateful if someone could help me out. I’ve pasted my Direct3D 8 and viewport and projection matrix setup functions below.
  
HRESULT InitDirect3D()
{
	// Initialises Direct3D for full screen mode


	// Create Direct3D interface

	if ((lpD3D = Direct3DCreate8(D3D_SDK_VERSION)) == NULL)
	{
		OutputDebugString("\n\nError creating Direct3D Interface - CDXSetup::InitDirect3D()\n\n");
		return E_FAIL;
	}

	// Declare structure to hold information about rendering method

	memset(&D3DPresParameters, 0, sizeof(D3DPresParameters));
		
	// Set screen dimensions and full screen mode

	D3DPresParameters.BackBufferWidth					= SCREEN_WIDTH;
	D3DPresParameters.BackBufferHeight					= SCREEN_HEIGHT;
	D3DPresParameters.BackBufferFormat					= D3DFMT_A8R8G8B8;
	D3DPresParameters.Windowed							= FALSE;
	
	// Set number of backbuffers and flipping mode

	D3DPresParameters.BackBufferCount					= 1;
	D3DPresParameters.MultiSampleType					= D3DMULTISAMPLE_NONE;
	D3DPresParameters.SwapEffect						= D3DSWAPEFFECT_DISCARD;

	// Allow Direct3D to manage depth buffer and set depth buffer format to 16bit

	D3DPresParameters.EnableAutoDepthStencil			= TRUE;
	D3DPresParameters.AutoDepthStencilFormat			= D3DFMT_D16;

	// Use default refresh rate

	D3DPresParameters.FullScreen_RefreshRateInHz		= 0;
	D3DPresParameters.FullScreen_PresentationInterval	= D3DPRESENT_INTERVAL_IMMEDIATE;

	// Allow back buffer to be accessed for 2D work

	D3DPresParameters.Flags								= D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
	
	// Aquire IDirect3DDevice8 pointer

	if (FAILED(lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hMainWindowHandle, 
								   D3DCREATE_HARDWARE_VERTEXPROCESSING, &D3DPresParameters, &lpD3DDevice)))
	{
		OutputDebugString("\n\nError creating Direct3D Device - CDXSetup::InitDirect3D()\n\n");
		return E_FAIL;
	}

	// Set to use default vertex shader

	lpD3DDevice->SetVertexShader(FLEXIBLE_VERTEX_DEF);

	// Enable filtering if a texture is too close or too far away

	lpD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
	lpD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);

	// Load default texture for Direct3D

	lptDefaultTexture = HLoadImage("Graphics\\DefaultTexture.jpg");

	return S_OK;
}




HRESULT InitD3DViewPortAndProj()
{
	// Setup viewport

	// Set viewport values

	D3DVIEWPORT8		vpViewPort;
	vpViewPort.X		= 0;
	vpViewPort.Y		= 0;
	vpViewPort.Width	= SCREEN_WIDTH;
	vpViewPort.Height	= SCREEN_HEIGHT;
	vpViewPort.MinZ		= 0.0f;
	vpViewPort.MaxZ		= 1.0f;

	// Create viewport

	if (FAILED(lpD3DDevice->SetViewport(&vpViewPort)))
		return E_FAIL;


    // Setup projection matrix	

	// Clear projection matrix and set variables required to calculate matrix

	D3DXMATRIX xmatProjectionMatrix;
	memset(&xmatProjectionMatrix, 0, sizeof(D3DXMATRIX));
	float fScreenAspect = float(SCREEN_WIDTH) / float(SCREEN_HEIGHT);
	float fFOV = D3DX_PI / 4;

	// Use furnction to build left-handed perspective projection matrix based on FOV

	D3DXMatrixPerspectiveFovLH(&xmatProjectionMatrix, fFOV, fScreenAspect, 1.0f, 1000.0f);

	// Set device to use projection matrix

	if (FAILED(lpD3DDevice->SetTransform(D3DTS_PROJECTION, &xmatProjectionMatrix)))
		return E_FAIL;

	return S_OK;
}
  
I don’t think the second function is necessary just to display a point but after I was getting nothing I thought I’d put it in to see if it would do any good. Is there anything wrong with the DirectX setup or anything essential that’s missing? Thanks a lot.

Share this post


Link to post
Share on other sites
Wreakon    121
" // Set viewport values D3DVIEWPORT8 vpViewPort; vpViewPort.X = 0; vpViewPort.Y = 0; vpViewPort.Width = SCREEN_WIDTH; vpViewPort.Height = SCREEN_HEIGHT; vpViewPort.MinZ = 0.0f; vpViewPort.MaxZ = 1.0f; // Create viewport if (FAILED(lpD3DDevice->SetViewport(&vpViewPort))) return E_FAIL;"

Usually I dont mind setting the viewport, and it can mess up the screen you are looking at.

Here are some tips:
1.) Check all errors
2.) Turn on Debug Mode in DirectX and debug see if you get any errors (warnings are mostly ok to get stuff to "work")
3.) I dont see where you have set the view and world matrices
- You set the D3DTS_PROJECTION which seems fine
- Make sure to set the D3DTS_VIEW to something like:

LookAt = 0 0 0
Pos = 0 0 -5 ( - 5 = zoom, farther from 0 = zoom out )
Up = 0 1 0

4.) Make sure you place that sprite to 0 0 0 or better yet -1, -1, 0
5.) You are initializing a Depth Buffer, make sure to clear it each frame by:

device->Clear(D3D.._TARGET|D3D.._ZBUFFER, ....) (check documentation for Clear() 4 exact names of flags.)

-------
"Programming is like sex make one mistake, and you have to support it forever."
Homepage: http://students.washington.edu/andrey

Share this post


Link to post
Share on other sites
chaser01    122
Thanks a lot for the suggestions Wreakon. I’m not getting any errors or warnings with debug on, and I’m clearing the back buffer and Z buffer every frame. I have a function to set the view matrix, but I’d actually forgotten to set the world matrix I sorted that out but it’s still not working.

I think the main problem is that I don’t have a great understanding of what’s going on with Direct3D, so I think I might just buy a different Direct3D book and start again.

StuartSlater, I’ll take a look at those samples tonight and see if they help me out before ordering a new book.

Thanks again for your help

Share this post


Link to post
Share on other sites
Fidelio_    122
quote:

Just over two weeks ago I decided to try something in 3D, so I wrote some Direct3D set up code and a bit of code to display a single point on the screen and a single line. Unfortunately when I ran it I just got a black screen. Two weeks down the line I’ve still just got a black screen and still can’t work out what’s going wrong.



First rule of testing Direct3D: don''t clear the screen to black, use blue or green. Many things can cause your triangles to turn out black, eg. missing normals, bad lighting, on a blue screen you''d see them as black, with a black screen you see nothing.

Also, your object might fall behind the viewport of 0.0f and 1.0f. Try 1.0f and 1000.0f.



Share this post


Link to post
Share on other sites