goat3d

diff libs/openctm/openctmpp.h @ 14:188c697b3b49

- added a document describing the goat3d file format chunk hierarchy - started an alternative XML-based file format - added the openctm library
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Sep 2013 04:47:05 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/openctm/openctmpp.h	Thu Sep 26 04:47:05 2013 +0300
     1.3 @@ -0,0 +1,377 @@
     1.4 +//-----------------------------------------------------------------------------
     1.5 +// Product:     OpenCTM
     1.6 +// File:        openctmpp.h
     1.7 +// Description: C++ wrapper for the OpenCTM API.
     1.8 +//-----------------------------------------------------------------------------
     1.9 +// Copyright (c) 2009-2010 Marcus Geelnard
    1.10 +//
    1.11 +// This software is provided 'as-is', without any express or implied
    1.12 +// warranty. In no event will the authors be held liable for any damages
    1.13 +// arising from the use of this software.
    1.14 +//
    1.15 +// Permission is granted to anyone to use this software for any purpose,
    1.16 +// including commercial applications, and to alter it and redistribute it
    1.17 +// freely, subject to the following restrictions:
    1.18 +//
    1.19 +//     1. The origin of this software must not be misrepresented; you must not
    1.20 +//     claim that you wrote the original software. If you use this software
    1.21 +//     in a product, an acknowledgment in the product documentation would be
    1.22 +//     appreciated but is not required.
    1.23 +//
    1.24 +//     2. Altered source versions must be plainly marked as such, and must not
    1.25 +//     be misrepresented as being the original software.
    1.26 +//
    1.27 +//     3. This notice may not be removed or altered from any source
    1.28 +//     distribution.
    1.29 +//-----------------------------------------------------------------------------
    1.30 +
    1.31 +// To disable C++ extensions, define OPENCTM_NO_CPP
    1.32 +#ifndef OPENCTM_NO_CPP
    1.33 +
    1.34 +#ifndef __OPENCTMPP_H_
    1.35 +#define __OPENCTMPP_H_
    1.36 +
    1.37 +// Just in case (if this file was included from outside openctm.h)...
    1.38 +#ifndef __OPENCTM_H_
    1.39 +#include "openctm.h"
    1.40 +#endif
    1.41 +
    1.42 +#include <exception>
    1.43 +
    1.44 +/// OpenCTM exception. When an error occurs, a \c ctm_error exception is
    1.45 +/// thrown. Its what() function returns the name of the OpenCTM error code
    1.46 +/// (for instance "CTM_INVALID_OPERATION").
    1.47 +class ctm_error: public std::exception
    1.48 +{
    1.49 +  private:
    1.50 +    CTMenum mErrorCode;
    1.51 +
    1.52 +  public:
    1.53 +    explicit ctm_error(CTMenum aError)
    1.54 +    {
    1.55 +      mErrorCode = aError;
    1.56 +    }
    1.57 +
    1.58 +    virtual const char* what() const throw()
    1.59 +    {
    1.60 +      return ctmErrorString(mErrorCode);
    1.61 +    }
    1.62 +
    1.63 +    CTMenum error_code() const throw()
    1.64 +    {
    1.65 +      return mErrorCode;
    1.66 +    }
    1.67 +};
    1.68 +
    1.69 +
    1.70 +/// OpenCTM importer class. This is a C++ wrapper class for an OpenCTM import
    1.71 +/// context. Usage example:
    1.72 +///
    1.73 +/// @code
    1.74 +///   // Create a new OpenCTM importer object
    1.75 +///   CTMimporter ctm;
    1.76 +///
    1.77 +///   // Load the OpenCTM file
    1.78 +///   ctm.Load("mymesh.ctm");
    1.79 +///
    1.80 +///   // Access the mesh data
    1.81 +///   vertCount = ctm.GetInteger(CTM_VERTEX_COUNT);
    1.82 +///   vertices = ctm.GetFloatArray(CTM_VERTICES);
    1.83 +///   triCount = ctm.GetInteger(CTM_TRIANGLE_COUNT);
    1.84 +///   indices = ctm.GetIntegerArray(CTM_INDICES);
    1.85 +///
    1.86 +///   // Deal with the mesh (e.g. transcode it to our internal representation)
    1.87 +///   // ...
    1.88 +/// @endcode
    1.89 +
    1.90 +class CTMimporter {
    1.91 +  private:
    1.92 +    /// The OpenCTM context handle.
    1.93 +    CTMcontext mContext;
    1.94 +
    1.95 +    /// Check for OpenCTM errors, and throw an exception if an error has
    1.96 +    /// occured.
    1.97 +    void CheckError()
    1.98 +    {
    1.99 +      CTMenum err = ctmGetError(mContext);
   1.100 +      if(err != CTM_NONE)
   1.101 +        throw ctm_error(err);
   1.102 +    }
   1.103 +
   1.104 +  public:
   1.105 +    /// Constructor
   1.106 +    CTMimporter()
   1.107 +    {
   1.108 +      mContext = ctmNewContext(CTM_IMPORT);
   1.109 +    }
   1.110 +
   1.111 +    /// Destructor
   1.112 +    ~CTMimporter()
   1.113 +    {
   1.114 +      ctmFreeContext(mContext);
   1.115 +    }
   1.116 +
   1.117 +    /// Wrapper for ctmGetInteger()
   1.118 +    CTMuint GetInteger(CTMenum aProperty)
   1.119 +    {
   1.120 +      CTMuint res = ctmGetInteger(mContext, aProperty);
   1.121 +      CheckError();
   1.122 +      return res;
   1.123 +    }
   1.124 +
   1.125 +    /// Wrapper for ctmGetFloat()
   1.126 +    CTMfloat GetFloat(CTMenum aProperty)
   1.127 +    {
   1.128 +      CTMfloat res = ctmGetFloat(mContext, aProperty);
   1.129 +      CheckError();
   1.130 +      return res;
   1.131 +    }
   1.132 +
   1.133 +    /// Wrapper for ctmGetIntegerArray()
   1.134 +    const CTMuint * GetIntegerArray(CTMenum aProperty)
   1.135 +    {
   1.136 +      const CTMuint * res = ctmGetIntegerArray(mContext, aProperty);
   1.137 +      CheckError();
   1.138 +      return res;
   1.139 +    }
   1.140 +
   1.141 +    /// Wrapper for ctmGetFloatArray()
   1.142 +    const CTMfloat * GetFloatArray(CTMenum aProperty)
   1.143 +    {
   1.144 +      const CTMfloat * res = ctmGetFloatArray(mContext, aProperty);
   1.145 +      CheckError();
   1.146 +      return res;
   1.147 +    }
   1.148 +
   1.149 +    /// Wrapper for ctmGetNamedUVMap()
   1.150 +    CTMenum GetNamedUVMap(const char * aName)
   1.151 +    {
   1.152 +      CTMenum res = ctmGetNamedUVMap(mContext, aName);
   1.153 +      CheckError();
   1.154 +      return res;
   1.155 +    }
   1.156 +
   1.157 +    /// Wrapper for ctmGetUVMapString()
   1.158 +    const char * GetUVMapString(CTMenum aUVMap, CTMenum aProperty)
   1.159 +    {
   1.160 +      const char * res = ctmGetUVMapString(mContext, aUVMap, aProperty);
   1.161 +      CheckError();
   1.162 +      return res;
   1.163 +    }
   1.164 +
   1.165 +    /// Wrapper for ctmGetUVMapFloat()
   1.166 +    CTMfloat GetUVMapFloat(CTMenum aUVMap, CTMenum aProperty)
   1.167 +    {
   1.168 +      CTMfloat res = ctmGetUVMapFloat(mContext, aUVMap, aProperty);
   1.169 +      CheckError();
   1.170 +      return res;
   1.171 +    }
   1.172 +
   1.173 +    /// Wrapper for ctmGetNamedAttribMap()
   1.174 +    CTMenum GetNamedAttribMap(const char * aName)
   1.175 +    {
   1.176 +      CTMenum res = ctmGetNamedAttribMap(mContext, aName);
   1.177 +      CheckError();
   1.178 +      return res;
   1.179 +    }
   1.180 +
   1.181 +    /// Wrapper for ctmGetAttribMapString()
   1.182 +    const char * GetAttribMapString(CTMenum aAttribMap, CTMenum aProperty)
   1.183 +    {
   1.184 +      const char * res = ctmGetAttribMapString(mContext, aAttribMap, aProperty);
   1.185 +      CheckError();
   1.186 +      return res;
   1.187 +    }
   1.188 +
   1.189 +    /// Wrapper for ctmGetAttribMapFloat()
   1.190 +    CTMfloat GetAttribMapFloat(CTMenum aAttribMap, CTMenum aProperty)
   1.191 +    {
   1.192 +      CTMfloat res = ctmGetAttribMapFloat(mContext, aAttribMap, aProperty);
   1.193 +      CheckError();
   1.194 +      return res;
   1.195 +    }
   1.196 +
   1.197 +    /// Wrapper for ctmGetString()
   1.198 +    const char * GetString(CTMenum aProperty)
   1.199 +    {
   1.200 +      const char * res = ctmGetString(mContext, aProperty);
   1.201 +      CheckError();
   1.202 +      return res;
   1.203 +    }
   1.204 +
   1.205 +    /// Wrapper for ctmLoad()
   1.206 +    void Load(const char * aFileName)
   1.207 +    {
   1.208 +      ctmLoad(mContext, aFileName);
   1.209 +      CheckError();
   1.210 +    }
   1.211 +
   1.212 +    /// Wrapper for ctmLoadCustom()
   1.213 +    void LoadCustom(CTMreadfn aReadFn, void * aUserData)
   1.214 +    {
   1.215 +      ctmLoadCustom(mContext, aReadFn, aUserData);
   1.216 +      CheckError();
   1.217 +    }
   1.218 +
   1.219 +    // You can not copy nor assign from one CTMimporter object to another, since
   1.220 +    // the object contains hidden state. By declaring these dummy prototypes
   1.221 +    // without an implementation, you will at least get linker errors if you try
   1.222 +    // to copy or assign a CTMimporter object.
   1.223 +    CTMimporter(const CTMimporter& v);
   1.224 +    CTMimporter& operator=(const CTMimporter& v);
   1.225 +};
   1.226 +
   1.227 +
   1.228 +/// OpenCTM exporter class. This is a C++ wrapper class for an OpenCTM export
   1.229 +/// context. Usage example:
   1.230 +/// @code
   1.231 +/// void MySaveFile(CTMuint aVertCount, CTMuint aTriCount, CTMfloat * aVertices,
   1.232 +///   CTMuint * aIndices, const char * aFileName)
   1.233 +/// {
   1.234 +///   // Create a new OpenCTM exporter object
   1.235 +///   CTMexporter ctm;
   1.236 +///
   1.237 +///   // Define our mesh representation to OpenCTM (store references to it in
   1.238 +///   // the context)
   1.239 +///   ctm.DefineMesh(aVertices, aVertCount, aIndices, aTriCount, NULL);
   1.240 +///
   1.241 +///   // Save the OpenCTM file
   1.242 +///   ctm.Save(aFileName);
   1.243 +/// }
   1.244 +/// @endcode
   1.245 +
   1.246 +class CTMexporter {
   1.247 +  private:
   1.248 +    /// The OpenCTM context handle.
   1.249 +    CTMcontext mContext;
   1.250 +
   1.251 +    /// Check for OpenCTM errors, and throw an exception if an error has
   1.252 +    /// occured.
   1.253 +    void CheckError()
   1.254 +    {
   1.255 +      CTMenum err = ctmGetError(mContext);
   1.256 +      if(err != CTM_NONE)
   1.257 +        throw ctm_error(err);
   1.258 +    }
   1.259 +
   1.260 +  public:
   1.261 +    /// Constructor
   1.262 +    CTMexporter()
   1.263 +    {
   1.264 +      mContext = ctmNewContext(CTM_EXPORT);
   1.265 +    }
   1.266 +
   1.267 +    /// Destructor
   1.268 +    ~CTMexporter()
   1.269 +    {
   1.270 +      ctmFreeContext(mContext);
   1.271 +    }
   1.272 +
   1.273 +    /// Wrapper for ctmCompressionMethod()
   1.274 +    void CompressionMethod(CTMenum aMethod)
   1.275 +    {
   1.276 +      ctmCompressionMethod(mContext, aMethod);
   1.277 +      CheckError();
   1.278 +    }
   1.279 +
   1.280 +    /// Wrapper for ctmCompressionLevel()
   1.281 +    void CompressionLevel(CTMuint aLevel)
   1.282 +    {
   1.283 +      ctmCompressionLevel(mContext, aLevel);
   1.284 +      CheckError();
   1.285 +    }
   1.286 +
   1.287 +    /// Wrapper for ctmVertexPrecision()
   1.288 +    void VertexPrecision(CTMfloat aPrecision)
   1.289 +    {
   1.290 +      ctmVertexPrecision(mContext, aPrecision);
   1.291 +      CheckError();
   1.292 +    }
   1.293 +
   1.294 +    /// Wrapper for ctmVertexPrecisionRel()
   1.295 +    void VertexPrecisionRel(CTMfloat aRelPrecision)
   1.296 +    {
   1.297 +      ctmVertexPrecisionRel(mContext, aRelPrecision);
   1.298 +      CheckError();
   1.299 +    }
   1.300 +
   1.301 +    /// Wrapper for ctmNormalPrecision()
   1.302 +    void NormalPrecision(CTMfloat aPrecision)
   1.303 +    {
   1.304 +      ctmNormalPrecision(mContext, aPrecision);
   1.305 +      CheckError();
   1.306 +    }
   1.307 +
   1.308 +    /// Wrapper for ctmUVCoordPrecision()
   1.309 +    void UVCoordPrecision(CTMenum aUVMap, CTMfloat aPrecision)
   1.310 +    {
   1.311 +      ctmUVCoordPrecision(mContext, aUVMap, aPrecision);
   1.312 +      CheckError();
   1.313 +    }
   1.314 +
   1.315 +    /// Wrapper for ctmAttribPrecision()
   1.316 +    void AttribPrecision(CTMenum aAttribMap, CTMfloat aPrecision)
   1.317 +    {
   1.318 +      ctmAttribPrecision(mContext, aAttribMap, aPrecision);
   1.319 +      CheckError();
   1.320 +    }
   1.321 +
   1.322 +    /// Wrapper for ctmFileComment()
   1.323 +    void FileComment(const char * aFileComment)
   1.324 +    {
   1.325 +      ctmFileComment(mContext, aFileComment);
   1.326 +      CheckError();
   1.327 +    }
   1.328 +
   1.329 +    /// Wrapper for ctmDefineMesh()
   1.330 +    void DefineMesh(const CTMfloat * aVertices, CTMuint aVertexCount, 
   1.331 +      const CTMuint * aIndices, CTMuint aTriangleCount,
   1.332 +      const CTMfloat * aNormals)
   1.333 +    {
   1.334 +      ctmDefineMesh(mContext, aVertices, aVertexCount, aIndices, aTriangleCount,
   1.335 +                    aNormals);
   1.336 +      CheckError();
   1.337 +    }
   1.338 +
   1.339 +    /// Wrapper for ctmAddUVMap()
   1.340 +    CTMenum AddUVMap(const CTMfloat * aUVCoords, const char * aName,
   1.341 +      const char * aFileName)
   1.342 +    {
   1.343 +      CTMenum res = ctmAddUVMap(mContext, aUVCoords, aName, aFileName);
   1.344 +      CheckError();
   1.345 +      return res;
   1.346 +    }
   1.347 +
   1.348 +    /// Wrapper for ctmAddAttribMap()
   1.349 +    CTMenum AddAttribMap(const CTMfloat * aAttribValues, const char * aName)
   1.350 +    {
   1.351 +      CTMenum res = ctmAddAttribMap(mContext, aAttribValues, aName);
   1.352 +      CheckError();
   1.353 +      return res;
   1.354 +    }
   1.355 +
   1.356 +    /// Wrapper for ctmSave()
   1.357 +    void Save(const char * aFileName)
   1.358 +    {
   1.359 +      ctmSave(mContext, aFileName);
   1.360 +      CheckError();
   1.361 +    }
   1.362 +
   1.363 +    /// Wrapper for ctmSaveCustom()
   1.364 +    void SaveCustom(CTMwritefn aWriteFn, void * aUserData)
   1.365 +    {
   1.366 +      ctmSaveCustom(mContext, aWriteFn, aUserData);
   1.367 +      CheckError();
   1.368 +    }
   1.369 +
   1.370 +    // You can not copy nor assign from one CTMexporter object to another, since
   1.371 +    // the object contains hidden state. By declaring these dummy prototypes
   1.372 +    // without an implementation, you will at least get linker errors if you try
   1.373 +    // to copy or assign a CTMexporter object.
   1.374 +    CTMexporter(const CTMexporter& v);
   1.375 +    CTMexporter& operator=(const CTMexporter& v);
   1.376 +};
   1.377 +
   1.378 +#endif // __OPENCTMPP_H_
   1.379 +
   1.380 +#endif // OPENCTM_NO_CPP