Click to See Complete Forum and Search --> : Can i use <vector> arrays with "vertex arrays" of opengl?
engr_tj
August 29th, 2008, 01:19 AM
*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)
Hobson
August 29th, 2008, 02:35 AM
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:
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
engr_tj
August 29th, 2008, 03:30 AM
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!)
engr_tj
August 29th, 2008, 06:35 AM
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?
Hobson
August 29th, 2008, 07:08 AM
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:
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.