Obj Loader无法正常显示

时间:2013-09-16 09:17:24

标签: c++ opengl loader freeglut wavefront

所以这是使用opengl的c ++中的obj加载器 它似乎显示我输入的立方体作为一个平面它似乎正确定位它们但我尝试过的所有对象都作为一个平面出现(见图片)

https://docs.google.com/file/d/0B8AdFl9H3IJVRG9FbFo1UnpWaUE/edit?usp=sharing

我已经尝试过其他三维物体了,它们似乎都像这架飞机一样出现(尺寸可能不同)

我尝试了什么

  • 在线浏览其他obj加载器
  • 慢慢地完成调试
  • 正确检查向量负载
  • 检查读数是否为corect
  • 列表项

typedef.h

#pragma

typedef struct {
    float x,y,z;

} points;

typedef struct {
    float vn[3];    // store the ve3rtex normals
} normal;                       


typedef struct {
    float vt[3];    // store the texture coordinates
} coordinate;


typedef struct {
    int shaders;
    points p[3];
    normal norm[3];
    coordinate coord[3];
} face;

loader.h

#pragma once

#include <string>
#include <vector>
#include <fstream>
#include <GL/freeglut.h>

#include<iostream> // this is just for testing, you can remove this with all the cout's

#include "typedef.h"


class Loader
{
public:
    Loader(std::string input);
    ~Loader(void);

    void draw(); // this function takes the obj file and draws it

private:

    std::ifstream m_inFile;


    // the list of vectors that i will be using
    std::vector<points> m_points;
    std::vector<normal> m_normals;
    std::vector<coordinate> m_coords;
    std::vector<face> m_faces;

    void process(std::string input);

    void inputPoints(points temp);
    void inputNormals(normal temp);
    void inputCoordinates(coordinate temp);
    void createFaces(face temp);



};

loader.cpp

#include "Loader.h"


Loader::Loader(std::string input)
{
    process(input);
}


Loader::~Loader(void)
{

}

void Loader::process(std::string input)
{
    std::string identifier; //used to identify where the input should go


    points temppoint;
    normal tempnormal;
    coordinate tempcoord;
    face tempface; 


    std::string read;       // used to read the curent line

    int readNum; // this is the number that has just been read in
    char skip; // a char to skip thr /
    int i;
    int count= 1;


    m_inFile.open(input);
    /* // check to see if it read
    if(!m_inFile) 
        std::cout << "did not read";
    else
        std::cout << "file read";
    */


    //creation of the reading loop
    //while(!m_inFile.eof())
    m_inFile >> identifier;

    do {
         // check to see what the opening is

        if (identifier =="#")
        {
            getline(m_inFile,read); // use this to read the whole line

        }
        else if(identifier == "v")
        {
            m_inFile >> temppoint.x >> temppoint.y >> temppoint.z;
            inputPoints(temppoint);
        }
        else if(identifier == "vn")
        {
            m_inFile >> tempnormal.vn[0] >> tempnormal.vn[1] >> tempnormal.vn[2];
            inputNormals(tempnormal);
        }
        else if (identifier == "vt")
        {
            m_inFile >> tempcoord.vt[0] >> tempcoord.vt[1] >> tempcoord.vt[2];
            inputCoordinates(tempcoord);
        }
        else if(identifier == "f")
        {


            for( i =0; i < 3; i++)
            {
                //std::cout << "loops: " << count << std::endl;
                count++;
                //if(read == "Material.001")
                //  std::cout << std::endl;

                //std::cout << "Iteration: " << i << std::endl;
                m_inFile >> readNum;
                if(readNum == 0)
                    break;

                readNum--;

                tempface.p[i].x = m_points[readNum].x;

                tempface.p[i].y = m_points[readNum].x;

                tempface.p[i].z = m_points[readNum].z;



                m_inFile >> skip >> readNum;

                readNum--;

                tempface.coord[i].vt[0] = m_coords[readNum].vt[0];

                tempface.coord[i].vt[1] = m_coords[readNum].vt[1];

                tempface.coord[i].vt[2] = m_coords[readNum].vt[2];



                m_inFile >> skip >> readNum;

                readNum--;

                tempface.norm[i].vn[0] = m_normals[readNum].vn[0];

                tempface.norm[i].vn[1] = m_normals[readNum].vn[1];

                tempface.norm[i].vn[2] = m_normals[readNum].vn[2];



            }

            createFaces(tempface);

        }
        else 
        {
            getline(m_inFile,read);
            std::cout << "Not Processed " << identifier << " " << read <<std::endl;
        }

        m_inFile >> identifier;

    } while (!m_inFile.eof());

}

void Loader::inputPoints(points temp)
{
    m_points.push_back(temp);

}

void Loader::inputNormals(normal temp)
{
    m_normals.push_back(temp);
}

void Loader::inputCoordinates(coordinate temp)
{
    m_coords.push_back(temp);
}

void Loader::createFaces(face temp)
{
    m_faces.push_back(temp);
}

void Loader::draw()
{
    int i;
    int j;
    glBegin(GL_TRIANGLES);
    for (i=0; i < m_faces.size();i++)
    {
        //std::cout<<"glBegin" <<std::endl;

        for(j = 0 ; j < 3; j++)
        {
            //std::cout << "Vn1: "<< m_faces[i].norm[j].vn[0] << "Vn2: " << m_faces[i].norm[j].vn[1] <<"Vn3: "<< m_faces[i].norm[j].vn[2] << std::endl;
            //std::cout << "X: " << m_faces[i].p[j].x << "Y: " << m_faces[i].p[j].y << "Z: " << m_faces[i].p[j].z << std::endl;
            //glNormal3f(m_faces[i].norm[j].vn[0],m_faces[i].norm[j].vn[1],m_faces[i].norm[j].vn[2]);
            glVertex3f(m_faces[i].p[j].x,m_faces[i].p[j].y,m_faces[i].p[j].z);
        }
        //glEnd();
        //std::cout << "glEnd()" << std::endl <<std::endl;
    }
    glEnd();
}

在主内部我调用一个名为banner

的函数
void Banner()
   {




    glTranslatef(15000.0,11000.0,25000.0);
    glScalef(100,100,100);
    lod.draw();




}

这是一个糟糕的全球

0 个答案:

没有答案