• Advertisement
Sign in to follow this  

Z-buffer problem

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

this is my object class and some member functions:
class jObject {
public:
	bool MeshFromX(std::wstring filename, IDirect3DDevice9* &Device);
	void render(IDirect3DDevice9* &Device);
	void release();
	D3DXMATRIX Matrix;
private:
	ID3DXMesh*                      dxMesh;
	std::vector<D3DMATERIAL9>       dxMaterials;
	std::vector<IDirect3DTexture9*> dxTextures;
};

bool jObject::MeshFromX(std::wstring filename, IDirect3DDevice9* &Device)
{
	HRESULT hr = 0;

	// Load the XFile data.

	ID3DXBuffer* adjBuffer  = 0;
	ID3DXBuffer* mtrlBuffer = 0;
	DWORD        numMtrls   = 0;

	hr = D3DXLoadMeshFromX(  
		filename.c_str(),
		D3DXMESH_MANAGED,
		Device,
		&adjBuffer,
		&mtrlBuffer,
		0,
		&numMtrls,
		&dxMesh);

	if(FAILED(hr))
	{
		::MessageBox(0, L"D3DXLoadMeshFromX() - FAILED", 0, 0);
		return false;
	}

	// Extract the materials, and load textures.

	if( mtrlBuffer != 0 && numMtrls != 0 )
	{
		D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();

		for(uint i = 0; i < numMtrls; i++)
		{
			// the MatD3D property doesn't have an ambient value set
			// when its loaded, so set it now:
			mtrls.MatD3D.Ambient = mtrls.MatD3D.Diffuse;

			dxMaterials.push_back( mtrls.MatD3D );

			if( mtrls.pTextureFilename != 0 )
			{
				IDirect3DTexture9* tex = 0;
				D3DXCreateTextureFromFile(
					Device,
					MultiCharToUniChar(mtrls.pTextureFilename).c_str(),
					&tex);
				dxTextures.push_back( tex );
			}
			else
			{
				dxTextures.push_back( 0 );
			}
		}
	}
	mtrlBuffer->Release(); // done w/ buffer

	// Optimize the mesh.

	hr = dxMesh->OptimizeInplace(		
		D3DXMESHOPT_ATTRSORT |
		D3DXMESHOPT_COMPACT  |
		D3DXMESHOPT_VERTEXCACHE,
		(DWORD*)adjBuffer->GetBufferPointer(),
		0, 0, 0);

	adjBuffer->Release(); // done w/ buffer

	if(FAILED(hr))
	{
		::MessageBox(0, L"OptimizeInplace() - FAILED", 0, 0);
		return false;
	}
	return true;
}

void jObject::render(IDirect3DDevice9* &Device)
{
	Device->SetTransform(D3DTS_WORLD, &Matrix);
	for(uint i = 0; i < dxMaterials.size(); i++)
	{
		Device->SetMaterial( &dxMaterials );
		Device->SetTexture(0, dxTextures);
		dxMesh->DrawSubset(i);
	}
}

void jObject::release()
{
	if(dxMesh) dxMesh->Release();

	if(!dxTextures.empty())
		for(uint i = 0; i < dxTextures.size(); i++)
			if(dxTextures) dxTextures->Release();
}

before the main loop, I initialize the objects: Floor.MeshFromX(L"floor.x", pd3dDevice); Crate.MeshFromX(L"crate.x", pd3dDevice); And in the main rendering callback I just use the class' rendering functions: Floor.render(pd3dDevice); Crate.render(pd3dDevice); This is the result:
">video Never mind the simulation.. Notice the glitches.. And notice also that I put the rendering for crate last.. well, that's because if switch the order of Floor and Crate.render(), the floor would overwrite the crate even though spatially the crate should be drawn.. That's why my guess is that it's a depth buffer issue.. not really a genius revelation, but I can't see the source of the problem or the solution..

Share this post


Link to post
Share on other sites
Advertisement
What format is your depth buffer? D3DFMT_D16 is pretty low precision and would cause issues like this.

Also, what are your near and far clip planes set to? You really want the near clip plane at > 0.1f (The further the better) and the far clip plane at < 10000.0f (The nearer the better).

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
What format is your depth buffer? D3DFMT_D16 is pretty low precision and would cause issues like this.

Also, what are your near and far clip planes set to? You really want the near clip plane at > 0.1f (The further the better) and the far clip plane at < 10000.0f (The nearer the better).


yeah, I had my near clipping plane at 0.0.. as soon as I changed to 0.1 everything turned OK..

and I was just getting warmed up for a major code revision.. :P

tnx.. again..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement