nuclear@14: //-----------------------------------------------------------------------------
nuclear@14: // Product:     OpenCTM
nuclear@14: // File:        compressRAW.c
nuclear@14: // Description: Implementation of the RAW compression method.
nuclear@14: //-----------------------------------------------------------------------------
nuclear@14: // Copyright (c) 2009-2010 Marcus Geelnard
nuclear@14: //
nuclear@14: // This software is provided 'as-is', without any express or implied
nuclear@14: // warranty. In no event will the authors be held liable for any damages
nuclear@14: // arising from the use of this software.
nuclear@14: //
nuclear@14: // Permission is granted to anyone to use this software for any purpose,
nuclear@14: // including commercial applications, and to alter it and redistribute it
nuclear@14: // freely, subject to the following restrictions:
nuclear@14: //
nuclear@14: //     1. The origin of this software must not be misrepresented; you must not
nuclear@14: //     claim that you wrote the original software. If you use this software
nuclear@14: //     in a product, an acknowledgment in the product documentation would be
nuclear@14: //     appreciated but is not required.
nuclear@14: //
nuclear@14: //     2. Altered source versions must be plainly marked as such, and must not
nuclear@14: //     be misrepresented as being the original software.
nuclear@14: //
nuclear@14: //     3. This notice may not be removed or altered from any source
nuclear@14: //     distribution.
nuclear@14: //-----------------------------------------------------------------------------
nuclear@14: 
nuclear@14: #include "openctm.h"
nuclear@14: #include "internal.h"
nuclear@14: 
nuclear@14: #ifdef __DEBUG_
nuclear@14: #include <stdio.h>
nuclear@14: #endif
nuclear@14: 
nuclear@14: 
nuclear@14: //-----------------------------------------------------------------------------
nuclear@14: // _ctmCompressMesh_RAW() - Compress the mesh that is stored in the CTM
nuclear@14: // context using the RAW method, and write it the the output stream in the CTM
nuclear@14: // context.
nuclear@14: //-----------------------------------------------------------------------------
nuclear@14: int _ctmCompressMesh_RAW(_CTMcontext * self)
nuclear@14: {
nuclear@14:   CTMuint i;
nuclear@14:   _CTMfloatmap * map;
nuclear@14: 
nuclear@14: #ifdef __DEBUG_
nuclear@14:   printf("COMPRESSION METHOD: RAW\n");
nuclear@14: #endif
nuclear@14: 
nuclear@14:   // Write triangle indices
nuclear@14: #ifdef __DEBUG_
nuclear@14:   printf("Inidices: %d bytes\n", (CTMuint)(self->mTriangleCount * 3 * sizeof(CTMuint)));
nuclear@14: #endif
nuclear@14:   _ctmStreamWrite(self, (void *) "INDX", 4);
nuclear@14:   for(i = 0; i < self->mTriangleCount * 3; ++ i)
nuclear@14:     _ctmStreamWriteUINT(self, self->mIndices[i]);
nuclear@14: 
nuclear@14:   // Write vertices
nuclear@14: #ifdef __DEBUG_
nuclear@14:   printf("Vertices: %d bytes\n", (CTMuint)(self->mVertexCount * 3 * sizeof(CTMfloat)));
nuclear@14: #endif
nuclear@14:   _ctmStreamWrite(self, (void *) "VERT", 4);
nuclear@14:   for(i = 0; i < self->mVertexCount * 3; ++ i)
nuclear@14:     _ctmStreamWriteFLOAT(self, self->mVertices[i]);
nuclear@14: 
nuclear@14:   // Write normals
nuclear@14:   if(self->mNormals)
nuclear@14:   {
nuclear@14: #ifdef __DEBUG_
nuclear@14:     printf("Normals: %d bytes\n", (CTMuint)(self->mVertexCount * 3 * sizeof(CTMfloat)));
nuclear@14: #endif
nuclear@14:     _ctmStreamWrite(self, (void *) "NORM", 4);
nuclear@14:     for(i = 0; i < self->mVertexCount * 3; ++ i)
nuclear@14:       _ctmStreamWriteFLOAT(self, self->mNormals[i]);
nuclear@14:   }
nuclear@14: 
nuclear@14:   // Write UV maps
nuclear@14:   map = self->mUVMaps;
nuclear@14:   while(map)
nuclear@14:   {
nuclear@14: #ifdef __DEBUG_
nuclear@14:     printf("UV coordinates (%s): %d bytes\n", map->mName ? map->mName : "no name", (CTMuint)(self->mVertexCount * 2 * sizeof(CTMfloat)));
nuclear@14: #endif
nuclear@14:     _ctmStreamWrite(self, (void *) "TEXC", 4);
nuclear@14:     _ctmStreamWriteSTRING(self, map->mName);
nuclear@14:     _ctmStreamWriteSTRING(self, map->mFileName);
nuclear@14:     for(i = 0; i < self->mVertexCount * 2; ++ i)
nuclear@14:       _ctmStreamWriteFLOAT(self, map->mValues[i]);
nuclear@14:     map = map->mNext;
nuclear@14:   }
nuclear@14: 
nuclear@14:   // Write attribute maps
nuclear@14:   map = self->mAttribMaps;
nuclear@14:   while(map)
nuclear@14:   {
nuclear@14: #ifdef __DEBUG_
nuclear@14:     printf("Vertex attributes (%s): %d bytes\n", map->mName ? map->mName : "no name", (CTMuint)(self->mVertexCount * 4 * sizeof(CTMfloat)));
nuclear@14: #endif
nuclear@14:     _ctmStreamWrite(self, (void *) "ATTR", 4);
nuclear@14:     _ctmStreamWriteSTRING(self, map->mName);
nuclear@14:     for(i = 0; i < self->mVertexCount * 4; ++ i)
nuclear@14:       _ctmStreamWriteFLOAT(self, map->mValues[i]);
nuclear@14:     map = map->mNext;
nuclear@14:   }
nuclear@14: 
nuclear@14:   return 1;
nuclear@14: }
nuclear@14: 
nuclear@14: //-----------------------------------------------------------------------------
nuclear@14: // _ctmUncompressMesh_RAW() - Uncmpress the mesh from the input stream in the
nuclear@14: // CTM context using the RAW method, and store the resulting mesh in the CTM
nuclear@14: // context.
nuclear@14: //-----------------------------------------------------------------------------
nuclear@14: int _ctmUncompressMesh_RAW(_CTMcontext * self)
nuclear@14: {
nuclear@14:   CTMuint i;
nuclear@14:   _CTMfloatmap * map;
nuclear@14: 
nuclear@14:   // Read triangle indices
nuclear@14:   if(_ctmStreamReadUINT(self) != FOURCC("INDX"))
nuclear@14:   {
nuclear@14:     self->mError = CTM_BAD_FORMAT;
nuclear@14:     return 0;
nuclear@14:   }
nuclear@14:   for(i = 0; i < self->mTriangleCount * 3; ++ i)
nuclear@14:     self->mIndices[i] = _ctmStreamReadUINT(self);
nuclear@14: 
nuclear@14:   // Read vertices
nuclear@14:   if(_ctmStreamReadUINT(self) != FOURCC("VERT"))
nuclear@14:   {
nuclear@14:     self->mError = CTM_BAD_FORMAT;
nuclear@14:     return 0;
nuclear@14:   }
nuclear@14:   for(i = 0; i < self->mVertexCount * 3; ++ i)
nuclear@14:     self->mVertices[i] = _ctmStreamReadFLOAT(self);
nuclear@14: 
nuclear@14:   // Read normals
nuclear@14:   if(self->mNormals)
nuclear@14:   {
nuclear@14:     if(_ctmStreamReadUINT(self) != FOURCC("NORM"))
nuclear@14:     {
nuclear@14:       self->mError = CTM_BAD_FORMAT;
nuclear@14:       return 0;
nuclear@14:     }
nuclear@14:     for(i = 0; i < self->mVertexCount * 3; ++ i)
nuclear@14:       self->mNormals[i] = _ctmStreamReadFLOAT(self);
nuclear@14:   }
nuclear@14: 
nuclear@14:   // Read UV maps
nuclear@14:   map = self->mUVMaps;
nuclear@14:   while(map)
nuclear@14:   {
nuclear@14:     if(_ctmStreamReadUINT(self) != FOURCC("TEXC"))
nuclear@14:     {
nuclear@14:       self->mError = CTM_BAD_FORMAT;
nuclear@14:       return 0;
nuclear@14:     }
nuclear@14:     _ctmStreamReadSTRING(self, &map->mName);
nuclear@14:     _ctmStreamReadSTRING(self, &map->mFileName);
nuclear@14:     for(i = 0; i < self->mVertexCount * 2; ++ i)
nuclear@14:       map->mValues[i] = _ctmStreamReadFLOAT(self);
nuclear@14:     map = map->mNext;
nuclear@14:   }
nuclear@14: 
nuclear@14:   // Read attribute maps
nuclear@14:   map = self->mAttribMaps;
nuclear@14:   while(map)
nuclear@14:   {
nuclear@14:     if(_ctmStreamReadUINT(self) != FOURCC("ATTR"))
nuclear@14:     {
nuclear@14:       self->mError = CTM_BAD_FORMAT;
nuclear@14:       return 0;
nuclear@14:     }
nuclear@14:     _ctmStreamReadSTRING(self, &map->mName);
nuclear@14:     for(i = 0; i < self->mVertexCount * 4; ++ i)
nuclear@14:       map->mValues[i] = _ctmStreamReadFLOAT(self);
nuclear@14:     map = map->mNext;
nuclear@14:   }
nuclear@14: 
nuclear@14:   return 1;
nuclear@14: }