Trouble with color with 3D lines

I just learned how to plot 3D lines - I mean actual 3D lines.

The problem I hit is with their colors.

I cannot quite control it - need advice what to read or in case I am missing an "obvious" declaration or something.

I declare the line like:

Verts = new CustomVertex.PositionColored[2];

Verts[0] = new CustomVertex.PositionColored(0, 0, -100, Color.Red.ToArgb());

Verts[1] = new CustomVertex.PositionColored(0, 0, +100, Color.Red.ToArgb());

Then I draw it like:

device.DrawUserPrimitives(PrimitiveType.LineList, 1, Verts);

I noticed that its color - you see up I set it to RED - varies depending on things I do not yet understand - like for example

If I had defined a material for a mesh with yellow - then the line appears in yellow.

There must be some setting that needs to be reset every time right bevore the DrawUserPrimitves command.

On Drunken Hyena I found a project - works fine but the result are 2D lines - goot for some of the syntax - but once you get to 3D and start rendering meshes and all and suddenly your lines have a mind of their own what color the like - huh ...

Many Many thanks



Answer this question

Trouble with color with 3D lines

  • pcompassion

    This way you're not effectively disabling lighting. You must explicitly disable it (as well as change some other states) by changing the values for these states in Device.RenderState. In particular, you're interested in these states: Device.RenderState.Lighting and Device.RenderState.ColorVertex.

  • John Bowen

    Hi Wessam

    Huh ... I see ... But if I disable lighting, how will I see the color of the meshes.

    Let's say you have a single mesh - a red sphere.

    If you have lighting - you see the red ball.

    If I turn off the lighting - how will I see it


  • omasoud

    Disable lighting, and enable the per-vertex color state.

  • Tim Mavers

    D3D works as a state machine. Meaning that you setup your device states (render states, textures, lighting, material, ...etc) first, then you draw objects that use that setup. Then, you change the states again to something else, then draw other objects with the new states and so on... So in your case, you can start by enabling lighting, drawing the red sphere, then disable lighting and drawing the colored lines.

  • MartinMalek

    Wow ... looks beautiful man ... THANK YOU !!!

    Here are the winning lines (between Begin and End Scene)

    device.RenderState.Lighting = false;

    device.RenderState.ColorVertex = true;

    device.VertexFormat = CustomVertex.PositionColored.Format;

    device.DrawUserPrimitives(PrimitiveType.LineList, 1, Verts);

    For all others reading this - please pay attention to "PositionColored".

    One more thing ... I just sacred myself ... all my meshes were suddenly black and I almost decided to ask Wessam "what is wrong - where's the light switch for the meshes "

    It turned that I forgot my lights disabled in the code that defines the light-sources - this before Wessam wrote that it is not enough.

    Once I put

    device.RenderState.Lighting = true;

    device.Lights[0].Enabled = true;

    The whole thing lit up in a beutiful way.

    Wessam - look at the counter man - you made close to a hundred forum readers happy ...


  • Joshizzle

    Wessam, Thanks for the reply (sorry I am a bit late).

    Thanks to you I do understand it better - but still no luck.

    To keep it simple I decided to create a project of just couple of lines of code.

    I declared a single 3D line as shown in my first message above.

    Then I commented out the entire section about lights - all gone.

    Then my Render method has only a couple of lines of code - exactly these here - no more.

    device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Navy, 1.0f, 0);

    device.BeginScene();

    device.VertexFormat = CustomVertex.PositionNormal.Format;

    device.DrawUserPrimitives(PrimitiveType.LineList, 1, Verts);

    device.EndScene();

    device.Present();

    That is all - nothing to it.

    Result - I can see the line in the middle. I can move the camera around it - means it is a true 3D-line.

    But I do not see colors - The line is all black.

    What am I missing in the Vertex declarations related to the device.

    You could test this in a project of yours - just copy paste in it.

    If you see the line colored with the color you declared - then I am definitely missing a declaration or two.

    Many sincere thanks for the help man - I am sure many in this forum will benefit from your answers.

    PS. I also just tried these:

    CustomVertex.PositionNormalColored

    CustomVertex.PositionColored

    CustomVertex.PositionColoredTextured

    CustomVertex.TransformedColored

    Nope -- No luck ...


  • Trouble with color with 3D lines