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
111
WFObj.cc
111
WFObj.cc
@ -10,6 +10,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
#include "WFObj.hh"
|
#include "WFObj.hh"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -83,23 +84,54 @@ int WFObj::filesize(const char * filename)
|
|||||||
return st.st_size;
|
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();
|
clear();
|
||||||
|
|
||||||
int size = filesize(filename.c_str());
|
if (loadFile)
|
||||||
if (size < 1)
|
{
|
||||||
return false;
|
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;
|
||||||
|
|
||||||
ifstream ifs(filename.c_str());
|
ifstream ifs(filename.c_str());
|
||||||
if (!ifs.is_open())
|
if (!ifs.is_open())
|
||||||
return false;
|
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];
|
char buf[size+1];
|
||||||
|
|
||||||
string buildup;
|
string buildup;
|
||||||
while (ifs.good())
|
while (istr.good())
|
||||||
{
|
{
|
||||||
ifs.getline(buf, size+1);
|
istr.getline(buf, size+1);
|
||||||
string input = trimString(buf);
|
string input = trimString(buf);
|
||||||
int sz = input.size();
|
int sz = input.size();
|
||||||
if (sz == 0 || input[0] == '#')
|
if (sz == 0 || input[0] == '#')
|
||||||
@ -115,14 +147,9 @@ bool WFObj::load(const string & filename, loadTextureFunc_t loadTexture)
|
|||||||
buildup = "";
|
buildup = "";
|
||||||
processInputLine(input);
|
processInputLine(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
ifs.close();
|
|
||||||
m_loadTexture = loadTexture;
|
|
||||||
m_fileName = filename;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void WFObj::processInputLine(const std::string & input)
|
void WFObj::processInputLine(const std::string & input)
|
||||||
{
|
{
|
||||||
string line = input;
|
string line = input;
|
||||||
@ -284,7 +311,9 @@ GLuint WFObj::render()
|
|||||||
{
|
{
|
||||||
if (m_data[i].size() >= 2)
|
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,23 +399,53 @@ int WFMtl::filesize(const char * filename)
|
|||||||
return st.st_size;
|
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();
|
clear();
|
||||||
|
|
||||||
int size = filesize(filename.c_str());
|
if (loadFile)
|
||||||
if (size < 1)
|
{
|
||||||
return false;
|
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;
|
||||||
|
|
||||||
ifstream ifs(filename.c_str());
|
ifstream ifs(filename.c_str());
|
||||||
if (!ifs.is_open())
|
if (!ifs.is_open())
|
||||||
return false;
|
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];
|
char buf[size+1];
|
||||||
|
|
||||||
string buildup;
|
string buildup;
|
||||||
while (ifs.good())
|
while (istr.good())
|
||||||
{
|
{
|
||||||
ifs.getline(buf, size+1);
|
istr.getline(buf, size+1);
|
||||||
string input = trimString(buf);
|
string input = trimString(buf);
|
||||||
int sz = input.size();
|
int sz = input.size();
|
||||||
if (sz == 0 || input[0] == '#')
|
if (sz == 0 || input[0] == '#')
|
||||||
@ -402,10 +461,6 @@ bool WFMtl::load(const string & filename, WFObj::loadTextureFunc_t loadTexture)
|
|||||||
buildup = "";
|
buildup = "";
|
||||||
processInputLine(input);
|
processInputLine(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
ifs.close();
|
|
||||||
m_loadTexture = loadTexture;
|
|
||||||
m_fileName = filename;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
WFObj.hh
13
WFObj.hh
@ -8,7 +8,11 @@ class WFObj
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef GLuint (*loadTextureFunc_t)(const char * texture);
|
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();
|
GLuint render();
|
||||||
const float * const getAABB() { return m_aabb; }
|
const float * const getAABB() { return m_aabb; }
|
||||||
|
|
||||||
@ -31,10 +35,12 @@ protected:
|
|||||||
Vertex readVertex(const std::vector<std::string> & parts);
|
Vertex readVertex(const std::vector<std::string> & parts);
|
||||||
void parseVertexIndices(const std::string & vtxref, int * ret);
|
void parseVertexIndices(const std::string & vtxref, int * ret);
|
||||||
void updateAABB(const float * const vertex);
|
void updateAABB(const float * const vertex);
|
||||||
|
bool load(std::istream & istr, unsigned int size);
|
||||||
|
|
||||||
/* variables */
|
/* variables */
|
||||||
std::vector< std::vector<std::string> > m_data;
|
std::vector< std::vector<std::string> > m_data;
|
||||||
loadTextureFunc_t m_loadTexture;
|
loadTextureFunc_t m_loadTexture;
|
||||||
|
loadFileFunc_t m_loadFile;
|
||||||
std::string m_fileName;
|
std::string m_fileName;
|
||||||
float m_aabb[6];
|
float m_aabb[6];
|
||||||
bool m_loadedVertex;
|
bool m_loadedVertex;
|
||||||
@ -44,7 +50,9 @@ protected:
|
|||||||
class WFMtl
|
class WFMtl
|
||||||
{
|
{
|
||||||
public:
|
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 renderBegin(const std::string & mtlname);
|
||||||
void renderEnd(const std::string & mtlname);
|
void renderEnd(const std::string & mtlname);
|
||||||
|
|
||||||
@ -54,6 +62,7 @@ protected:
|
|||||||
int filesize(const char * filename);
|
int filesize(const char * filename);
|
||||||
void processInputLine(const std::string & input);
|
void processInputLine(const std::string & input);
|
||||||
void pushAttributes();
|
void pushAttributes();
|
||||||
|
bool load(std::istream & istr, unsigned int size);
|
||||||
|
|
||||||
/* variables */
|
/* variables */
|
||||||
std::map< std::string, std::vector< std::vector<std::string> > > m_data;
|
std::map< std::string, std::vector< std::vector<std::string> > > m_data;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user