Greetings guys,
so I was making an ObjLoader and am loading the vertices correct , but when am trying to draw it's not working at all , it gives me a white rectangle :s, here is the source code:
Display All
Display All
where the vertices are :
and am loading it correctly , what am doing wrong here !?
here is an image with the issue
[IMG:http://i48.tinypic.com/i1lf2a.jpg]
so I was making an ObjLoader and am loading the vertices correct , but when am trying to draw it's not working at all , it gives me a white rectangle :s, here is the source code:
Source Code
- //loader
- struct Model
- {
- double X,Y,Z;
- std::vector<std::array<float,3>> Vertices;
- std::vector<std::array<float,3>> Faces;
- std::vector<std::array<float,3>> VerticesNormals;
- std::vector<std::array<float,3>> VerticesTexture;
- };
- class ModelsLoader
- {
- public:
- ModelsLoader(void);
- ~ModelsLoader(void);
- Model LoadModel(const char * ModelName);
- std::vector<Model> GetModels();
- private:
- unsigned int Split(const std::string &txt, std::vector<std::string> &strs, char ch);
- std::vector<Model> Models;
- };
- Model ModelsLoader::LoadModel(const char * ModelName){
- std::ifstream fs(ModelName);
- std::string line;
- Model m;
- if(fs.is_open()){
- std::vector<std::string> strs;
- std::vector<std::string> faces;
- std::array<float,3> temp;
- m.X=0;
- m.Y=0;
- m.Z=0;
- while (std::getline(fs,line))
- {
- Split(line,strs,' ');
- if (line.compare(0, 2, "v ") == 0 ){
- temp[0] = ::atof(strs[2].c_str());
- temp[1] = ::atof(strs[3].c_str());
- temp[2] = ::atof(strs[4].c_str());
- m.Vertices.push_back(temp);
- }
- if (line.compare(0, 1, "f") == 0 ){
- for (int i = 1; i < 4; i++)
- {
- Split(strs[i],faces, '/');
- temp[0] = ::atof(faces[0].c_str());
- temp[1] = ::atof(faces[1].c_str());
- temp[2] = ::atof(faces[2].c_str());
- m.Faces.push_back(temp);
- }
- }
- if (line.compare(0, 2, "vn") == 0 ){
- temp[0] = ::atof(strs[1].c_str());
- temp[1] = ::atof(strs[2].c_str());
- temp[2] = ::atof(strs[3].c_str());
- m.VerticesNormals.push_back(temp);
- }
- if (line.compare(0, 2, "vt") == 0 ){
- temp[0] = ::atof(strs[2].c_str());
- temp[1] = ::atof(strs[3].c_str());
- temp[2] = 0.0f;
- m.VerticesTexture.push_back(temp);
- }
- }
- Models.push_back(m);
- fs.close();
- }
- return m;
- }
Source Code
- //drawing
- void handleResize(int w, int h) {
- //Tell OpenGL how to convert from coordinates to pixel values
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
- //Set the camera perspective
- glLoadIdentity(); //Reset the camera
- gluPerspective(45.0, //The camera angle
- (double)w / (double)h, //The width-to-height ratio
- 1.0, //The near z clipping coordinate
- 200.0); //The far z clipping coordinate
- }
- void initRendering() {
- //Makes 3D drawing work when something is in front of something else
- glEnable(GL_DEPTH_TEST);
- }
- void drawScene() {
- //Clear information from last draw
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
- glLoadIdentity(); //Reset the drawing perspective
- auto model = loader.GetModels()[0];
- glBegin(GL_TRIANGLES); //Begin quadrilateral coordinates
- for (int i = 0; i < model.Vertices.size(); i++)
- {
- glVertex3f(model.Vertices[i][0],model.Vertices[i][1],model.Vertices[i][2]);
- }
- glEnd(); //End triangle coordinates
- glutSwapBuffers(); //Send the 3D scene to the screen
- }
- int main(int argc, char** argv) {
- //Initialize GLUT
- loader.LoadModel("test.obj");
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(400, 400); //Set the window size
- //Create the window
- glutCreateWindow("test the loader");
- initRendering(); //Initialize rendering
- //Set handler functions for drawing, keypresses, and window resizes
- glutDisplayFunc(drawScene);
- glutKeyboardFunc(handleKeypress);
- glutReshapeFunc(handleResize);
- glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
- return 0; //This line is never reached
- }
where the vertices are :
and am loading it correctly , what am doing wrong here !?
here is an image with the issue
[IMG:http://i48.tinypic.com/i1lf2a.jpg]
The post was edited 1 time, last by Abanoub ().