2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
28 #if defined ( __linux__ ) || defined ( __APPLE__ )
29 #define filelength(f) Q_filelength(f)
31 #define filelength(f) filelength(fileno(f))
35 ** MD3_ComputeTagFromTri
37 void MD3_ComputeTagFromTri( md3Tag_t *pTag, float pTri[3][3] ){
39 vec3_t axes[3], sides[3];
40 int longestSide, shortestSide, hypotSide;
45 memset( axes, 0, sizeof( axes ) );
46 memset( sides, 0, sizeof( sides ) );
51 for ( j = 0; j < 3; j++ )
53 sides[j][0] = pTri[( j + 1 ) % 3][0] - pTri[j][0];
54 sides[j][1] = pTri[( j + 1 ) % 3][1] - pTri[j][1];
55 sides[j][2] = pTri[( j + 1 ) % 3][2] - pTri[j][2];
57 len[j] = ( float ) sqrt( DotProduct( sides[j], sides[j] ) );
61 if ( len[0] > len[1] && len[0] > len[2] ) {
62 longestSide = 0; shortestSide = 1; origin = 2;
64 else if ( len[1] > len[0] && len[1] > len[2] ) {
65 longestSide = 1; shortestSide = 2; origin = 0;
67 else if ( len[2] > len[0] && len[2] > len[1] ) {
68 longestSide = 2; shortestSide = 0; origin = 1;
72 Error( "invalid tag triangle, must be a right triangle with unequal length sides" );
75 if ( len[0] > len[1] && len[0] > len[2] ) {
79 else if ( len[1] > len[0] && len[1] > len[2] ) {
83 else if ( len[2] > len[0] && len[2] > len[1] ) {
89 if ( len[0] > len[1] && len[0] > len[2] ) {
92 else if ( len[1] > len[0] && len[1] > len[2] ) {
95 else if ( len[2] > len[0] && len[2] > len[1] ) {
98 len[longestSide] = -1;
100 if ( len[0] > len[1] && len[0] > len[2] ) {
103 else if ( len[1] > len[0] && len[1] > len[2] ) {
106 else if ( len[2] > len[0] && len[2] > len[1] ) {
109 len[shortestSide] = -1;
113 // VectorNormalize( sides[shortestSide], axes[0] );
114 // VectorNormalize( sides[longestSide], axes[1] );
115 VectorNormalize( sides[longestSide], axes[0] );
116 VectorNormalize( sides[shortestSide], axes[1] );
118 // project shortest side so that it is exactly 90 degrees to the longer side
119 d = DotProduct( axes[0], axes[1] );
120 VectorMA( axes[0], -d, axes[1], axes[0] );
121 VectorNormalize( axes[0], axes[0] );
123 CrossProduct( sides[longestSide], sides[shortestSide], axes[2] );
124 VectorNormalize( axes[2], axes[2] );
126 pTag->origin[0] = pTri[origin][0];
127 pTag->origin[1] = pTri[origin][1];
128 pTag->origin[2] = pTri[origin][2];
130 VectorCopy( axes[0], pTag->axis[0] );
131 VectorCopy( axes[1], pTag->axis[1] );
132 VectorCopy( axes[2], pTag->axis[2] );
140 void MD3_Dump( const char *filename ){
143 md3Surface_t *pSurface;
150 if ( ( fp = fopen( filename, "rb" ) ) == 0 ) {
151 Error( "Unable to open '%s'\n", filename );
154 fileSize = filelength( fp );
155 _buffer = malloc( fileSize );
156 fread( _buffer, fileSize, 1, fp );
159 buffer = ( char * ) _buffer;
160 header = *( md3Header_t * ) _buffer;
162 if ( header.ident != MD3_IDENT ) {
163 Error( "Incorrect ident for '%s'\n", filename );
166 printf( "Contents of '%s'\n", filename );
167 printf( " version: %d\n", header.version );
168 printf( " name: %s\n", header.name );
169 printf( " num frames: %d\n", header.numFrames );
170 printf( " num tags: %d\n", header.numTags );
171 printf( " num surfaces: %d\n", header.numSurfaces );
172 printf( " num skins: %d\n", header.numSkins );
173 printf( " file size: %ld\n", fileSize );
175 printf( "--- TAGS ---\n" );
176 pTag = ( md3Tag_t * ) ( ( ( char * ) buffer ) + header.ofsTags );
177 for ( i = 0; i < header.numTags; i++, pTag++ )
179 printf( " tag %d ('%s')\n", i, pTag->name );
180 printf( " origin: %f,%f,%f\n", pTag->origin[0], pTag->origin[1], pTag->origin[2] );
181 printf( " vf: %f,%f,%f\n", pTag->axis[0][0], pTag->axis[0][1], pTag->axis[0][2] );
182 printf( " vr: %f,%f,%f\n", pTag->axis[1][0], pTag->axis[1][1], pTag->axis[1][2] );
183 printf( " vu: %f,%f,%f\n", pTag->axis[2][0], pTag->axis[2][1], pTag->axis[2][2] );
186 printf( "--- SURFACES ---\n" );
187 pSurface = ( md3Surface_t * ) ( ( ( char * ) buffer ) + header.ofsSurfaces );
189 for ( i = 0; i < header.numSurfaces; i++ )
193 md3Shader_t *pShader = ( md3Shader_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsShaders );
195 printf( "\n surface %d ('%s')\n", i, pSurface->name );
196 printf( " num frames: %d\n", pSurface->numFrames );
197 printf( " num shaders: %d\n", pSurface->numShaders );
198 printf( " num tris: %d\n", pSurface->numTriangles );
199 printf( " num verts: %d\n", pSurface->numVerts );
201 if ( pSurface->numShaders > 0 ) {
202 printf( " --- SHADERS ---\n" );
204 for ( j = 0; j < pSurface->numShaders; j++, pShader++ )
206 printf( " shader %d ('%s')\n", j, pShader->name );
209 pSurface = ( md3Surface_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsEnd );