std::vector::size with glDrawElements crashes?

Posted by NoobScratcher on Game Development See other posts from Game Development or by NoobScratcher
Published on 2012-04-15T20:20:13Z Indexed on 2012/04/15 23:46 UTC
Read the original article Hit count: 230

Filed under:
|
|

( win32 / OpenGL 3.3 / GLSL 330 )

I decided after a long time of trying to do a graphical user interface using just opengl graphics to go back to a gui toolkit and so in the process have had to port alot of my code to win32.

But I have a problem with my glDrawElement function.

my program compiles and runs fine until it gets to glDrawElements then crashes.. which is rather annoying right.

so I was trying to figure out why and I found out its std::vector::size member not returning the correct amount of faces in the unsigned interger vector eg, "vector<unsigned int>faces; "

so when I use cout << faces.size() << endl;

I got 68 elements????

instead of 24 as you can see here in this .obj file:

# Blender v2.61 (sub 0) OBJ File: ''
# www.blender.org
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
s off
f 1 2 3 4     
f 5 8 7 6     
f 1 5 6 2    
f 2 6 7 3     <--- 24 Faces not 68?
f 3 7 8 4     
f 5 1 4 8    

I'm using a parser I created to get the faces/vertexes in my .obj file:

  char modelbuffer [20000];
  int MAX_BUFF = 20000;
  unsigned int face[3];

   FILE * pfile;

   pfile = fopen(szFileName,  "rw");

   while(fgets(modelbuffer,  MAX_BUFF, pfile) != NULL)
   {              
   if('v')
      {
        Point p;

        sscanf(modelbuffer, "v %f %f %f",  &p.x, &p.y, &p.z);

        points.push_back(p);

        cout << " p.x = " << p.x << " p.y = " << p.y << " p.z = " << p.x << endl;
      }

    if('f')
      {

      sscanf(modelbuffer, "f %d %d %d %d", &face[0], &face[1], &face[2],  &face[3]);         

         cout << "face[0] = " << face[0] << " face[1] = " << face[1] << " face[2] = " << face[2] << " face[3] = " << face[3] << "\n";

          faces.push_back(face[0] - 1);
          faces.push_back(face[1] - 1);
          faces.push_back(face[2] - 1);
          faces.push_back(face[3] - 1);
          cout << face[0] - 1 << face[1] - 1 << face[2] - 1 << face[3] - 1 << endl;        
      }     
   }

using this struct to store the x,y,z positions also this vector was used with Point:

vector<Point>points;

struct Point 
{

float x,  y, z;

};

If someone could tell me why its not working and how to fix it that would be awesome I also provide a pastebin to the full source code if you want a closer look.

http://pastebin.com/gznYLVw7

© Game Development or respective owner

Related posts about c++

Related posts about vector