fixed error in loader
This commit is contained in:
parent
abde81c527
commit
b413515214
@ -45,5 +45,9 @@ int main(){
|
||||
for(GraphNode* gn: path){
|
||||
std::cout << gn->getValue() << std::endl;
|
||||
}*/
|
||||
std::vector<Mesh*> meshes = Loader::loadMesh("/data/Qt_workspace/sparrowengine/data/boulder.obj");
|
||||
Loader::setObjDirectory("../data/");
|
||||
Loader::setMtlDirectory("../data/");
|
||||
Loader::setTexDirectory("../data/");
|
||||
std::vector<Mesh*> meshes = Loader::loadMesh("sword.obj");
|
||||
|
||||
}
|
||||
|
@ -11,6 +11,11 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
std::string Loader::obj_directory = "";
|
||||
std::string Loader::mtl_directory = "";
|
||||
std::string Loader::tex_directory = "";
|
||||
|
||||
|
||||
std::string* Loader::loadTextFile(const std::string &filename)
|
||||
{
|
||||
std::ifstream t(filename);
|
||||
@ -28,7 +33,7 @@ std::string* Loader::loadTextFile(const std::string &filename)
|
||||
Image* Loader::loadImage(const std::string &filename, bool hasAlpha)
|
||||
{
|
||||
sf::Image sfImg;
|
||||
sfImg.loadFromFile(filename);
|
||||
sfImg.loadFromFile(tex_directory+filename);
|
||||
Image* img = new Image();
|
||||
img->depth = hasAlpha ? 32 : 24;
|
||||
img->width = sfImg.getSize().x;
|
||||
@ -64,8 +69,7 @@ std::vector<Mesh*> Loader::loadMesh(const std::string &filename){
|
||||
RESOURCE_ADD(defaultMat,Material,"default");
|
||||
}
|
||||
Material* currentMat = defaultMat;
|
||||
|
||||
std::ifstream file(filename);
|
||||
std::ifstream file(obj_directory + filename);
|
||||
|
||||
if(!file.is_open())
|
||||
{
|
||||
@ -150,22 +154,23 @@ std::vector<Mesh*> Loader::loadMesh(const std::string &filename){
|
||||
break;
|
||||
case 'm': // mtllib
|
||||
{
|
||||
char* mat_filename;
|
||||
char mat_filename[256];
|
||||
std::sscanf(line.c_str(),"mtllib %s",mat_filename);
|
||||
loadMTL(mat_filename);
|
||||
loadMTL(std::string(mat_filename));
|
||||
break;
|
||||
}
|
||||
case 'u':
|
||||
{
|
||||
// usemtl
|
||||
std::string mat_name;
|
||||
std::sscanf(line.c_str(),"u %s",mat_name);
|
||||
currentMat = RESOURCE_GET(Material, mat_name);
|
||||
char mat_name[256];
|
||||
std::sscanf(line.c_str(),"usemtl %s",mat_name);
|
||||
std::string material_name(mat_name);
|
||||
currentMat = RESOURCE_GET(Material, material_name);
|
||||
if(currentMat == NULL)
|
||||
{
|
||||
fprintf(stderr, "cannot find any material named : %s.\n", mat_name.c_str());
|
||||
fprintf(stderr, "cannot find any material named : %s.\n", material_name.c_str());
|
||||
currentMat = new PhongMaterial();
|
||||
RESOURCE_ADD(currentMat,Material,mat_name);
|
||||
RESOURCE_ADD(currentMat,Material,material_name);
|
||||
}
|
||||
currentMesh->setMaterial(currentMat);
|
||||
}
|
||||
@ -190,6 +195,8 @@ std::vector<Mesh*> Loader::loadMesh(const std::string &filename){
|
||||
return meshes;
|
||||
}
|
||||
|
||||
|
||||
//move this to an utils file ?
|
||||
std::vector<std::string> split(const std::string &line, char sep){
|
||||
std::vector<std::string> tokens;
|
||||
std::size_t start=0, end=0;
|
||||
@ -205,14 +212,11 @@ std::vector<std::string> split(const std::string &line, char sep){
|
||||
bool Loader::loadMTL(const std::string &filename)
|
||||
{
|
||||
std::string line;
|
||||
std::ifstream file(filename);
|
||||
std::string foo = filename;
|
||||
std::cout << foo;
|
||||
std::ifstream file(mtl_directory + filename);
|
||||
|
||||
if(!file.is_open())
|
||||
{
|
||||
std::wcout << "filename" << std::endl;
|
||||
// fprintf(stderr, "can't load %s.\n", filename.c_str());
|
||||
fprintf(stderr, "can't load %s.\n", filename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -306,6 +310,16 @@ bool Loader::loadMTL(const std::string &filename)
|
||||
return hasNormalMap;
|
||||
}
|
||||
|
||||
void Loader::setObjDirectory(std::string dir_){
|
||||
obj_directory = dir_;
|
||||
}
|
||||
void Loader::setMtlDirectory(std::string dir_){
|
||||
mtl_directory = dir_;
|
||||
}
|
||||
void Loader::setTexDirectory(std::string dir_){
|
||||
tex_directory = dir_;
|
||||
}
|
||||
|
||||
/*
|
||||
//glfinish
|
||||
void temp_glfinish(){
|
||||
|
@ -9,11 +9,19 @@ class Mesh;
|
||||
|
||||
class Loader
|
||||
{
|
||||
static std::string obj_directory;
|
||||
static std::string mtl_directory;
|
||||
static std::string tex_directory;
|
||||
|
||||
public:
|
||||
static std::string* loadTextFile(const std::string &filename);
|
||||
static Image* loadImage(const std::string &filename, bool hasAlpha = true);
|
||||
static std::vector<Mesh*> loadMesh(const std::string &filename);
|
||||
static bool loadMTL(const std::string &filename);
|
||||
|
||||
static void setObjDirectory(std::string);
|
||||
static void setMtlDirectory(std::string);
|
||||
static void setTexDirectory(std::string);
|
||||
};
|
||||
|
||||
#endif // LOADER_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user