Jump to content
  • Advertisement
Sign in to follow this  
MrDoomMaster

Color format not working?

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

If you intended to correct an error in the post then please contact us.

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

        m_Object->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &m_AdapterInfo);

        // 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.SwapEffect          = D3DSWAPEFFECT_DISCARD;
        d3p.Flags               = dp.LockableBackbuffer ? D3DPRESENTFLAG_LOCKABLE_BACKBUFFER : 0;
        d3p.hDeviceWindow       = m_Window->GetWindowHandle();

        m_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
            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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Source
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.


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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!