1

I am trying to draw some textures, for example red cube 200x200 with color - D3DCOLOR redtz = D3DCOLOR_RGBA(255, 0, 0, 100), but alpha channel is always 255. I dont know where is my mistake, im newbie in D3D. Thanks in advance. Here's my code:

void CRender::BoxFilled(float x, float y, float w, float h, DWORD color)
    {
        vertex V[4];

        V[0].color = V[1].color = V[2].color = V[3].color = color;

        V[0].z = V[1].z = V[2].z = V[3].z = 0;
        V[0].rhw = V[1].rhw = V[2].rhw = V[3].rhw = 0;

        V[0].x = x;
        V[0].y = y;
        V[1].x = x + w;
        V[1].y = y;
        V[2].x = x + w;
        V[2].y = y + h;
        V[3].x = x;
        V[3].y = y + h;

        unsigned short indexes[] = { 0, 1, 3, 1, 2, 3 };

        m_pD3dDev->CreateVertexBuffer(4 * sizeof(vertex), D3DUSAGE_WRITEONLY, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &g_pVB, NULL);
        m_pD3dDev->CreateIndexBuffer(2 * sizeof(short), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIB, NULL);

        VOID* pVertices;
        g_pVB->Lock(0, sizeof(V), (void**)&pVertices, 0);
        memcpy(pVertices, V, sizeof(V));
        g_pVB->Unlock();

        VOID* pIndex;
        g_pIB->Lock(0, sizeof(indexes), (void**)&pIndex, 0);
        memcpy(pIndex, indexes, sizeof(indexes));
        g_pIB->Unlock();

        m_pD3dDev->SetTexture(0, NULL);
        m_pD3dDev->SetPixelShader(NULL);
        m_pD3dDev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
        m_pD3dDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
        m_pD3dDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

        m_pD3dDev->SetStreamSource(0, g_pVB, 0, sizeof(vertex));
        m_pD3dDev->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
        m_pD3dDev->SetIndices(g_pIB);

        m_pD3dDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);

        g_pVB->Release();
        g_pIB->Release();
    }

and

void fillrgba(int x, int y, int w, int h, DWORD color)
{
    D3DXVECTOR2 vLine[2];

    pLine->SetWidth(w);
    pLine->SetAntialias(false);
    pLine->SetGLLines(true);

    vLine[0].x = x + w / 2;
    vLine[0].y = y;
    vLine[1].x = x + w / 2;
    vLine[1].y = y + h;

    pLine->Begin();
    pLine->Draw(vLine, 2, color);
    pLine->End();
}

everytime alpha is 255

Asesh
  • 3,186
  • 2
  • 21
  • 31
  • What variable is holding the value of alpha? color in fillrbga or BoxFilled? – Asesh Oct 06 '17 at 10:49
  • What kind of texture are you using? Does that texture format support alpha channel? Your question isn't clear enough – Asesh Oct 06 '17 at 10:56
  • for example void fillrgba(int x, int y, int w, int h, DWORD color). i stole that code from some tutorials, but alpha still doesnt work – Марк Колесников Oct 07 '17 at 16:10
  • this is color which i use D3DCOLOR redtz = D3DCOLOR_RGBA(255, 0, 0, 100) – Марк Колесников Oct 07 '17 at 16:12
  • You didn't answer this question: What kind of texture are you using? Does that texture format support alpha channel? – Asesh Oct 07 '17 at 16:14
  • as i said, im newbie, idk about texture. For example look at this source (or screen) https://www.mpgh.net/forum/showthread.php?t=1119047 alpha is working (on screenshot). I tried code from his source, but alpha still doesnt work – Марк Колесников Oct 07 '17 at 16:26
  • Then you should refer to this tutorial: http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-10 – Asesh Oct 07 '17 at 16:34
  • You should use DirectX texture tool to find out if the image you are loading supports alpha channel or not. This tutorial discusses about rendering textures with alpha channel: https://www.codeproject.com/Articles/10849/Rendering-Textures-with-Alpha-Channels-and-Color-K – Asesh Oct 07 '17 at 16:37
  • Why are you using the deprecated Direct3D 9 API instead of say DirectX 11? – Chuck Walbourn Oct 24 '17 at 04:49

0 Answers0