110 lines
2.8 KiB
C++
110 lines
2.8 KiB
C++
|
|
#ifndef WFOBJ_H
|
|
#define WFOBJ_H
|
|
|
|
#include <GL/gl.h>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <map>
|
|
|
|
class WFObj
|
|
{
|
|
public:
|
|
/* types */
|
|
typedef bool (*loadfile_t)(const char *fname, Buffer *buff);
|
|
typedef GLuint (*loadtexture_t)(const char *fname);
|
|
|
|
class Buffer
|
|
{
|
|
public:
|
|
uint8_t *data;
|
|
size_t length;
|
|
};
|
|
|
|
class WFMtl
|
|
{
|
|
public:
|
|
WFMtl(WFObj * obj) { m_obj = obj; }
|
|
bool load(const FileLoader::Path & path);
|
|
void renderBegin(const std::string & mtlname,
|
|
bool doTextureInfo = true,
|
|
bool enableBlending = false);
|
|
void renderEnd(const std::string & mtlname,
|
|
bool doTextureInfo = true,
|
|
bool enableBlending = false);
|
|
|
|
protected:
|
|
/* methods */
|
|
void clear();
|
|
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;
|
|
std::string m_currentMaterialName;
|
|
bool m_attributesPushed;
|
|
FileLoader::Path m_path;
|
|
WFObj * m_obj;
|
|
};
|
|
|
|
enum { VERTEX, VERTEX_TEXTURE, VERTEX_NORMAL, VERTEX_TYPES };
|
|
|
|
/* constructors */
|
|
WFObj(loadfile_t lf = NULL, loadtexture_t lt = NULL);
|
|
~WFObj();
|
|
|
|
/* methods */
|
|
bool load(const char *fname);
|
|
bool load(const Buffer &buff);
|
|
const float * const getAABB() { return m_aabb; }
|
|
|
|
protected:
|
|
/* types */
|
|
class Vertex
|
|
{
|
|
public:
|
|
float operator[](int idx) const { return data[idx]; }
|
|
float & operator[](int idx) { return data[idx]; }
|
|
float * getData() { return data; }
|
|
private:
|
|
float data[4];
|
|
};
|
|
|
|
class VertexRef
|
|
{
|
|
public:
|
|
VertexRef() : vertex(0), texture(0), normal(0) {}
|
|
int vertex;
|
|
int texture;
|
|
int normal;
|
|
};
|
|
|
|
class Face
|
|
{
|
|
public:
|
|
VertexRef vertices[3];
|
|
};
|
|
|
|
/* methods */
|
|
void clear();
|
|
void processInputLine(const std::string & input);
|
|
Vertex readVertex(const std::vector<std::string> & parts);
|
|
std::vector<Face> readFaces(const std::vector<std::string> & parts);
|
|
VertexRef readVertexRef(const std::string ref);
|
|
void updateAABB(const float * const vertex);
|
|
static Buffer loadfile(const char *path);
|
|
std::string getLine(const Buff & buff, size_t idx, size_t *update_idx);
|
|
|
|
/* variables */
|
|
std::vector<Vector> m_vertices[VERTEX_TYPES];
|
|
std::map< std::string, std::vector< Face > > m_faces;
|
|
FileLoader::Path m_path;
|
|
float m_aabb[6];
|
|
loadfile_t m_loadfile;
|
|
loadtexture_t m_loadtexture;
|
|
std::string m_current_material_name;
|
|
};
|
|
|
|
#endif
|