Archived

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

vImTo

ZBuffering? hiding my model!!

Recommended Posts

Hello, Im relativly new to DX programming. but I''ve got a problem that has had me pulling my hair out for weeks. I have a window with only one object in it (car.x) at 0,0,0 the camera is set back. When ZBuffering is of the car renders fine (with the exception of no zbuffering eg the wing mirror from the oposite side renders at the front) when Zbuffering is enabled nothing renders, with no error messages or failed operations. I''ve played with the projection matrix and still nothing. This is my first C++ DX project, I''ve written DX stuff in Delphi before using near identical initalisation routine with zbuffering working fine. I''m running PIII 733 with a Matrox G400Max so I know it is capable. This is been bugging me, I''ve had a number of rewrites but with no success. I''ve got a fealing is something simple, but I just can''t see it. the my camera object is a CD3DCamera from d3dutil.cpp from the SDK examples.
  
//---** Initaise D3D **-------------------------------------------------------//

HRESULT CDXScene::StartDirect3D(HWND hwnd)
{

	D3DDEVTYPE dtype;
	int vertexprocessing;
	HRESULT r = 0;

	bool StartDirect3D = false;

    g_pD3D8 = Direct3DCreate8(D3D_SDK_VERSION);

    if (g_pD3D8 == NULL)
	{
		OutputDebugString( "DXError : Could not create IDirect3D8 object\n" );
		return E_FAIL;
	}


    ZeroMemory(&D3Dpp, sizeof(D3Dpp));

    D3Dpp.Windowed      = !initFullscreen;
    D3Dpp.hDeviceWindow = hwnd;

	
  if (initZBuffer) 
    {
      D3Dpp.EnableAutoDepthStencil = true;
      D3Dpp.AutoDepthStencilFormat = D3DFMT_D16;
    }

    if (initFullscreen) 
	{
      D3Dpp.BackBufferWidth  = initWidth;
      D3Dpp.BackBufferHeight = initHeight;

	  switch (initBpp)
	  {
		  case 16 : D3Dpp.BackBufferFormat = D3DFMT_R5G6B5;
					break;
          case 32 : D3Dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
					break;
		  default : g_pD3D8 = NULL;
                    OutputDebugString("DXError : Backbuffer init error\n");
					break;
      }

      D3Dpp.BackBufferCount = initBackBufferCount;
      D3Dpp.SwapEffect      = D3DSWAPEFFECT_DISCARD;
    }
    else
    { // Windowed mode

      if (FAILED(g_pD3D8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &D3Ddm)))
	  {
		g_pD3D8 = NULL; 
		OutputDebugString("DXError : Could not get display adapter information\n");
		return E_FAIL;
	  }

      D3Dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
      D3Dpp.BackBufferFormat = D3Ddm.Format;
    }


  // Create the Direct3D device.

  if (initHAL) 
  { 
	  dtype = D3DDEVTYPE_HAL;
  } else 
  {
	  dtype = D3DDEVTYPE_REF;
  }

  // Hardware T&L?

  this->g_pD3D8->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &D3DCaps8);

 // HardwareVertexProcessing = D3DCaps8.DevCaps and D3DDEVCAPS_HWTRANSFORMANDLIGHT ;

  HardwareVertexProcessing = false ;

  if (HardwareVertexProcessing) { vertexprocessing = D3DCREATE_HARDWARE_VERTEXPROCESSING; }
  else {vertexprocessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING;}

  r = this->g_pD3D8->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, 
	  D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3Dpp, &g_pD3DDev8);

  if (FAILED(r))
  {
	  this->g_pD3D8 = NULL;
		OutputDebugString("DXError : Could not create render device\n");
		if (r == D3DERR_INVALIDCALL ){ OutputDebugString("           Invalid Call\n");}
		return E_FAIL;
  }

  // Get Device Caps

  ZeroMemory(&D3DDevCaps8, sizeof(D3DDevCaps8));
  if (FAILED(g_pD3DDev8->GetDeviceCaps(&D3DDevCaps8)) )
  {
    this->g_pD3DDev8 = NULL;
    this->g_pD3D8 = NULL;
		OutputDebugString("DXError : Could not get device capabilities\n");
		return E_FAIL;
  }


    // Set up the textures

    g_pD3DDev8->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    g_pD3DDev8->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    g_pD3DDev8->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
    g_pD3DDev8->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    g_pD3DDev8->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );

    // Set miscellaneous render states

    g_pD3DDev8->SetRenderState( D3DRS_DITHERENABLE,   FALSE );
    g_pD3DDev8->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
    g_pD3DDev8->SetRenderState( D3DRS_ZENABLE,        TRUE );
    g_pD3DDev8->SetRenderState( D3DRS_AMBIENT,        0x00444444 );

    // Set the world matrix

    D3DXMATRIX matIdentity;
    D3DXMatrixIdentity( &matIdentity );
    g_pD3DDev8->SetTransform( D3DTS_WORLD,  &matIdentity );

    // Set the projection matrix

    FLOAT fAspect = ((FLOAT)initWidth) / initHeight;

    // Setup a material

    D3DMATERIAL8 mtrl;
    D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f, 1.0f );
    g_pD3DDev8->SetMaterial( &mtrl );

    // Set up lighting states

    D3DLIGHT8 light;
    D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.1f, -1.0f, 0.1f );
    g_pD3DDev8->SetLight( 0, &light );
    g_pD3DDev8->LightEnable( 0, TRUE );
    g_pD3DDev8->SetRenderState( D3DRS_LIGHTING, TRUE );

	

	MyCamera.SetViewParams(D3DXVECTOR3( 20, 5.0, 0.0), D3DXVECTOR3( 0.0, 0, 0), D3DXVECTOR3( 0, 1, 0));
	
	g_pD3DDev8->SetTransform( D3DTS_VIEW, &MyCamera.GetViewMatrix());

	MyCamera.SetProjParams(D3DX_PI/4, fAspect,0.1f,100.0f);

    g_pD3DDev8->SetTransform( D3DTS_PROJECTION, &MyCamera.GetProjMatrix() );

	//Setup Timer

		// Get the number of counts per second

	QueryPerformanceFrequency( (LARGE_INTEGER*)&g_Frequency );

	// If the frequency is 0 then this system does not have

	// high performance timers

	if( g_Frequency == 0 )
	{
		SetError( "The system does not support high resolution timing" );
		return E_FAIL;
	}

    DXReady = true;
	SetError("DXStartedOK");
    return S_OK;
}
  
ANY help suggestion, would be very appreciated. Many Thank, Gordon. P|-|34R t|-|3 |\|3\/\/|\|3SS

Share this post


Link to post
Share on other sites
You need to clear the Z-buffer, otherwise D3D thinks there is something in front of your model, and doesn''t draw it.

  
pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f ,0);


HTH, Steve

Share this post


Link to post
Share on other sites
Steveyboy .
Thanx for your prompt reply! yep, I knew it would be something stupid.. and I was convinced it was in my init code.
u would not beleive the amout of time ive been starting at the code.

I had the clear in but forgot the zbuffer

  Scene.g_pD3DDev8->Clear( 0, NULL, D3DCLEAR_TARGET, Scene.m_BackgroundColour, 1.0f, 0 );  


Thanks again.

Gordon.

PS How cool is gamedev.net where else would you get a responce in about a hour! Cheers peeps

P|-|34R t|-|3 |\|3\/\/|\|3SS

Share this post


Link to post
Share on other sites