I’m relatively new to OpenGL and have been using the GLTools library it provides. One of the classes, GLTriangleBatch, in part, deals with batches of vertexs. The class has a global attribute pointer pVerts declared as follows:
typedef float M3DVector3f[3];
M3DVector3f *pVerts;
When an initializing function is called, the above pointer is delegated a block of memory with the number of indexs being an int provided to the function as an argument(nMaxIndexes):
pVerts = new M3DVector3f[nMaxIndexes];
Another function is given an array of 3 M3DVector3fs that comprise vertex points of a triangle. This function determines which individual vertices are added to the batch or not.
For example, if the point to be added already exists in pVerts, the index of that point in pVerts is added to another array which holds the index of all points that will be drawn. Therefore pVerts contains only unique points. Therefore the actual index of pVerts will never reach nMaxIndexes.
However, in the case that it is a unique vertex, it is added to pVerts as follows:
memcpy(pVerts[nNumVerts], verts[iVertex], sizeof(M3DVector3f));
where nNumVerts is the current index of pVerts, verts[] is the array of 3 M3DVector3f’s that comprise the triangle being added with iVertex being the index of the particular vertexbeing added.
Now i use another function that calculates the positions of vertex points to build a (rough) sphere. The sphere i build in my program ends up with nMaxIndexes = 2028, and when the sphere is completed nNumVerts = 378.
Now following all of this i assumed pVerts pointed to an array of index ranging 0 to 2027, but only indexes 0 to 377 being populated with data. But when i put a breakpoint directly after all the vertices are added, pVerts points only a single M3DVector3f.
I need access to each of these individual points as I plan to use this sphere as a collidible boundary. I have added a getVertices function that returns pVerts, but the returned pointer only points to a single M3DVector3f? What am i missing?
This is not so much a 3D graphics or OpenGL problem, but a lack of understanding how arrays work in C and C++.
An array in C is a set of contigous address space backed by system memory. The contents are addressed by a base pointer and an offset against this base pointer, through a mechanism called pointer arithmetic.
Say
pis a pointer of typeT, in C syntax writtenT *p. Now letppoint to some contigous memory ofnelements of typeT, allocated either withnew T[n](C++) ormalloc(sizeof(t)). To access the i-th element you need the address of that element and dereference the pointer to that address. C has a mechanism called pointer arithmetic for this:p + iis evaluated to the pointer to the i-th element in a contigous set of elements starting atp. To get the value of the element you have to dereference using the*operator:*(p+i). A shorthand form is provided by the index operator[]and actuallyp[i]is exactly the same as*(p+i).This is a little known fact and has interesting consequences. Pointer arithmetic commutes, i.e.
p + i == i + pand applying that to the index operator one would expect thatp[i] == i[p]which is actually the case.Getting back to your original problem: You’re using C++, so I recommend you don’t bother with manually allocated arrays at all. Instead you better use a standard container, namely std::vector, available through
#include <vector>(no trailing.h!)Instead of
M3DVector3f *pVertswhich is just a pointer, not an full qualified array(!), you writestd::vector<M3DVector3f> Vertsand this actually is what you’d call array (the C++ STL calls it vector to distinguish it from plain arrays; this might sound odd at first, but once you’ve understood vector spaces from a higher mathematics point of view it actually makes sense). Instead ofmemcpy(...)you should be able to use the assignment operator=on vector elements, i.e.Verts[nNumVerts] = verts[iVertex]. If the compiler complains, overload the assignment operator: