Jump to content

  • Log In with Google      Sign In   
  • Create Account

Quick question about vertex arrays


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 link102   Members   -  Reputation: 252

Like
0Likes
Like

Posted 30 October 2011 - 02:34 PM

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.

Sponsor:

#2 Erik Rufelt   Crossbones+   -  Reputation: 3638

Like
1Likes
Like

Posted 30 October 2011 - 02:39 PM

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));


#3 link102   Members   -  Reputation: 252

Like
0Likes
Like

Posted 30 October 2011 - 02:58 PM

works.

Thank you :)

#4 Tispe   Members   -  Reputation: 1046

Like
0Likes
Like

Posted 31 October 2011 - 03:32 AM

Will this work if the vector becomes fragmented in memory?

#5 unbird   Crossbones+   -  Reputation: 6012

Like
0Likes
Like

Posted 31 October 2011 - 03:51 AM

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.

#6 Erik Rufelt   Crossbones+   -  Reputation: 3638

Like
0Likes
Like

Posted 31 October 2011 - 04:52 AM

An alternative that should work for other types of lists too is:
CUSTOMVERTEX *destination = (CUSTOMVERTEX*)pVoid;

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS