CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2008
    Posts
    17

    Can i use <vector> arrays with "vertex arrays" of opengl?

    *I am confused if i can use <vector> arrays with vertex arrays in opengl.
    <vector> is a class that makes memory handling easy.
    My vector arrays are large upto 1000 entries.

    And can someone tell me

    *Which is the faster way to implement big arrays(vectors, or c like arrays)

    *What is the best way to optimize opengl rendering operations(vertex arrays or display list or something else)

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Can i use <vector> arrays with "vertex arrays" of opengl?

    Yes, you may use std::vector class in style like you use plain C buffers, but you have to keep some things in mind:

    - to pass a buffer, you need to use following construct:
    Code:
    vector<float> vertexArray;
    fillWithSomeData(vertexArray);
    //use vertex list with example function (no idea if it is gl-correct :D )
    glBufferDataARB( GL_ARRAY_BUFFER_ARB, vertexArray.size()*sizeof(float), &vertexArray[0], GL_STATIC_DRAW_ARB );
    - function receiving buffer must not reallocate it (i.e. grow, move, etc), nor free it, but it may modify its content
    - buffer pointer received this way becomes useless when vector grows, because it might be reallocated by internal mechanisms of vector class. The best way is to not store this address, just get it every time it is needed.

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Aug 2008
    Posts
    17

    Re: Can i use <vector> arrays with "vertex arrays" of opengl?

    Thnx.

    Now i will use vector to store my data and will not have to worry about memory exceptions
    I was getting them only when running build .exe file(strange!)

  4. #4
    Join Date
    Aug 2008
    Posts
    17

    Re: Can i use <vector> arrays with "vertex arrays" of opengl?

    Hey, what is a correct way of sending a vector to a function


    std::vector<model> ptree;

    loadobj("d:\\tree.txt",&ptree[0]);


    and loadobj func is

    void loadobj(char *filename, model *a)
    {
    ..
    }


    is it ok?

  5. #5
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Can i use <vector> arrays with "vertex arrays" of opengl?

    It depends on what function loadobj does, but it seems to be wrong. First of all, you violated a rule stating that function receiving buffer must not reallocate it. As far as I understand, you are trying to fill empty buffer with model objects, which is not correct to be done in this way.
    Second thing is that you need not to pass a buffer here: you might pass a container as a whole by reference, like this:
    Code:
    vector<model> ptree;
    loadobj("d:\\tree.txt", ptree);
    
    void loadobj(const char* const path, vector<model>& vec) {
    
       FILE* f = fopen(path, ....);
    
       model m;
       //read some data from file, fill in a model struct...
       //....
    
       vec.push_back(model);  //put new model into a container
    }
    It is possible, of course, if loadobj function is written by you in a manner which does not require passing it a buffer.

    Sending vector as buffer should be used only with functions, which require a buffer as input parameter: generally, these are plain old C functions, which do not understand C++ containers.

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured