Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mesh_xml.cpp File Reference
#include "mesh.h"
#include "mesh_xml.h"
#include "aux_texture.h"
#include "aux_logo.h"
#include "vegastrike.h"
#include <iostream>
#include <fstream>
#include <expat.h>
#include <float.h>
#include <assert.h>
#include "ani_texture.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <values.h>
#include "xml_support.h"
#include "vec.h"
#include "config_xml.h"
#include "vs_globals.h"
#include "cmd/script/mission.h"
#include "cmd/script/flightgroup.h"
#include "hashtable.h"

Go to the source code of this file.

Functions

static float max (float x, float y)
 
static float min (float x, float y)
 
enum BLENDFUNC parse_alpha (const char *tmp)
 
bool shouldreflect (string r)
 
void updateMax (Vector &mn, Vector &mx, const GFXVertex &ver)
 
void LaunchConverter (const char *input, const char *output, const char *args="obc")
 
bool isBFXM (VSFile &f)
 
void CopyFile (VSFile &src, VSFile &dst)
 
static void SumNormal (vector< GFXVertex > &vertices, int i1, int i2, int i3, vector< float > &weights)
 
static void SumLineNormal (vector< GFXVertex > &vertices, int i1, int i2, vector< float > &weights)
 
static void SumNormals (vector< GFXVertex > &vertices, vector< int > &indices, size_t begin, size_t end, POLYTYPE polytype, vector< float > &weights)
 
static void ClearTangents (vector< GFXVertex > &vertices)
 
static float faceTSPolarity (const Vector &T, const Vector &B, const Vector &N)
 
static float faceTSWeight (vector< GFXVertex > &vertices, int i1, int i2, int i3)
 
static void computeTangentspace (vector< GFXVertex > &vertices, int i1, int i2, int i3, Vector &T, Vector &B, Vector &N)
 
static void SumTangent (vector< GFXVertex > &vertices, int i1, int i2, int i3, vector< float > &weights)
 
static void SumTangents (vector< GFXVertex > &vertices, vector< int > &indices, size_t begin, size_t end, POLYTYPE polytype, vector< float > &weights)
 
static void NormalizeTangents (vector< GFXVertex > &vertices, vector< float > &weights)
 
static void NormalizeNormals (vector< GFXVertex > &vertices, vector< float > &weights)
 

Variables

const bool USE_RECALC_NORM = true
 
const bool FLAT_SHADE = true
 
Hashtable< std::string,
std::vector< Mesh * >
, MESH_HASTHABLE_SIZE
bfxmHashTable
 

Function Documentation

static void ClearTangents ( vector< GFXVertex > &  vertices)
static

Definition at line 1510 of file mesh_xml.cpp.

References Vector.

Referenced by Mesh::PostProcessLoading().

1511 {
1512  for (vector< GFXVertex >::iterator it = vertices.begin(); it != vertices.end(); ++it)
1513  it->SetTangent( Vector( 0, 0, 0 ), 0 );
1514 }
static void computeTangentspace ( vector< GFXVertex > &  vertices,
int  i1,
int  i2,
int  i3,
Vector T,
Vector B,
Vector N 
)
static

Definition at line 1541 of file mesh_xml.cpp.

References GFXVertex::s, GFXVertex::t, t1(), Vector, GFXVertex::x, GFXVertex::y, and GFXVertex::z.

Referenced by SumTangent().

1542 {
1543  const GFXVertex &v1 = vertices[i1];
1544  const GFXVertex &v2 = vertices[i2];
1545  const GFXVertex &v3 = vertices[i3];
1546 
1547  //compute deltas. I think that the fact we use (*2-*1) and (*3-*1) is arbitrary, but I could be wrong
1548  Vector p0( v1.x, v1.y, v1.z );
1549  Vector p1( v2.x, v2.y, v2.z );
1550  Vector p2( v3.x, v3.y, v3.z );
1551  p1 -= p0;
1552  p2 -= p0;
1553 
1554  float s1, t1;
1555  float s2, t2;
1556  s1 = v2.s-v1.s;
1557  s2 = v3.s-v1.s;
1558  t1 = v2.t-v1.t;
1559  t2 = v3.t-v1.t;
1560 
1561  //and now a myracle happens...
1562  T = t2*p1-t1*p2;
1563  B = s1*p2-s2*p1;
1564  N = p1.Cross( p2 );
1565 
1566  //normalize
1567  T.Normalize();
1568  B.Normalize();
1569  N.Normalize();
1570 }
void CopyFile ( VSFile src,
VSFile dst 
)

