Advertisement

Adding more vertices to my vertex buffer

Started by April 29, 2002 03:02 AM
3 comments, last by Sponzie 22 years, 7 months ago
I''ll just show the code to explain what I''m trying to do; (sVertex has x,y,z and u,v coords) sVertex Verts[] = { { 0.0f, 500.0f, 400.0f, 1.0f, 0.0f }, {400.0f, 500.0f, 400.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 400.0f, 1.0f, 1.0f }, {400.0f, 0.0f, 400.0f, 0.0f, 1.0f } }; Verts[5] = (sVertex){500.0f, 500.0f, 400.0f, 1.0f, 0.0f }; Verts[6] = .... etc this obviously doesn''t work. How should I append more vertices to my array?
There are several ways to do what you want. The easiest way for me to tell you how is to include the header file vector, and use that (that being a vector). STL containers are platform independant STANDARD TEMPLATE LIBRARY data structures.

I started to try to walk you though how to do this, but unless you know how to use templates, I don't see how I coule help you use vectors...

Another way to do what you want is to have one array created dynamically, and have it copy all of the old elements then add a new one. Then if you have to add another you just create another dynamic array and delete the old one. One way is to create your own array class (that does all the allocation/deallocation of the arrays and overload operators so that you can copy and add an element using one operator ( such as =+ )). This way you could just have an assignment like (assuming you name the class Array)

      Array verts(25);  // have your class use the argument as the size                  // of your array.verts =+ verts(1.0f);  // have the operator overloaded and return                       // the argument to allow cascading.      


If you implement this in your class the right way, your program would look like you are using a built in data type.

Good luck!


edit: the include vector went away.

If at first you don't succeed, use profanity and try, try again.


[edited by - grandmlee on April 29, 2002 7:27:44 AM]
If at first you don't succeed, use profanity and try, try again.
Advertisement
Dynamically allocating a new array every time I want to add some vertices would probably be very slow, right? So I guess I''ll have to look into the STL vector.. Thanks for your help!
OK, I set up a vector for my vertices like this:
vector'<'sVertex'>' Verts;

Then I pushed_back some vertices on the vector, created a vertex buffer and tried to copy the contents of the vector to the vertex buffer. Like this:

memcpy(Ptr, Verts, sizeof(Verts));
(between calls to lock and unlock ofcourse)

This doesn't work.. what am I doing wrong? How should I copy the vertices to the vertex buffer?

edit: '<'sVertex'>' disappeared






[edited by - Sponzie on April 29, 2002 9:15:29 AM]

[edited by - Sponzie on April 29, 2002 9:16:32 AM]
I couldn''t figure out how to copy the vector to the vertexbuffer directly, so i created a dynamic array (of the vector''s size), copied the contents of the vector to the array, and finally copied the array to the VB. There must be a simpler solution, but until i hear one i''m gonna have to stick to this

This topic is closed to new replies.

Advertisement