PDA

View Full Version : Lighting Off in DirectX


Faelenor
10-07-2005, 06:13 AM
Hi!

I want to draw a mesh using a unique color with lighting off. I'm used to OpenGL, I know that in OpenGL, I just have to do the following:

glDisable(GL_LIGHTING);
glColor3f(r,g,b);
DrawMesh();

In DirectX, I tried to turn the lighting off, but I don't know how to define the color. In the doc, it seems that we have to define a vertex color for each vertex, but I think that it's a waste of memory, because the color is constant... Do you know how to do that?

Thx,

Francis

moe
10-07-2005, 09:14 AM
I don’t have all the renderstates in mind right now but you can use material colours instead of vertex colour.

Faelenor
10-07-2005, 10:29 AM
Yeah, I know, it's the DiffuseMaterialSource (also Ambient, Specular and Emissive) but it doesn't work. Everything is always white, even if I alpha blend it. That's weird, because I set the material to black with an alpha less than 1 and it's still white.

And also, if the lighting is off, I don't understand what will happen exactly if I set the material source. There should be a unique color setting when the lighting is off, not an ambient, diffuse, specular and emissive, because there's no lighting calculation...

My settings are the following:

device.RenderState.ZBufferFunction = Direct3D.Compare.LessEqual;
device.VertexFormat = Direct3D.CustomVertex.PositionOnly.Format;
device.RenderState.AlphaBlendEnable = true;
device.RenderState.SourceBlend = Direct3D.Blend.SourceAlpha;
device.RenderState.DestinationBlend = Direct3D.Blend.InvSourceAlpha;
device.Material = new Material(); // Default is black
device.RenderState.Lighting = false;
device.RenderState.DiffuseMaterialSource = Direct3D.ColorSource.Material;
device.RenderState.AmbientMaterialSource = Direct3D.ColorSource.Material;
device.RenderState.EmissiveMaterialSource = Direct3D.ColorSource.Material;
device.RenderState.SpecularMaterialSource = Direct3D.ColorSource.Material;
device.RenderState.ColorVertex = true;
DrawMesh();

Steven Hansen
10-07-2005, 10:54 AM
Hi!

In DirectX, I tried to turn the lighting off, but I don't know how to define the color. In the doc, it seems that we have to define a vertex color for each vertex, but I think that it's a waste of memory, because the color is constant... Do you know how to do that?
Francis

You weren't exceptionally clear on what you wanted, but assuming I've figured it out, here's the deal.

First, if you turn off lighting, there are no lights - no materials, etc. No need to set any of that stuff, and it will confuse you if you do.

If you want all faces to be a particular color, then you can use a TFactor. I'm not exactly sure how this is done in MDX (C#) but in C++, it looks something like the following:


Device->SetRenderState(D3DRS_TEXTUREFACTOR, (DWORD) colorYouWant);
Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
Device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);


That is the FFP version anyway...

Additionally, you could write some vertex shaders and then write the colors into the shader even though it wasn't stored in the stream.

Faelenor
10-07-2005, 12:44 PM
Thanks! That was what I was looking for. I just had to add the same code for alpha operation and argument and it worked!

Steven Hansen
10-07-2005, 02:26 PM
Thanks! That was what I was looking for. I just had to add the same code for alpha operation and argument and it worked!

:yes: Cool.

roger_hq
10-10-2005, 05:30 PM
Hi,

I'm having a similar problem, but I'm using per-vertex colors and I want to see the per-vertex colors. So lets say I've previously rendered a mesh with a material with lights on, and then I want to render a vertex buffer with no lights using per-vertex colors (I've created the vertex buffer with the FVF having a color component). I can't seem to get this to work at all. I turn the lighting off and I have played with DiffuseMaterialSource, AmbientMaterialSource, etc. to no avail. My mesh just ends up being white or black or anything other than what the vertex colors suggest. Any suggestions on how to play with render states in order to get the unlit mesh to be displayed with per-vertex colors?

Thanks...

Francois Hamel
10-10-2005, 06:17 PM
pDevice->SetRenderState(D3DRS_COLORVERTEX, TRUE);
pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);

pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
pDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);



that should do it, IF you have indeed some vertex colors in your vertex buffer.

D3DTA_DIFFUSE is the result of the lighting equation if D3DRS_LIGHTING is activated. Else it's just the vertex color itself.

roger_hq
10-11-2005, 07:27 AM
Hrm... I've tried that, it doesn't seem to work. My vertices are just black, regardless of what I set the vertex colors to. Could there be something wrong with my FVF and my vertex definition?


#define D3DFVF_COLOR_VERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1|D3DFVF_DIFFU SE)

struct D3D9COLORVERTEX
{
D3DXVECTOR3 p;
D3DXVECTOR3 n;
FLOAT tu, tv;
DWORD c;
};


I'm wondering if its wrong to have normals in there if you want to use per-vertex colors. I know normals are only used for lighting, but it would be nice to have a generic vertex type that one could use lit or unlit and get the desired effect.

Goz
10-11-2005, 07:40 AM
Well the normal will just get ignored but i think you have colour in the wrong place ... the colour should come after the normal and before the uv.

bladder
10-11-2005, 08:58 AM
yeah that's what it might be. When using the FFP (Fixed Function Pipeline - When you're not using vertex/pixel shaders then vertices go down through the FFP), D3D expects a specific order in the vertices :-

- XYZ or XYZRHW
- PointSize (not sure of this - may be after the Normal)
- Normal
- Diffuse
- Specular
- TEXn

Though I *think* with newer cards that implement the FFP using the programmable pipeline the order doesnt really matter. Does anyone know if this is true or not?

roger_hq
10-11-2005, 10:10 AM
Arg. That seems to be the case. An article here:

http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp

Seems to shed light on this topic. I'll try this when I get home tonight. Thanks!

Francois Hamel
10-11-2005, 03:36 PM
Though I *think* with newer cards that implement the FFP using the programmable pipeline the order doesnt really matter. Does anyone know if this is true or not?

it shouldn`t matter theoretically, but unfortunatly in really it does matter. Some cards just *need* the correct order or some specific combinaisons that map directly to FVFs.

I just happenned to hit a bug today with an NVIDIA card related to vertex declarations (DX9 instead of regular FVF). I was using a 4 float position and even though I specified that those positions weren`t pre-transformed, it acted as if I had the FVF_XYZRHW flag or something. Changing the vertex declaration to 3 floats solved the problem.

An ATI card I had previously just didn`t have this problem, but maybe some older cards do like we had many problems regarding vertex declarations orders with an ATI 7500 using the FFP.

roger_hq
10-12-2005, 09:44 AM
Yup, that seemed to fix it. Thanks all for your replies!