Definition at line 1176 of file mesh_xml.cpp.

References VSFileSystem::VSFile::Read(), sizeof(), and VSFileSystem::VSFile::Write().

1177 {
1178  size_t hm;
1179  size_t srcstruct;
1180  size_t *srcptr = &srcstruct;
1181  while ( ( hm = src.Read( srcptr, sizeof (srcstruct) ) ) )
1182  dst.Write( srcptr, hm );
1183 }
static float faceTSPolarity ( const Vector T,
const Vector B,
const Vector N 
)
static

Definition at line 1516 of file mesh_xml.cpp.

Referenced by SumTangent().

1517 {
1518  if (T.Cross( B ).Dot( N ) >= 0.f)
1519  return -1.f;
1520  else
1521  return 1.f;
1522 }
static float faceTSWeight ( vector< GFXVertex > &  vertices,
int  i1,
int  i2,
int  i3 
)
static

Definition at line 1524 of file mesh_xml.cpp.

References Vector, GFXVertex::x, GFXVertex::y, and GFXVertex::z.

Referenced by SumTangent().

1525 {
1526  const GFXVertex &vtx1 = vertices[i1];
1527  const GFXVertex &vtx2 = vertices[i2];
1528  const GFXVertex &vtx3 = vertices[i3];
1529 
1530  Vector v1( vtx2.x-vtx1.x,
1531  vtx2.y-vtx1.y,
1532  vtx2.z-vtx1.z );
1533  Vector v2( vtx3.x-vtx1.x,
1534  vtx3.y-vtx1.y,
1535  vtx3.z-vtx1.z );
1536  v1.Normalize();
1537  v2.Normalize();
1538  return 1.f-fabsf( v1.Dot( v2 ) );
1539 }
bool isBFXM ( VSFile f)

Definition at line 1165 of file mesh_xml.cpp.

References VSFileSystem::VSFile::GoTo(), and VSFileSystem::VSFile::Read().

1166 {
1167  char bfxm[4];
1168  f.Read( &bfxm[0], 1 );
1169  f.Read( &bfxm[1], 1 );
1170  f.Read( &bfxm[2], 1 );
1171  f.Read( &bfxm[3], 1 );
1172  f.GoTo( 0 );
1173  return bfxm[0] == 'B' && bfxm[1] == 'F' && bfxm[2] == 'X' && bfxm[3] == 'M';
1174 }
void LaunchConverter ( const char *  input,
const char *  output,
const char *  args = "obc" 
)

Definition at line 1128 of file mesh_xml.cpp.

References VSFileSystem::datadir, and VSFileSystem::vs_fprintf().

