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

标签 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;

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

感谢您的帮助。

最佳答案

我很确定问题出在这里:

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

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

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

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

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

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

关于c++ - 传递的顶点/索引缓冲区引用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12018995/

相关文章:

已分配 C++ 指针 -> 错误 : pointer being freed was not allocated

c++ - 如何设计一个平面以在任何窗口大小下位于视口(viewport)的中心

c++ - 使用 C++ 播放 avi

graphics - 如何将此 HLSL 像素着色器校正为四边形的圆角?

android - LibGDX 保存纹理以避免上下文丢失

c++ - Opengl 3+ 绘制不同颜色的线条

c++ - SFML 2.1 "setFont"未处理的异常

c# - directx/directshow preparesurface 失败/无法呈现图像

.net - stdout 是否附加了缓冲区大小?

c++ - 快板 C++;闪烁位图