Question

For some reason beyond my understanding, my 3D scene disappears when I call

this->sp->Begin(D3DX10_SPRITE_SORT_TEXTURE);

Where this->sp is LPD3DX10SPRITE.

to render my 2D scene. My 3D scene also disappears when I call

RECT rc = {5, 5, 0, 0};
this->TR.Font->DrawText(
    NULL, 
    SS.str().c_str(), 
    -1, 
    &rc, 
    DT_NOCLIP, 
    D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f));

Where this->TR.Font is ID3DX10Font.

Should I be using a ID3D10RenderTargetView for each scene and then combine at the end of my draw function?

If it is any help, here is Draw() function.

void Draw()
    {
        this->GDM.Clear(Color(100, 149, 237));

        this->GDM.Device->RSSetState(this->GDM.RasterizerState);
        this->GDM.Device->OMSetBlendState(this->GDM.BlendState, 0, 0xffffffff);
        this->GDM.EnableZBuffer(true);

        static float r;
        D3DXMATRIX w;

        D3DXMatrixIdentity(&w);
        //D3DXMatrixRotationY(&w, r);
        r += 0.01f * (3.14f / 180.0f);// * (float)Time->Scalar;


        this->effect.WorldMatrix->SetMatrix(w);
        this->effect.ViewMatrix->SetMatrix(viewMatrix);
        this->effect.ProjectionMatrix->SetMatrix(projectionMatrix);

        Vertex_PosCol * v = NULL;

        this->VertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**) &v);

        v[0] = Vertex_PosCol(D3DXVECTOR3(-1,-1,0), D3DXVECTOR4(1,0,0,1));
        v[1] = Vertex_PosCol(D3DXVECTOR3(0,1,0), D3DXVECTOR4(0,1,0,1));
        v[2] = Vertex_PosCol(D3DXVECTOR3(1,-1,0), D3DXVECTOR4(0,0,1,1));

        this->VertexBuffer->Unmap();

        this->GDM.Device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
        //this->GDM.Device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINESTRIP);

        D3D10_TECHNIQUE_DESC techDesc;
        this->effect.Technique->GetDesc( &techDesc );

        for (int p = 0; p < techDesc.Passes; p++)
        {
            this->effect.Technique->GetPassByIndex( p )->Apply( 0 );
            this->GDM.Device->Draw(3, 0);
        }

        this->GDM.EnableZBuffer(false);
        this->sBatch->Begin();

        if (loaded)
        {
            this->sBatch->Draw(
                s1,
                this->Input->Mouse.Position.x,
                this->Input->Mouse.Position.y,
                200,
                200,
                45.0f * (3.14f / 180.0f));
            this->sBatch->Draw(s2, x, y, 200, 200);
        }

        this->sBatch->End();

        SS << "Graphics Test And Development" << "\n";
        SS << "FPS: " << this->Time->FPS << "\n";
        SS << "X: " << x << "\n";
        SS << "Y: " << y << "\n";

        RECT rc = {5, 5, 0, 0};

        this->TR.RenderText(this->SS.str().c_str(), 5, 5, &Color(0.0f, 1.0f, 0.0f, 1.0f));

        this->SS.str("");
    }
Was it helpful?

Solution

As Mentioned in the comments above, setting the D3DX10_SPRITE_SAVE_STATE solved the problem as far as rendering 2D with 3D. It however did not stop DrawText from removing the 3D scene. Years ago I had this working so i spent hour trying to find my old code. Sadly I could not but i did manage to track down the tutorial I followed at that time.

Here is the solution:

Instead of creating and and discarding the vertices every frame the way I was:

this->VertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**) &v);

v[0] = Vertex_PosCol(D3DXVECTOR3(-1,-1,0), D3DXVECTOR4(1,0,0,1));
v[1] = Vertex_PosCol(D3DXVECTOR3(0,1,0), D3DXVECTOR4(0,1,0,1));
v[2] = Vertex_PosCol(D3DXVECTOR3(1,-1,0), D3DXVECTOR4(0,0,1,1));

this->VertexBuffer->Unmap();