1129 {
1130  string intmp = string( "\"" )+input+string( "\"" );
1131  string outtmp = string( "\"" )+output+string( "\"" );
1132 #ifndef _WIN32
1133  int pid = fork();
1134  if (!pid) {
1135  string soundserver_path = VSFileSystem::datadir+"/bin/mesher";
1136  string firstarg = string( "\"" )+soundserver_path+string( "\"" );
1137  pid = execlp( soundserver_path.c_str(), soundserver_path.c_str(), input, output, args, NULL );
1138  soundserver_path = VSFileSystem::datadir+"/mesher";
1139  firstarg = string( "\"" )+soundserver_path+string( "\"" );
1140  pid = execlp( soundserver_path.c_str(), soundserver_path.c_str(), input, output, args, NULL );
1141  VSFileSystem::vs_fprintf( stderr, "Unable to spawn converter\n" );
1142  exit( -1 );
1143  } else {
1144  if (pid == -1) {
1145  VSFileSystem::vs_fprintf( stderr, "Unable to spawn converter\n" );
1146  exit( -1 );
1147  }
1148  int mystat = 0;
1149  waitpid( pid, &mystat, 0 );
1150  }
1151 #else
1152  string ss_path = VSFileSystem::datadir+"\\bin\\mesher.exe";
1153  string firstarg = string( "\"" )+ss_path+string( "\"" );
1154  int pid = spawnl( P_WAIT, ss_path.c_str(), firstarg.c_str(), intmp.c_str(), outtmp.c_str(), args, NULL );
1155  if (pid == -1) {
1156  ss_path = VSFileSystem::datadir+"\\mesher.exe";
1157  firstarg = string( "\"" )+ss_path+string( "\"" );
1158  int pid = spawnl( P_WAIT, ss_path.c_str(), firstarg.c_str(), intmp.c_str(), outtmp.c_str(), args, NULL );
1159  if (pid == -1)
1160  VSFileSystem::vs_fprintf( stderr, "Unable to spawn obj converter Error (%d)\n", pid );
1161  }
1162 #endif
1163 }
static float max ( float  x,
float  y 
)
inlinestatic

Definition at line 35 of file mesh_xml.cpp.

References x, and y.

Referenced by updateMax().

36 {
37  if (x > y) return x;
38  else return y;
39 }
static float min ( float  x,
float  y 
)
inlinestatic

Definition at line 45 of file mesh_xml.cpp.

References x, and y.

Referenced by updateMax().

46 {
47  if (x < y) return x;
48  else return y;
49 }
static void NormalizeNormals ( vector< GFXVertex > &  vertices,
vector< float > &  weights 
)
static

Definition at line 1676 of file mesh_xml.cpp.

References GFXVertex::GetNormal(), GFXVertex::i, GFXVertex::j, GFXVertex::k, UniverseUtil::sqrt(), and v.

Referenced by Mesh::PostProcessLoading().

1677 {
1678  for (size_t i = 0, n = vertices.size(); i < n; ++i) {
1679  GFXVertex &v = vertices[i];
1680  float w = weights[i];
1681 
1682  if (w > 0) {
1683  //Renormalize
1684  float mag = v.GetNormal().MagnitudeSquared();
1685  if (mag < 0.00001)
1686  mag = 1.f;
1687  else
1688  mag = 1.f/sqrt(mag);
1689  v.i *= mag;
1690  v.j *= mag;
1691  v.k *= mag;
1692  }
1693  }
1694 }
static void NormalizeTangents ( vector< GFXVertex > &  vertices,
vector< float > &  weights 
)
static

Definition at line 1654 of file mesh_xml.cpp.

References GFXVertex::tw, GFXVertex::tx, GFXVertex::ty, GFXVertex::tz, v, and Vector.

Referenced by Mesh::PostProcessLoading().

1655 {
1656  for (size_t i = 0, n = vertices.size(); i < n; ++i) {
1657  GFXVertex &v = vertices[i];
1658  float w = weights[i];
1659 
1660  if (w > 0) {
1661  //Average (shader will normalize)
1662  float iw = (w < 0.001) ? 1.f : (1.f / w);
1663  v.tx *= iw;
1664  v.ty *= iw;
1665  v.tz *= iw;
1666  v.tw *= iw;
1667  }
1668 
1669  // Don't let null vectors around (they create NaNs within shaders when normalized)
1670  // Since they happen regularly on sphere polar caps, replace them with a suitable value there (+x)
1671  if (Vector(v.tx, v.ty, v.tz).MagnitudeSquared() < 0.00001)
1672  v.tx = 0.001;
1673  }
1674 }
enum BLENDFUNC parse_alpha ( const char *  tmp)

Definition at line 146 of file mesh_xml.cpp.

