2 Copyright (C) 1999-2007 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
22 #include "globaldefs.h"
29 #if GDEF_OS_LINUX || GDEF_OS_MACOS
30 #define filelength(f) Q_filelength(f)
32 #define filelength(f) filelength(fileno(f))
36 ** MD3_ComputeTagFromTri
38 void MD3_ComputeTagFromTri( md3Tag_t *pTag, float pTri[3][3] ){
40 vec3_t axes[3], sides[3];
41 int longestSide, shortestSide, hypotSide;
46 memset( axes, 0, sizeof( axes ) );
47 memset( sides, 0, sizeof( sides ) );
52 for ( j = 0; j < 3; j++ )
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];
58 len[j] = ( float ) sqrt( DotProduct( sides[j], sides[j] ) );
62 if ( len[0] > len[1] && len[0] > len[2] ) {
63 longestSide = 0; shortestSide = 1; origin = 2;
65 else if ( len[1] > len[0] && len[1] > len[2] ) {
66 longestSide = 1; shortestSide = 2; origin = 0;
68 else if ( len[2] > len[0] && len[2] > len[1] ) {
69 longestSide = 2; shortestSide = 0; origin = 1;
73 Error( "invalid tag triangle, must be a right triangle with unequal length sides" );
76 if ( len[0] > len[1] && len[0] > len[2] ) {
80 else if ( len[1] > len[0] && len[1] > len[2] ) {
84 else if ( len[2] > len[0] && len[2] > len[1] ) {
90 if ( len[0] > len[1] && len[0] > len[2] ) {
93 else if ( len[1] > len[0] && len[1] > len[2] ) {
96 else if ( len[2] > len[0] && len[2] > len[1] ) {
99 len[longestSide] = -1;
101 if ( len[0] > len[1] && len[0] > len[2] ) {
104 else if ( len[1] > len[0] && len[1] > len[2] ) {
107 else if ( len[2] > len[0] && len[2] > len[1] ) {
110 len[shortestSide] = -1;
114 // VectorNormalize( sides[shortestSide], axes[0] );
115 // VectorNormalize( sides[longestSide], axes[1] );
116 VectorNormalize( sides[longestSide], axes[0] );
117 VectorNormalize( sides[shortestSide], axes[1] );
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] );
124 CrossProduct( sides[longestSide], sides[shortestSide], axes[2] );
125 VectorNormalize( axes[2], axes[2] );
127 pTag->origin[0] = pTri[origin][0];
128 pTag->origin[1] = pTri[origin][1];
129 pTag->origin[2] = pTri[origin][2];
131 VectorCopy( axes[0], pTag->axis[0] );
132 VectorCopy( axes[1], pTag->axis[1] );
133 VectorCopy( axes[2], pTag->axis[2] );
141 void MD3_Dump( const char *filename ){
144 md3Surface_t *pSurface;
151 if ( ( fp = fopen( filename, "rb" ) ) == 0 ) {
152 Error( "Unable to open '%s'\n", filename );
155 fileSize = filelength( fp );
156 _buffer = malloc( fileSize );
157 fread( _buffer, fileSize, 1, fp );
160 buffer = ( char * ) _buffer;
161 header = *( md3Header_t * ) _buffer;
163 if ( header.ident != MD3_IDENT ) {
164 Error( "Incorrect ident for '%s'\n", filename );
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 );
176 printf( "--- TAGS ---\n" );
177 pTag = ( md3Tag_t * ) ( ( ( char * ) buffer ) + header.ofsTags );
178 for ( i = 0; i < header.numTags; i++, pTag++ )
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] );
187 printf( "--- SURFACES ---\n" );
188 pSurface = ( md3Surface_t * ) ( ( ( char * ) buffer ) + header.ofsSurfaces );
190 for ( i = 0; i < header.numSurfaces; i++ )
194 md3Shader_t *pShader = ( md3Shader_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsShaders );
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 );
202 if ( pSurface->numShaders > 0 ) {
203 printf( " --- SHADERS ---\n" );
205 for ( j = 0; j < pSurface->numShaders; j++, pShader++ )
207 printf( " shader %d ('%s')\n", j, pShader->name );
210 pSurface = ( md3Surface_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsEnd );