-
Notifications
You must be signed in to change notification settings - Fork 8
/
AccessObj.h
194 lines (168 loc) · 4.71 KB
/
AccessObj.h
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// AccessObj.h: interface for the CAccessObj class.
//
//////////////////////////////////////////////////////////////////////
#ifndef _MY_ACCESS_OBJ_
#define _MY_ACCESS_OBJ_
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include "Point3D.h"
#define objMax(a,b) (((a)>(b))?(a):(b))
#define objMin(a,b) (((a)<(b))?(a):(b))
#define objAbs(x) (((x)>0.f)?(x):(-x))
#define Tri(x) (m_pModel->pTriangles[(x)])
// --------------------------------------------------------------------
// COBJtriangle: defines a triangle in a model.
class COBJtriangle
{
public:
unsigned int vindices[3]; // array of triangle vertex indices
unsigned int nindices[3]; // array of triangle normal indices
unsigned int findex; // index of triangle facet normal
COBJtriangle()
{
vindices[0] = vindices[1] = vindices[2] =
nindices[0] = nindices[1] = nindices[2] = 0;
}
~COBJtriangle() {}
};// ------------------------------------------------------------------
// --------------------------------------------------------------------
// COBJgroup: defines a group in a model.
class COBJgroup
{
public:
char name[256]; // name of this group
unsigned int nTriangles; // number of triangles in this group
unsigned int* pTriangles; // array of triangle indices
float m_fTran_X;
float m_fTran_Y;
float m_fTran_Z;
float m_fScale_X;
float m_fScale_Y;
float m_fScale_Z;
float m_fRotate_X;
float m_fRotate_Y;
float m_fRotate_Z;
class COBJgroup* next; // pointer to next group in model
COBJgroup()
{
nTriangles = 0;
pTriangles = NULL;
next = NULL;
}
~COBJgroup()
{
if (nTriangles)
delete [] pTriangles;
pTriangles = NULL;
nTriangles = 0;
}
};// ------------------------------------------------------------------
// --------------------------------------------------------------------
// COBJmodel: defines a model.
class COBJmodel
{
public:
char pathname[256]; // path to this model
unsigned int nVertices; // number of vertices in model
CPoint3D* vpVertices; // array of vertices
unsigned int nNormals; // number of normals in model
CPoint3D* vpNormals; // array of normals
unsigned int nFacetnorms; // number of facetnorms in model
CPoint3D* vpFacetNorms; // array of facetnorms
unsigned int nTriangles; // number of triangles in model
COBJtriangle* pTriangles; // array of triangles
unsigned int nGroups; // number of groups in model
COBJgroup* pGroups; // linked list of groups
CPoint3D position; // position of the model
// construction
COBJmodel()
{
nVertices = 0;
vpVertices = NULL;
nNormals = 0;
vpNormals = NULL;
nFacetnorms = 0;
vpFacetNorms= NULL;
nTriangles = 0;
pTriangles = NULL;
nGroups = 0;
pGroups = NULL;
position = CPoint3D(0, 0, 0);
}
// free all memory
void Destory()
{
COBJgroup *group;
if (vpVertices) delete [] vpVertices;
if (vpNormals) delete [] vpNormals;
if (vpFacetNorms) delete [] vpFacetNorms;
if (pTriangles) delete [] pTriangles;
while(pGroups)
{
group = pGroups;
pGroups = pGroups->next;
delete group;
}
nVertices = 0;
vpVertices = NULL;
nNormals = 0;
vpNormals = NULL;
nFacetnorms = 0;
vpFacetNorms = NULL;
nTriangles = 0;
pTriangles = NULL;
nGroups = 0;
pGroups = NULL;
position = CPoint3D(0, 0, 0);
}
// destruction
~COBJmodel()
{
Destory();
}
};// ------------------------------------------------------------------
// --------------------------------------------------------------------
// A temporal calss
class OBJnode
{
public:
unsigned int triIdx;
unsigned int index; // index in triangle
OBJnode()
: triIdx(0)
, index(0)
{
}
};// ------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
// Definition of the OBJ R/W class
///////////////////////////////////////////////////////////////////////////////
class CAccessObj
{
public:
CAccessObj();
~CAccessObj();
COBJmodel *m_pModel;
protected:
CPoint3D m_vMax, m_vMin;
void CalcBoundingBox();
bool Equal(CPoint3D * u, CPoint3D * v, float epsilon);
COBJgroup* FindGroup(char* name);
COBJgroup* AddGroup(char* name);
char* DirName(char* path);
bool FirstPass(FILE* file);
void SecondPass(FILE* file);
void Dimensions(float* dimensions);
void Scale(float scale);
void ReverseWinding();
void FacetNormals();
void VertexNormals(float angle);
public:
void Destory();
void Boundingbox(CPoint3D &vMax, CPoint3D &vMin);
bool LoadOBJ(const char* filename);
void UnifiedModel();
};
#endif