I created a Vertex and Index buffer in the Load() function:

    UINT VerticeCount = 3;
    ULONG IndexCount = 3;

    Vertex_PosCol * vertices = NULL;
    vertices = new Vertex_PosCol[VerticeCount];

    vertices[0].Position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);  // Bottom left.
    vertices[0].Color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);

    vertices[1].Position = D3DXVECTOR3(0.0f, 1.0f, 0.0f);  // Top middle.
    vertices[1].Color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);

    vertices[2].Position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);  // Bottom right.
    vertices[2].Color = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);

    unsigned long * indices = new unsigned long[IndexCount];

    indices[0] = 0;  // Bottom left.
    indices[1] = 1;  // Top middle.
    indices[2] = 2;  // Bottom right.

    D3D10_BUFFER_DESC vbd;
    vbd.Usage = D3D10_USAGE_DEFAULT;
    vbd.ByteWidth = sizeof(Vertex_PosCol) * VerticeCount;
    vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;

    D3D10_SUBRESOURCE_DATA vertexData;
    vertexData.pSysMem = vertices;

    HRESULT result = this->GDM.Device->CreateBuffer(&vbd, &vertexData, &VertexBuffer);

    UINT stride = sizeof( Vertex_PosCol );
    UINT offset = 0;

    if(FAILED(result))
    {
        bool b = false;
    }

    D3D10_BUFFER_DESC ibd;
    ibd.Usage = D3D10_USAGE_DEFAULT;
    ibd.ByteWidth = sizeof(unsigned long) * IndexCount;
    ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;

    D3D10_SUBRESOURCE_DATA indexData;
    indexData.pSysMem = indices;

    result = this->GDM.Device->CreateBuffer(&ibd, &indexData, &IndexBuffer);
    if(FAILED(result))
    {
        bool b = false;
    }

    delete [] vertices;
    delete [] indices;

then in my Draw() function I set the vertex and index buffers and the primitive topology as well as the input layout for that specific buffer. Then I draw the 3D scene and then the text. Here is the Draw() function code:

void Draw()
{
    this->GDM.Clear(Color(100, 149, 237));

    this->GDM.Device->RSSetState(this->GDM.RasterizerState);
    this->GDM.Device->OMSetBlendState(this->GDM.BlendState, 0, 0xffffffff);
    this->GDM.EnableZBuffer(true);

    static float r;
    D3DXMATRIX w;

    D3DXMatrixIdentity(&w);
    //D3DXMatrixRotationY(&w, r);
    r += 0.01f * (3.14f / 180.0f);// * (float)Time->Scalar;


    this->effect.WorldMatrix->SetMatrix(w);
    this->effect.ViewMatrix->SetMatrix(viewMatrix);
    this->effect.ProjectionMatrix->SetMatrix(projectionMatrix);


    UINT stride = sizeof(Vertex_PosCol);
    UINT offset = 0;

    this->GDM.Device->IASetVertexBuffers(0, 1, &this->VertexBuffer, &stride, &offset);
    this->GDM.Device->IASetIndexBuffer(this->IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
    this->GDM.Device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
    //this->GDM.Device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINESTRIP);

    this->GDM.Device->IASetInputLayout(Vertex_PosCol::GetInputLayout());

    D3D10_TECHNIQUE_DESC techDesc;
    this->effect.Technique->GetDesc( &techDesc );

    for (int p = 0; p < techDesc.Passes; p++)
    {
        this->effect.Technique->GetPassByIndex( p )->Apply( 0 );
        this->GDM.Device->DrawIndexed(3,0,0);
    }

    this->GDM.EnableZBuffer(false);
    this->sBatch->Begin();

    if (loaded)
    {
        this->sBatch->Draw(
            s1,
            this->Input->Mouse.Position.x,
            this->Input->Mouse.Position.y,
            200,
            200,
            45.0f * (3.14f / 180.0f));
        this->sBatch->Draw(s2, x, y, 200, 200);
    }

    this->sBatch->End();

    SS << "Graphics Test And Development" << "\n";
    SS << "FPS: " << this->Time->FPS << "\n";
    SS << "X: " << x << "\n";
    SS << "Y: " << y << "\n";

    RECT rc = {5, 5, 0, 0};

    this->TR.RenderText(this->SS.str().c_str(), 5, 5, &Color(0.0f, 1.0f, 0.0f, 1.0f));

    this->SS.str("");
}

I spent far longer on this than I should have because of stupid mistakes. When i created the bufferes, I created a D3D10_SUBRESOURCE_DATA object for each the vertex and index buffers but I forgot to set them and as a result, I was getting no 3D scene at all.

With the current 3D scene rendering method that I am using, D3DX10_SPRITE_SAVE_STATE as a flag in LPD3DX10SPRITE.Begin() is not necessary as far as I can tell but I am going to keep it regardless unless it happens to bring up any performance issues but I doubt that that would ever be the case.

Anyway, I do realize that a simple "hey, I changed this and it works" may have been enough to suffice as an answer but I wanted to be thorough. Hopes this helps someone!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top