goat3d

view libs/openctm/openctmpp.h @ 75:76dea247f75c

in progress
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 May 2014 00:50:16 +0300
parents
children
line source
1 //-----------------------------------------------------------------------------
2 // Product: OpenCTM
3 // File: openctmpp.h
4 // Description: C++ wrapper for the OpenCTM API.
5 //-----------------------------------------------------------------------------
6 // Copyright (c) 2009-2010 Marcus Geelnard
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not
22 // be misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //-----------------------------------------------------------------------------
28 // To disable C++ extensions, define OPENCTM_NO_CPP
29 #ifndef OPENCTM_NO_CPP
31 #ifndef __OPENCTMPP_H_
32 #define __OPENCTMPP_H_
34 // Just in case (if this file was included from outside openctm.h)...
35 #ifndef __OPENCTM_H_
36 #include "openctm.h"
37 #endif
39 #include <exception>
41 /// OpenCTM exception. When an error occurs, a \c ctm_error exception is
42 /// thrown. Its what() function returns the name of the OpenCTM error code
43 /// (for instance "CTM_INVALID_OPERATION").
44 class ctm_error: public std::exception
45 {
46 private:
47 CTMenum mErrorCode;
49 public:
50 explicit ctm_error(CTMenum aError)
51 {
52 mErrorCode = aError;
53 }
55 virtual const char* what() const throw()
56 {
57 return ctmErrorString(mErrorCode);
58 }
60 CTMenum error_code() const throw()
61 {
62 return mErrorCode;
63 }
64 };
67 /// OpenCTM importer class. This is a C++ wrapper class for an OpenCTM import
68 /// context. Usage example:
69 ///
70 /// @code
71 /// // Create a new OpenCTM importer object
72 /// CTMimporter ctm;
73 ///
74 /// // Load the OpenCTM file
75 /// ctm.Load("mymesh.ctm");
76 ///
77 /// // Access the mesh data
78 /// vertCount = ctm.GetInteger(CTM_VERTEX_COUNT);
79 /// vertices = ctm.GetFloatArray(CTM_VERTICES);
80 /// triCount = ctm.GetInteger(CTM_TRIANGLE_COUNT);
81 /// indices = ctm.GetIntegerArray(CTM_INDICES);
82 ///
83 /// // Deal with the mesh (e.g. transcode it to our internal representation)
84 /// // ...
85 /// @endcode
87 class CTMimporter {
88 private:
89 /// The OpenCTM context handle.
90 CTMcontext mContext;
92 /// Check for OpenCTM errors, and throw an exception if an error has
93 /// occured.
94 void CheckError()
95 {
96 CTMenum err = ctmGetError(mContext);
97 if(err != CTM_NONE)
98 throw ctm_error(err);
99 }
101 public:
102 /// Constructor
103 CTMimporter()
104 {
105 mContext = ctmNewContext(CTM_IMPORT);
106 }
108 /// Destructor
109 ~CTMimporter()
110 {
111 ctmFreeContext(mContext);
112 }
114 /// Wrapper for ctmGetInteger()
115 CTMuint GetInteger(CTMenum aProperty)
116 {
117 CTMuint res = ctmGetInteger(mContext, aProperty);
118 CheckError();
119 return res;
120 }
122 /// Wrapper for ctmGetFloat()
123 CTMfloat GetFloat(CTMenum aProperty)
124 {
125 CTMfloat res = ctmGetFloat(mContext, aProperty);
126 CheckError();
127 return res;
128 }
130 /// Wrapper for ctmGetIntegerArray()
131 const CTMuint * GetIntegerArray(CTMenum aProperty)
132 {
133 const CTMuint * res = ctmGetIntegerArray(mContext, aProperty);
134 CheckError();
135 return res;
136 }
138 /// Wrapper for ctmGetFloatArray()
139 const CTMfloat * GetFloatArray(CTMenum aProperty)
140 {
141 const CTMfloat * res = ctmGetFloatArray(mContext, aProperty);
142 CheckError();
143 return res;
144 }
146 /// Wrapper for ctmGetNamedUVMap()
147 CTMenum GetNamedUVMap(const char * aName)
148 {
149 CTMenum res = ctmGetNamedUVMap(mContext, aName);
150 CheckError();
151 return res;
152 }
154 /// Wrapper for ctmGetUVMapString()
155 const char * GetUVMapString(CTMenum aUVMap, CTMenum aProperty)
156 {
157 const char * res = ctmGetUVMapString(mContext, aUVMap, aProperty);
158 CheckError();
159 return res;
160 }
162 /// Wrapper for ctmGetUVMapFloat()
163 CTMfloat GetUVMapFloat(CTMenum aUVMap, CTMenum aProperty)
164 {
165 CTMfloat res = ctmGetUVMapFloat(mContext, aUVMap, aProperty);
166 CheckError();
167 return res;
168 }
170 /// Wrapper for ctmGetNamedAttribMap()
171 CTMenum GetNamedAttribMap(const char * aName)
172 {
173 CTMenum res = ctmGetNamedAttribMap(mContext, aName);
174 CheckError();
175 return res;
176 }
178 /// Wrapper for ctmGetAttribMapString()
179 const char * GetAttribMapString(CTMenum aAttribMap, CTMenum aProperty)
180 {
181 const char * res = ctmGetAttribMapString(mContext, aAttribMap, aProperty);
182 CheckError();
183 return res;
184 }
186 /// Wrapper for ctmGetAttribMapFloat()
187 CTMfloat GetAttribMapFloat(CTMenum aAttribMap, CTMenum aProperty)
188 {
189 CTMfloat res = ctmGetAttribMapFloat(mContext, aAttribMap, aProperty);
190 CheckError();
191 return res;
192 }
194 /// Wrapper for ctmGetString()
195 const char * GetString(CTMenum aProperty)
196 {
197 const char * res = ctmGetString(mContext, aProperty);
198 CheckError();
199 return res;
200 }
202 /// Wrapper for ctmLoad()
203 void Load(const char * aFileName)
204 {
205 ctmLoad(mContext, aFileName);
206 CheckError();
207 }
209 /// Wrapper for ctmLoadCustom()
210 void LoadCustom(CTMreadfn aReadFn, void * aUserData)
211 {
212 ctmLoadCustom(mContext, aReadFn, aUserData);
213 CheckError();
214 }
216 // You can not copy nor assign from one CTMimporter object to another, since
217 // the object contains hidden state. By declaring these dummy prototypes
218 // without an implementation, you will at least get linker errors if you try
219 // to copy or assign a CTMimporter object.
220 CTMimporter(const CTMimporter& v);
221 CTMimporter& operator=(const CTMimporter& v);
222 };
225 /// OpenCTM exporter class. This is a C++ wrapper class for an OpenCTM export
226 /// context. Usage example:
227 /// @code
228 /// void MySaveFile(CTMuint aVertCount, CTMuint aTriCount, CTMfloat * aVertices,
229 /// CTMuint * aIndices, const char * aFileName)
230 /// {
231 /// // Create a new OpenCTM exporter object
232 /// CTMexporter ctm;
233 ///
234 /// // Define our mesh representation to OpenCTM (store references to it in
235 /// // the context)
236 /// ctm.DefineMesh(aVertices, aVertCount, aIndices, aTriCount, NULL);
237 ///
238 /// // Save the OpenCTM file
239 /// ctm.Save(aFileName);
240 /// }
241 /// @endcode
243 class CTMexporter {
244 private:
245 /// The OpenCTM context handle.
246 CTMcontext mContext;
248 /// Check for OpenCTM errors, and throw an exception if an error has
249 /// occured.
250 void CheckError()
251 {
252 CTMenum err = ctmGetError(mContext);
253 if(err != CTM_NONE)
254 throw ctm_error(err);
255 }
257 public:
258 /// Constructor
259 CTMexporter()
260 {
261 mContext = ctmNewContext(CTM_EXPORT);
262 }
264 /// Destructor
265 ~CTMexporter()
266 {
267 ctmFreeContext(mContext);
268 }
270 /// Wrapper for ctmCompressionMethod()
271 void CompressionMethod(CTMenum aMethod)
272 {
273 ctmCompressionMethod(mContext, aMethod);
274 CheckError();
275 }
277 /// Wrapper for ctmCompressionLevel()
278 void CompressionLevel(CTMuint aLevel)
279 {
280 ctmCompressionLevel(mContext, aLevel);
281 CheckError();
282 }
284 /// Wrapper for ctmVertexPrecision()
285 void VertexPrecision(CTMfloat aPrecision)
286 {
287 ctmVertexPrecision(mContext, aPrecision);
288 CheckError();
289 }
291 /// Wrapper for ctmVertexPrecisionRel()
292 void VertexPrecisionRel(CTMfloat aRelPrecision)
293 {
294 ctmVertexPrecisionRel(mContext, aRelPrecision);
295 CheckError();
296 }
298 /// Wrapper for ctmNormalPrecision()
299 void NormalPrecision(CTMfloat aPrecision)
300 {
301 ctmNormalPrecision(mContext, aPrecision);
302 CheckError();
303 }
305 /// Wrapper for ctmUVCoordPrecision()
306 void UVCoordPrecision(CTMenum aUVMap, CTMfloat aPrecision)
307 {
308 ctmUVCoordPrecision(mContext, aUVMap, aPrecision);
309 CheckError();
310 }
312 /// Wrapper for ctmAttribPrecision()
313 void AttribPrecision(CTMenum aAttribMap, CTMfloat aPrecision)
314 {
315 ctmAttribPrecision(mContext, aAttribMap, aPrecision);
316 CheckError();
317 }
319 /// Wrapper for ctmFileComment()
320 void FileComment(const char * aFileComment)
321 {
322 ctmFileComment(mContext, aFileComment);
323 CheckError();
324 }
326 /// Wrapper for ctmDefineMesh()
327 void DefineMesh(const CTMfloat * aVertices, CTMuint aVertexCount,
328 const CTMuint * aIndices, CTMuint aTriangleCount,
329 const CTMfloat * aNormals)
330 {
331 ctmDefineMesh(mContext, aVertices, aVertexCount, aIndices, aTriangleCount,
332 aNormals);
333 CheckError();
334 }
336 /// Wrapper for ctmAddUVMap()
337 CTMenum AddUVMap(const CTMfloat * aUVCoords, const char * aName,
338 const char * aFileName)
339 {
340 CTMenum res = ctmAddUVMap(mContext, aUVCoords, aName, aFileName);
341 CheckError();
342 return res;
343 }
345 /// Wrapper for ctmAddAttribMap()
346 CTMenum AddAttribMap(const CTMfloat * aAttribValues, const char * aName)
347 {
348 CTMenum res = ctmAddAttribMap(mContext, aAttribValues, aName);
349 CheckError();
350 return res;
351 }
353 /// Wrapper for ctmSave()
354 void Save(const char * aFileName)
355 {
356 ctmSave(mContext, aFileName);
357 CheckError();
358 }
360 /// Wrapper for ctmSaveCustom()
361 void SaveCustom(CTMwritefn aWriteFn, void * aUserData)
362 {
363 ctmSaveCustom(mContext, aWriteFn, aUserData);
364 CheckError();
365 }
367 // You can not copy nor assign from one CTMexporter object to another, since
368 // the object contains hidden state. By declaring these dummy prototypes
369 // without an implementation, you will at least get linker errors if you try
370 // to copy or assign a CTMexporter object.
371 CTMexporter(const CTMexporter& v);
372 CTMexporter& operator=(const CTMexporter& v);
373 };
375 #endif // __OPENCTMPP_H_
377 #endif // OPENCTM_NO_CPP