Gouraud Shading

Started by
2 comments, last by Lythic 21 years, 6 months ago
Hi all, I have a shading problem. In my application I am rendering a sphere, and I have a point light rotating around it. It works as expected except for one thing... the sphere mesh I am using does not have a sufficient enough poly count as to hide the unsightly shading as the light rotates around. It performs semi-smooth / jumpy shading as the light moves around the sphere. I've read up about this, and from what I understand, my mesh has vertex normals which the D3D lighting engine uses to determine the rendered color of the mesh faces (I have not set up any SHADEMODE render states, so it uses the default of D3DSHADE_GOURAUD). It seems to me that this is where my problem lies. I've read up about Gouraud shading, which appears to use these vertex normals to calculate shading. If I understand correctly, my mesh does not have those normals defined in such a way to create a smooth shading across face-edges... each triangle seems to have each vertex normal the same as the face normal vector. Anyway, the point of my post is this: If my assumptions above are correct (I'd sincerely appreciate correction if not), is there an accepted/well-understood method to generate the vertex normals I need? I'd appreciate any help which would put me on the right track. Thanks. [edited by - Lythic on September 30, 2002 7:03:23 PM] [edited by - Lythic on September 30, 2002 7:05:00 PM]
Advertisement
Try using D3DXComputeNormals if your mesh is stored in an ID3DXMesh object. Also, try lowering your light''s attentuation0 to something like 0.5 or 0.2 and see if it makes it smoother.



Jim Adams
home.att.net/~rpgbook
Author, Programming Role-Playing Games with DirectX
and Focus On: Advanced Animation with DirectX

Test


  Real reply to come  
Hi Jim,

Thanks for your response. I was sure that your suggestions would drive this issue home. It made perfect sense, but I encountered some unexpected results that I was hoping someone might be able to clear up. I'll summarise the events:

"Try using D3DXComputeNormals if your mesh is stored in an ID3DXMesh object" - I was sure this would fix the problem, as I felt from the visuals that the vertex normals in my mesh were not averaged with the adjacent faces.

Reading up about D3DXComputeNormals, it takes 2 params, and seems to solve specifically the problem I thought I had. Unfortunately, my mesh was not a ID3DXMesh object. I converted it in a few minutes, and implemented the call to D3DXComputeNormals. Here is my code:


    HRESULT hr;LPD3DXBUFFER pAdjacencyBuffer = NULL;hr = D3DXLoadMeshFromX( "sphere.x", D3DXMESH_SYSTEMMEM, m_pd3dDevice, &pAdjacencyBuffer, NULL, NULL, &m_pMesh );if(FAILED(hr))    return hr;hr = D3DXComputeNormals( m_pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer() )if(FAILED(hr))    return hr;    


This executed without any (perceived) problems or exceptions, and rendered the mesh... but in exactly the same way as before. There seemed to be no difference at all to the shading of te rendered sphere. Still not smooothly shaded.

Viewing this sphere mesh in "Mesh Viewer" I see a smooth-shaded rendered sphere... so I know it's possible with this specific mesh, but I don't understand why computing the vertex normals didn't work. Everything I have read indicates that this is the cause. In fact, I still think it is the cause, and my opinion now is that somehow D3DXComputeNormals is not successfully computing the vertex normals (Insanity is creeping up on me).

When I tried this initially, I had the second parameter of D3DXComputeNormals as NULL, which also resulted in a rendered non-smooth shaded sphere. I guessed that the second parameter was needed (a buffer indicating adjacent vertices/faces), and the SDK documentation seemed to say the same thing. I googled the D3DXComputeNormals, and could not find any more information other than what the SDK had already stated.

I played with the lighting attenuation0 but any value betwee 0.0 and 1.0 has no effect. attenuation1 seems to have an effect on the shading, but nothing to fix the issue.

I am convinced that a smoothly shaded sphere can be rendered without incurring insane poly counts, but it just is not happening.

I would be grateful if someone could point me in the right direction.

Once again, thanks for the help.

[edited by - Lythic on October 1, 2002 3:21:00 PM]

This topic is closed to new replies.

Advertisement