Lighting a Direct3D 9 mesh sphere

This is a discussion on Lighting a Direct3D 9 mesh sphere within the Game Programming forums, part of the General Programming Boards category; I'm lighting a sphere as described in the title with diffuse and specular lighting. I have used, individually and in ...

Lighting a Direct3D 9 mesh sphere

I'm lighting a sphere as described in the title with diffuse and specular lighting. I have used, individually and in conjunction, directional and point lighting, but each time I try, a strange effect occurs. The way the sphere vertices are lit depends on the angle I am looking at it from, and if you could see this you would appreciate how ridiculous it looks. Has anyone succeeded in lighting a mesh sphere before?

There is an easier way to compute the normals on a sphere. Since all points are equidistant from the center the normal will always be the normalized distance between the vertex on the sphere and the center of the sphere. Since your sphere's are centered around 0,0,0 in model space you can simply normalize the Vertex to get the normal. You are essentially then clamping the vertex to the surface of a unit sphere.

Note that this is per vertex normals, not per triangle face. So this sets your system up quite nicely to do some per-pixel dot3 lighting in a shader.

Code:

mSphere=Mesh.Sphere(device,1.0f,32,16);
mSphere.ComputeNormals();

I wouldn't do it that way. There is a possibility that even though your code works your sphere object might be going out of scope upon returning from Mesh::Sphere(). Also you might be creating a COM resource leak.

Thanks heaps for the code, I haven't found anything nearly as good on the net. Unfortunately it's a bit above my level so it will take some time to work out.

As for the scope and resource leak issues, remember I'm using C#, everything should technically be managed for me. I think I will however go back to using C++. The only reason I was using C# was to learn a .NET language, but I do sorely miss the satisfaction of good C++ code.

The light is fixed and the camera is moving around in a circle. Maybe the pictures don't illustrate the problem, but believe me if you ran the program you would see what I mean. I can post the binary if you trust me not to infect you.

Anyways if your camera is moving around the object and the object is part of your world then it is really the object that is moving, not the camera. Your calculations must be in world space not view space. It seems to me that they are in view space.

The camera is moving in space and rotating but to show this you really must rotate the world and move the world, not the camera. You always translate the camera back to 0,0,0 or the origin of the world since the camera is assumed to always be the origin of your world. Everything else is relative to the camera.