1 #ifndef __TREMODEL_H__
2 #define __TREMODEL_H__
3 
4 #include <TCFoundation/TCAlertSender.h>
5 #include <TCFoundation/TCTypedObjectArray.h>
6 #include <TRE/TREShapeGroup.h>
7 #include <TRE/TREColoredShapeGroup.h>
8 #include <TRE/TREVertexKey.h>
9 #include <TRE/TRESmoother.h>
10 #include <TCFoundation/TCVector.h>
11 #include <TCFoundation/TCStlIncludes.h>
12 
13 struct TREVertex;
14 class TRESubModel;
15 class TREMainModel;
16 class TREColoredShapeGroup;
17 class TREVertexArray;
18 class TCImage;
19 
20 class TRENormalInfo : public TCAlertSender
21 {
22 public:
TRENormalInfo(void)23 	TRENormalInfo(void)
24 		:m_normals(NULL),
25 		m_normalIndex(-1),
26 		m_smoother(NULL),
27 		m_smootherIndex(-1) {};
28 
29 	TREVertexArray *m_normals;
30 	int m_normalIndex;
31 	TRESmoother *m_smoother;
32 	int m_smootherIndex;
33 };
34 
35 typedef TCTypedObjectArray<TRESubModel> TRESubModelArray;
36 typedef TCTypedObjectArray<TREShapeGroup> TREShapeGroupArray;
37 typedef TCTypedObjectArray<TREColoredShapeGroup> TREColoredShapeGroupArray;
38 typedef TCTypedObjectArray<TRENormalInfo> TRENormalInfoArray;
39 typedef std::map<TREVertexKey, TRESmoother> TREConditionalMap;
40 typedef std::set<TREVertexKey> TREVertexKeySet;
41 typedef std::map<TREVertexKey, TREVertexKeySet> TREEdgeMap;
42 
43 typedef enum
44 {
45 	TREMStandard,
46 	TREMFirst = TREMStandard,
47 	TREMLines,
48 	TREMEdgeLines,
49 	TREMConditionalLines,
50 	TREMStud,
51 	TREMBFC,
52 	TREMStudBFC,
53 	TREMTransparent,
54 	TREMLast = TREMTransparent
55 } TREMSection;
56 
57 class TREModel : public TCAlertSender
58 {
59 public:
60 	enum TexmapType
61 	{
62 		TTPlanar,
63 		TTCylindrical,
64 		TTSpherical,
65 	};
66 	struct TexmapInfo
67 	{
68 		struct GeomSubInfo
69 		{
70 			IntSet triangles;
71 			IntSet quads;
72 			IntSet triStrips;
73 			IntSet quadStrips;
74 		};
75 		struct GeomInfo
76 		{
77 			GeomSubInfo standard;
78 			GeomSubInfo colored;
79 		};
80 		TexmapInfo(void);
81 		TexmapInfo(TexmapType type, const std::string &filename,
82 			const TCVector *otherPoints, const TCFloat *extra);
83 		bool texmapEquals(const TexmapInfo &other);
84 		void copyPoints(const TCVector *otherPoints);
85 		void transform(const TCFloat* matrix);
86 		void calcCylFields(void);
87 		void calcSphereFields(void);
88 		TCVector cylDirectionFrom(const TCVector& point);
89 		TCVector directionFrom(const TCVector& point, const TCVector& norm);
90 		TCFloat distanceToPlane(const TCVector& point,
91 			const TCVector& planePoint, const TCVector& planeNormal);
92 		void calcTextureCoords(const TCVector* ppoints,
93 			TCVector* textureCoords);
94 		TCFloat calcSAngle(const TCVector& point, bool isFirst,
95 			TCVector& baseDir, TCFloat& baseAngle);
96 		void calcCylTextureCoords(const TCVector* ppoints,
97 			TCVector* textureCoords);
98 		void calcSphereTextureCoords(const TCVector* ppoints,
99 			TCVector* textureCoords);
100 		TexmapType type;
101 		std::string filename;
102 		TCVector points[3];
103 		TCVector normal;
104 		TCVector normal2;
105 		TCVector dir;
106 		TCVector a;
107 		TCFloat cylHeight;
108 		TCFloat sAngle;
109 		TCFloat tAngle;
110 		bool sAngleIs360;
111 		GeomInfo standard;
112 		GeomInfo bfc;
113 		GeomInfo transparent;
114 		int subModelOffset;
115 		int subModelCount;
116 	};
117 	typedef std::list<TexmapInfo> TexmapInfoList;
118 
119 	TREModel(void);
120 	TREModel(const TREModel &other);
121 	TREModel(const TREModel &other, bool shallow);
122 	virtual TCObject *copy(void) const;
123 	virtual TREModel *shallowCopy(void);
setMainModel(TREMainModel * mainModel)124 	virtual void setMainModel(TREMainModel *mainModel)
125 	{
126 		m_mainModel = mainModel;
127 	}
getMainModel(void)128 	virtual TREMainModel *getMainModel(void) const { return m_mainModel; }
129 	virtual void setName(const char *name);
getName(void)130 	virtual const char *getName(void) const { return m_name; }
131 	virtual TRESubModel *addSubModel(const TCFloat *matrix, TREModel *model,
132 		bool invert);
133 	virtual TRESubModel *addSubModel(TCULong color, TCULong edgeColor,
134 		const TCFloat *matrix, TREModel *model, bool invert);
135 	virtual void addLine(const TCVector *vertices);
136 	virtual void addLine(TCULong color, const TCVector *vertices);
137 	virtual void addEdgeLine(const TCVector *vertices, TCULong color = 0);
138 	virtual void addConditionalLine(const TCVector *vertices,
139 		const TCVector *controlPoints, TCULong color = 0);
140 	virtual void addConditionalLine(const TCVector &p1, const TCVector &p2,
141 		const TCVector &c1, const TCVector &c2);
142 	virtual void addTriangle(const TCVector *vertices);
143 	virtual void addTriangle(const TCVector *vertices,
144 		const TCVector *normals);
145 	virtual void addTriangle(TCULong color, const TCVector *vertices);
146 	virtual void addTriangle(TCULong color, const TCVector *vertices,
147 		const TCVector *normals);
148 	virtual void addBFCTriangle(const TCVector *vertices);
149 	virtual void addBFCTriangle(const TCVector *vertices,
150 		const TCVector *normals);
151 	virtual void addBFCTriangle(TCULong color, const TCVector *vertices);
152 	virtual void addBFCTriangle(TCULong color, const TCVector *vertices,
153 		const TCVector *normals);
154 	virtual void addQuad(const TCVector *vertices);
155 	virtual void addQuad(TCULong color, const TCVector *vertices);
156 	virtual void addBFCQuad(const TCVector *vertices);
157 	virtual void addBFCQuad(TCULong color, const TCVector *vertices);
158 	virtual void addTriangleStrip(const TCVector *vertices,
159 		const TCVector *normals, int count, bool flat = false);
160 	virtual void addTriangleStrip(TREShapeGroup *shapeGroup,
161 		const TCVector *vertices, const TCVector *normals, int count,
162 		bool flat = false);
163 	virtual void addBFCTriangleStrip(const TCVector *vertices,
164 		const TCVector *normals, int count, bool flat = false);
165 	virtual void addTriangleFan(const TCVector *vertices,
166 		const TCVector *normals, const TCVector *textureCoords, int count,
167 		bool flat = false);
168 	virtual void addTriangleFan(TCULong color, const TCVector *vertices,
169 		const TCVector *normals, int count, bool flat = false);
170 	virtual void addTriangleFan(TREShapeGroup *shapeGroup,
171 		const TCVector *vertices, const TCVector *normals,
172 		const TCVector *textureCoords, int count, bool flat = false);
173 	virtual void addTriangleFan(TREColoredShapeGroup *shapeGroup, TCULong color,
174 		const TCVector *vertices, const TCVector *normals, int count,
175 		bool flat = false);
176 	virtual void addBFCTriangleFan(const TCVector *vertices,
177 		const TCVector *normals, const TCVector *textureCoords, int count,
178 		bool flat = false);
179 	virtual void addBFCTriangleFan(TCULong color, const TCVector *vertices,
180 		const TCVector *normals, int count, bool flat = false);
181 	virtual void addQuadStrip(const TCVector *vertices, const TCVector *normals,
182 		int count, bool flat = false);
183 	virtual void addQuadStrip(TREShapeGroup *shapeGroup,
184 		const TCVector *vertices, const TCVector *normals, int count,
185 		bool flat);
186 	virtual void addQuadStrip(TCULong color, const TCVector *vertices,
187 		const TCVector *normals, int count, bool flat = false);
188 	virtual void addQuadStrip(TREColoredShapeGroup *shapeGroup, TCULong color,
189 		const TCVector *vertices, const TCVector *normals, int count,
190 		bool flat = false);
191 	virtual void addBFCQuadStrip(const TCVector *vertices,
192 		const TCVector *normals, int count, bool flat = false);
193 	virtual void addBFCQuadStrip(TCULong color, const TCVector *vertices,
194 		const TCVector *normals, int count, bool flat = false);
195 	void compile(TREMSection section, bool colored,
196 		bool nonUniform = false, bool skipTexmapped = false);
197 	void draw(TREMSection section);
198 	void draw(TREMSection section, bool colored,
199 		bool subModelsOnly = false, bool nonUniform = false,
200 		bool skipTexmapped = false);
201 	virtual void drawColored(TREMSection section);
setPartFlag(bool value)202 	virtual void setPartFlag(bool value) { m_flags.part = value; }
isPart(void)203 	virtual bool isPart(void) { return m_flags.part != false; }
setNoShrinkFlag(bool value)204 	virtual void setNoShrinkFlag(bool value) { m_flags.noShrink = value; }
getNoShrinkFlag(void)205 	virtual bool getNoShrinkFlag(void) { return m_flags.noShrink != false; }
isFlattened(void)206 	virtual bool isFlattened(void) { return m_flags.flattened != false; }
207 	virtual void flatten(void);
208 	virtual void smooth(void);
209 	virtual void addCylinder(const TCVector &center, TCFloat radius,
210 		TCFloat height, int numSegments, int usedSegments = -1,
211 		bool bfc = false, TCULong color = 0, TCULong edgeColor = 0);
212 	virtual void addSlopedCylinder(const TCVector &center, TCFloat radius,
213 		TCFloat height, int numSegments, int usedSegments = -1, bool bfc = false);
214 	virtual void addSlopedCylinder2(const TCVector &center, TCFloat radius,
215 		TCFloat height, int numSegments, int usedSegments = -1, bool bfc = false);
216 	virtual void addStudDisc(const TCVector &center, TCFloat radius,
217 		int numSegments, int usedSegments = -1, bool bfc = false);
218 	virtual void addChrd(const TCVector &center, TCFloat radius, int numSegments,
219 		int usedSegments = -1, bool bfc = false);
220 	virtual void addDisc(const TCVector &center, TCFloat radius, int numSegments,
221 		int usedSegments = -1, bool bfc = false, bool stud = false);
222 	virtual void addNotDisc(const TCVector &center, TCFloat radius,
223 		int numSegments, int usedSegments = -1, bool bfc = false);
224 	virtual void addTangent(const TCVector &center, TCFloat radius,
225 		int numSegments, int usedSegments = -1, bool bfc = false);
226 	virtual void addCone(const TCVector &center, TCFloat radius, TCFloat height,
227 		int numSegments, int usedSegments = -1, bool bfc = false,
228 		TCULong color = 0, TCULong edgeColor = 0);
229 	virtual void addEighthSphere(const TCVector& center, TCFloat radius,
230 		int numSegments, bool bfc);
231 	virtual void addTorusIO(bool inner, const TCVector& center, TCFloat yRadius,
232 		TCFloat xzRadius, int numSegments, int usedSegments, int minorSegments,
233 		bool bfc);
234 	virtual void addOpenCone(const TCVector &center, TCFloat radius1,
235 		TCFloat radius2, TCFloat height, int numSegments, int usedSegments = -1,
236 		bool bfc = false, TCULong color = 0, TCULong edgeColor = 0);
237 	virtual void addCircularEdge(const TCVector &center, TCFloat radius,
238 		int numSegments, int usedSegments = -1, TCULong color = 0);
239 	virtual void addRing(const TCVector &center, TCFloat radius1, TCFloat radius2,
240 		int numSegments, int usedSegments = -1, bool bfc = false);
241 	virtual void addOpenConeConditionals(TCVector *points, int numSegments,
242 		int usedSegments, TCULong color = 0);
243 	virtual void addSlopedCylinder2Conditionals(TCVector *points,
244 		int numSegments, int usedSegments);
245 	virtual void addTorusIOConditionals(bool innder, TCVector *points,
246 		int numSegments, int usedSegments, int minorSegments,
247 		const TCVector& center, TCFloat radius, TCFloat height);
248 	virtual void addEighthSphereConditionals(TCVector *points, int numSegments);
249 	TCVector calcIntersection(int i, int j, int num, TCVector* zeroXPoints,
250 		TCVector* zeroYPoints, TCVector* zeroZPoints);
251 	virtual void getBoundingBox(TCVector& min, TCVector& max);
252 	virtual void scanPoints(TCObject *scanner,
253 		TREScanPointCallback scanPointCallback, const TCFloat *matrix);
254 	virtual void unshrinkNormals(const TCFloat *scaleMatrix);
255 	void unshrinkNormals(const TCFloat *matrix, const TCFloat *unshrinkMatrix);
256 	TREModel *getUnMirroredModel(void);
257 	TREModel *getInvertedModel(void);
258 	virtual void uncompile(bool includeSubModels = true);
259 	virtual void nextStep(void);
getCurStepIndex(void)260 	virtual int getCurStepIndex(void) const { return m_curStepIndex; }
isLineSection(int section)261 	bool isLineSection(int section)
262 	{
263 		return isLineSection((TREMSection)section);
264 	}
isLineSection(TREMSection section)265 	bool isLineSection(TREMSection section)
266 	{
267 		return section == TREMLines || section == TREMEdgeLines;
268 	}
269 	virtual void transferColored(TREShapeGroup::TRESTransferType type,
270 		TREMSection section, const TCFloat *matrix, bool bfcInvert);
271 	virtual void transferColoredSubModels(TREShapeGroup::TRESTransferType type,
272 		TREMSection section, const TCFloat *matrix, bool bfcInvert);
273 	virtual void transfer(TREShapeGroup::TRESTransferType type, TCULong color,
274 		TREMSection section, const TCFloat *matrix, bool bfcInvert);
275 	virtual void transferSubModels(TREShapeGroup::TRESTransferType type,
276 		TCULong color, TREMSection section, const TCFloat *matrix,
277 		bool bfcInvert = false);
278 	virtual void cleanupTransfer(TREShapeGroup::TRESTransferType type,
279 		TREMSection section);
280 	virtual TCObject *getAlertSender(void);
281 	virtual void saveSTL(FILE *file, float scale);
282 	virtual void startTexture(int type, const std::string &filename,
283 		TCImage *image, const TCVector *points, const TCFloat *extra);
284 	virtual bool endTexture(void);
285 	virtual void finishPart(void);
286 	virtual void finishParts(void);
287 	virtual void shrinkParts(void);
288 
getShapes(void)289 	TREShapeGroup **getShapes(void) { return m_shapes; }
getColoredShapes(void)290 	TREColoredShapeGroup **getColoredShapes(void) { return m_coloredShapes; }
getShape(int index)291 	TREShapeGroup *getShape(int index) { return m_shapes[index]; }
getColoredShape(int index)292 	TREColoredShapeGroup *getColoredShape(int index)
293 	{
294 		return m_coloredShapes[index];
295 	}
getSubModels(void)296 	TRESubModelArray *getSubModels(void) { return m_subModels; }
297 	int getSubModelCount(void) const;
298 	void activateTexmap(const TexmapInfo &texmapInfo);
299 	void disableTexmaps(void);
300 	TexmapInfo *getActiveTexmapInfo(void);
getTexmapInfos(void)301 	const TexmapInfoList &getTexmapInfos(void) const { return m_texmapInfos; }
302 protected:
303 	struct TRETriangle
304 	{
305 		int index;
306 		TREVertex *points[3];
307 	};
308 	typedef std::list<TRETriangle> TRETriangleList;
309 	typedef std::map<TREVertexKey, TRETriangleList> TRETrianglesMap;
310 
311 	virtual ~TREModel(void);
312 	virtual void dealloc(void);
313 	virtual void setup(TREMSection section);
314 	virtual void setupLines(void);
315 	virtual void setupStandard(void);
316 	virtual void setupStud(void);
317 	virtual void setupStudBFC(void);
318 	virtual void setupBFC(void);
319 	virtual void setupColored(TREMSection section);
320 	virtual void setupColored(void);
321 	virtual void setupColoredStud(void);
322 	virtual void setupColoredStudBFC(void);
323 	virtual void setupColoredLines(void);
324 	virtual void setupColoredBFC(void);
325 	virtual void setupEdges(void);
326 	virtual void setupColoredEdges(void);
327 	virtual void setupConditional(void);
328 	virtual void setupColoredConditional(void);
329 	virtual void flatten(TREModel *model, const TCFloat *matrix, TCULong color,
330 		bool colorSet, TCULong edgeColor, bool edgeColorSet,
331 		bool includeShapes, bool skipTexmapped = false);
332 	virtual void checkGLError(char *msg);
333 	void setCirclePoint(TCFloat angle, TCFloat radius, const TCVector& center,
334 		TCVector& point);
335 	void scanBoundingBoxPoint(const TCVector &point);
336 	virtual void calculateBoundingBox(void);
337 	virtual void quadStripToQuad(int index, const TCVector *stripVertices,
338 		const TCVector *stripNormals, TCVector *quadVertices,
339 		TCVector *quadNormals);
340 	virtual void triangleStripToTriangle(int index,
341 		const TCVector *stripVertices, const TCVector *stripNormals,
342 		TCVector *triangleVertices, TCVector *triangleNormals);
343 	virtual void triangleFanToTriangle(int index, const TCVector *fanVertices,
344 		const TCVector *fanNormals, const TCVector *fanTextureCoords,
345 		TCVector *triangleVertices, TCVector *triangleNormals,
346 		TCVector *triangleTextureCoords);
347 	virtual void unMirror(TREModel *originalModel);
348 	virtual void invert(TREModel *originalModel);
349 	virtual bool checkShapeGroupPresent(TREShapeGroup *shapeGroup,
350 		TREMSection section, bool colored);
351 	virtual bool checkSectionPresent(TREMSection section);
352 	virtual bool checkColoredSectionPresent(TREMSection section);
353 	virtual bool checkSectionPresent(TREMSection section, bool colored);
354 	virtual int sphereIndex(int i, int j, int usedSegments);
355 	virtual void setSectionPresent(TREMSection section, bool colored);
356 	virtual bool isSectionPresent(TREMSection section, bool colored);
357 	virtual bool shouldLoadConditionalLines(void);
358 	virtual void genStudTextureCoords(TCVector *textureCoords, int vertexCount);
359 	static bool findEdge(const TREEdgeMap& edgeMap, const TREVertexKey& vertex0,
360 		const TREVertexKey& vertex1);
361 	virtual void fillConditionalMap(TREConditionalMap &conditionalMap,
362 		const TREEdgeMap& edgeMap);
363 	virtual void fillConditionalMap(TREConditionalMap &conditionalMap,
364 		const TREEdgeMap& edgeMap, TREShapeGroup *shapeGroup);
365 	virtual void fillEdgeMap(TREEdgeMap &edgeMap);
366 	virtual void fillEdgeMap(TREEdgeMap &edgeMap, TREShapeGroup *shapeGroup);
367 	virtual void addConditionalPoint(TREConditionalMap &conditionalMap,
368 		const TREVertexArray *vertices, int index0, int index1,
369 		const TREVertexKey &vertexKey);
370 	virtual void calcShapeNormals(TREConditionalMap &conditionalMap,
371 		TRENormalInfoArray *normalInfos, TREShapeType shapeType);
372 	virtual void calcShapeNormals(TREConditionalMap &conditionalMap,
373 		TRENormalInfoArray *normalInfos, TREMSection section,
374 		TREShapeType shapeType);
375 	virtual void calcShapeNormals(TREConditionalMap &conditionalMap,
376 		TRENormalInfoArray *normalInfos, TREShapeGroup *shapeGroup,
377 		TREShapeType shapeType);
378 	virtual void processSmoothEdge(TREConditionalMap &conditionalMap,
379 		TRENormalInfoArray *normalInfos, const TREVertexArray *vertices,
380 		TREVertexArray *normals, int index0, int index1, int index2);
381 	virtual int getConditionalLine(TREConditionalMap &conditionalMap,
382 		const TREVertex point0, const TREVertex point1, TRESmoother *&smoother);
383 	void applyShapeNormals(TRENormalInfoArray *normalInfos);
384 	void finishShapeNormals(TREConditionalMap &conditionalMap);
385 	void flattenNonUniform(void);
386 	void flattenConditionals(const TCFloat *matrix, TCULong edgeColor,
387 		bool edgeColorSet);
388 	void removeConditionals(void);
389 	void findLights(void);
390 	void findLights(float *matrix);
391 	void calcTangentControlPoint(TCVector &controlPoint, int index,
392 		int numSegments);
393 	void saveSTLShapes(TREShapeGroup *shapes[], FILE *file, const TCFloat *matrix,
394 		float scale);
395 	void saveSTL(FILE *file, const TCFloat *matrix, float scale);
396 	void scaleConditionalControlPoints(TREShapeGroup *shapeGroup);
397 	void scaleConditionalControlPoint(int index, int cpIndex,
398 		TREVertexArray *vertices);
399 	int getShapeCount(TREMSection section, TREShapeType shapeType,
400 		bool colored);
401 	GLuint *getListIDs(bool colored, bool skipTexmapped);
402 
403 	static void uncompileListID(GLuint &listID);
404 	static void setGlNormalize(bool value);
405 	static void printStlTriangle(FILE *file, TREVertexArray *vertices,
406 		TCULongArray *indices, int ix, int i0, int i1, int i2,
407 		const TCFloat *matrix, float scale);
408 	static void printStlStrips(FILE *file, TREShapeGroup *shapeGroup,
409 		TREShapeType shapeType, const TCFloat *matrix, float scale);
410 
411 	char *m_name;
412 	TREMainModel *m_mainModel;
413 	TRESubModelArray *m_subModels;
414 	TREShapeGroup *m_shapes[TREMLast + 1];
415 	TREColoredShapeGroup *m_coloredShapes[TREMLast + 1];
416 	GLuint m_listIDs[TREMLast + 1];
417 	GLuint m_coloredListIDs[TREMLast + 1];
418 	GLuint m_texListIDs[TREMLast + 1];
419 	GLuint m_texColoredListIDs[TREMLast + 1];
420 	TREModel *m_unMirroredModel;
421 	TREModel *m_invertedModel;
422 	TCULong m_sectionsPresent;
423 	TCULong m_coloredSectionsPresent;
424 	TCVector m_boundingMin;
425 	TCVector m_boundingMax;
426 	int m_curStepIndex;
427 	IntVector m_stepCounts;
428 	TexmapInfoList m_texmapInfos;
429 	struct
430 	{
431 		bool part:1;
432 		bool noShrink:1;
433 		bool boundingBox:1;
434 		bool unshrunkNormals:1;
435 		bool unMirrored:1;
436 		bool inverted:1;
437 		bool flattened:1;
438 	} m_flags;
439 };
440 
441 #endif // __TREMODEL_H__
442