0

I noticed some strangeness when passing a VERTEX structure to a buffer. At first I thought it was a faulty custom function for reading into .obj files, but debugging further narrowed the problem. Take for instance:

struct VERTEX{ 
    XMFLOAT3 Translation; 
    D3DXCOLOR Color;
};

and it's instance:

VERTEX foo[] = {
        {XMFLOAT3(-0.5f, 0.0f, 0.0f), D3DXCOLOR(1.0f,1.0f,1.0f,1.0f)}, 
        {XMFLOAT3(-0.5f, 0.5f, 0.0f), D3DXCOLOR(1.0f,1.0f,1.0f,1.0f)},
        {XMFLOAT3(0.0f, 0.0f, 0.0f), D3DXCOLOR(1.0f,1.0f,1.0f,1.0f)}, 
        {XMFLOAT3(0.0f, 0.5f, 0.0f), D3DXCOLOR(1.0f,1.0f,1.0f,1.0f)},
        {XMFLOAT3(0.9f, 0.0f, 0.0f), D3DXCOLOR(0.0f,1.0f,1.0f,1.0f)},
        {XMFLOAT3(2.0f, 0.9f, 0.0f), D3DXCOLOR(0.0f,1.0f,0.0f,1.0f)},
     };

Work perfectly when passed into a vertex buffer. Dynamically creating an array instance of the structure yields strange results. I'll see one vertex show up, or none. Here's an example, imagine foo as being a function returning a [working] VERTEX* array of vertices:

VERTEX *bar= new VERTEX[barsize];
VERTEX foo[] = {
        {XMFLOAT3(-0.5f, 0.0f, 0.0f), D3DXCOLOR(1.0f,1.0f,1.0f,1.0f)}, 
        {XMFLOAT3(-0.5f, 0.5f, 0.0f), D3DXCOLOR(1.0f,1.0f,1.0f,1.0f)},
        {XMFLOAT3(0.0f, 0.0f, 0.0f), D3DXCOLOR(1.0f,1.0f,1.0f,1.0f)}, 
        {XMFLOAT3(0.0f, 0.5f, 0.0f), D3DXCOLOR(1.0f,1.0f,1.0f,1.0f)},
        {XMFLOAT3(0.9f, 0.0f, 0.0f), D3DXCOLOR(0.0f,1.0f,1.0f,1.0f)},
        {XMFLOAT3(2.0f, 0.9f, 0.0f), D3DXCOLOR(0.0f,1.0f,0.0f,1.0f)},
     };
bar=foo;

And later on...

memcpy(MappedResource.pData, &bar, sizeof(bar));

Am I doing something funny trying to access the data that foo points to, such that only the first set of coordinates get passed in?

*edit: When writing this post, foo and bar got mixed. This post now shows the correct example.

Rashid Ellis
  • 330
  • 3
  • 22
  • 1
    your code is very confused, what does foo do? you said it is a function, but it is an array in your code, and foo=bar will get an uninitialized value. – zdd Jan 09 '14 at 02:02
  • I just edited the post, sorry. I dulled down the problem a bit. The original code has a function that parses an obj file for vertices. It returns a `VEXTEX*` array(represented as `VERTEX foo[]`), which is copied over to bar. I noticed that a `VERTEX` array works fine, but when I allocate using `new`, I can access the elements in the new array, but D3D can't - it seems to just draw a single point. I believe it to be a pointer issue. – Rashid Ellis Jan 09 '14 at 02:10

1 Answers1

1

The problem is this line

bar=foo;

memcpy(MappedResource.pData, &bar, sizeof(bar));

bar is a pointer of VERTEX, not a array of VERTEX, sizeof(bar) == 4 on x86 machine, so you will not get all the element copied.

you can first count how many Vertex element you need, and then when using memcpy, use sizeof(VERTEX) * vertex_count.

zdd
  • 8,258
  • 8
  • 46
  • 75