简体   繁体   English

DirectX 10 3D场景随着2D绘图消失

[英]DirectX 10 3D scene disappears with 2D drawing

For some reason beyond my understanding, my 3D scene disappears when I call 由于某些我无法理解的原因,当我打电话时,我的3D场景消失了

this->sp->Begin(D3DX10_SPRITE_SORT_TEXTURE);

Where this->sp is LPD3DX10SPRITE. 其中this-> sp是LPD3DX10SPRITE。

to render my 2D scene. 渲染我的2D场景。 My 3D scene also disappears when I call 当我打电话时,我的3D场景也消失了

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. 其中-> TR.Font是ID3DX10Font。

Should I be using a ID3D10RenderTargetView for each scene and then combine at the end of my draw function? 我应该为每个场景使用一个ID3D10RenderTargetView,然后在我的绘图函数的末尾合并吗?

If it is any help, here is Draw() function. 如果有帮助,这里是Draw()函数。

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("");
    }

As Mentioned in the comments above, setting the D3DX10_SPRITE_SAVE_STATE solved the problem as far as rendering 2D with 3D. 如上面的注释所述,设置D3DX10_SPRITE_SAVE_STATE解决了使用3D渲染2D的问题。 It however did not stop DrawText from removing the 3D scene. 但是,它并没有阻止DrawText删除3D场景。 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: 我在Load()函数中创建了一个顶点和索引缓冲区:

    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. 然后在Draw()函数中设置顶点和索引缓冲区以及原始拓扑以及该特定缓冲区的输入布局。 Then I draw the 3D scene and then the text. 然后绘制3D场景,然后绘制文本。 Here is the Draw() function code: 这是Draw()函数代码:

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. 创建缓冲区时,我为每个顶点和索引缓冲区都创建了一个D3D10_SUBRESOURCE_DATA对象,但由于忘记设置它们,结果根本没有3D场景。

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. 使用我当前使用的3D场景渲染方法,据我所知,D3DX10_SPRITE_SAVE_STATE作为LPD3DX10SPRITE.Begin()中的标志是不必要的,但我会保留它,除非它碰巧引起任何性能问题,但是我怀疑情况是否会如此。

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! 希望这对某人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM