goat3d

diff libs/openctm/compressRAW.c @ 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/compressRAW.c	Thu Sep 26 04:47:05 2013 +0300
     1.3 @@ -0,0 +1,181 @@
     1.4 +//-----------------------------------------------------------------------------
     1.5 +// Product:     OpenCTM
     1.6 +// File:        compressRAW.c
     1.7 +// Description: Implementation of the RAW compression method.
     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 +#include "openctm.h"
    1.32 +#include "internal.h"
    1.33 +
    1.34 +#ifdef __DEBUG_
    1.35 +#include <stdio.h>
    1.36 +#endif
    1.37 +
    1.38 +
    1.39 +//-----------------------------------------------------------------------------
    1.40 +// _ctmCompressMesh_RAW() - Compress the mesh that is stored in the CTM
    1.41 +// context using the RAW method, and write it the the output stream in the CTM
    1.42 +// context.
    1.43 +//-----------------------------------------------------------------------------
    1.44 +int _ctmCompressMesh_RAW(_CTMcontext * self)
    1.45 +{
    1.46 +  CTMuint i;
    1.47 +  _CTMfloatmap * map;
    1.48 +
    1.49 +#ifdef __DEBUG_
    1.50 +  printf("COMPRESSION METHOD: RAW\n");
    1.51 +#endif
    1.52 +
    1.53 +  // Write triangle indices
    1.54 +#ifdef __DEBUG_
    1.55 +  printf("Inidices: %d bytes\n", (CTMuint)(self->mTriangleCount * 3 * sizeof(CTMuint)));
    1.56 +#endif
    1.57 +  _ctmStreamWrite(self, (void *) "INDX", 4);
    1.58 +  for(i = 0; i < self->mTriangleCount * 3; ++ i)
    1.59 +    _ctmStreamWriteUINT(self, self->mIndices[i]);
    1.60 +
    1.61 +  // Write vertices
    1.62 +#ifdef __DEBUG_
    1.63 +  printf("Vertices: %d bytes\n", (CTMuint)(self->mVertexCount * 3 * sizeof(CTMfloat)));
    1.64 +#endif
    1.65 +  _ctmStreamWrite(self, (void *) "VERT", 4);
    1.66 +  for(i = 0; i < self->mVertexCount * 3; ++ i)
    1.67 +    _ctmStreamWriteFLOAT(self, self->mVertices[i]);
    1.68 +
    1.69 +  // Write normals
    1.70 +  if(self->mNormals)
    1.71 +  {
    1.72 +#ifdef __DEBUG_
    1.73 +    printf("Normals: %d bytes\n", (CTMuint)(self->mVertexCount * 3 * sizeof(CTMfloat)));
    1.74 +#endif
    1.75 +    _ctmStreamWrite(self, (void *) "NORM", 4);
    1.76 +    for(i = 0; i < self->mVertexCount * 3; ++ i)
    1.77 +      _ctmStreamWriteFLOAT(self, self->mNormals[i]);
    1.78 +  }
    1.79 +
    1.80 +  // Write UV maps
    1.81 +  map = self->mUVMaps;
    1.82 +  while(map)
    1.83 +  {
    1.84 +#ifdef __DEBUG_
    1.85 +    printf("UV coordinates (%s): %d bytes\n", map->mName ? map->mName : "no name", (CTMuint)(self->mVertexCount * 2 * sizeof(CTMfloat)));
    1.86 +#endif
    1.87 +    _ctmStreamWrite(self, (void *) "TEXC", 4);
    1.88 +    _ctmStreamWriteSTRING(self, map->mName);
    1.89 +    _ctmStreamWriteSTRING(self, map->mFileName);
    1.90 +    for(i = 0; i < self->mVertexCount * 2; ++ i)
    1.91 +      _ctmStreamWriteFLOAT(self, map->mValues[i]);
    1.92 +    map = map->mNext;
    1.93 +  }
    1.94 +
    1.95 +  // Write attribute maps
    1.96 +  map = self->mAttribMaps;
    1.97 +  while(map)
    1.98 +  {
    1.99 +#ifdef __DEBUG_
   1.100 +    printf("Vertex attributes (%s): %d bytes\n", map->mName ? map->mName : "no name", (CTMuint)(self->mVertexCount * 4 * sizeof(CTMfloat)));
   1.101 +#endif
   1.102 +    _ctmStreamWrite(self, (void *) "ATTR", 4);
   1.103 +    _ctmStreamWriteSTRING(self, map->mName);
   1.104 +    for(i = 0; i < self->mVertexCount * 4; ++ i)
   1.105 +      _ctmStreamWriteFLOAT(self, map->mValues[i]);
   1.106 +    map = map->mNext;
   1.107 +  }
   1.108 +
   1.109 +  return 1;
   1.110 +}
   1.111 +
   1.112 +//-----------------------------------------------------------------------------
   1.113 +// _ctmUncompressMesh_RAW() - Uncmpress the mesh from the input stream in the
   1.114 +// CTM context using the RAW method, and store the resulting mesh in the CTM
   1.115 +// context.
   1.116 +//-----------------------------------------------------------------------------
   1.117 +int _ctmUncompressMesh_RAW(_CTMcontext * self)
   1.118 +{
   1.119 +  CTMuint i;
   1.120 +  _CTMfloatmap * map;
   1.121 +
   1.122 +  // Read triangle indices
   1.123 +  if(_ctmStreamReadUINT(self) != FOURCC("INDX"))
   1.124 +  {
   1.125 +    self->mError = CTM_BAD_FORMAT;
   1.126 +    return 0;
   1.127 +  }
   1.128 +  for(i = 0; i < self->mTriangleCount * 3; ++ i)
   1.129 +    self->mIndices[i] = _ctmStreamReadUINT(self);
   1.130 +
   1.131 +  // Read vertices
   1.132 +  if(_ctmStreamReadUINT(self) != FOURCC("VERT"))
   1.133 +  {
   1.134 +    self->mError = CTM_BAD_FORMAT;
   1.135 +    return 0;
   1.136 +  }
   1.137 +  for(i = 0; i < self->mVertexCount * 3; ++ i)
   1.138 +    self->mVertices[i] = _ctmStreamReadFLOAT(self);
   1.139 +
   1.140 +  // Read normals
   1.141 +  if(self->mNormals)
   1.142 +  {
   1.143 +    if(_ctmStreamReadUINT(self) != FOURCC("NORM"))
   1.144 +    {
   1.145 +      self->mError = CTM_BAD_FORMAT;
   1.146 +      return 0;
   1.147 +    }
   1.148 +    for(i = 0; i < self->mVertexCount * 3; ++ i)
   1.149 +      self->mNormals[i] = _ctmStreamReadFLOAT(self);
   1.150 +  }
   1.151 +
   1.152 +  // Read UV maps
   1.153 +  map = self->mUVMaps;
   1.154 +  while(map)
   1.155 +  {
   1.156 +    if(_ctmStreamReadUINT(self) != FOURCC("TEXC"))
   1.157 +    {
   1.158 +      self->mError = CTM_BAD_FORMAT;
   1.159 +      return 0;
   1.160 +    }
   1.161 +    _ctmStreamReadSTRING(self, &map->mName);
   1.162 +    _ctmStreamReadSTRING(self, &map->mFileName);
   1.163 +    for(i = 0; i < self->mVertexCount * 2; ++ i)
   1.164 +      map->mValues[i] = _ctmStreamReadFLOAT(self);
   1.165 +    map = map->mNext;
   1.166 +  }
   1.167 +
   1.168 +  // Read attribute maps
   1.169 +  map = self->mAttribMaps;
   1.170 +  while(map)
   1.171 +  {
   1.172 +    if(_ctmStreamReadUINT(self) != FOURCC("ATTR"))
   1.173 +    {
   1.174 +      self->mError = CTM_BAD_FORMAT;
   1.175 +      return 0;
   1.176 +    }
   1.177 +    _ctmStreamReadSTRING(self, &map->mName);
   1.178 +    for(i = 0; i < self->mVertexCount * 4; ++ i)
   1.179 +      map->mValues[i] = _ctmStreamReadFLOAT(self);
   1.180 +    map = map->mNext;
   1.181 +  }
   1.182 +
   1.183 +  return 1;
   1.184 +}