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