References CONSTALPHA, CONSTCOLOR, DESTALPHA, DESTCOLOR, INVCONSTALPHA, INVCONSTCOLOR, INVDESTALPHA, INVDESTCOLOR, INVSRCALPHA, INVSRCCOLOR, ONE, SRCALPHA, SRCALPHASAT, SRCCOLOR, and ZERO.

Referenced by Mission::call_unit_launch(), and parse_dual_alpha().

147 {
148  if (strcmp( tmp, "ZERO" ) == 0)
149  return ZERO;
150  if (strcmp( tmp, "ONE" ) == 0)
151  return ONE;
152  if (strcmp( tmp, "SRCCOLOR" ) == 0)
153  return SRCCOLOR;
154  if (strcmp( tmp, "INVSRCCOLOR" ) == 0)
155  return INVSRCCOLOR;
156  if (strcmp( tmp, "SRCALPHA" ) == 0)
157  return SRCALPHA;
158  if (strcmp( tmp, "INVSRCALPHA" ) == 0)
159  return INVSRCALPHA;
160  if (strcmp( tmp, "DESTALPHA" ) == 0)
161  return DESTALPHA;
162  if (strcmp( tmp, "INVDESTALPHA" ) == 0)
163  return INVDESTALPHA;
164  if (strcmp( tmp, "DESTCOLOR" ) == 0)
165  return DESTCOLOR;
166  if (strcmp( tmp, "INVDESTCOLOR" ) == 0)
167  return INVDESTCOLOR;
168  if (strcmp( tmp, "SRCALPHASAT" ) == 0)
169  return SRCALPHASAT;
170  if (strcmp( tmp, "CONSTALPHA" ) == 0)
171  return CONSTALPHA;
172  if (strcmp( tmp, "INVCONSTALPHA" ) == 0)
173  return INVCONSTALPHA;
174  if (strcmp( tmp, "CONSTCOLOR" ) == 0)
175  return CONSTCOLOR;
176  if (strcmp( tmp, "INVCONSTCOLOR" ) == 0)
177  return INVCONSTCOLOR;
178  return ZERO;
179 }
bool shouldreflect ( string  r)

Definition at line 246 of file mesh_xml.cpp.

References i, int, and strtoupper().

247 {
248  if (strtoupper( r ) == "FALSE")
249  return false;
250  int i;
251  for (i = 0; i < (int) r.length(); ++i)
252  if (r[i] != '0' && r[i] != '.' && r[i] != '+' && r[i] != 'e')
253  return true;
254  return false;
255 }
static void SumLineNormal ( vector< GFXVertex > &  vertices,
int  i1,
int  i2,
vector< float > &  weights 
)
static

Definition at line 1428 of file mesh_xml.cpp.

References Vector, x, y, and z.

Referenced by SumNormals().

1429 {
1430  Vector v1( vertices[i2].x-vertices[i1].x,
1431  vertices[i2].y-vertices[i1].y,
1432  vertices[i2].z-vertices[i1].z );
1433  static Vector v2( 0.3408, 0.9401, 0.0005 );
1434  v1.Normalize();
1435  Vector N = v1.Cross( v2 );
1436 
1437  vertices[i1].i += N.i;
1438  vertices[i1].j += N.j;
1439  vertices[i1].k += N.k;
1440  weights[i1] += 1;
1441 
1442  vertices[i2].i += N.i;
1443  vertices[i2].j += N.j;
1444  vertices[i2].k += N.k;
1445  weights[i2] += 1;
1446 }
static void SumNormal ( vector< GFXVertex > &  vertices,
int  i1,
int  i2,
int  i3,
vector< float > &  weights 
)
static

Definition at line 1398 of file mesh_xml.cpp.

References Vector, x, y, and z.

Referenced by SumNormals().

