首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DirectX 11:如何勾勒出边界框?

DirectX 11:如何勾勒出边界框?
EN

Stack Overflow用户
提问于 2014-02-21 21:25:25
回答 1查看 3.9K关注 0票数 0

在一个2d平台游戏中,我想要可视化我创建的边界框来帮助调试。如何在Visual C++ 2012中完成此操作?

EN

回答 1

Stack Overflow用户

发布于 2014-02-22 01:12:47

首先定义一个简单的顶点结构:

代码语言:javascript
复制
struct Vertex
{
    D3DXVECTOR3 position; //a 3D point even in 2D rendering
};

现在,您可以创建顶点和索引数组:

代码语言:javascript
复制
Vertex *vertices;
unsigned long *indices = new unsigned long[5];
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;

//create the vertex array
vertices = new Vertex[5];
if(!vertices)
{
    //handle error
}
//load the vertex array with data
vertices[0].position = D3DXVECTOR3(left, top, 0.0f);
vertices[1].position = D3DXVECTOR3(right, top, 0.0f);
vertices[2].position = D3DXVECTOR3(right, bottom, 0.0f);
vertices[3].position = D3DXVECTOR3(left, bottom, 0.0f);
vertices[4].position = D3DXVECTOR3(left, top, 0.0f);
//create the index array
indices = new unsigned long[5];
if(!indices)
{
    //handle error
}
//load the index array with data
for(i=0; i<5; i++)
    indices[i] = i;

并将它们加载到缓冲区中:

代码语言:javascript
复制
ID3D11Buffer *vertexBuffer, *indexBuffer;
HRESULT result;

//set up the description of the dynamic vertex buffer
vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC; //enables recreation and movement of vertices
vertexBufferDesc.ByteWidth = sizeof(Vertex) * 5;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; //couples with dynamic
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
//give the subresource structure a pointer to the vertex data
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;

//now create the vertex buffer
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &vertexBuffer);
if(FAILED(result))
{
    //handle error
}

//set up the description of the static index buffer
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * 5;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
//give the subresource structure a pointer to the index data
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;

//create the index buffer
result = device->CreateBuffer(&indexBufferDesc, &indexData, &indexBuffer);
if(FAILED(result))
{
    //handle error
}

设置要渲染的矩形,如下所示:

代码语言:javascript
复制
unsigned int stride = sizeof(Vertex);
unsigned int offset = 0;

deviceContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
deviceContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP);

现在使用您选择的着色器进行渲染,记住将orthographic matrix传递给着色器而不是透视矩阵。瞧!矩形。但是你还不能移动它。为此,您必须定义另一个函数:

代码语言:javascript
复制
bool UpdateRectBuffers(ID3D11Buffer *vertexBuffer, ID3D11DeviceContext *deviceContext, float top, float left, float bottom, float right)
{
    Vertex *vertices;
    D3D11_MAPPED_SUBRESOURCE mappedResource;
    VertexType *verticesPtr;
    HRESULT result;

    //create a temporary vertex array to fill with the updated data
    vertices = new Vertex[5];
    if(!vertices)
    {
        return false;
    }
    vertices[0].position = D3DXVECTOR3(left, top, 0.0f);
    vertices[1].position = D3DXVECTOR3(right, top, 0.0f);
    vertices[2].position = D3DXVECTOR3(right, bottom, 0.0f);
    vertices[3].position = D3DXVECTOR3(left, bottom, 0.0f);
    vertices[4].position = D3DXVECTOR3(left, top, 0.0f);

    //lock the vertex buffer so it can be written to
    result = deviceContext->Map(vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
    if(FAILED(result))
    {
        return false;
    }

    verticesPtr = (Vertex*)mappedResource.pData;

    //copy the data into the vertex buffer
    memcpy(verticesPtr, (void*)vertices, (sizeof(Vertex) * 5));

    deviceContext->Unmap(vertexBuffer, 0);
    delete [] vertices;
    vertices = 0;

    return true;
}

这段代码的依赖项是float topfloat leftfloat rightfloat leftID3D11DeviceContext *deviceContextID3D11Device *device

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21935712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档