Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6061869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:59:11+00:00 2026-05-23T08:59:11+00:00

I’m relatively new to OpenGL and have been using the GLTools library it provides.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:59:11+00:00Added an answer on May 23, 2026 at 8:59 am

    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 p is a pointer of type T, in C syntax written T *p. Now let p point to some contigous memory of n elements of type T, allocated either with new T[n] (C++) or malloc(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 + i is evaluated to the pointer to the i-th element in a contigous set of elements starting at p. 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 actually p[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 + p and applying that to the index operator one would expect that p[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 *pVerts which is just a pointer, not an full qualified array(!), you write std::vector<M3DVector3f> Verts and 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 of memcpy(...) 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:

    struct M3DVertex3f {        
        float v[3] __attribute__ ((packed)); // this is a static array, so no double pointers!
        M3DVertex3f &operator=(M3DVertex3f const &src);
    }
    
    M3DVertex3f &M3DVertex3f::operator=(M3DVertex3f const &src)
    {
        for(int i=0;i<3;i++)
             v[i] = src.v[i];       
        return *this;        
    }
    
    std::vector<M3DVertex3f> Verts;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.