[英]DirectX/C++ How to Dereference Vector Data Array to Initialize D3D11 SUBRESOURCE DATA
I am trying to pass a pointer to an array of Vertex Data, (base class member) into a method used to initialize the Direct3D Vertex Buffer, (ID3D11Buffer). 我试图将指向Vertex Data数组(基类成员)的指针传递到用于初始化Direct3D Vertex Buffer(ID3D11Buffer)的方法中。
I want to use the variable, "v4", the pointer to the array, but I think I am not dereferencing the pointer correctly. 我想使用变量“ v4”,即指向数组的指针,但我认为我没有正确地取消引用指针。 Could someone point me in the right direction?
有人可以指出我正确的方向吗?
I have tried many ways to make this assignment, but I can only get a basic array work, (sometimes I end up with casting to "const void *" errors). 我尝试了许多方法来进行此分配,但是我只能得到基本的数组工作(有时我最终会转换为“ const void *”错误)。
How would I use a pointer? 我将如何使用指针?
The line I am having issue with is ... vertexSubResourceData.pSysMem = v1; 我遇到的问题是... vertexSubResourceData.pSysMem = v1;
(ps Using the VS C++ Nov 2012 CTP compiler .. not released yet ..) (ps使用VS C ++ 2012年11月CTP编译器..尚未发布..)
* Answered: Have confirmed that it is a Visual Studio 2012 C++ Nov. CTP Compiler Bug. * 回答:已确认这是Visual Studio 2012 C ++ 11月CTP编译器错误。 Arrays of Vector structures declared, even outside of the class, using C++ 11 initialization syntax are being interpreted incorrectly, (unless of course I am using the incorrect array initialization syntax).
使用C ++ 11初始化语法声明的Vector结构数组,即使在类之外也被错误地解释(当然,除非我使用的是错误的数组初始化语法)。 Also Note: This only pertains to initialization lists associated with pointers.
另请注意:这仅与与指针关联的初始化列表有关。 *
*
Thanks! 谢谢!
// Simplified Code //简化代码
void Triangle::InitializeVertexBuffer()
{
struct V
{
float X, Y, Z; // vertex position
};
// Compiles But doesn't work as desired.
// changed from V* v4 = new V[] // Compiled without range.
// Pointer to this data would ideally come from base static pointer, or file.
V* v4 = new V[3]
{
{ 0.50f, 0.5f, 0.0f },
{ 0.50f, -0.5f, 0.0f },
{ -0.50f, -0.5f, 0.0f },
};
// Compiles and Works to prove compiler issue with initialization lists.
V* vectorList = new V[3];
vectorList[0].X = 0.5f;
vectorList[0].Y = 0.5f;
vectorList[0].Z = 0.00f;
vectorList[1].X = 0.5f;
vectorList[1].Y = -0.5f;
vectorList[1].Z = 0.00f;
vectorList[2].X = -0.5f;
vectorList[2].Y = -0.5f;
vectorList[2].Z = 0.00f;
unsigned int size = sizeof(V) * 3; //sizeof(* Model::Vertices);
// The Description of the Vertex Buffer
D3D11_BUFFER_DESC vertexBufferDescription = {0};
vertexBufferDescription.ByteWidth = size;
vertexBufferDescription.BindFlags = D3D11_BIND_VERTEX_BUFFER;
// Model Data to be transferred to GPU Buffer.
D3D11_SUBRESOURCE_DATA vertexSubResourceData = {0};
vertexSubResourceData.SysMemPitch = 0;
vertexSubResourceData.SysMemSlicePitch = 0;
// Can't figure out how to dereference v4
vertexSubResourceData.pSysMem = vectorList; // works, but using "v4" doesn't.
// vertexSubResourceData.pSysMem = Model::Vertices;
// This is what I /really/ want .. Pointer to vertice array data from base class
NS::DeviceManager::Device->CreateBuffer(
&vertexBufferDescription,
&vertexSubResourceData,
&NS::ModelRenderer::VertexBuffer);
}
When you write V* v4 = new V[]{...};
当你写
V* v4 = new V[]{...};
it is illegal C++. 这是非法的C ++。 The CTP compiler is accepting malformed code.
CTP编译器接受格式错误的代码。 Try
V* v4 = new V[3]{...};
尝试
V* v4 = new V[3]{...};
Checking the C++ standard, from 5.3.4p1: 从5.3.4p1开始检查C ++标准:
[ expression ] attribute-specifier-seqopt
[表达式] attribute-specifier-seqopt
So, it looks like an expression is required in the []
. 因此,看起来
[]
需要一个表达式。 I believe new V[]
is an extension for compilers (still checking into this). 我相信
new V[]
是编译器的扩展(仍然对此进行检查)。
And as ildjarn points out, you want vertexSubResourceData.pSysMem = v4;
就像ildjarn指出的那样,您需要
vertexSubResourceData.pSysMem = v4;
as &4
would you give you the address of the pointer, not the array. 与
&4
一样,您会给您指针的地址,而不是数组。
Update 更新资料
I think this is a bug in the compiler. 我认为这是编译器中的错误。 Try the old C++03 syntax:
V* v4 = new V[3]; v4[0] = ...;
尝试使用旧的C ++ 03语法:
V* v4 = new V[3]; v4[0] = ...;
V* v4 = new V[3]; v4[0] = ...;
and if that works, then it definitely is a bug. 如果可行,那肯定是一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.