Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

( 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
share|improve this question
add comment

1 Answer

I think your error is in the parser. The code after the lines if('v') and if('f') always enter those branches (you're basically saying if(true) two times). So your code basically pushes something back into the vector every time the loop executes (I think this is basically once for every line in your file? I'm bad with C functions).

I think you mean to say something like

if(modelbuffer[currentindex] == 'v') 

etc.

As a bonus, since you're using vector I'm strongly assuming that you have access to a C++ compiler. Please use std::ifstream and getline. Thanks :-)

share|improve this answer
    
my char variable doesn't have a member called current index :P how would I get one lol EDIT: tried using ifstream but could not find a better way to loop through the hole file finding the v/f unless I would use eof but thats error prone due to a flag set –  NoobScratcher Apr 15 '12 at 20:28
    
Well, define and initialize the variable outside your loop (unsigned int currentindex = 0) and increment it by whatever amount of chars fgets() reads. I don't know how to determine that, with fgets, though. I would use std::ifstream and save all the lines that youre reading into a vector of std::string, which makes operating on these things a lot easier. –  TravisG Apr 15 '12 at 20:31
    
Is that eof() error chance really relevant to your project? Are you somehow operating in an environment where eof() is wrong a lot? –  TravisG Apr 15 '12 at 20:34
    
not really shore just didn't want to take a risk with it but I guess it could work for now. :) I already got code to do it so it won't be very hard to get it to work thank god. –  NoobScratcher Apr 15 '12 at 20:35
1  
Well, you don't have to call me god just for that. But you can accept my answer and upvote it –  TravisG Apr 15 '12 at 20:36
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.