Jump to content


Lighting Off in DirectX


13 replies to this topic

#1 Faelenor

    Member

  • Members
  • PipPip
  • 44 posts

Posted 07 October 2005 - 01:13 PM

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

#2 moe

    Valued Member

  • Members
  • PipPipPip
  • 270 posts

Posted 07 October 2005 - 04:14 PM

I don’t have all the renderstates in mind right now but you can use material colours instead of vertex colour.

#3 Faelenor

    Member

  • Members
  • PipPip
  • 44 posts

Posted 07 October 2005 - 05:29 PM

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();



#4 Steven Hansen

    Member

  • Members
  • PipPip
  • 86 posts

Posted 07 October 2005 - 05:54 PM

Faelenor said:

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.

#5 Faelenor

    Member

  • Members
  • PipPip
  • 44 posts

Posted 07 October 2005 - 07: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!

#6 Steven Hansen

    Member

  • Members
  • PipPip
  • 86 posts

Posted 07 October 2005 - 09:26 PM

Faelenor said:

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.

#7 roger_hq

    New Member

  • Members
  • Pip
  • 9 posts

Posted 11 October 2005 - 12:30 AM

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...
Deep Blue Future Games
http://www.deepbluefuture.com

#8 Francois Hamel

    Valued Member

  • Members
  • PipPip
  • 86 posts

Posted 11 October 2005 - 01:17 AM


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.
I post with style, do you?
http://fhamel.blogspot.com/

#9 roger_hq

    New Member

  • Members
  • Pip
  • 9 posts

Posted 11 October 2005 - 02:27 PM

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_DIFFUSE)


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.
Deep Blue Future Games
http://www.deepbluefuture.com

#10 Goz

    Senior Member

  • Members
  • PipPipPipPip
  • 574 posts

Posted 11 October 2005 - 02:40 PM

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.

#11 bladder

    DevMaster Staff

  • Moderators
  • 1057 posts

Posted 11 October 2005 - 03:58 PM

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?

#12 roger_hq

    New Member

  • Members
  • Pip
  • 9 posts

Posted 11 October 2005 - 05:10 PM

Arg. That seems to be the case. An article here:

http://msdn.microsof...ts/vformats.asp

Seems to shed light on this topic. I'll try this when I get home tonight. Thanks!
Deep Blue Future Games
http://www.deepbluefuture.com

#13 Francois Hamel

    Valued Member

  • Members
  • PipPip
  • 86 posts

Posted 11 October 2005 - 10:36 PM

bladder said:

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.
I post with style, do you?
http://fhamel.blogspot.com/

#14 roger_hq

    New Member

  • Members
  • Pip
  • 9 posts

Posted 12 October 2005 - 04:44 PM

Yup, that seemed to fix it. Thanks all for your replies!
Deep Blue Future Games
http://www.deepbluefuture.com





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users