Color format not working?

This topic is 4482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey everyone, I am creating a grid using a vertex buffer and D3DPT_LINELIST. I set each vertex color to: D3DCOLOR_XRGB(0, 50, 50), the line appears dark green D3DCOLOR_XRGB(0, 50, 150), the line appears the EXACT same color as (0,50,50) D3DCOLOR_XRGB(0, 10, 100), the line is COMPLETELY black. As you can see, my colors are not displaying properly at all. I am using 24-bit color on my desktop and 32 bit color in fullscreen mode, as you'll see below. Lighting is disabled to test the purity of the colors. These invalid color displays occur in both fullscreen AND windowed mode. Here is how I have initialized my device:
        m_Object = Direct3DCreate9(D3D_SDK_VERSION);

// Create Device
D3DPRESENT_PARAMETERS d3p = {0};

if(dp.EnableDepth)
{
d3p.EnableAutoDepthStencil = TRUE;
d3p.AutoDepthStencilFormat = D3DFMT_D24X8;
}

m_Window->GetClientSize(m_Resolution);

d3p.BackBufferFormat    = dp.Fullscreen ? D3DFMT_A8R8G8B8 : m_AdapterInfo.Format;
d3p.Windowed            = dp.Fullscreen ? FALSE : TRUE;
d3p.BackBufferWidth     = m_Resolution.x;
d3p.BackBufferHeight    = m_Resolution.y;
d3p.Flags               = dp.LockableBackbuffer ? D3DPRESENTFLAG_LOCKABLE_BACKBUFFER : 0;
d3p.hDeviceWindow       = m_Window->GetWindowHandle();

m_Window->GetWindowHandle(), D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3p, &m_Device);

if(dp.EnableDepth)
{
// Initialize View Volume
D3DVIEWPORT9 vp;

vp.X        = 0;
vp.Y        = 0;
vp.Width    = m_Resolution.x;
vp.Height   = m_Resolution.y;
vp.MinZ     = 0.0f;
vp.MaxZ     = 1.0f;

m_Device->SetViewport(&vp);

// Initialize Projection Matrix
D3DXMATRIX pm;

float Aspect = (float)m_Resolution.x / m_Resolution.y;
float FOV = PI / 4;

D3DXMatrixPerspectiveFovLH(&pm, FOV, Aspect, 1.0f, 1000.0f);

m_Device->SetTransform(D3DTS_PROJECTION, &pm);
}

// Render States
m_Device->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(60, 60, 60));
m_Device->SetRenderState(D3DRS_LIGHTING, (dp.EnableLighting && dp.EnableDepth) ? TRUE : FALSE);
m_Device->SetRenderState(D3DRS_CULLMODE, (dp.EnableCulling) ? D3DCULL_CCW : D3DCULL_NONE);
m_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

// Texture States
m_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);


I also display a mesh with a texture with this set of lines, and the mesh displays PERFECTLY! So, if the LINES are not coloring properly but the MESH is, this might have something to do with the vertex buffer, right? Here is my vertex format and vertex buffer initialization:
            // Vertex Format
#define FVF_3D (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1)

// Vertex Structure
struct Vertex3D
{
float x;                                ///< Vertex X-Coordinate
float y;                                ///< Vertex Y-Coordinate
float z;                                ///< Vertex Z-Coordinate
float nx;                               ///< Vertex Normal X-Coordinate
float ny;                               ///< Vertex Normal Y-Coordinate
float nz;                               ///< Vertex Normal Z-Coordinate
D3DCOLOR Color;                         ///< Vertex Color (Blend)
float tu;                               ///< U Texture Coordinate
float tv;                               ///< V Texture Coordinate
};

// Vertex Buffer Initialization
m_Device->CreateVertexBuffer(
sizeof(Vertex3D) * NumVertices, D3DUSAGE_WRITEONLY,
FVF_3D, D3DPOOL_DEFAULT, &m_VertexBuffer, 0);


If you don't see the error here, what else can I check? Is there more source code you would like me to post? Keep in mind that lighting IS disabled and depth IS enabled.

Share on other sites
Just a quick questions...

If you want colored vertices, why do you have a normal?

Share on other sites
I need to have some sort of "global" vertex format, because my library can't afford to have so many different ones. I figure a normal is not harmful if created for every vertex, and is especially not harmful when lighting is disabled. Would anyone disagree with this?

In any case, I still would like to know why I'm having color problems. Thank you for your time and help!

Share on other sites
Try taking the normal off of the vertex structure and from the fvf code. Some drivers might not like diffuse and normal components in same vertex when in fixed-function mode.

