goat3d
view libs/openctm/compressRAW.c @ 65:046f7df5844d
fixed readme
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 17 Apr 2014 08:56:46 +0300 |
parents | |
children |
line source
1 //-----------------------------------------------------------------------------
2 // Product: OpenCTM
3 // File: compressRAW.c
4 // Description: Implementation of the RAW compression method.
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 #include "openctm.h"
29 #include "internal.h"
31 #ifdef __DEBUG_
32 #include <stdio.h>
33 #endif
36 //-----------------------------------------------------------------------------
37 // _ctmCompressMesh_RAW() - Compress the mesh that is stored in the CTM
38 // context using the RAW method, and write it the the output stream in the CTM
39 // context.
40 //-----------------------------------------------------------------------------
41 int _ctmCompressMesh_RAW(_CTMcontext * self)
42 {
43 CTMuint i;
44 _CTMfloatmap * map;
46 #ifdef __DEBUG_
47 printf("COMPRESSION METHOD: RAW\n");
48 #endif
50 // Write triangle indices
51 #ifdef __DEBUG_
52 printf("Inidices: %d bytes\n", (CTMuint)(self->mTriangleCount * 3 * sizeof(CTMuint)));
53 #endif
54 _ctmStreamWrite(self, (void *) "INDX", 4);
55 for(i = 0; i < self->mTriangleCount * 3; ++ i)
56 _ctmStreamWriteUINT(self, self->mIndices[i]);
58 // Write vertices
59 #ifdef __DEBUG_
60 printf("Vertices: %d bytes\n", (CTMuint)(self->mVertexCount * 3 * sizeof(CTMfloat)));
61 #endif
62 _ctmStreamWrite(self, (void *) "VERT", 4);
63 for(i = 0; i < self->mVertexCount * 3; ++ i)
64 _ctmStreamWriteFLOAT(self, self->mVertices[i]);
66 // Write normals
67 if(self->mNormals)
68 {
69 #ifdef __DEBUG_
70 printf("Normals: %d bytes\n", (CTMuint)(self->mVertexCount * 3 * sizeof(CTMfloat)));
71 #endif
72 _ctmStreamWrite(self, (void *) "NORM", 4);
73 for(i = 0; i < self->mVertexCount * 3; ++ i)
74 _ctmStreamWriteFLOAT(self, self->mNormals[i]);
75 }
77 // Write UV maps
78 map = self->mUVMaps;
79 while(map)
80 {
81 #ifdef __DEBUG_
82 printf("UV coordinates (%s): %d bytes\n", map->mName ? map->mName : "no name", (CTMuint)(self->mVertexCount * 2 * sizeof(CTMfloat)));
83 #endif
84 _ctmStreamWrite(self, (void *) "TEXC", 4);
85 _ctmStreamWriteSTRING(self, map->mName);
86 _ctmStreamWriteSTRING(self, map->mFileName);
87 for(i = 0; i < self->mVertexCount * 2; ++ i)
88 _ctmStreamWriteFLOAT(self, map->mValues[i]);
89 map = map->mNext;
90 }
92 // Write attribute maps
93 map = self->mAttribMaps;
94 while(map)
95 {
96 #ifdef __DEBUG_
97 printf("Vertex attributes (%s): %d bytes\n", map->mName ? map->mName : "no name", (CTMuint)(self->mVertexCount * 4 * sizeof(CTMfloat)));
98 #endif
99 _ctmStreamWrite(self, (void *) "ATTR", 4);
100 _ctmStreamWriteSTRING(self, map->mName);
101 for(i = 0; i < self->mVertexCount * 4; ++ i)
102 _ctmStreamWriteFLOAT(self, map->mValues[i]);
103 map = map->mNext;
104 }
106 return 1;
107 }
109 //-----------------------------------------------------------------------------
110 // _ctmUncompressMesh_RAW() - Uncmpress the mesh from the input stream in the
111 // CTM context using the RAW method, and store the resulting mesh in the CTM
112 // context.
113 //-----------------------------------------------------------------------------
114 int _ctmUncompressMesh_RAW(_CTMcontext * self)
115 {
116 CTMuint i;
117 _CTMfloatmap * map;
119 // Read triangle indices
120 if(_ctmStreamReadUINT(self) != FOURCC("INDX"))
121 {
122 self->mError = CTM_BAD_FORMAT;
123 return 0;
124 }
125 for(i = 0; i < self->mTriangleCount * 3; ++ i)
126 self->mIndices[i] = _ctmStreamReadUINT(self);
128 // Read vertices
129 if(_ctmStreamReadUINT(self) != FOURCC("VERT"))
130 {
131 self->mError = CTM_BAD_FORMAT;
132 return 0;
133 }
134 for(i = 0; i < self->mVertexCount * 3; ++ i)
135 self->mVertices[i] = _ctmStreamReadFLOAT(self);
137 // Read normals
138 if(self->mNormals)
139 {
140 if(_ctmStreamReadUINT(self) != FOURCC("NORM"))
141 {
142 self->mError = CTM_BAD_FORMAT;
143 return 0;
144 }
145 for(i = 0; i < self->mVertexCount * 3; ++ i)
146 self->mNormals[i] = _ctmStreamReadFLOAT(self);
147 }
149 // Read UV maps
150 map = self->mUVMaps;
151 while(map)
152 {
153 if(_ctmStreamReadUINT(self) != FOURCC("TEXC"))
154 {
155 self->mError = CTM_BAD_FORMAT;
156 return 0;
157 }
158 _ctmStreamReadSTRING(self, &map->mName);
159 _ctmStreamReadSTRING(self, &map->mFileName);
160 for(i = 0; i < self->mVertexCount * 2; ++ i)
161 map->mValues[i] = _ctmStreamReadFLOAT(self);
162 map = map->mNext;
163 }
165 // Read attribute maps
166 map = self->mAttribMaps;
167 while(map)
168 {
169 if(_ctmStreamReadUINT(self) != FOURCC("ATTR"))
170 {
171 self->mError = CTM_BAD_FORMAT;
172 return 0;
173 }
174 _ctmStreamReadSTRING(self, &map->mName);
175 for(i = 0; i < self->mVertexCount * 4; ++ i)
176 map->mValues[i] = _ctmStreamReadFLOAT(self);
177 map = map->mNext;
178 }
180 return 1;
181 }