所以这是使用opengl的c ++中的obj加载器它似乎将我输入的立方体显示为平面它似乎正确定位它们但我尝试过的所有对象都作为平面出现(见图)
https://docs.google.com/file/d/0B8AdFl9H3IJVRG9FbFo1UnpWaUE/edit?usp=sharing
我尝试过其他 3d 对象,它们似乎都像这架飞机一样出现(可能大小不同)
我试过的
- 在线查看其他 obj loader
- 慢慢运行调试
- 检查向量是否正确加载
- 检查读入是否正确
- 项目清单
类型定义.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;
加载器.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);
};
加载程序.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();
}
在main里面我调用了一个叫做banner的函数
void Banner()
{
glTranslatef(15000.0,11000.0,25000.0);
glScalef(100,100,100);
lod.draw();
}
它被糟糕地创建为一个全球性的