Advertisement Jump to content
Sign in to follow this  
link102

Quick question about vertex arrays

This topic is 2636 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any way of adding data into the vertice container as if it's an array?

struct CUSTOMVERTEX { FLOAT x, y, z; };

CUSTOMVERTEX Vertices[] = {
{ 0.0f, 0.0f, 0.0f},
{ 1.0f, 0.0f, 0.0f},
{ 0.0f, 0.0f, 1.0f},
{ 1.0f, 0.0f, 1.0f},
};

d3ddev->CreateVertexBuffer(sizeof(Vertices) * sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);

VOID* pVoid;
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, Vertices, sizeof(Vertices));
v_buffer->Unlock();

from that into something like this:

// NOTE: speudo code

CUSTOMVERTEX Vertices[];
Vertices.add(0.0f, 0.0f, 0.0f);
Vertices.add(1.0f, 0.0f, 0.0f);
Vertices.add(0.0f, 0.0f, 1.0f);
Vertices.add(1.0f, 0.0f, 1.0f);

d3ddev->CreateVertexBuffer(sizeof(Vertices) * sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);

VOID* pVoid;
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, Vertices, sizeof(Vertices));
v_buffer->Unlock();

If you're wondering, I'm planning on stuffing this into a for loop.

Share this post


Link to post
Share on other sites
Advertisement
Yes. Something like this for example:

#include <vector>

..


struct CUSTOMVERTEX {
float x, y, z;

CUSTOMVERTEX(float x, float y, float z) : x(x), y(y), z(z) {}
};

...

std::vector<CUSTOMVERTEX> vertices;
vertices.push_back(CUSTOMVERTEX(1.0f, 2.0f, 3.0f));
vertices.push_back(CUSTOMVERTEX(0.0f, 1.0f, 2.0f));
...

CreateVertexBuffer(vertices.size() * sizeof(CUSTOMVERTEX), ....);

memcpy(pVoid, &vertices[0], vertices.size() * sizeof(CUSTOMVERTEX));

Share this post


Link to post
Share on other sites
A stl::vector is a wrapper for a resizable array, not a linked list, so no fragmentation here. Otherwise you couldn't do that block copy with memcpy.

Share this post


Link to post
Share on other sites
An alternative that should work for other types of lists too is:

CUSTOMVERTEX *destination = (CUSTOMVERTEX*)pVoid;

std::copy(vertices.begin(), vertices.end(), destination);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!