Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Dynamic vertex array

  1. #1
    Guest

    Dynamic vertex array

    How can I create a dynamic vertex array with C/C++?
    I need to use DrawArray or DrawRangeElements for a varied number of vertices. I want to add/remove any number of vertices per frame to the array as needed.
    I'll need several arrays for different states, but I'm trying to stay simple for now. (Isn't this what some engines do ? )

    So far I found the C++ "placement new" but no good example of a dynamic vertex array.

  2. #2
    Member Contributor uruk's Avatar
    Join Date
    Dec 2004
    Location
    acasa :) sau la munca :(
    Posts
    66

    Re: Dynamic vertex array

    i'm not sure i understand what you want, but is it realloc()?
    Stat rosa pristina nomine, nomina nuda tenemus.

  3. #3
    Guest

    Re: Dynamic vertex array

    But is it ok to realloc() a lot ? I read it uses malloc() to resize.. so it might fragment the memory and eventually crash. I need to change the vertex/color/texture coords of the array as the program runs, per frame if possible.

    I want to store up vertices and use a single call to DrawArray() functions to render it. I'm suprised there are no examples of this technique.. seems to be what the commands are made for.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jan 2004
    Posts
    314

    Re: Dynamic vertex array

    Use realloc in larger increments, and don't shrink it.

    E.g.:

    Code :
     const int increment=64; // you may want to experiment with this value
     vertex *buffer=NULL;
     int allocated=0;
     int used=0;
     
     // returns pointer to vertex where you can store your data - NULL if failed to allocate
     vertex *addvertex()
     {
      vertex *newbuffer;
      vertex *newvertex;
     
      if(used==allocated) { // buffer full or no buffer
       newbuffer = (vertex *)realloc(buffer,sizeof(vertex)*(allocated + increment);
       if(newbuffer) {
        buffer = newbuffer;
        allocated+=increment;
       }
      }
      if(used<allocated) {
       newvertex = buffer+used;
       used++;
       return newvertex;
      }
      return NULL;
     }
     
     // reset the vertex pointer to the first in the array
     resetbuffer()
     {
      used=0;
     }
    This way, you keep the number of reallocations and therefore fragmentation and expensive copy operations down, while remaining flexible w.r.t. the maximum number of vertices.
    Of course, "malloc-wise" it would be even better to allocate the buffer in its entirety if you know the maximum size tat will be used.

  5. #5
    Guest

    Re: Dynamic vertex array

    Hey even a example, thanks!

    So using this could I change for example the 3rd thru 5th indices of the array as a triangle's texture coords changes?

  6. #6
    Senior Member Regular Contributor
    Join Date
    Jan 2004
    Posts
    314

    Re: Dynamic vertex array

    The only thing this does is it allows you to have a buffer large enough to dump your vertex array into.

    When you want to fill the vertex array, just resetbuffer and use the addvertex function on every vertex you want to send to the array.
    Finally, the number of elements in the vertexpointer call would be "used".

    But it is a throwaway buffer in the sense that you have to fill it everytime.

    If you want to allocate "static" vertex arrays (e.g. for every mesh one vertex array) you don't really need realloc at all.

  7. #7
    Guest

    Re: Dynamic vertex array

    Thank you T101, it's giving better results so far than placement new was.

    More stupid questions..

    How can I assign x,y,z values to the vertices? I do this in my main loop right now to test, but I changed addvert() to accept a vertex*.
    Code :
    for (loop = 0; loop < num_verts; loop++){ 
     vertex vert; vert.x=1; vert.y=5; vert.z=200;
    addvert( &amp;vert  ); 
      }
    What array can I use for DrawArray()? I assumed "buffer" and it draws, but I'm not sure if that's right. Do I free() "buffer" ?

    I guess I can malloc a big buffer as long as I can change the vertex values during runtime. But most vertex array demos I've seen so far seem to use static arrays, and I need some non-static ones

  8. #8
    Senior Member Regular Contributor
    Join Date
    Jan 2004
    Posts
    314

    Re: Dynamic vertex array

    Only free the buffer when you're completely sick of rendering - in other words, when you're ending the program, or loading up another game level or something.

    If you "reset" the buffer before you fill it, it does not normally have to reallocate anything once it has grown to the necessary size.

    The pointer you supply to OpenGL depends on the definition of the vertex. I kept that out of it because I don't know what you're trying to do per vertex.
    But suppose you have a vertex structure as follows:

    Code :
    typedef struct _vertex {
     struct {
      GLfloat X,Y,Z;
     } vector;
     struct {
      GLfloat S,T;
     } texcoords;
     struct {
      GLfloat X,Y,Z;
     } normal;
    } vertex;
    You would use as the stride:
    sizeof(vertex)

    As the pointer for the vertex vector:
    &(buffer[0].vector)

    As the pointer for the vertex normal:
    &(buffer[0].normal)

    etc. (You may have multiple S/T coordinates, or per vertex colors, it all depends on what you want to do with it)

  9. #9
    Guest

    Re: Dynamic vertex array

    Ok I'm getting there.. Yes I call reset() and it's steady memory wise. I'm not sure what you mean by stride, are you talking about interleaving ?
    I'm not going for anything fancy here, not even normals. Just x,y,z, s,t, and colors..
    Could an interleaved array work best?

    I'm trying for a particle system if anyone is curious.

  10. #10
    Guest

    Re: Dynamic vertex array

    Hmm well I guessed to use the "stride" of glVertexPointer to draw some triangles but that didn't work. Tried:
    Code :
    glVertexPointer(  3,  GL_FLOAT,  sizeof(&amp;(buffer[0].vector)) ,    buffer   );

Page 1 of 2 12 LastLast

Similar Threads

  1. vertex buffer object with dynamic color array
    By saam_b in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-01-2012, 05:43 PM
  2. Dynamic array of uniforms
    By Dark_Raven in forum OpenGL: GLSL
    Replies: 8
    Last Post: 01-18-2011, 06:35 AM
  3. glvertexpointer and dynamic array
    By kasemodz in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 12-01-2009, 01:13 PM
  4. using dynamic index with static vertex-array with VBO// Normals
    By mcbastian in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 10-19-2004, 04:26 PM
  5. dynamic array?
    By detzel in forum OpenGL: Advanced Coding
    Replies: 7
    Last Post: 02-02-2001, 09:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean