Add material library as class
This commit is contained in:
parent
8db0b0581a
commit
725f061219
|
@ -1,23 +0,0 @@
|
|||
#include "material.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
Material::Material(const std::string &name)
|
||||
{
|
||||
std::ifstream file("/data/materials/" + name, std::ios::in);
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (line.substr(0, 7) == "map_Kd ")
|
||||
{
|
||||
_texture = new Texture(line.substr(7).c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Material::use() const
|
||||
{
|
||||
_texture->use();
|
||||
}
|
|
@ -3,16 +3,17 @@
|
|||
|
||||
#include "texture.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
class Material
|
||||
{
|
||||
public:
|
||||
Material(const std::string &name);
|
||||
void use() const;
|
||||
inline void use() const;
|
||||
|
||||
private:
|
||||
Texture *_texture;
|
||||
Texture *texture;
|
||||
};
|
||||
|
||||
void Material::use() const
|
||||
{
|
||||
texture->use();
|
||||
};
|
||||
|
||||
#endif // _MATERIAL_HPP_
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "model.hpp"
|
||||
|
||||
#include "mtllib.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
|
@ -7,6 +9,8 @@ Model::Model(const std::string &name)
|
|||
{
|
||||
std::ifstream file("/data/models/" + name, std::ios::in);
|
||||
|
||||
Mtllib *mtllib = nullptr;
|
||||
|
||||
std::vector<glm::vec3> tmp_positions;
|
||||
std::vector<glm::vec2> tmp_tex_coords;
|
||||
std::vector<glm::vec3> tmp_normals;
|
||||
|
@ -72,7 +76,12 @@ Model::Model(const std::string &name)
|
|||
else
|
||||
if (line.substr(0, 7) == "mtllib ")
|
||||
{
|
||||
_material = new Material(line.substr(7).c_str());
|
||||
mtllib = new Mtllib(line.substr(7));
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 7) == "usemtl ")
|
||||
{
|
||||
_material = mtllib->materials[line.substr(7)];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
#include "mtllib.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
Mtllib::Mtllib(const std::string &name)
|
||||
{
|
||||
std::ifstream file("/data/materials/" + name, std::ios::in);
|
||||
|
||||
Material *material = nullptr;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (!material && line.substr(0, 7) != "newmtl ")
|
||||
continue;
|
||||
|
||||
if (line.substr(0, 7) == "newmtl ")
|
||||
{
|
||||
material = new Material();
|
||||
materials[line.substr(7)] = material;
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 7) == "map_Kd ")
|
||||
{
|
||||
material->texture = new Texture(line.substr(7).c_str());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef _MTLLIB_HPP_
|
||||
#define _MTLLIB_HPP_
|
||||
|
||||
#include "material.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
class Mtllib
|
||||
{
|
||||
public:
|
||||
Mtllib(const std::string &name);
|
||||
|
||||
std::map<std::string, Material*> materials;
|
||||
};
|
||||
|
||||
#endif // _MTLLIB_HPP_
|
Reference in New Issue