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 Q_filelength
33 ** MD3_ComputeTagFromTri
35 void MD3_ComputeTagFromTri( md3Tag_t *pTag, const float pTri[3][3] ){
37 vec3_t axes[3], sides[3];
38 int longestSide, shortestSide, hypotSide;
43 memset( axes, 0, sizeof( axes ) );
44 memset( sides, 0, sizeof( sides ) );
49 for ( j = 0; j < 3; j++ )
51 sides[j][0] = pTri[( j + 1 ) % 3][0] - pTri[j][0];
52 sides[j][1] = pTri[( j + 1 ) % 3][1] - pTri[j][1];
53 sides[j][2] = pTri[( j + 1 ) % 3][2] - pTri[j][2];
55 len[j] = ( float ) sqrt( DotProduct( sides[j], sides[j] ) );
59 if ( len[0] > len[1] && len[0] > len[2] ) {
60 longestSide = 0; shortestSide = 1; origin = 2;
62 else if ( len[1] > len[0] && len[1] > len[2] ) {
63 longestSide = 1; shortestSide = 2; origin = 0;
65 else if ( len[2] > len[0] && len[2] > len[1] ) {
66 longestSide = 2; shortestSide = 0; origin = 1;
70 Error( "invalid tag triangle, must be a right triangle with unequal length sides" );
73 if ( len[0] > len[1] && len[0] > len[2] ) {
77 else if ( len[1] > len[0] && len[1] > len[2] ) {
81 else if ( len[2] > len[0] && len[2] > len[1] ) {
87 if ( len[0] > len[1] && len[0] > len[2] ) {
90 else if ( len[1] > len[0] && len[1] > len[2] ) {
93 else if ( len[2] > len[0] && len[2] > len[1] ) {
96 len[longestSide] = -1;
98 if ( len[0] > len[1] && len[0] > len[2] ) {
101 else if ( len[1] > len[0] && len[1] > len[2] ) {
104 else if ( len[2] > len[0] && len[2] > len[1] ) {
107 len[shortestSide] = -1;
111 // VectorNormalize( sides[shortestSide], axes[0] );
112 // VectorNormalize( sides[longestSide], axes[1] );
113 VectorNormalize( sides[longestSide], axes[0] );
114 VectorNormalize( sides[shortestSide], axes[1] );
116 // project shortest side so that it is exactly 90 degrees to the longer side
117 d = DotProduct( axes[0], axes[1] );
118 VectorMA( axes[0], -d, axes[1], axes[0] );
119 VectorNormalize( axes[0], axes[0] );
121 CrossProduct( sides[longestSide], sides[shortestSide], axes[2] );
122 VectorNormalize( axes[2], axes[2] );
124 pTag->origin[0] = pTri[origin][0];
125 pTag->origin[1] = pTri[origin][1];
126 pTag->origin[2] = pTri[origin][2];
128 VectorCopy( axes[0], pTag->axis[0] );
129 VectorCopy( axes[1], pTag->axis[1] );
130 VectorCopy( axes[2], pTag->axis[2] );
138 void MD3_Dump( const char *filename ){
141 md3Surface_t *pSurface;
148 if ( ( fp = fopen( filename, "rb" ) ) == 0 ) {
149 Error( "Unable to open '%s'\n", filename );
152 fileSize = filelength( fileno( fp ) );
153 _buffer = malloc( filelength( fileno( fp ) ) );
154 fread( _buffer, fileSize, 1, fp );
157 buffer = ( char * ) _buffer;
158 header = *( md3Header_t * ) _buffer;
160 if ( header.ident != MD3_IDENT ) {
161 Error( "Incorrect ident for '%s'\n", filename );
164 printf( "Contents of '%s'\n", filename );
165 printf( " version: %d\n", header.version );
166 printf( " name: %s\n", header.name );
167 printf( " num frames: %d\n", header.numFrames );
168 printf( " num tags: %d\n", header.numTags );
169 printf( " num surfaces: %d\n", header.numSurfaces );
170 printf( " num skins: %d\n", header.numSkins );
171 printf( " file size: %d\n", fileSize );
173 printf( "--- TAGS ---\n" );
174 pTag = ( md3Tag_t * ) ( ( ( char * ) buffer ) + header.ofsTags );
175 for ( i = 0; i < header.numTags; i++, pTag++ )
177 printf( " tag %d ('%s')\n", i, pTag->name );
178 printf( " origin: %f,%f,%f\n", pTag->origin[0], pTag->origin[1], pTag->origin[2] );
179 printf( " vf: %f,%f,%f\n", pTag->axis[0][0], pTag->axis[0][1], pTag->axis[0][2] );
180 printf( " vr: %f,%f,%f\n", pTag->axis[1][0], pTag->axis[1][1], pTag->axis[1][2] );
181 printf( " vu: %f,%f,%f\n", pTag->axis[2][0], pTag->axis[2][1], pTag->axis[2][2] );
184 printf( "--- SURFACES ---\n" );
185 pSurface = ( md3Surface_t * ) ( ( ( char * ) buffer ) + header.ofsSurfaces );
187 for ( i = 0; i < header.numSurfaces; i++ )
191 md3Shader_t *pShader = ( md3Shader_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsShaders );
193 printf( "\n surface %d ('%s')\n", i, pSurface->name );
194 printf( " num frames: %d\n", pSurface->numFrames );
195 printf( " num shaders: %d\n", pSurface->numShaders );
196 printf( " num tris: %d\n", pSurface->numTriangles );
197 printf( " num verts: %d\n", pSurface->numVerts );
199 if ( pSurface->numShaders > 0 ) {
200 printf( " --- SHADERS ---\n" );
202 for ( j = 0; j < pSurface->numShaders; j++, pShader++ )
204 printf( " shader %d ('%s')\n", j, pShader->name );
207 pSurface = ( md3Surface_t * ) ( ( ( char * ) pSurface ) + pSurface->ofsEnd );