1

我创建了一个 obj 加载器并检查了模型加载器功能是否正常工作。它输出所有相关信息,让我认为这不是我遇到的问题。一切似乎都工作正常,但对象只是没有绘制到 opengl 窗口。

glEnableClientState(GL_VERTEX_ARRAY);

// tell OpenGL where the vertices are with glVertexPointer()
glVertexPointer(3, GL_FLOAT, 0, &vertices[0]);


//*************************************************************
// Set color and depth clear value
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f,    0.f);

// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);

// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);

这就是我认为可能是问题所在。或在这里..

if((float)Clock.GetElapsedTime()>REFRESH_RATE){
        // Clear colour and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        //glTranslatef(0.f, 0.f, -50.f);


        //draw the triangles with glDrawArrays() and then with       glDrawElements()
        Clock.Reset();
        glDrawArrays(GL_TRIANGLES, 0, vertices.size()/3);

    }

这是模型加载器功能...

bool ModelLoader(string fileName, vector<GLfloat>& vertices, vector<GLushort>& indices, vector<GLfloat>& normals)
GLushort ind1, ind2, ind3; // declaring gl variables and variables
GLfloat vertex[3];
GLushort texel[3];
GLfloat normal[3];
GLuint indvalue = 1;
fstream objectFile;
string dataLine;
stringstream mystream;
GLfloat x, y, z;
vertices.clear();
indices.clear();
vector<GLfloat> localVertices;

for (int i = 0; i < 3; i++)
{
    vertices.push_back(0);
    cout << endl << "Vertices Pushed Back" << endl;
}

for (int i = 0; i < 3; i++)
{
    localVertices.push_back(0);
    cout << endl << "Local Vertices Pushed Back" << endl;
}

objectFile.open("objs/Cube.obj"); 
if (!objectFile.good()) // error checking to see if object file is loaded
    cout<<endl<<"Error: Could not load object "<<endl;

while (!objectFile.eof())
{
    getline(objectFile, dataLine,'\n'); // get line of the file
    mystream.clear(); // clear stream
    mystream.str(dataLine); // stream = dataline
    if (dataLine.empty()) // skip line if empty
    {
        cout << endl << " line is empty: Skipped line" << endl;
    }
    else if (dataLine[0] == 'v' && dataLine[1] == ' ') // if first char is v and second is space
    {
        mystream.ignore(2); // ignore the first two chars
        mystream >> x >> y >> z; // input local vertices to x, y and z
        localVertices.push_back(x); // push back each vertex
        localVertices.push_back(y);
        localVertices.push_back(z);
        printf("\nVertices: %f, %f, %f", x, y, z); // output for debugging
        cout << endl << "Local x, y and z vertices pushed back" << endl;
    }
    else if (dataLine[0] == 'f' && dataLine[1] == ' ') // if the first characted of the line is an f and the second is a space
    {
        cout << endl << "f found" << endl; // output for debugging
        cout << mystream.str() << endl;

        mystream.ignore(2); // ignore first two characters

        for(int i = 0; i < 3; ++i)
        {
            cout << endl << mystream.str()<< endl;

            mystream >> vertex[i]; // put value of mystream into vertex i 
            cout << endl << vertex[i] << endl; // output for debugging 

            mystream.ignore(1);

            if (mystream.peek() == '/') // if the next char = /
            {
                mystream.ignore(1); // ignore that char
            }

            else
            {
                mystream >> texel[i]; // input  value from my stream into texel i
                cout << endl << texel[i] << endl;
                mystream.ignore(1);
            }
            // ****************************** //
            mystream >> normal[i];
            cout << endl << normal[i] << endl;
            // ****************************** //
        }

        vertices.push_back(localVertices[vertex[0]* 3 + 0]); // x value  push back local vertices
        vertices.push_back(localVertices[vertex[0]* 3 + 1]); // y value 
        vertices.push_back(localVertices[vertex[0]* 3 + 2]); // z value 
        vertices.push_back(localVertices[vertex[1]* 3 + 0]); // x value 
        vertices.push_back(localVertices[vertex[1]* 3 + 1]); // y value 
        vertices.push_back(localVertices[vertex[1]* 3 + 2]); // z value 
        vertices.push_back(localVertices[vertex[2]* 3 + 0]); // x value 
        vertices.push_back(localVertices[vertex[2]* 3 + 1]); // y value 
        vertices.push_back(localVertices[vertex[2]* 3 + 2]); // z value 
        indices.push_back(indvalue); ++indvalue;  // push back indvalues 
        indices.push_back(indvalue); ++indvalue;
        indices.push_back(indvalue); ++indvalue;
        cout << endl << "vertices and indices pushed back" << endl;



    }

    mystream.clear(); // clear the stream
}
objectFile.close(); // close the file
cout << endl << "Function complete" << endl;
return true;
}
4

0 回答 0