Mesh Rendering: Z-Buffer and Lighting

This is a discussion on Mesh Rendering: Z-Buffer and Lighting within the Game Programming forums, part of the General Programming Boards category; Hullo again all, I attached an image below of what I'm rendering (an improvement since last time). The top-left image ...

  1. #1
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361

    Mesh Rendering: Z-Buffer and Lighting

    Hullo again all, I attached an image below of what I'm rendering (an improvement since last time).

    The top-left image is rendering with a D3DFILL_SOLID option.

    The top-right is with a D3DFILL_WIREFRAME option. (Note, for the wireframe, I turned culling off).

    The bottom-centre ship is what the mesh viewer included with the DirectX SDK renders (slightly better quality than mine, I'll admit)

    As you can see, the guns should not be being rendered behind the ship, and nor should that cylinder be showing through the top of my ship. I just can't seem to figure out why...

    My Render States include:
    Code:
    	D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
    	D3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
    	D3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
    	D3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
    	D3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
    	D3DDevice->SetRenderState(D3DRS_AMBIENT, 0x00AAAAAA);
    I don't think the following code is the issue, but I'll post it anyways:
    Code:
    //LOADING A MESH OBJECT FROM A .X FILE
    bool cMesh::MeshCreateObject(IDirect3DDevice9* D3DDevice, char* FileName)
    {
    	ID3DXMesh*		TempMesh				= NULL;	
    	ID3DXBuffer*	TempMeshMaterialBuffer	= NULL;
    	D3DXMATERIAL*	TempMeshMaterials		= NULL;
    	unsigned long	X = 0;
    
    	MeshD3DDevice = D3DDevice;
    
    	if(FAILED(D3DXLoadMeshFromX(FileName, D3DXMESH_SYSTEMMEM, D3DDevice, NULL, &TempMeshMaterialBuffer, NULL, &MeshNumMaterials, &TempMesh)))
    		return false;
    
    	TempMeshMaterials = (D3DXMATERIAL*)TempMeshMaterialBuffer->GetBufferPointer();
    
    	MeshMaterials = new D3DMATERIAL9[MeshNumMaterials];
    	MeshTextures  = new IDirect3DTexture9*[MeshNumMaterials];
    
    	for(X = 0; X < MeshNumMaterials; X++)
    	{
    		MeshMaterials[X] = TempMeshMaterials[X].MatD3D;
    
    		MeshMaterials[X].Ambient = MeshMaterials[X].Diffuse;
    
    		if(FAILED(D3DXCreateTextureFromFile(
    			D3DDevice,
    			TempMeshMaterials[X].pTextureFilename,
    			&MeshTextures[X])))
    		{
    			MeshTextures[X] = NULL;
    		}
    	}
    
    	SafeRelease(TempMeshMaterialBuffer);
    
    	TempMesh->CloneMeshFVF(D3DXMESH_MANAGED, MESH_D3DFVF_CUSTOMVERTEX, MeshD3DDevice, &Mesh);
    
    	SafeRelease(TempMesh);
    
    	D3DXComputeNormals(Mesh, NULL);
    
    	return true;
    }
    
    //RENDERING MY MESH
    unsigned long cMesh::MeshRender(void)
    {
    	unsigned long X;
    
    	if(Mesh != NULL)
    	{
    	    for(X = 0; X < MeshNumMaterials; X++)
            {
    	        MeshD3DDevice->SetMaterial(&MeshMaterials[X]);
    	        MeshD3DDevice->SetTexture(0, MeshTextures[X]);
            
    	        Mesh->DrawSubset(X);
    	    }
    
    	    return Mesh->GetNumFaces();
    	}
    
    	return 0;
    }
    Umm...yeah, I don't know what else would be relevant to this problem. I don't get why the Z-Buffer isn't handling the rendering...

    Any changes to culling fuddles it up some more, but the Z-Issue is still there. Are there any ideas about what's going on?


    On another topic
    I was hoping somebody could explain the differences between the Attenuation0, Attenuation1, and Attenuation2 properties of a D3DLIGHT9 object. MSDN gives the same definition for each, and then explains that they each behave differently, without saying how. It has something to do with how the light dies off as it gets further from the source, but I was hoping somebody knew a bit more about them...


    Thanks for givin' this a read. Ideas and comments are always appreciated.
    Attached Images Attached Images  
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Very strange problem. Seems to me the zbuffer should handle this quite easily. Make sure your editor is configured for Direct3D and not OpenGL. In Direct3D Z increases as you move into the screen and in OGL it decreases. If your vertexes are being written incorrectly to disk then when you load them, they won't be correct for Direct3D. This is all I can think of. For an explanation of Attenutation consult the SDK help file. It has a very lengthy section on it and how it computes each one.

    The SDK version looks better because they are doing lighting correctly. It looks as though your one of your quads does not have the correct normals.

  3. #3
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Hmm, I didn't make this model (was sent by a friend), so that could very well be the case. I'll remake the model with my editor (which is setup for Direct3D for sure) and hopefully some good news will pop up

    And SDK, I've gotta learn to go there by habit

    Thanks for the ridiculously quick response
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361

    I may have some issues...

    03.JPG is how 3D Studio Max renders it...not bad eh?

    02.JPG is how the Direct3D SDK Mesh Viewer renders it (After using conv3ds.exe to create a .X file)...slight step downhill...

    01.JPG is how my program renders the .X file...

    I don't know what to say.

    I'll try out Milkshape to see if that gives me any better results, I just thought I'd share the ridiculousness I'm having to put up with
    Attached Images Attached Images    
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Ok then this is most likely a case of the zbuffer either not working correctly or a video card driver issue. It looks to me as if the zbuffer is off even though you have turned it on. I would find the return value from the SetRenderState for the Zbuffer call. Also make sure that when you clear the screen you also specify to clear the ZBuffer as well.

    If you do not clear the ZBuffer then it is like allocating memory but not clearing it to 0. This means that any range of values could be in the ZBuffer, thus causing the odd behavior. It is possible that the current value in the buffer passes the test and/or fails it at certain times. This would result in a very strange and deformed render.

  6. #6
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I was playing a game called "Let's see how many times Bubba tells me to make sure the Z-Buffer is enabled, before I actually enable it".

    Seeing as how I've been suggesting that people start using fflush(), I guess it's fitting I made such a careless mistake here...
    Code:
    D3DPRESENT_PARAMETERS PP;
    ...
    PP.EnableAutoDepthStencil	= FALSE;
    PP.AutoDepthStencilFormat	= D3DFMT_UNKNOWN;
    Has now become:
    Code:
    D3DPRESENT_PARAMETERS PP;
    ...
    PP.EnableAutoDepthStencil	= TRUE;
    PP.AutoDepthStencilFormat	= D3DFMT_D16;
    And voila.

    It's like a demented game where you're trying to get out of a room...filled with leeches...and Oprah...and it's a race against time before you go mad, so you have to find the key to the door, and I just watch you run around frantically while the key's in my pocket. But I keep telling you I don't have pockets.

    Really, thanks for the time you put into this Bubba.
    Attached Images Attached Images  
    Last edited by Epo; 04-19-2005 at 08:51 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    So even though you were turning the ZBuffer on with a render state, your buffer params specified that you did not want to use a zbuffer so the render state function failed in all cases.

    Interesting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL lighting
    By Zishaan in forum Game Programming
    Replies: 6
    Last Post: 04-21-2007, 03:24 PM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. OpenGL lighting problems
    By sirSolarius in forum Game Programming
    Replies: 0
    Last Post: 12-01-2003, 08:11 PM
  5. Using D3DFVF_XYZRHW flag for a Vertex Buffer
    By lightatdawn in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2001, 02:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21