传递顶点/索引缓冲区的引用不起作用

时间:2012-08-18 13:33:20

标签: c++ directx buffer

我有点卡住了,我不确切地知道我的问题可能在哪里。我正在渲染由四叉树划分的地形。现在我试图使这个四元树的边界可见,以便进行调试。因此,我为地形上的每个节点设置一个新的缓冲区,只有那些可见的调试四边形应该被渲染。我遇到的问题是,我通过引用将那些节点缓冲区传递给另一个类(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;

}

有两个类,其中节点定义的“QuadTreeClass”和“DebugTreeClass”,其中每个节点的缓冲区应该被填充并且调试四边形的渲染发生。 如果我正在调试“DebugTreeClass :: InitializeBuffer()”方法,我可以看到缓冲区应该成功创建,所以我很确定通过引用此方法传递出错。也许我只是盲目......

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我很确定问题就在这里:

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

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

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

您要返回的其他指针也一样。

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

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