   Confused by D3D lighting specifics! PLEASE?
  I'm using a pre-lit terrain, but with D3D lighting enabled for ambient lighting etc.I have my ambient light level coming from the material, and set the D3DRS_AMBIENT renderstate to 0xffffffff (white).Then I store my precalculated direct lighting in the diffuse component of my vertex and tell d3d to get emissive lighting from here (D3DMS_MSCOLOR1 or similar). Finally I have a couple of textures set up normally for multitexturing in the stages.I understood that the texture stages give a final colour per pixel, this is then multiplied by the total lighting Ambient+Direct+Specular+Emissive if D3D lighting is enabled.Thus my textures should be lit by a global ambient colour and a per-vertex emissive colour, so giving the shadows etc which I've pre-calculated into the diffuse colours.But what happens is the Ambient works, and some constant emissive is added for some reason. ie the scene looks like it just has an ambient light.If I add to the texture stages another, multiplying the textures by the same diffuse value as the emissive is supposedly coming from, then I now get all the shadows. But that's really odd IMO. Can anyone suggest what the problem is and how to fix it?Please help I have a couple of stages to add to my engine to make it look a load better & an interview for a game programming job next week![edited by - d000hg on July 31, 2003 9:34:08 AM]
quote:I understood that the texture stages give a final colour per pixel, this is then multiplied by the total lighting Ambient+Direct+Specular+Emissive if D3D lighting is enabledNope. In your texturestagestates, DIFFUSE refers to vertex color0 and vertex color1 only when lighting is disabled.  When lighting is enabled DIFFUSE and SPECULAR refer to the output of D3D lighting.D3D allows you to use material or vertex colors as inputs during it''s calculations, using DIFFUSEMATERIALCOLORSOURCE, SPECULAR..., AMBIENT..., and EMISSIVE... .The diffuse color is the sum of all lights diffuse contributions, ambient contributions, and emissive contributions.  Specular is output seperately, but only if SPECULARENABLE is set to true.This output is clamped between 0 and 1.  Since your ambient is already 1,1,1 (0xFFFFFF), no other lights will affect your scene.  In general you want to keep ambient as low as possible.In general you then modulate (multiply) this light value by your texture.  Use Modulate2X if possible, as it lets you brighten and darken things (128 is normal bright, 255 is double bright).After the texture stages have done their thing, specular is added, and fog is blended, then this color is written to, or mixed with, the backbuffer.;  That makes sense I think but:I want to blend a cloud texture so that the non-ambient light is darkened as a cloud passes overhead. So the lighting is ambient+cloud*direct. But the way you describe it, the light is all added together then stored in the diffuse component, making this impossible? ;  No, it''s not impossible. It''s actually pretty easy.The lighting will be calculated on a per-vertex basis before the actual rendering of the polygon, meaning the cloud-texture you want to use won''t affect the vertex-lighting at all.Do a simple test, skip all the emissive and ambient stuff first, just go with a directional light and a base-texture for the terrain. No emissive, no ambient, none of that.After getting that to work, set the cloud-shadow in the next texturestage, and the blendop to modulate. As long as the color of the cloudshadow is below 1,1,1, it will darken the previous stage. After you''ve gotten that up and going, start using your precalculated lighting and that stuff.In the beginning, keep it simple. Don''t try to implement everything at once, that will only mess with your head. The more variables you have, the more variables you have to wade through once something starts to mess up.;  Well I got it working, the main problem is my card''s (GF2MX) limited set of what you can do and what parameters you can put in what stages!What I''m now doing:Ambient lighting set to zero, emissive being stored in diffuse.Ambient light is actually stored in the TextureFactor renderstate.Stage Zero: (Cloud Texture * Diffuse) + TextureFactorStage One: Current * Ground TextureThis works exactly as you''d hope. Thanks guys for your help, much appreciated. ;  As tok said, it''s not impossible, there are various ways to get the effect.  You could do a second pass with just the texture and do a subtract blend operation.  You could design you texture such that white is full-brightness, and you introduce grey where clouds are, to reduce the overall light (possibly below your ambient level, possibly not).There are usually several ways to go about any given effect using the fixed pipeline, but sometimes it''s quite a hassle trying to come up with something that works in the fixed pipeline.  Vertex Shaders, and Pixel Shaders, open up possibilities.  You can do lighting however you want in a shader.  If you want ambient seperate, write it that way.In this case though, there are several techniques available without having to learn shaders.  Lots of people only vaguely understand how the texturestagestates work, and what each operation does.  I suggest you spend a good chunk of time learning what everything does.  It really pays off.  You''ll think of an effect, and immediately think "hey, I could do that using (some operation) and then doing (some other operation)".  If you don''t really understand how the texture stages work, you''ll waste a lot of time trying different combinations of things, not really understanding why it works, or fails to work.Open the D3DTEXTUREOP entry in the help.  The entire list of operations will be there, with a comment about what the operation does.  Then grab a copy of nVidia''s "blendview". ( http://developer.nvidia.com/object/blendview_dx7_pipelinesetup.html ).  It''s DX7, but most of the operations are there.  Some parts of this program are "very" confusingly done, but it should still help you get an idea of how the states work.Things to look out for in blendview:By default the texture format doesn''t have alpha.Sometimes refuses to load or null a texture.After hitting the 8-stage button (not the stages2-7 button) you''re better off quitting and reloading than trying to get back to anything useful.In the alpha control at the bottom, the slider controls tfactor alpha, or adjusts the texture''s alpha, depending on which check box it hit.  The color box is the tfactor color.When enabling lighting you can''t set the material color sourcesAnyway, I found this a great tool for learning how it all worked.  ATI had some tool where you could see the output of each stage, which was nice, but it supported very few operations, and only 2 stages, and I can''t remember what it was called. 