1399 {
1400  Vector v1( vertices[i2].x-vertices[i1].x,
1401  vertices[i2].y-vertices[i1].y,
1402  vertices[i2].z-vertices[i1].z );
1403  Vector v2( vertices[i3].x-vertices[i1].x,
1404  vertices[i3].y-vertices[i1].y,
1405  vertices[i3].z-vertices[i1].z );
1406  v1.Normalize();
1407  v2.Normalize();
1408  Vector N = v1.Cross( v2 );
1409  float w = 1.f-0.9*fabsf( v1.Dot( v2 ) );
1410  N *= w;
1411 
1412  vertices[i1].i += N.i;
1413  vertices[i1].j += N.j;
1414  vertices[i1].k += N.k;
1415  weights[i1] += w;
1416 
1417  vertices[i2].i += N.i;
1418  vertices[i2].j += N.j;
1419  vertices[i2].k += N.k;
1420  weights[i2] += w;
1421 
1422  vertices[i3].i += N.i;
1423  vertices[i3].j += N.j;
1424  vertices[i3].k += N.k;
1425  weights[i3] += w;
1426 }
static void SumNormals ( vector< GFXVertex > &  vertices,
vector< int > &  indices,
size_t  begin,
size_t  end,
POLYTYPE  polytype,
vector< float > &  weights 
)
static

Definition at line 1448 of file mesh_xml.cpp.

References GFXLINE, GFXLINESTRIP, GFXPOINT, GFXPOLY, GFXQUAD, GFXQUADSTRIP, GFXTRI, GFXTRIFAN, GFXTRISTRIP, SumLineNormal(), and SumNormal().

Referenced by Mesh::PostProcessLoading().

1454 {
1455  int flip = 0;
1456  size_t i;
1457  switch (polytype)
1458  {
1459  case GFXTRI:
1460  if (end <= 2)
1461  break;
1462  end -= 2;
1463  for (; begin < end; begin += 3)
1464  SumNormal( vertices, indices[begin], indices[begin+1], indices[begin+2], weights );
1465  break;
1466  case GFXQUAD:
1467  if (end <= 3)
1468  break;
1469  end -= 3;
1470  for (; begin < end; begin += 4) {
1471  SumNormal( vertices, indices[begin], indices[begin+1], indices[begin+2], weights );
1472  SumNormal( vertices, indices[begin], indices[begin+2], indices[begin+3], weights );
1473  }
1474  break;
1475  case GFXTRISTRIP:
1476  case GFXQUADSTRIP:
1477  if (end <= 2)
1478  break;
1479  end -= 2;
1480  for (; begin < end; ++begin, flip ^= 1)
1481  SumNormal( vertices, indices[begin], indices[begin+1+flip], indices[begin+2-flip], weights );
1482  break;
1483  case GFXTRIFAN:
1484  if (end <= 2)
1485  break;
1486  end -= 2;
1487  for (i = begin; begin < end; ++begin)
1488  SumNormal( vertices, indices[i], indices[begin+1], indices[begin+2], weights );
1489  break;
1490  case GFXLINESTRIP:
1491  if (end <= 1)
1492  break;
1493  end -= 1;
1494  for (i = begin; begin < end; ++begin)
1495  SumLineNormal( vertices, indices[begin], indices[begin+1], weights );
1496  break;
1497  case GFXLINE:
1498  if (end <= 1)
1499  break;
1500  end -= 1;
1501  for (i = begin; begin < end; begin += 2)
1502  SumLineNormal( vertices, indices[begin], indices[begin+1], weights );
1503  break;
1504  case GFXPOLY:
1505  case GFXPOINT:
1506  break;
1507  }
1508 }
static void SumTangent ( vector< GFXVertex > &  vertices,
int  i1,
int  i2,
int  i3,
vector< float > &  weights 
)
static

Definition at line 1572 of file mesh_xml.cpp.

References computeTangentspace(), faceTSPolarity(), faceTSWeight(), GFXVertex::tw, GFXVertex::tx, GFXVertex::ty, GFXVertex::tz, and Vector.

Referenced by SumTangents().

