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.

2
  • 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. Commented Jan 9, 2014 at 2: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. Commented Jan 9, 2014 at 2:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

How would I reference the array sizeof() being pointed to: memcpy(MappedResource.pData, &bar, sizeof(&*bar));?
How can I get the sizeof(VERTEX) in memory through it's pointer VERTEX*
Thanks. It was making me crazy. I found this too: stackoverflow.com/questions/492384/…
You definitely helped my understanding of my "missing memory" a bit further.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.