]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3data/md3lib.c
q3map2: -fs_nobasepath implies -fs_nomagicpath
[xonotic/netradiant.git] / tools / quake3 / q3data / md3lib.c
1 /*
2    Copyright (C) 1999-2006 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         len[hypotSide] = -1;
89
90         if ( len[0] > len[1] && len[0] > len[2] ) {
91                 longestSide = 0;
92         }
93         else if ( len[1] > len[0] && len[1] > len[2] ) {
94                 longestSide = 1;
95         }
96         else if ( len[2] > len[0] && len[2] > len[1] ) {
97                 longestSide = 2;
98         }
99         len[longestSide] = -1;
100
101         if ( len[0] > len[1] && len[0] > len[2] ) {
102                 shortestSide = 0;
103         }
104         else if ( len[1] > len[0] && len[1] > len[2] ) {
105                 shortestSide = 1;
106         }
107         else if ( len[2] > len[0] && len[2] > len[1] ) {
108                 shortestSide = 2;
109         }
110         len[shortestSide] = -1;
111
112
113
114 //      VectorNormalize( sides[shortestSide], axes[0] );
115 //      VectorNormalize( sides[longestSide], axes[1] );
116         VectorNormalize( sides[longestSide], axes[0] );
117         VectorNormalize( sides[shortestSide], axes[1] );
118
119         // project shortest side so that it is exactly 90 degrees to the longer side
120         d = DotProduct( axes[0], axes[1] );
121         VectorMA( axes[0], -d, axes[1], axes[0] );
122         VectorNormalize( axes[0], axes[0] );
123
124         CrossProduct( sides[longestSide], sides[shortestSide], axes[2] );
125         VectorNormalize( axes[2], axes[2] );
126
127         pTag->origin[0] = pTri[origin][0];
128         pTag->origin[1] = pTri[origin][1];
129         pTag->origin[2] = pTri[origin][2];
130
131         VectorCopy( axes[0], pTag->axis[0] );
132         VectorCopy( axes[1], pTag->axis[1] );
133         VectorCopy( axes[2], pTag->axis[2] );
134 }
135
136 /*
137    ==============
138    MD3_Dump
139    ==============
140  */
141 void MD3_Dump( const char *filename ){
142         md3Header_t header;
143         md3Tag_t *pTag;
144         md3Surface_t *pSurface;
145         FILE *fp;
146         void *_buffer;
147         void *buffer;
148         long fileSize;
149         int i;
150
151         if ( ( fp = fopen( filename, "rb" ) ) == 0 ) {
152                 Error( "Unable to open '%s'\n", filename );
153         }
154
155         fileSize = filelength( fp );
156         _buffer = malloc( fileSize );
157         fread( _buffer, fileSize, 1, fp );
158         fclose( fp );
159
160         buffer = ( char * ) _buffer;
161         header = *( md3Header_t * ) _buffer;
162
163         if ( header.ident != MD3_IDENT ) {
164                 Error( "Incorrect ident for '%s'\n", filename );
165         }
166
167         printf( "Contents of '%s'\n", filename );
168         printf( "  version:        %d\n", header.version );
169         printf( "  name:           %s\n", header.name );
170         printf( "  num frames:     %d\n", header.numFrames );
171         printf( "  num tags:       %d\n", header.numTags );
172         printf( "  num surfaces:   %d\n", header.numSurfaces );
173         printf( "  num skins:      %d\n", header.numSkins );
174         printf( "  file size:      %ld\n", fileSize );
175
176         printf( "--- TAGS ---\n" );
177         pTag = ( md3Tag_t * ) ( ( ( char * ) buffer ) + header.ofsTags );
178         for ( i = 0; i < header.numTags; i++, pTag++ )
179         {
180                 printf( "  tag %d ('%s')\n", i, pTag->name );
181                 printf( "    origin: %f,%f,%f\n", pTag->origin[0], pTag->origin[1], pTag->origin[2] );
182                 printf( "        vf: %f,%f,%f\n", pTag->axis[0][0], pTag->axis[0][1], pTag->axis[0][2] );
183                 printf( "        vr: %f,%f,%f\n", pTag->axis[1][0], pTag->axis[1][1], pTag->axis[1][2] );
184                 printf( "        vu: %f,%f,%f\n", pTag->axis[2][0], pTag->axis[2][1], pTag->axis[2][2] );
185         }
186
187         printf( "--- SURFACES ---\n" );
188         pSurface = ( md3Surface_t * ) ( ( ( char * ) buffer ) + header.ofsSurfaces );
189
190         for ( i = 0; i < header.numSurfaces; i++ )
191         {
192                 int j;
193
194                 md3Shader_t *pShader = ( md3Shader_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsShaders );
195
196                 printf( "\n  surface %d ('%s')\n", i, pSurface->name );
197                 printf( "    num frames: %d\n", pSurface->numFrames );
198                 printf( "    num shaders: %d\n", pSurface->numShaders );
199                 printf( "    num tris: %d\n", pSurface->numTriangles );
200                 printf( "    num verts: %d\n", pSurface->numVerts );
201
202                 if ( pSurface->numShaders > 0 ) {
203                         printf( "    --- SHADERS ---\n" );
204
205                         for ( j = 0; j < pSurface->numShaders; j++, pShader++ )
206                         {
207                                 printf( "    shader %d ('%s')\n", j, pShader->name );
208                         }
209                 }
210                 pSurface = ( md3Surface_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsEnd );
211         }
212
213         free( _buffer );
214 }