Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: b8286dcb9c709f51c7cb428bf6ca032869f5be97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
   model/objfile.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

/* http://www.royriggs.com/obj.html
   http://en.wikipedia.org/wiki/Wavefront_.obj_file */
   
#ifndef __INCLUDED_MODEL_OBJFILE_H__
#define __INCLUDED_MODEL_OBJFILE_H__

#include "math/vector3f.h"
#include "model/model.h"
#include "model/material.h"
#include "model/fragment.h"
#include "model/triangle.h"
#include "model/quad.h"
#include "filesystem/filestream.h"

#include <string>
#include <map>
#include <vector>
#include <utility>

namespace model
{

/// class to parse the .obj file structure and load geometry data into a model
class OBJFile
{
public:
	/**
	 * @brief load a .obj file into a Model
	 * @param name	name of the model to be loaded, without .obj extension.
	 * If the file can not be read, load() returns the NULL-pointer
	 */
	static Model *load(std::string const &name);

private:

	/**
	 * @brief type definition for a list of materials in the ASE file
	 */
	typedef std::map<size_t, Material *> MaterialList;

	/**
	 * @brief type definition for a list of vertices
	 */
	typedef std::map<size_t, math::Vector3f *> VertexList;
	typedef std::map<size_t, math::Vector3f *> NormalList;
	typedef std::map<size_t, math::Vector2f *> UVList;

	/**
	 * @brief type definition for a list of faces
	 */
	typedef std::list<std::pair<size_t, Triangle *> > TriList;
	typedef std::list<std::pair<size_t, Quad *> > 	  QuadList;

	OBJFile(std::string const &name);
	~OBJFile();

	/**
	 * @brief read the .obj file
	 */
	bool read();
	
	/**
	 * @brief parse a line in the .obj file
	 */
	void readline(std::string const &line);
	
	inline const std::string &name() const {
		return objfile_name;
	}

	inline bool is_open() {
		return objfile_ifs.is_open();
	}

	inline size_t vertexcount() const {
		return obj_vertexlist.size();
	}

	inline FragmentGroup *fragmentgroup() {
		return obj_fragmentgroup;
	}
	
	inline const math::BoundingBox3f & box() const {
		return obj_box;
	}

	math::Vector3f *find_vertex(size_t index);
	
	math::Vector3f *find_normal(size_t index);
	
	math::Vector2f *find_uv(size_t index);
	
	std::string		objfile_name;

	filesystem::IFileStream	objfile_ifs;

	MaterialList	obj_materiallist;
	VertexList		obj_vertexlist;
	UVList			obj_uvlist;
	NormalList		obj_normallist;
	TriList			obj_trilist;
	QuadList		obj_quadlist;

	math::BoundingBox3f	obj_box;

	FragmentGroup		*obj_fragmentgroup;

	size_t			obj_normalcount;
	
	bool			obj_load_clip;
	CollisionModel		*obj_collisionmodel;
};

} // namespace model

#endif // __INCLUDED_MODEL_OBJFILE_H__