load materials into Material objects
This commit is contained in:
parent
874be25658
commit
ffe2fe1d92
87
WFObj.cc
87
WFObj.cc
@ -122,6 +122,7 @@ void WFObj::clear()
|
|||||||
for (int i = 0; i < sizeof(m_vertices)/sizeof(m_vertices[0]); i++)
|
for (int i = 0; i < sizeof(m_vertices)/sizeof(m_vertices[0]); i++)
|
||||||
m_vertices[i].clear();
|
m_vertices[i].clear();
|
||||||
m_faces.clear();
|
m_faces.clear();
|
||||||
|
m_materials.clear();
|
||||||
m_valid = false;
|
m_valid = false;
|
||||||
m_path = "";
|
m_path = "";
|
||||||
m_current_material_name = "";
|
m_current_material_name = "";
|
||||||
@ -192,8 +193,7 @@ string WFObj::getLine(const Buffer & buff, size_t idx, size_t *update_idx)
|
|||||||
|
|
||||||
void WFObj::processInputLine(const std::string & input)
|
void WFObj::processInputLine(const std::string & input)
|
||||||
{
|
{
|
||||||
string line = input;
|
vector<string> tokens = tokenize(input);
|
||||||
vector<string> tokens = tokenize(line);
|
|
||||||
|
|
||||||
if (tokens.size() == 0)
|
if (tokens.size() == 0)
|
||||||
return;
|
return;
|
||||||
@ -329,7 +329,88 @@ WFObj::VertexRef WFObj::readVertexRef(const std::string ref)
|
|||||||
|
|
||||||
void WFObj::loadMaterial(const std::string & name)
|
void WFObj::loadMaterial(const std::string & name)
|
||||||
{
|
{
|
||||||
/* TODO */
|
Buffer buff;
|
||||||
|
if (!m_loadfile(name.c_str(), &buff))
|
||||||
|
{
|
||||||
|
cerr << "WFObj: error: couldn't open material file '" << name << "'"
|
||||||
|
<< endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t idx = 0;
|
||||||
|
string mat_name;
|
||||||
|
while (idx < buff.length)
|
||||||
|
{
|
||||||
|
string input = trimString(getLine(buff, idx, &idx));
|
||||||
|
if (input.size() == 0 || input[0] == '#')
|
||||||
|
continue;
|
||||||
|
vector<string> tokens = tokenize(input);
|
||||||
|
if (tokens.size() < 2)
|
||||||
|
continue;
|
||||||
|
if (tokens[0] == "newmtl")
|
||||||
|
{
|
||||||
|
mat_name = tokens[1];
|
||||||
|
m_materials[mat_name] = Material();
|
||||||
|
}
|
||||||
|
else if (mat_name == "")
|
||||||
|
{
|
||||||
|
cerr << "WFObj: error: material directive"
|
||||||
|
" with no 'newmtl' statement" << endl;
|
||||||
|
}
|
||||||
|
else if (tokens[0] == "Ns")
|
||||||
|
{
|
||||||
|
m_materials[mat_name].shininess = atof(tokens[1].c_str());
|
||||||
|
m_materials[mat_name].flags |= Material::MAT_SHININESS;
|
||||||
|
}
|
||||||
|
else if (tokens[0] == "Ka")
|
||||||
|
{
|
||||||
|
if (tokens.size() >= 4)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
m_materials[mat_name].ambient[i] =
|
||||||
|
atof(tokens[i + 1].c_str());
|
||||||
|
m_materials[mat_name].flags |= Material::MAT_AMBIENT;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cerr << "WFObj: error: 'Ka' material directive requires"
|
||||||
|
" 3 parameters" << endl;
|
||||||
|
}
|
||||||
|
else if (tokens[0] == "Kd")
|
||||||
|
{
|
||||||
|
if (tokens.size() >= 4)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
m_materials[mat_name].diffuse[i] =
|
||||||
|
atof(tokens[i + 1].c_str());
|
||||||
|
m_materials[mat_name].flags |= Material::MAT_DIFFUSE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cerr << "WFObj: error: 'Kd' material directive requires"
|
||||||
|
" 3 parameters" << endl;
|
||||||
|
}
|
||||||
|
else if (tokens[0] == "Ks")
|
||||||
|
{
|
||||||
|
if (tokens.size() >= 4)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
m_materials[mat_name].specular[i] =
|
||||||
|
atof(tokens[i + 1].c_str());
|
||||||
|
m_materials[mat_name].flags |= Material::MAT_SPECULAR;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cerr << "WFObj: error: 'Ks' material directive requires"
|
||||||
|
" 3 parameters" << endl;
|
||||||
|
}
|
||||||
|
else if (tokens[0] == "Ni" || tokens[0] == "d" || tokens[0] == "illum")
|
||||||
|
{
|
||||||
|
/* ignore these parameters */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cerr << "WFObj: error: unrecognized material directive: '"
|
||||||
|
<< tokens[0] << "'" << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WFObj::loadfile(const char *path, Buffer *buff)
|
bool WFObj::loadfile(const char *path, Buffer *buff)
|
||||||
|
20
WFObj.h
20
WFObj.h
@ -60,6 +60,25 @@ protected:
|
|||||||
VertexRef vertices[3];
|
VertexRef vertices[3];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Material
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum {
|
||||||
|
MAT_SHININESS = 0x01,
|
||||||
|
MAT_AMBIENT = 0x02,
|
||||||
|
MAT_DIFFUSE = 0x04,
|
||||||
|
MAT_SPECULAR = 0x08,
|
||||||
|
MAT_TEXTURE = 0x10
|
||||||
|
};
|
||||||
|
Material() : flags(0) {}
|
||||||
|
float shininess;
|
||||||
|
float ambient[3];
|
||||||
|
float diffuse[3];
|
||||||
|
float specular[3];
|
||||||
|
std::string texture;
|
||||||
|
int flags;
|
||||||
|
};
|
||||||
|
|
||||||
/* methods */
|
/* methods */
|
||||||
void clear();
|
void clear();
|
||||||
void processInputLine(const std::string & input);
|
void processInputLine(const std::string & input);
|
||||||
@ -74,6 +93,7 @@ protected:
|
|||||||
/* variables */
|
/* variables */
|
||||||
std::vector<Vertex> m_vertices[VERTEX_TYPES];
|
std::vector<Vertex> m_vertices[VERTEX_TYPES];
|
||||||
std::map< std::string, std::vector< Face > > m_faces;
|
std::map< std::string, std::vector< Face > > m_faces;
|
||||||
|
std::map< std::string, Material > m_materials;
|
||||||
float m_aabb[6];
|
float m_aabb[6];
|
||||||
std::string m_path;
|
std::string m_path;
|
||||||
loadfile_t m_loadfile;
|
loadfile_t m_loadfile;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user