0

我有点卡住了,我不知道我的问题可能出在哪里。我正在渲染一个由四叉树划分的地形。现在我试图使这个四叉树的边界可见以用于调试目的。因此,我为地形上的每个节点设置了一个新的缓冲区,并且只有那些可见的调试四边形应该被渲染。我遇到的问题是,我通过引用将这些节点缓冲区传递给另一个类(DebugTreeClass)的方法以在那里创建它们。但似乎这些缓冲区在调用该方法后保持为空。这是我的定义:

1. m_debugTree->InitializeBuffer(node->vertexBufferDebug, node->indexBufferDebug, node->positionX, node->positionZ, node->width, device);

2. bool InitializeBuffer(ID3D11Buffer*,ID3D11Buffer*, float, float, float,ID3D11Device*);

这是创建索引/顶点缓冲区的方法:

bool DebugTreeClass::InitializeBuffer(ID3D11Buffer* vertexBuffer,ID3D11Buffer* indexBuffer, float positionX, float positionZ, float width, ID3D11Device* deviceContext){
VertexType* vertices;
unsigned long* indices;
int index, i, j;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;

m_vertexCount = 8; // to form a quad with lines we need 24 points.

m_indexCount = 24;

// Create the vertex array.
vertices = new VertexType[m_vertexCount];
if(!vertices)
{
    return false;
}

// Create the index array.
indices = new unsigned long[m_indexCount];
if(!indices)
{
    return false;
}

//DEFINE THE 8 POINTS FOR THE QUAD
//UPPER LEFT
vertices[0].position = D3DXVECTOR3(positionX, width / 2, positionZ);
vertices[0].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
// UPPER RIGHT
vertices[1].position = D3DXVECTOR3(positionX + width, width / 2, positionZ);
vertices[1].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
// BOTTOM RIGHT
vertices[2].position = D3DXVECTOR3(positionX + width, -(width / 2), positionZ);
vertices[2].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
// BOTTOM LEFT
vertices[3].position = D3DXVECTOR3(positionX, -(width / 2), positionZ);
vertices[3].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
// UPPER LEFT BACKSIDE
vertices[4].position = D3DXVECTOR3(positionX, width / 2, positionZ + width);
vertices[4].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
// UPPER RIGHT BACKSIDE
vertices[5].position = D3DXVECTOR3(positionX + width, width / 2, positionZ + width);
vertices[5].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
// BOTTOM RIGHT BACKSIDE
vertices[6].position = D3DXVECTOR3(positionX + width, -(width / 2), positionZ + width);
vertices[6].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
// BOTTOM LEFT BACKSIDE
vertices[7].position = D3DXVECTOR3(positionX, -(width / 2), positionZ + width);
vertices[7].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);

//SET UP THE INDEXBUFFER
indices[0] = 0;
indices[1] = 1;
indices[2] = 1;
indices[3] = 2;
indices[4] = 2;
indices[5] = 3;
indices[6] = 3;
indices[7] = 0;
indices[8] = 0;
indices[9] = 4;
indices[10] = 1;
indices[11] = 5;
indices[12] = 2;
indices[13] = 6;
indices[14] = 3;
indices[15] = 7;
indices[16] = 4;
indices[17] = 5;
indices[18] = 5;
indices[19] = 6;
indices[20] = 6;
indices[21] = 7;
indices[22] = 7;
indices[23] = 4;

// Set up the description of the static vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
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 = deviceContext->CreateBuffer(&vertexBufferDesc, &vertexData, &vertexBuffer);
if(FAILED(result))
{
    return false;
}

// Set up the description of the static index buffer.
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
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 = deviceContext->CreateBuffer(&indexBufferDesc, &indexData, &indexBuffer);
if(FAILED(result))
{
    return false;
}

// Release the arrays now that the buffers have been created and loaded.
delete [] vertices;
vertices = 0;

delete [] indices;
indices = 0;

return true;

}

有 2 个类,定义节点的“QuadTreeClass”和应该填充每个节点的缓冲区并渲染调试四边形的“DebugTreeClass”。如果我调试“DebugTreeClass::InitializeBuffer()”方法,我可以看到缓冲区应该成功创建,所以我很确定通过引用传递此方法有问题。也许我只是瞎了眼......

谢谢你的帮助。

4

1 回答 1

2

我很确定问题出在这里:

bool DebugTreeClass::InitializeBuffer(ID3D11Buffer* vertexBuffer,...
...
result = deviceContext->CreateBuffer(&vertexBufferDesc, &vertexData, &vertexBuffer)

您可能期望 vertexBuffer 到达“InitializeBuffer”的调用者,但这不会发生。您在谈论引用,但实际上并没有传递引用。这可能会解决它:

bool DebugTreeClass::InitializeBuffer(ID3D11Buffer*& vertexBuffer,...

您要返回的其他指针也是如此。

您的原始版本将旧的(可能未初始化——您应该启用警告)指针变量从调用者复制到函数的“vertexBuffer”变量中,并使用它。“vertexBuffer”是函数的局部变量,它从未被传回。

通过我的更改,它变成了“对指针的引用”。现在,“vertexBuffer”指针实际上与传递给函数调用的指针相同,使用相同的内存。因此,当您更改该指针时,调用者中的版本会更改。

于 2012-08-18T13:44:38.327 回答