Mercurial > hg > forks > gldragon
diff dmmodel.h @ 19:a329f0216491
Implement PLY file format parsing and extremely simplistic scene setup file format.
Not finished yet.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 07 Nov 2019 20:15:33 +0200 |
parents | |
children | 1404dfcee7b8 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dmmodel.h Thu Nov 07 20:15:33 2019 +0200 @@ -0,0 +1,187 @@ +// +// +// +#ifndef DMMODEL_H +#define DMMODEL_H 1 + +#include "dmutil.h" +#include <cstdint> +#include <fstream> +#include <unordered_map> + + +#define PLY_PROP_VERTEX_INDICES "vertex_indices" +#define PLY_ELEM_FACE "face" +#define PLY_ELEM_VERTEX "vertex" + + +enum DMPLYFormat +{ + PLY_FMT_UNKNOWN, + PLY_FMT_ASCII, + PLY_FMT_BIN_LE, + PLY_FMT_BIN_BE +}; + + +enum DMPLYPropType +{ + PLY_TYPE_NONE, + PLY_TYPE_LIST, + + PLY_TYPE_UINT8, + PLY_TYPE_INT8, + PLY_TYPE_INT16, + PLY_TYPE_UINT16, + PLY_TYPE_INT32, + PLY_TYPE_UINT32, + PLY_TYPE_FLOAT, + PLY_TYPE_DOUBLE +}; + + +/* Structures + */ +union DMPLYPropValue +{ + double v_double; + float v_float; + unsigned int v_uint; + int v_int; +}; + + +struct DMPLYFileProperty +{ + std::string name; + DMPLYPropType + type, + list_num_type, + list_values_type; + + DMPLYPropValue value, list_num_value; + std::vector<DMPLYPropValue> list_values; +}; + + +struct DMPLYFileElement +{ + int value; + std::string name; + + std::unordered_map<std::string, DMPLYFileProperty> prop_map; + std::vector<DMPLYFileProperty *> properties; + + DMPLYFileProperty *checkProp(const std::string &prop) + { + if (prop_map.count(prop)) + return &prop_map[prop]; + else + return 0; + } +}; + + +struct DMTextFileInfo +{ + int nline, state; + std::string filename; + std::string line; + std::ifstream file; +}; + + +struct DMPLYFileInfo : DMTextFileInfo +{ + DMPLYFormat format; + + std::unordered_map<std::string, DMPLYFileElement> elem_map; + std::vector<DMPLYFileElement *> elements; + DMPLYFileElement *element; + + DMPLYFileInfo() + { + element = 0; + format = PLY_FMT_UNKNOWN; + } + + DMPLYFileElement *checkElem(const std::string &elem) + { + if (elem_map.count(elem)) + return &elem_map[elem]; + else + return 0; + } +}; + + +struct DMColor +{ + int r, g, b, alpha; +}; + + +struct DMVertex +{ + float x, y, z; +}; + + +struct DMModel +{ + int nvertices, nfaces; + std::vector<DMVertex> vertices, normals; + std::vector<unsigned int> faces; + + DMColor color; + DMVertex translate, scale, rotate; + + unsigned int id_prog, id_fs, id_vs; + + bool loadFromPLY(const std::string &filename); + bool loadFromPLY(const std::string &filename, DMPLYFileInfo &info); + + void printInfo() + { + printf( + "MODEL: scale <%1.5f, %1.5f, %1.5f>\n" + "MODEL: translate <%1.5f, %1.5f, %1.5f>\n" + "MODEL: rotate <%1.5f, %1.5f, %1.5f>\n" + , + scale.x, scale.y, scale.z, + translate.x, translate.y, translate.z, + rotate.x, rotate.y, rotate.z); + } + + DMModel() + { + translate.x = translate.y = translate.z = 0; + rotate.x = rotate.y = rotate.z = 0; + scale.x = scale.y = scale.z = 1; + } +}; + + +struct DMLight +{ + DMVertex pos; + float ambient[4], diffuse[4], specular[4]; +}; + + +struct DMCamera +{ + DMVertex pos, lookAt; +}; + + +struct DMSimpleScene +{ + DMModel model; + DMCamera camera; + std::vector<DMLight> lights; + + bool loadInfo(const std::string &filename); +}; + +#endif