added support for a file-loading function, possibly to load objects/materials from data in memory
git-svn-id: svn://anubis/misc/wfobj@41 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
parent
9c3944c98e
commit
64fea9c370
87
WFObj.cc
87
WFObj.cc
@ -10,6 +10,7 @@
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include "WFObj.hh"
|
||||
using namespace std;
|
||||
|
||||
@ -83,10 +84,27 @@ int WFObj::filesize(const char * filename)
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
bool WFObj::load(const string & filename, loadTextureFunc_t loadTexture)
|
||||
bool WFObj::load(const string & filename,
|
||||
loadTextureFunc_t loadTexture,
|
||||
loadFileFunc_t loadFile)
|
||||
{
|
||||
clear();
|
||||
|
||||
if (loadFile)
|
||||
{
|
||||
unsigned int size;
|
||||
const char * filedat = (char *) loadFile(filename.c_str(), &size);
|
||||
if (filedat && size > 0)
|
||||
{
|
||||
string str(filedat, size);
|
||||
stringstream istr(str, ios_base::in);
|
||||
load(istr, size);
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
int size = filesize(filename.c_str());
|
||||
if (size < 1)
|
||||
return false;
|
||||
@ -94,12 +112,26 @@ bool WFObj::load(const string & filename, loadTextureFunc_t loadTexture)
|
||||
ifstream ifs(filename.c_str());
|
||||
if (!ifs.is_open())
|
||||
return false;
|
||||
|
||||
load(ifs, size);
|
||||
|
||||
ifs.close();
|
||||
}
|
||||
|
||||
m_loadTexture = loadTexture;
|
||||
m_loadFile = loadFile;
|
||||
m_fileName = filename;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WFObj::load(std::istream & istr, unsigned int size)
|
||||
{
|
||||
char buf[size+1];
|
||||
|
||||
string buildup;
|
||||
while (ifs.good())
|
||||
while (istr.good())
|
||||
{
|
||||
ifs.getline(buf, size+1);
|
||||
istr.getline(buf, size+1);
|
||||
string input = trimString(buf);
|
||||
int sz = input.size();
|
||||
if (sz == 0 || input[0] == '#')
|
||||
@ -115,14 +147,9 @@ bool WFObj::load(const string & filename, loadTextureFunc_t loadTexture)
|
||||
buildup = "";
|
||||
processInputLine(input);
|
||||
}
|
||||
|
||||
ifs.close();
|
||||
m_loadTexture = loadTexture;
|
||||
m_fileName = filename;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void WFObj::processInputLine(const std::string & input)
|
||||
{
|
||||
string line = input;
|
||||
@ -284,7 +311,9 @@ GLuint WFObj::render()
|
||||
{
|
||||
if (m_data[i].size() >= 2)
|
||||
{
|
||||
material.load(basePath(m_fileName) + m_data[i][1], m_loadTexture);
|
||||
material.load(basePath(m_fileName) + m_data[i][1],
|
||||
m_loadTexture,
|
||||
m_loadFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -370,10 +399,27 @@ int WFMtl::filesize(const char * filename)
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
bool WFMtl::load(const string & filename, WFObj::loadTextureFunc_t loadTexture)
|
||||
bool WFMtl::load(const string & filename,
|
||||
WFObj::loadTextureFunc_t loadTexture,
|
||||
WFObj::loadFileFunc_t loadFile)
|
||||
{
|
||||
clear();
|
||||
|
||||
if (loadFile)
|
||||
{
|
||||
unsigned int size;
|
||||
const char * filedat = (char *) loadFile(filename.c_str(), &size);
|
||||
if (filedat && size > 0)
|
||||
{
|
||||
string str(filedat, size);
|
||||
stringstream istr(str, ios_base::in);
|
||||
load(istr, size);
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
int size = filesize(filename.c_str());
|
||||
if (size < 1)
|
||||
return false;
|
||||
@ -381,12 +427,25 @@ bool WFMtl::load(const string & filename, WFObj::loadTextureFunc_t loadTexture)
|
||||
ifstream ifs(filename.c_str());
|
||||
if (!ifs.is_open())
|
||||
return false;
|
||||
|
||||
load(ifs, size);
|
||||
|
||||
ifs.close();
|
||||
}
|
||||
|
||||
m_loadTexture = loadTexture;
|
||||
m_fileName = filename;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WFMtl::load(std::istream & istr, unsigned int size)
|
||||
{
|
||||
char buf[size+1];
|
||||
|
||||
string buildup;
|
||||
while (ifs.good())
|
||||
while (istr.good())
|
||||
{
|
||||
ifs.getline(buf, size+1);
|
||||
istr.getline(buf, size+1);
|
||||
string input = trimString(buf);
|
||||
int sz = input.size();
|
||||
if (sz == 0 || input[0] == '#')
|
||||
@ -402,10 +461,6 @@ bool WFMtl::load(const string & filename, WFObj::loadTextureFunc_t loadTexture)
|
||||
buildup = "";
|
||||
processInputLine(input);
|
||||
}
|
||||
|
||||
ifs.close();
|
||||
m_loadTexture = loadTexture;
|
||||
m_fileName = filename;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
13
WFObj.hh
13
WFObj.hh
@ -8,7 +8,11 @@ class WFObj
|
||||
{
|
||||
public:
|
||||
typedef GLuint (*loadTextureFunc_t)(const char * texture);
|
||||
bool load(const std::string & filename, loadTextureFunc_t loadTexture);
|
||||
typedef void * (*loadFileFunc_t)(const char * filename,
|
||||
unsigned int * length);
|
||||
bool load(const std::string & filename,
|
||||
loadTextureFunc_t loadTexture = NULL,
|
||||
loadFileFunc_t loadFile = NULL);
|
||||
GLuint render();
|
||||
const float * const getAABB() { return m_aabb; }
|
||||
|
||||
@ -31,10 +35,12 @@ protected:
|
||||
Vertex readVertex(const std::vector<std::string> & parts);
|
||||
void parseVertexIndices(const std::string & vtxref, int * ret);
|
||||
void updateAABB(const float * const vertex);
|
||||
bool load(std::istream & istr, unsigned int size);
|
||||
|
||||
/* variables */
|
||||
std::vector< std::vector<std::string> > m_data;
|
||||
loadTextureFunc_t m_loadTexture;
|
||||
loadFileFunc_t m_loadFile;
|
||||
std::string m_fileName;
|
||||
float m_aabb[6];
|
||||
bool m_loadedVertex;
|
||||
@ -44,7 +50,9 @@ protected:
|
||||
class WFMtl
|
||||
{
|
||||
public:
|
||||
bool load(const std::string & filename, WFObj::loadTextureFunc_t loadTexture);
|
||||
bool load(const std::string & filename,
|
||||
WFObj::loadTextureFunc_t loadTexture = NULL,
|
||||
WFObj::loadFileFunc_t loadFile = NULL);
|
||||
void renderBegin(const std::string & mtlname);
|
||||
void renderEnd(const std::string & mtlname);
|
||||
|
||||
@ -54,6 +62,7 @@ protected:
|
||||
int filesize(const char * filename);
|
||||
void processInputLine(const std::string & input);
|
||||
void pushAttributes();
|
||||
bool load(std::istream & istr, unsigned int size);
|
||||
|
||||
/* variables */
|
||||
std::map< std::string, std::vector< std::vector<std::string> > > m_data;
|
||||
|
Loading…
x
Reference in New Issue
Block a user