]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3data/md3lib.c
gcc: appease the hardening warnings
[xonotic/netradiant.git] / tools / quake3 / q3data / md3lib.c
1 /*
2    Copyright (C) 1999-2007 id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "globaldefs.h"
23 #include <assert.h>
24 #if GDEF_OS_WINDOWS
25 #include <io.h>
26 #endif
27 #include "md3lib.h"
28
29 #if GDEF_OS_LINUX || GDEF_OS_MACOS
30 #define filelength(f) Q_filelength(f)
31 #else
32 #define filelength(f) filelength(fileno(f))
33 #endif
34
35 /*
36 ** MD3_ComputeTagFromTri
37 */
38 void MD3_ComputeTagFromTri( md3Tag_t *pTag, float pTri[3][3] ){
39         float len[3];
40         vec3_t axes[3], sides[3];
41         int longestSide, shortestSide, hypotSide;
42         int origin;
43         int j;
44         float d;
45
46         memset( axes, 0, sizeof( axes ) );
47         memset( sides, 0, sizeof( sides ) );
48
49         //
50         // compute sides
51         //
52         for ( j = 0; j < 3; j++ )
53         {
54                 sides[j][0] = pTri[( j + 1 ) % 3][0] - pTri[j][0];
55                 sides[j][1] = pTri[( j + 1 ) % 3][1] - pTri[j][1];
56                 sides[j][2] = pTri[( j + 1 ) % 3][2] - pTri[j][2];
57
58                 len[j] = ( float ) sqrt( DotProduct( sides[j], sides[j] ) );
59         }
60
61 #if 0
62         if ( len[0] > len[1] && len[0] > len[2] ) {
63                 longestSide = 0; shortestSide = 1; origin = 2;
64         }
65         else if ( len[1] > len[0] && len[1] > len[2] ) {
66                 longestSide = 1; shortestSide = 2; origin = 0;
67         }
68         else if ( len[2] > len[0] && len[2] > len[1] ) {
69                 longestSide = 2; shortestSide = 0; origin = 1;
70         }
71         else
72         {
73                 Error( "invalid tag triangle, must be a right triangle with unequal length sides" );
74         }
75 #endif
76         if ( len[0] > len[1] && len[0] > len[2] ) {
77                 hypotSide = 0;
78                 origin = 2;
79         }
80         else if ( len[1] > len[0] && len[1] > len[2] ) {
81                 hypotSide = 1;
82                 origin = 0;
83         }
84         else if ( len[2] > len[0] && len[2] > len[1] ) {
85                 hypotSide = 2;
86                 origin = 1;
87         }
88         else {
89                 assert(0);
90         }
91         len[hypotSide] = -1;
92
93         if ( len[0] > len[1] && len[0] > len[2] ) {
94                 longestSide = 0;
95         }
96         else if ( len[1] > len[0] && len[1] > len[2] ) {
97                 longestSide = 1;
98         }
99         else if ( len[2] > len[0] && len[2] > len[1] ) {
100                 longestSide = 2;
101         }
102         else {
103                 assert(0);
104         }
105         len[longestSide] = -1;
106
107         if ( len[0] > len[1] && len[0] > len[2] ) {
108                 shortestSide = 0;
109         }
110         else if ( len[1] > len[0] && len[1] > len[2] ) {
111                 shortestSide = 1;
112         }
113         else if ( len[2] > len[0] && len[2] > len[1] ) {
114                 shortestSide = 2;
115         }
116         else {
117                 assert(0);
118         }
119         len[shortestSide] = -1;
120
121
122
123 //      VectorNormalize( sides[shortestSide], axes[0] );
124 //      VectorNormalize( sides[longestSide], axes[1] );
125         VectorNormalize( sides[longestSide], axes[0] );
126         VectorNormalize( sides[shortestSide], axes[1] );
127
128         // project shortest side so that it is exactly 90 degrees to the longer side
129         d = DotProduct( axes[0], axes[1] );
130         VectorMA( axes[0], -d, axes[1], axes[0] );
131         VectorNormalize( axes[0], axes[0] );
132
133         CrossProduct( sides[longestSide], sides[shortestSide], axes[2] );
134         VectorNormalize( axes[2], axes[2] );
135
136         pTag->origin[0] = pTri[origin][0];
137         pTag->origin[1] = pTri[origin][1];
138         pTag->origin[2] = pTri[origin][2];
139
140         VectorCopy( axes[0], pTag->axis[0] );
141         VectorCopy( axes[1], pTag->axis[1] );
142         VectorCopy( axes[2], pTag->axis[2] );
143 }
144
145 /*
146    ==============
147    MD3_Dump
148    ==============
149  */
150 void MD3_Dump( const char *filename ){
151         md3Header_t header;
152         md3Tag_t *pTag;
153         md3Surface_t *pSurface;
154         FILE *fp;
155         void *_buffer;
156         void *buffer;
157         long fileSize;
158         int i;
159
160         if ( ( fp = fopen( filename, "rb" ) ) == 0 ) {
161                 Error( "Unable to open '%s'\n", filename );
162         }
163
164         fileSize = filelength( fp );
165         _buffer = malloc( fileSize );
166         assert(fread( _buffer, fileSize, 1, fp ));
167         fclose( fp );
168
169         buffer = ( char * ) _buffer;
170         header = *( md3Header_t * ) _buffer;
171
172         if ( header.ident != MD3_IDENT ) {
173                 Error( "Incorrect ident for '%s'\n", filename );
174         }
175
176         printf( "Contents of '%s'\n", filename );
177         printf( "  version:        %d\n", header.version );
178         printf( "  name:           %s\n", header.name );
179         printf( "  num frames:     %d\n", header.numFrames );
180         printf( "  num tags:       %d\n", header.numTags );
181         printf( "  num surfaces:   %d\n", header.numSurfaces );
182         printf( "  num skins:      %d\n", header.numSkins );
183         printf( "  file size:      %ld\n", fileSize );
184
185         printf( "--- TAGS ---\n" );
186         pTag = ( md3Tag_t * ) ( ( ( char * ) buffer ) + header.ofsTags );
187         for ( i = 0; i < header.numTags; i++, pTag++ )
188         {
189                 printf( "  tag %d ('%s')\n", i, pTag->name );
190                 printf( "    origin: %f,%f,%f\n", pTag->origin[0], pTag->origin[1], pTag->origin[2] );
191                 printf( "        vf: %f,%f,%f\n", pTag->axis[0][0], pTag->axis[0][1], pTag->axis[0][2] );
192                 printf( "        vr: %f,%f,%f\n", pTag->axis[1][0], pTag->axis[1][1], pTag->axis[1][2] );
193                 printf( "        vu: %f,%f,%f\n", pTag->axis[2][0], pTag->axis[2][1], pTag->axis[2][2] );
194         }
195
196         printf( "--- SURFACES ---\n" );
197         pSurface = ( md3Surface_t * ) ( ( ( char * ) buffer ) + header.ofsSurfaces );
198
199         for ( i = 0; i < header.numSurfaces; i++ )
200         {
201                 int j;
202
203                 md3Shader_t *pShader = ( md3Shader_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsShaders );
204
205                 printf( "\n  surface %d ('%s')\n", i, pSurface->name );
206                 printf( "    num frames: %d\n", pSurface->numFrames );
207                 printf( "    num shaders: %d\n", pSurface->numShaders );
208                 printf( "    num tris: %d\n", pSurface->numTriangles );
209                 printf( "    num verts: %d\n", pSurface->numVerts );
210
211                 if ( pSurface->numShaders > 0 ) {
212                         printf( "    --- SHADERS ---\n" );
213
214                         for ( j = 0; j < pSurface->numShaders; j++, pShader++ )
215                         {
216                                 printf( "    shader %d ('%s')\n", j, pShader->name );
217                         }
218                 }
219                 pSurface = ( md3Surface_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsEnd );
220         }
221
222         free( _buffer );
223 }