1

Finally got my model loader working (based off a previous question). Stores all the values fine, runs glDrawElemts fine - yet it doesn't run glDrawArrays.

Yeah, first thing you'd say is 'glDrawArrays clearly isn't written out right'. That's the problem - I'm 99% sure it is.

Code is as follows:

bool modelLoader(string fileName,vector<GLfloat>& vertices, vector<GLushort>& indices)
{
fstream objFile;
vector<GLfloat> localVertices;
string dataLine;
stringstream mystream;
GLfloat x, y, z;
GLushort ind1, ind2, ind3;
char c, d; 
vertices.clear();
indices.clear();

for (int i = 0; i < 3; i++)
{
    vertices.push_back(0);
}


for (int i = 0; i < 3; i++)
{
    localVertices.push_back(0);
}


objFile.open(fileName);
if (!objFile.good())
{
    printf("Error with loader");
    return false; 
}

while (!objFile.eof())
{
    getline(objFile, dataLine);
    if (dataLine == "") 
    {                                                                  
        mystream.clear();                                              
        continue;                                                      
    }                                                                  
    mystream.clear();                                                  
    mystream.str(dataLine);                                            
    c = dataLine[0];                                                   
    d = dataLine[1];
    mystream.ignore(2);
    switch (c)
    {
    case 'v':
        {
            switch (d)
            {
            case 'n':
                { /* for normals */ break;}
            case ' ':
                mystream >> x >> y >> z;
                localVertices.push_back(x);
                localVertices.push_back(y);
                localVertices.push_back(z);
                printf("\nVertices: %f, %f, %f", x, y, z);
                break;
            default:
                {break;}
            }                                                                                                    
            break;                                                                                               
        }                                                                                                        
    case 'f':                                                                                                    
        {                                                                                                        
            //printf("F entered");                                                                               
            mystream >> ind1 >> ind2 >> ind3;                                                                    
            vertices.push_back(localVertices[ind1* 3 + 0]);                                                      
            vertices.push_back(localVertices[ind1* 3 + 1]);                                                      
            vertices.push_back(localVertices[ind1* 3 + 2]);                                                      
            vertices.push_back(localVertices[ind2* 3 + 0]);                                                      
            vertices.push_back(localVertices[ind2* 3 + 1]);                                                      
            vertices.push_back(localVertices[ind2* 3 + 2]);                                                      
            vertices.push_back(localVertices[ind3* 3 + 0]);                                                      
            vertices.push_back(localVertices[ind3* 3 + 1]);                                                     
            vertices.push_back(localVertices[ind3* 3 + 2]);
            indices.push_back(ind1);
            indices.push_back(ind2);
            indices.push_back(ind3);
            printf("\nIndices: %d, %d, %d", ind1, ind2, ind3);
            break;
        }
    case !'v' || !'f':
        {
            break;
        }
    default:
        {break;}
    }
    mystream.clear();
}
objFile.close(); 
return true;
 }

From here I go on to call the following in the main function:

vector<GLfloat> vertices;

vector<GLushort> indices;


if (!modelLoader("triangles.obj", vertices, indices))
{
    cout << "FAILED TO RUN: MODEL LOADER";

    return 1;
}

Insert a bunch of other malarky about setting the model view matrix, running a loop to update every iteration...

int size = vertices.size()-3;

glDrawArrays(GL_TRIANGLES, 3, (GLsizei)size);

Oh, and the triangles.obj file is:

v -10.0 10.0 10.0
v -10.0 -10.0 10.0
v 10.0 -10.0 10.0
v 10.0 10.0 10.0
v 20.0 10.0 10.0
v 20.0 -10.0 10.0
f 1 2 3
f 4 5 6

Having no joy at all. As I said, using DrawElements does this fine, yet causes an exception error when I try to draw any other .obj file.

Any clues as to where I'm going wrong?

genpfault
  • 51,148
  • 11
  • 85
  • 139
DerryHolt
  • 17
  • 1
  • 4
  • 2
    I'm still learning OpenGL myself, but shouldn't you be using *either* glDrawElements *or* glDrawArrays, not both? – Zutty Feb 07 '13 at 11:46
  • 2
    **[`while (!objFile.eof())` is wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)**. What learning resource told you to do this? – Lightness Races in Orbit Feb 07 '13 at 11:47
  • I am only using one at a time, don't worry! I'm just trying to get BOTH working (individually). – DerryHolt Feb 07 '13 at 11:48
  • @LightnessRacesinOrbit my tutor.. Hah! What alternative do you suggest? – DerryHolt Feb 07 '13 at 11:49
  • @DerryHolt: Click on the link and find out. Then send your tutor to my Stack Overflow profile and ask him to get in touch -- he is teaching _wrong_ things. – Lightness Races in Orbit Feb 07 '13 at 11:50
  • Woops. Missed that. Thanks! – DerryHolt Feb 07 '13 at 11:51
  • @LightnessRacesinOrbit It's quite peculiar, I've just had a quick skim through his model loader and he's using while (!infile.eof()) and it works perfectly. He doesn't even explicitly check .eof in his loop. It's extremely odd that two people using a very similar model loader are having contrasting results! – DerryHolt Feb 07 '13 at 11:58
  • @DerryHolt: I'm not saying that this is the solution to your problem. It usually manifests more subtly. – Lightness Races in Orbit Feb 07 '13 at 12:00
  • 1
    **What** is that "exception error"? Because honestly it's the most important part. – Bartek Banachewicz Feb 07 '13 at 16:59
  • Vector subscript out of range at line 1440. I've started rewriting the loader now and I've broken it a bit, so I'll let you know when I get it fixed up tomorrow exactly what the error is! – DerryHolt Feb 07 '13 at 23:33

1 Answers1

0

Are you intending not to draw the first triangle? The proper call to draw all triangles should be glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size()); Before that you also need to enable calls and set the arrays up by glEnableClientState(GL_VERTEX_ARRAY) and glVertexPointer(). The same for other attributes like color and normals if you want them. BTW, all of this is deprecated since OpenGL 3.0 onward, so you may want to learn the core 3.0 API instead if you are just starting.

Joe
  • 2,008
  • 1
  • 17
  • 25
  • In all honesty I don't intend on continuing openGL much past the end of this module for my degree. We start DirectX next year which is much more inviting! I'm using the enable client states and pointers, yes. – DerryHolt Feb 07 '13 at 23:28
  • Just wondering, but why is DirectX more appealing to you? There is no major functional difference that I am aware of. – Joe Feb 08 '13 at 23:49