Also, show us the part where you are filling the vertex buffer (assigning the colors to the vertices, to be exact).

Share on other sites
Do you get the same results by using D3DCOLOR_ARGB(255,R,G,B)? Also - make sure lighting is DISABLED (doesn't really work with line primitives).

Share on other sites
If I remember correctly, D3D parses FVF color as a DWORD, while D3DCOLOR is a struct with 3 floats in it.

struct Vertex3D    {        float x;                                ///< Vertex X-Coordinate        float y;                                ///< Vertex Y-Coordinate        float z;                                ///< Vertex Z-Coordinate        float nx;                               ///< Vertex Normal X-Coordinate        float ny;                               ///< Vertex Normal Y-Coordinate        float nz;                               ///< Vertex Normal Z-Coordinate        DWORD Color;                         ///< Vertex Color (Blend)        float tu;                               ///< U Texture Coordinate        float tv;                               ///< V Texture Coordinate    };

That should work.

Share on other sites
From the SDK:

Quote:
 typedef DWORD D3DCOLOR

Have you tried it with texture set to NULL?

Share on other sites
Quote:
Original post by Source
From the SDK:

Quote:
 typedef DWORD D3DCOLOR

Have you tried it with texture set to NULL?

This was definitely the problem, I didn't set the texture to NULL after drawing my mesh.

Also, I've been wanting my lines to work with lighting enabled. This way, if I scan a point light across my lines, only the part of the line receiving light will emit. My grid is along the X-Z plane, and my directional light source is pointing at (-1, -1, -1). Any advice?

Share on other sites
Well, take into account that fixed-function (per-vertex) lighting will only work on the vertexes, so scanning a point-light across a long line will have no effect whatsoever on the line centre.

If you set the normals up correctly then there is no real reason why lighting should fail on line primitives. If all of the line point normals are in the opposite direction to your light direction (ie [1,1,1]) then they will always be lit - unless they fall outside of the light's range.

Also remember that [1,1,1] is not actually normalised - so this may give strange results.

To get proper lighting on a line you will probably have to resort to using a fragment shader to achieve simple per-pixel lighting.

Share on other sites
Quote:
 Original post by SourceWell, take into account that fixed-function (per-vertex) lighting will only work on the vertexes, so scanning a point-light across a long line will have no effect whatsoever on the line centre.If you set the normals up correctly then there is no real reason why lighting should fail on line primitives. If all of the line point normals are in the opposite direction to your light direction (ie [1,1,1]) then they will always be lit - unless they fall outside of the light's range.Also remember that [1,1,1] is not actually normalised - so this may give strange results.To get proper lighting on a line you will probably have to resort to using a fragment shader to achieve simple per-pixel lighting.

Well I have never heard of fragment shaders, so if you could point me to a tutorial or something I would really appreciate it. Thank you very much for your help!

Share on other sites
Quote:
Original post by MrDoomMaster
Quote:
 Original post by SourceWell, take into account that fixed-function (per-vertex) lighting will only work on the vertexes, so scanning a point-light across a long line will have no effect whatsoever on the line centre.If you set the normals up correctly then there is no real reason why lighting should fail on line primitives. If all of the line point normals are in the opposite direction to your light direction (ie [1,1,1]) then they will always be lit - unless they fall outside of the light's range.Also remember that [1,1,1] is not actually normalised - so this may give strange results.To get proper lighting on a line you will probably have to resort to using a fragment shader to achieve simple per-pixel lighting.

Well I have never heard of fragment shaders, so if you could point me to a tutorial or something I would really appreciate it. Thank you very much for your help!

I'm not 100% sure of the details, but "fragment shaders" are OpenGL's equivalent of Direct3D's "Pixel Shaders". Check out Pixel Shader Basics in the documentation if you've not come across them yet [smile]

hth
Jack

Share on other sites
Yes I'm sorry i've been doing some cross-API work recently, so I just tend to refer to all per-pixel programs as fragment programs/shaders - Direct3D calls them pixel shaders, though i'm sure using them for the results you probably want is overkill - especially if you've never used them before.

Share on other sites
Quote:
 Also, I've been wanting my lines to work with lighting enabled. This way, if I scan a point light across my lines, only the part of the line receiving light will emit. My grid is along the X-Z plane, and my directional light source is pointing at (-1, -1, -1). Any advice?

Do you want the grid always to be lit , no matter whats the light status is ?
Cause if so, you can turn off light before rendering the grid, and then enable it exactly after. (though, I don't know if disabling and enabling light slows things or not)