do not call m_loadTexture if it is null, retabbed
git-svn-id: svn://anubis/misc/wfobj@42 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
parent
64fea9c370
commit
95bd26e1f2
805
WFObj.cc
805
WFObj.cc
@ -2,8 +2,8 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <ctype.h> // isspace()
|
#include <ctype.h> // isspace()
|
||||||
#include <string.h> // strlen()
|
#include <string.h> // strlen()
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -21,74 +21,74 @@ using namespace std;
|
|||||||
|
|
||||||
static string trimString(string s)
|
static string trimString(string s)
|
||||||
{
|
{
|
||||||
size_t lastpos = s.find_last_not_of(WHITESPACE);
|
size_t lastpos = s.find_last_not_of(WHITESPACE);
|
||||||
if (lastpos == string::npos)
|
if (lastpos == string::npos)
|
||||||
return "";
|
return "";
|
||||||
s.erase(lastpos + 1);
|
s.erase(lastpos + 1);
|
||||||
s.erase(0, s.find_first_not_of(WHITESPACE));
|
s.erase(0, s.find_first_not_of(WHITESPACE));
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
static string stripFirstToken(string & input)
|
static string stripFirstToken(string & input)
|
||||||
{
|
{
|
||||||
size_t firstnonspace = input.find_first_not_of(WHITESPACE);
|
size_t firstnonspace = input.find_first_not_of(WHITESPACE);
|
||||||
if (firstnonspace == string::npos)
|
if (firstnonspace == string::npos)
|
||||||
return "";
|
return "";
|
||||||
size_t spaceafter = input.find_first_of(WHITESPACE, firstnonspace);
|
size_t spaceafter = input.find_first_of(WHITESPACE, firstnonspace);
|
||||||
string token = input.substr(firstnonspace, spaceafter - firstnonspace);
|
string token = input.substr(firstnonspace, spaceafter - firstnonspace);
|
||||||
input.erase(0, spaceafter);
|
input.erase(0, spaceafter);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
static vector<string> splitString(const string & str, char delim)
|
static vector<string> splitString(const string & str, char delim)
|
||||||
{
|
{
|
||||||
vector<string> ret;
|
vector<string> ret;
|
||||||
string s = str;
|
string s = str;
|
||||||
size_t pos;
|
size_t pos;
|
||||||
while ( (pos = s.find(delim)) != string::npos )
|
while ( (pos = s.find(delim)) != string::npos )
|
||||||
{
|
{
|
||||||
string t = s.substr(0, pos);
|
string t = s.substr(0, pos);
|
||||||
ret.push_back(t);
|
ret.push_back(t);
|
||||||
s.erase(0, pos + 1);
|
s.erase(0, pos + 1);
|
||||||
}
|
}
|
||||||
if (s != "")
|
if (s != "")
|
||||||
ret.push_back(s);
|
ret.push_back(s);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static string basePath(const string & str)
|
static string basePath(const string & str)
|
||||||
{
|
{
|
||||||
string path = str;
|
string path = str;
|
||||||
size_t pos;
|
size_t pos;
|
||||||
if ( (pos = path.find_last_of("/\\")) != string::npos )
|
if ( (pos = path.find_last_of("/\\")) != string::npos )
|
||||||
{
|
{
|
||||||
path.erase(pos + 1);
|
path.erase(pos + 1);
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/****** WFObj functions ******/
|
/****** WFObj functions ******/
|
||||||
|
|
||||||
void WFObj::clear()
|
void WFObj::clear()
|
||||||
{
|
{
|
||||||
m_data = std::vector< std::vector<std::string> >();
|
m_data = std::vector< std::vector<std::string> >();
|
||||||
m_loadedVertex = false;
|
m_loadedVertex = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int WFObj::filesize(const char * filename)
|
int WFObj::filesize(const char * filename)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (stat(filename, &st))
|
if (stat(filename, &st))
|
||||||
return -1;
|
return -1;
|
||||||
return st.st_size;
|
return st.st_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WFObj::load(const string & filename,
|
bool WFObj::load(const string & filename,
|
||||||
loadTextureFunc_t loadTexture,
|
loadTextureFunc_t loadTexture,
|
||||||
loadFileFunc_t loadFile)
|
loadFileFunc_t loadFile)
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
if (loadFile)
|
if (loadFile)
|
||||||
{
|
{
|
||||||
@ -118,220 +118,220 @@ bool WFObj::load(const string & filename,
|
|||||||
ifs.close();
|
ifs.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_loadTexture = loadTexture;
|
m_loadTexture = loadTexture;
|
||||||
m_loadFile = loadFile;
|
m_loadFile = loadFile;
|
||||||
m_fileName = filename;
|
m_fileName = filename;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WFObj::load(std::istream & istr, unsigned int size)
|
bool WFObj::load(std::istream & istr, unsigned int size)
|
||||||
{
|
{
|
||||||
char buf[size+1];
|
char buf[size+1];
|
||||||
|
|
||||||
string buildup;
|
string buildup;
|
||||||
while (istr.good())
|
while (istr.good())
|
||||||
{
|
{
|
||||||
istr.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] == '#')
|
||||||
continue;
|
continue;
|
||||||
if (input[sz-1] == '\\')
|
if (input[sz-1] == '\\')
|
||||||
{
|
{
|
||||||
input[sz-1] = ' ';
|
input[sz-1] = ' ';
|
||||||
buildup = input;
|
buildup = input;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (buildup != "")
|
if (buildup != "")
|
||||||
input = buildup + input;
|
input = buildup + input;
|
||||||
buildup = "";
|
buildup = "";
|
||||||
processInputLine(input);
|
processInputLine(input);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFObj::processInputLine(const std::string & input)
|
void WFObj::processInputLine(const std::string & input)
|
||||||
{
|
{
|
||||||
string line = input;
|
string line = input;
|
||||||
vector<string> lineParts;
|
vector<string> lineParts;
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
string token = stripFirstToken(line);
|
string token = stripFirstToken(line);
|
||||||
if (token == "")
|
if (token == "")
|
||||||
break;
|
break;
|
||||||
lineParts.push_back(token);
|
lineParts.push_back(token);
|
||||||
}
|
}
|
||||||
if (lineParts.size() > 0)
|
if (lineParts.size() > 0)
|
||||||
m_data.push_back(lineParts);
|
m_data.push_back(lineParts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
GLuint WFObj::render()
|
GLuint WFObj::render()
|
||||||
{
|
{
|
||||||
GLuint list = glGenLists(1);
|
GLuint list = glGenLists(1);
|
||||||
glNewList(list, GL_COMPILE);
|
glNewList(list, GL_COMPILE);
|
||||||
int len = m_data.size();
|
int len = m_data.size();
|
||||||
enum { VERTEX, VERTEX_TEXTURE, VERTEX_NORMAL, VERTEX_TYPES };
|
enum { VERTEX, VERTEX_TEXTURE, VERTEX_NORMAL, VERTEX_TYPES };
|
||||||
vector<Vertex> vertices[VERTEX_TYPES];
|
vector<Vertex> vertices[VERTEX_TYPES];
|
||||||
int numVertsLast = 0;
|
int numVertsLast = 0;
|
||||||
bool inFace = false;
|
bool inFace = false;
|
||||||
bool inMaterial = false;
|
bool inMaterial = false;
|
||||||
string currentMaterialName;
|
string currentMaterialName;
|
||||||
WFMtl material;
|
WFMtl material;
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
string type = m_data[i][0];
|
string type = m_data[i][0];
|
||||||
if (type == "v")
|
if (type == "v")
|
||||||
vertices[VERTEX].push_back(readVertex(m_data[i]));
|
vertices[VERTEX].push_back(readVertex(m_data[i]));
|
||||||
else if (type == "vt")
|
else if (type == "vt")
|
||||||
vertices[VERTEX_TEXTURE].push_back(readVertex(m_data[i]));
|
vertices[VERTEX_TEXTURE].push_back(readVertex(m_data[i]));
|
||||||
else if (type == "vn")
|
else if (type == "vn")
|
||||||
vertices[VERTEX_NORMAL].push_back(readVertex(m_data[i]));
|
vertices[VERTEX_NORMAL].push_back(readVertex(m_data[i]));
|
||||||
else if (type == "f")
|
else if (type == "f")
|
||||||
{
|
{
|
||||||
int numVerts = m_data[i].size() - 1;
|
int numVerts = m_data[i].size() - 1;
|
||||||
if (inFace && (numVerts != numVertsLast || numVertsLast > 4))
|
if (inFace && (numVerts != numVertsLast || numVertsLast > 4))
|
||||||
{
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << "glEnd()" << endl;
|
cout << "glEnd()" << endl;
|
||||||
#endif
|
#endif
|
||||||
glEnd();
|
glEnd();
|
||||||
inFace = false;
|
inFace = false;
|
||||||
}
|
}
|
||||||
if (!inFace)
|
if (!inFace)
|
||||||
{
|
{
|
||||||
if (numVerts == 3)
|
if (numVerts == 3)
|
||||||
{
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << "glBegin(GL_TRIANGLES)" << endl;
|
cout << "glBegin(GL_TRIANGLES)" << endl;
|
||||||
#endif
|
#endif
|
||||||
glBegin(GL_TRIANGLES);
|
glBegin(GL_TRIANGLES);
|
||||||
}
|
}
|
||||||
else if (numVerts == 4)
|
else if (numVerts == 4)
|
||||||
{
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << "glBegin(GL_QUADS)" << endl;
|
cout << "glBegin(GL_QUADS)" << endl;
|
||||||
#endif
|
#endif
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << "glBegin(GL_POLYGON)" << endl;
|
cout << "glBegin(GL_POLYGON)" << endl;
|
||||||
#endif
|
#endif
|
||||||
glBegin(GL_POLYGON);
|
glBegin(GL_POLYGON);
|
||||||
}
|
}
|
||||||
inFace = true;
|
inFace = true;
|
||||||
}
|
}
|
||||||
for (int v = 1; v <= numVerts; v++)
|
for (int v = 1; v <= numVerts; v++)
|
||||||
{
|
{
|
||||||
int vertexIndices[3] = {0, 0, 0};
|
int vertexIndices[3] = {0, 0, 0};
|
||||||
parseVertexIndices(m_data[i][v], vertexIndices);
|
parseVertexIndices(m_data[i][v], vertexIndices);
|
||||||
for (int j = 0; j < 3; j++)
|
for (int j = 0; j < 3; j++)
|
||||||
{
|
{
|
||||||
if (vertexIndices[j] < 0)
|
if (vertexIndices[j] < 0)
|
||||||
vertexIndices[j] += vertices[j].size() + 1;
|
vertexIndices[j] += vertices[j].size() + 1;
|
||||||
}
|
}
|
||||||
bool valid = true;
|
bool valid = true;
|
||||||
for (int j = 0; j < 3; j++)
|
for (int j = 0; j < 3; j++)
|
||||||
{
|
{
|
||||||
if (vertexIndices[j] > vertices[j].size())
|
if (vertexIndices[j] > vertices[j].size())
|
||||||
{
|
{
|
||||||
valid = false;
|
valid = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (vertexIndices[VERTEX] <= 0)
|
if (vertexIndices[VERTEX] <= 0)
|
||||||
valid = false;
|
valid = false;
|
||||||
if (!valid)
|
if (!valid)
|
||||||
continue;
|
continue;
|
||||||
if (vertexIndices[VERTEX_NORMAL] != 0)
|
if (vertexIndices[VERTEX_NORMAL] != 0)
|
||||||
{
|
{
|
||||||
/* a normal is present */
|
/* a normal is present */
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glNormal3f(" <<
|
cout << " glNormal3f(" <<
|
||||||
vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1]
|
vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1]
|
||||||
[0] << ", " <<
|
[0] << ", " <<
|
||||||
vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1]
|
vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1]
|
||||||
[1] << ", " <<
|
[1] << ", " <<
|
||||||
vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1]
|
vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1]
|
||||||
[2] << ")" << endl;
|
[2] << ")" << endl;
|
||||||
#endif
|
#endif
|
||||||
glNormal3fv(vertices[VERTEX_NORMAL]
|
glNormal3fv(vertices[VERTEX_NORMAL]
|
||||||
[vertexIndices[VERTEX_NORMAL]-1].getData());
|
[vertexIndices[VERTEX_NORMAL]-1].getData());
|
||||||
}
|
}
|
||||||
if (vertexIndices[VERTEX_TEXTURE] != 0)
|
if (vertexIndices[VERTEX_TEXTURE] != 0)
|
||||||
{
|
{
|
||||||
/* a texture coordinate is present */
|
/* a texture coordinate is present */
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glTexCoord2f(" <<
|
cout << " glTexCoord2f(" <<
|
||||||
vertices[VERTEX_TEXTURE][vertexIndices[VERTEX_TEXTURE]-1]
|
vertices[VERTEX_TEXTURE][vertexIndices[VERTEX_TEXTURE]-1]
|
||||||
[0] << ", " <<
|
[0] << ", " <<
|
||||||
vertices[VERTEX_TEXTURE][vertexIndices[VERTEX_TEXTURE]-1]
|
vertices[VERTEX_TEXTURE][vertexIndices[VERTEX_TEXTURE]-1]
|
||||||
[1] << ')' << endl;
|
[1] << ')' << endl;
|
||||||
#endif
|
#endif
|
||||||
glTexCoord2fv(vertices[VERTEX_TEXTURE]
|
glTexCoord2fv(vertices[VERTEX_TEXTURE]
|
||||||
[vertexIndices[VERTEX_TEXTURE]-1].getData());
|
[vertexIndices[VERTEX_TEXTURE]-1].getData());
|
||||||
}
|
}
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glVertex3f(" <<
|
cout << " glVertex3f(" <<
|
||||||
vertices[VERTEX][vertexIndices[VERTEX]-1][0] << ", " <<
|
vertices[VERTEX][vertexIndices[VERTEX]-1][0] << ", " <<
|
||||||
vertices[VERTEX][vertexIndices[VERTEX]-1][1] << ", " <<
|
vertices[VERTEX][vertexIndices[VERTEX]-1][1] << ", " <<
|
||||||
vertices[VERTEX][vertexIndices[VERTEX]-1][2] << ")" <<
|
vertices[VERTEX][vertexIndices[VERTEX]-1][2] << ")" <<
|
||||||
" [" << vertexIndices[VERTEX] << "]" << endl;
|
" [" << vertexIndices[VERTEX] << "]" << endl;
|
||||||
#endif
|
#endif
|
||||||
glVertex3fv(vertices[VERTEX][vertexIndices[VERTEX]-1].getData());
|
glVertex3fv(vertices[VERTEX][vertexIndices[VERTEX]-1].getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
numVertsLast = numVerts;
|
numVertsLast = numVerts;
|
||||||
}
|
}
|
||||||
else if (type == "usemtl")
|
else if (type == "usemtl")
|
||||||
{
|
{
|
||||||
if (inFace)
|
if (inFace)
|
||||||
{
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << "glEnd()" << endl;
|
cout << "glEnd()" << endl;
|
||||||
#endif
|
#endif
|
||||||
glEnd();
|
glEnd();
|
||||||
inFace = false;
|
inFace = false;
|
||||||
}
|
}
|
||||||
if (inMaterial)
|
if (inMaterial)
|
||||||
{
|
{
|
||||||
material.renderEnd(currentMaterialName);
|
material.renderEnd(currentMaterialName);
|
||||||
inMaterial = false;
|
inMaterial = false;
|
||||||
}
|
}
|
||||||
if (m_data[i].size() >= 2)
|
if (m_data[i].size() >= 2)
|
||||||
{
|
{
|
||||||
currentMaterialName = m_data[i][1];
|
currentMaterialName = m_data[i][1];
|
||||||
material.renderBegin(currentMaterialName);
|
material.renderBegin(currentMaterialName);
|
||||||
inMaterial = true;
|
inMaterial = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (type == "mtllib")
|
else if (type == "mtllib")
|
||||||
{
|
{
|
||||||
if (m_data[i].size() >= 2)
|
if (m_data[i].size() >= 2)
|
||||||
{
|
{
|
||||||
material.load(basePath(m_fileName) + m_data[i][1],
|
material.load(basePath(m_fileName) + m_data[i][1],
|
||||||
m_loadTexture,
|
m_loadTexture,
|
||||||
m_loadFile);
|
m_loadFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (inFace)
|
if (inFace)
|
||||||
{
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << "glEnd()" << endl;
|
cout << "glEnd()" << endl;
|
||||||
#endif
|
#endif
|
||||||
glEnd();
|
glEnd();
|
||||||
inFace = false;
|
inFace = false;
|
||||||
}
|
}
|
||||||
if (inMaterial)
|
if (inMaterial)
|
||||||
{
|
{
|
||||||
material.renderEnd(currentMaterialName);
|
material.renderEnd(currentMaterialName);
|
||||||
inMaterial = false;
|
inMaterial = false;
|
||||||
}
|
}
|
||||||
glEndList();
|
glEndList();
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFObj::updateAABB(const float * const vertex)
|
void WFObj::updateAABB(const float * const vertex)
|
||||||
@ -362,21 +362,21 @@ void WFObj::updateAABB(const float * const vertex)
|
|||||||
|
|
||||||
WFObj::Vertex WFObj::readVertex(const vector<string> & parts)
|
WFObj::Vertex WFObj::readVertex(const vector<string> & parts)
|
||||||
{
|
{
|
||||||
int partslen = parts.size();
|
int partslen = parts.size();
|
||||||
Vertex v;
|
Vertex v;
|
||||||
for (int i = 1; i < partslen && i <= 4; i++)
|
for (int i = 1; i < partslen && i <= 4; i++)
|
||||||
{
|
{
|
||||||
sscanf(parts[i].c_str(), "%f", &v[i - 1]);
|
sscanf(parts[i].c_str(), "%f", &v[i - 1]);
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFObj::parseVertexIndices(const string & vtxref, int * ret)
|
void WFObj::parseVertexIndices(const string & vtxref, int * ret)
|
||||||
{
|
{
|
||||||
vector<string> parts = splitString(vtxref, '/');
|
vector<string> parts = splitString(vtxref, '/');
|
||||||
int num = parts.size();
|
int num = parts.size();
|
||||||
for (int i = 0; i < num && i < 3; i++)
|
for (int i = 0; i < num && i < 3; i++)
|
||||||
sscanf(parts[i].c_str(), "%d", ret + i);
|
sscanf(parts[i].c_str(), "%d", ret + i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -386,24 +386,24 @@ void WFObj::parseVertexIndices(const string & vtxref, int * ret)
|
|||||||
|
|
||||||
void WFMtl::clear()
|
void WFMtl::clear()
|
||||||
{
|
{
|
||||||
m_data = map< string, vector< vector<string> > >();
|
m_data = map< string, vector< vector<string> > >();
|
||||||
m_currentMaterialName = "";
|
m_currentMaterialName = "";
|
||||||
m_attributesPushed = false;
|
m_attributesPushed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int WFMtl::filesize(const char * filename)
|
int WFMtl::filesize(const char * filename)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (stat(filename, &st))
|
if (stat(filename, &st))
|
||||||
return -1;
|
return -1;
|
||||||
return st.st_size;
|
return st.st_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WFMtl::load(const string & filename,
|
bool WFMtl::load(const string & filename,
|
||||||
WFObj::loadTextureFunc_t loadTexture,
|
WFObj::loadTextureFunc_t loadTexture,
|
||||||
WFObj::loadFileFunc_t loadFile)
|
WFObj::loadFileFunc_t loadFile)
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
if (loadFile)
|
if (loadFile)
|
||||||
{
|
{
|
||||||
@ -433,205 +433,208 @@ bool WFMtl::load(const string & filename,
|
|||||||
ifs.close();
|
ifs.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_loadTexture = loadTexture;
|
m_loadTexture = loadTexture;
|
||||||
m_fileName = filename;
|
m_fileName = filename;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WFMtl::load(std::istream & istr, unsigned int size)
|
bool WFMtl::load(std::istream & istr, unsigned int size)
|
||||||
{
|
{
|
||||||
char buf[size+1];
|
char buf[size+1];
|
||||||
|
|
||||||
string buildup;
|
string buildup;
|
||||||
while (istr.good())
|
while (istr.good())
|
||||||
{
|
{
|
||||||
istr.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] == '#')
|
||||||
continue;
|
continue;
|
||||||
if (input[sz-1] == '\\')
|
if (input[sz-1] == '\\')
|
||||||
{
|
{
|
||||||
input[sz-1] = ' ';
|
input[sz-1] = ' ';
|
||||||
buildup = input;
|
buildup = input;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (buildup != "")
|
if (buildup != "")
|
||||||
input = buildup + input;
|
input = buildup + input;
|
||||||
buildup = "";
|
buildup = "";
|
||||||
processInputLine(input);
|
processInputLine(input);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFMtl::processInputLine(const std::string & input)
|
void WFMtl::processInputLine(const std::string & input)
|
||||||
{
|
{
|
||||||
string line = input;
|
string line = input;
|
||||||
vector<string> lineParts;
|
vector<string> lineParts;
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
string token = stripFirstToken(line);
|
string token = stripFirstToken(line);
|
||||||
if (token == "")
|
if (token == "")
|
||||||
break;
|
break;
|
||||||
lineParts.push_back(token);
|
lineParts.push_back(token);
|
||||||
}
|
}
|
||||||
if (lineParts.size() > 0)
|
if (lineParts.size() > 0)
|
||||||
{
|
{
|
||||||
if ( (lineParts.size() >= 2) && (lineParts[0] == "newmtl") )
|
if ( (lineParts.size() >= 2) && (lineParts[0] == "newmtl") )
|
||||||
{
|
{
|
||||||
m_currentMaterialName = lineParts[1];
|
m_currentMaterialName = lineParts[1];
|
||||||
}
|
}
|
||||||
else if (m_currentMaterialName != "")
|
else if (m_currentMaterialName != "")
|
||||||
{
|
{
|
||||||
m_data[m_currentMaterialName].push_back(lineParts);
|
m_data[m_currentMaterialName].push_back(lineParts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFMtl::renderBegin(const string & mtlname)
|
void WFMtl::renderBegin(const string & mtlname)
|
||||||
{
|
{
|
||||||
map< string, vector< vector<string> > >::iterator it = m_data.find(mtlname);
|
map< string, vector< vector<string> > >::iterator it = m_data.find(mtlname);
|
||||||
if (it == m_data.end())
|
if (it == m_data.end())
|
||||||
return;
|
return;
|
||||||
vector< vector<string> > & stmts = it->second;
|
vector< vector<string> > & stmts = it->second;
|
||||||
int num_stmts = stmts.size();
|
int num_stmts = stmts.size();
|
||||||
bool foundTexture = false;
|
bool foundTexture = false;
|
||||||
bool didSomething = false;
|
bool didSomething = false;
|
||||||
for (int i = 0; i < num_stmts; i++)
|
for (int i = 0; i < num_stmts; i++)
|
||||||
{
|
{
|
||||||
string & type = stmts[i][0];
|
string & type = stmts[i][0];
|
||||||
if (type == "Ka") /* set ambient color */
|
if (type == "Ka") /* set ambient color */
|
||||||
{
|
{
|
||||||
if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") )
|
if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") )
|
||||||
{
|
{
|
||||||
pushAttributes();
|
pushAttributes();
|
||||||
float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
||||||
for (int j = 0; j < 3; j++)
|
for (int j = 0; j < 3; j++)
|
||||||
sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]);
|
sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]);
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glMaterialfv(GL_FRONT, GL_AMBIENT, {";
|
cout << " glMaterialfv(GL_FRONT, GL_AMBIENT, {";
|
||||||
for (int j = 0; j < 4; j++)
|
for (int j = 0; j < 4; j++)
|
||||||
cout << mat[j] << (j < 3 ? ", " : "})");
|
cout << mat[j] << (j < 3 ? ", " : "})");
|
||||||
cout << endl;
|
cout << endl;
|
||||||
#endif
|
#endif
|
||||||
glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
|
glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
|
||||||
didSomething = true;
|
didSomething = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (type == "Kd") /* set diffuse color */
|
else if (type == "Kd") /* set diffuse color */
|
||||||
{
|
{
|
||||||
if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") )
|
if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") )
|
||||||
{
|
{
|
||||||
pushAttributes();
|
pushAttributes();
|
||||||
float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
||||||
for (int j = 0; j < 3; j++)
|
for (int j = 0; j < 3; j++)
|
||||||
sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]);
|
sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]);
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glMaterialfv(GL_FRONT, GL_DIFFUSE, {";
|
cout << " glMaterialfv(GL_FRONT, GL_DIFFUSE, {";
|
||||||
for (int j = 0; j < 4; j++)
|
for (int j = 0; j < 4; j++)
|
||||||
cout << mat[j] << (j < 3 ? ", " : "})");
|
cout << mat[j] << (j < 3 ? ", " : "})");
|
||||||
cout << endl;
|
cout << endl;
|
||||||
#endif
|
#endif
|
||||||
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
|
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
|
||||||
didSomething = true;
|
didSomething = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (type == "Ks") /* set specular color */
|
else if (type == "Ks") /* set specular color */
|
||||||
{
|
{
|
||||||
if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") )
|
if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") )
|
||||||
{
|
{
|
||||||
pushAttributes();
|
pushAttributes();
|
||||||
float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
||||||
for (int j = 0; j < 3; j++)
|
for (int j = 0; j < 3; j++)
|
||||||
sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]);
|
sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]);
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glMaterialfv(GL_FRONT, GL_SPECULAR, {";
|
cout << " glMaterialfv(GL_FRONT, GL_SPECULAR, {";
|
||||||
for (int j = 0; j < 4; j++)
|
for (int j = 0; j < 4; j++)
|
||||||
cout << mat[j] << (j < 3 ? ", " : "})");
|
cout << mat[j] << (j < 3 ? ", " : "})");
|
||||||
cout << endl;
|
cout << endl;
|
||||||
#endif
|
#endif
|
||||||
glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
|
glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
|
||||||
didSomething = true;
|
didSomething = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (type == "Ns") /* set shininess */
|
else if (type == "Ns") /* set shininess */
|
||||||
{
|
{
|
||||||
if (stmts[i].size() == 2)
|
if (stmts[i].size() == 2)
|
||||||
{
|
{
|
||||||
pushAttributes();
|
pushAttributes();
|
||||||
GLfloat shininess = 0.0f;
|
GLfloat shininess = 0.0f;
|
||||||
sscanf(stmts[i][1].c_str(), "%f", &shininess);
|
sscanf(stmts[i][1].c_str(), "%f", &shininess);
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glMaterialf(GL_FRONT, GL_SHININESS, "
|
cout << " glMaterialf(GL_FRONT, GL_SHININESS, "
|
||||||
<< shininess << ")" << endl;
|
<< shininess << ")" << endl;
|
||||||
#endif
|
#endif
|
||||||
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
|
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
|
||||||
didSomething = true;
|
didSomething = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (type == "map_Kd") /* load a diffuse texture */
|
else if (type == "map_Kd") /* load a diffuse texture */
|
||||||
{
|
{
|
||||||
/* TODO: figure out how i want to load the texture */
|
/* TODO: figure out how i want to load the texture */
|
||||||
if (stmts[i].size() == 2)
|
if (stmts[i].size() == 2)
|
||||||
{
|
{
|
||||||
GLuint tex = m_loadTexture( (basePath(m_fileName) +
|
if (m_loadTexture != NULL)
|
||||||
stmts[i][1]).c_str() );
|
{
|
||||||
if (tex > 0)
|
GLuint tex = m_loadTexture( (basePath(m_fileName) +
|
||||||
{
|
stmts[i][1]).c_str() );
|
||||||
|
if (tex > 0)
|
||||||
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glBindTexture(GL_TEXTURE_2D, " << tex << ")" << endl;
|
cout << " glBindTexture(GL_TEXTURE_2D, " << tex << ")" << endl;
|
||||||
#endif
|
#endif
|
||||||
glBindTexture(GL_TEXTURE_2D, tex);
|
glBindTexture(GL_TEXTURE_2D, tex);
|
||||||
foundTexture = true;
|
foundTexture = true;
|
||||||
didSomething = true;
|
didSomething = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (didSomething)
|
if (didSomething)
|
||||||
{
|
{
|
||||||
pushAttributes();
|
pushAttributes();
|
||||||
if (foundTexture)
|
if (foundTexture)
|
||||||
{
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glEnable(GL_TEXTURE_2D)" << endl;
|
cout << " glEnable(GL_TEXTURE_2D)" << endl;
|
||||||
#endif
|
#endif
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glDisable(GL_TEXTURE_2D)" << endl;
|
cout << " glDisable(GL_TEXTURE_2D)" << endl;
|
||||||
#endif
|
#endif
|
||||||
glDisable(GL_TEXTURE_2D);
|
glDisable(GL_TEXTURE_2D);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFMtl::pushAttributes()
|
void WFMtl::pushAttributes()
|
||||||
{
|
{
|
||||||
if (m_attributesPushed)
|
if (m_attributesPushed)
|
||||||
return;
|
return;
|
||||||
m_attributesPushed = true;
|
m_attributesPushed = true;
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT)" << endl;
|
cout << " glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT)" << endl;
|
||||||
#endif
|
#endif
|
||||||
glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT);
|
glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFMtl::renderEnd(const string & mtlname)
|
void WFMtl::renderEnd(const string & mtlname)
|
||||||
{
|
{
|
||||||
map< string, vector< vector<string> > >::iterator it = m_data.find(mtlname);
|
map< string, vector< vector<string> > >::iterator it = m_data.find(mtlname);
|
||||||
if (it == m_data.end())
|
if (it == m_data.end())
|
||||||
return;
|
return;
|
||||||
if (m_attributesPushed)
|
if (m_attributesPushed)
|
||||||
{
|
{
|
||||||
#ifdef DEBUGGL
|
#ifdef DEBUGGL
|
||||||
cout << " glPopAttrib()" << endl;
|
cout << " glPopAttrib()" << endl;
|
||||||
#endif
|
#endif
|
||||||
glPopAttrib();
|
glPopAttrib();
|
||||||
m_attributesPushed = false;
|
m_attributesPushed = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user