1573 {
1574  float w = faceTSWeight( vertices, i1, i2, i3 );
1575  Vector T, B, N;
1576  computeTangentspace( vertices, i1, i2, i3, T, B, N );
1577 
1578  float p = faceTSPolarity( T, B, N )*w;
1579  T *= w;
1580 
1581  GFXVertex &v1 = vertices[i1];
1582  GFXVertex &v2 = vertices[i2];
1583  GFXVertex &v3 = vertices[i3];
1584 
1585  v1.tx += T.x;
1586  v1.ty += T.y;
1587  v1.tz += T.z;
1588  v1.tw += p;
1589  weights[i1] += w;
1590 
1591  v2.tx += T.x;
1592  v2.ty += T.y;
1593  v2.tz += T.z;
1594  v2.tw += p;
1595  weights[i2] += w;
1596 
1597  v3.tx += T.x;
1598  v3.ty += T.y;
1599  v3.tz += T.z;
1600  v3.tw += p;
1601  weights[i3] += w;
1602 }
static void SumTangents ( vector< GFXVertex > &  vertices,
vector< int > &  indices,
size_t  begin,
size_t  end,
POLYTYPE  polytype,
vector< float > &  weights 
)
static

Definition at line 1604 of file mesh_xml.cpp.

References GFXLINE, GFXLINESTRIP, GFXPOINT, GFXPOLY, GFXQUAD, GFXQUADSTRIP, GFXTRI, GFXTRIFAN, GFXTRISTRIP, and SumTangent().

Referenced by Mesh::PostProcessLoading().

1610 {
1611  int flip = 0;
1612  size_t i;
1613  switch (polytype)
1614  {
1615  case GFXTRI:
1616  if (end <= 2)
1617  break;
1618  end -= 2;
1619  for (; begin < end; begin += 3)
1620  SumTangent( vertices, indices[begin], indices[begin+1], indices[begin+2], weights );
1621  break;
1622  case GFXQUAD:
1623  if (end <= 3)
1624  break;
1625  end -= 3;
1626  for (; begin < end; begin += 4) {
1627  SumTangent( vertices, indices[begin], indices[begin+1], indices[begin+2], weights );
1628  SumTangent( vertices, indices[begin], indices[begin+2], indices[begin+3], weights );
1629  }
1630  break;
1631  case GFXTRISTRIP:
1632  case GFXQUADSTRIP:
1633  if (end <= 2)
1634  break;
1635  end -= 2;
1636  for (; begin < end; ++begin, flip ^= 1)
1637  SumTangent( vertices, indices[begin], indices[begin+1+flip], indices[begin+2-flip], weights );
1638  break;
1639  case GFXTRIFAN:
1640  if (end <= 2)
1641  break;
1642  end -= 2;
1643  for (i = begin; begin < end; ++begin)
1644  SumTangent( vertices, indices[i], indices[begin+1], indices[begin+2], weights );
1645  break;
1646  case GFXLINE:
1647  case GFXLINESTRIP:
1648  case GFXPOLY:
1649  case GFXPOINT:
1650  break;
1651  }
1652 }
void updateMax ( Vector mn,
Vector mx,
const GFXVertex ver 
)

Definition at line 1116 of file mesh_xml.cpp.

References max(), min(), GFXVertex::x, GFXVertex::y, and GFXVertex::z.

Referenced by Mesh::Fork(), and Mesh::PostProcessLoading().

1117 {
1118  mn.i = min( ver.x, mn.i );
1119  mx.i = max( ver.x, mx.i );
1120  mn.j = min( ver.y, mn.j );
1121  mx.j = max( ver.y, mx.j );
1122  mn.k = min( ver.z, mn.k );
1123  mx.k = max( ver.z, mx.k );
1124 }

Variable Documentation

Hashtable< std::string, std::vector< Mesh* >, MESH_HASTHABLE_SIZE > bfxmHashTable

Definition at line 1256 of file mesh_xml.cpp.

Referenced by Mesh::LoadMeshes().

const bool FLAT_SHADE = true

Definition at line 1237 of file mesh_xml.cpp.

Referenced by Mesh::PostProcessLoading().

const bool USE_RECALC_NORM = true

Definition at line 1236 of file mesh_xml.cpp.