1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "3DModel.h"
#include "System/mmgr.h"
#include "3DOParser.h"
#include "S3OParser.h"
#include "Rendering/FarTextureHandler.h"
#include "Rendering/GL/myGL.h"
#include "Sim/Misc/CollisionVolume.h"
#include "System/Exceptions.h"
#include "System/Util.h"
#include <algorithm>
#include <cctype>
/** ****************************************************************************************************
* S3DModel
*/
S3DModelPiece* S3DModel::FindPiece( std::string name )
{
const ModelPieceMap::const_iterator it = pieces.find(name);
if (it != pieces.end()) return it->second;
return NULL;
}
/** ****************************************************************************************************
* S3DModelPiece
*/
void S3DModelPiece::DrawStatic() const
{
const bool needTrafo = (offset.SqLength() != 0.f);
if (needTrafo) {
glPushMatrix();
glTranslatef(offset.x, offset.y, offset.z);
}
if (!isEmpty)
glCallList(dispListID);
for (std::vector<S3DModelPiece*>::const_iterator ci = childs.begin(); ci != childs.end(); ++ci) {
(*ci)->DrawStatic();
}
if (needTrafo) {
glPopMatrix();
}
}
S3DModelPiece::~S3DModelPiece()
{
glDeleteLists(dispListID, 1);
delete colvol;
}
/** ****************************************************************************************************
* LocalModel
*/
LocalModelPiece* LocalModel::CreateLocalModelPieces(const S3DModelPiece* mpParent, size_t pieceNum)
{
LocalModelPiece* lmpParent = new LocalModelPiece(mpParent);
pieces.push_back(lmpParent);
LocalModelPiece* lmpChild = NULL;
for (unsigned int i = 0; i < mpParent->GetChildCount(); i++) {
lmpChild = CreateLocalModelPieces(mpParent->GetChild(i), ++pieceNum);
lmpChild->SetParent(lmpParent);
lmpParent->AddChild(lmpChild);
}
return lmpParent;
}
void LocalModel::SetLODCount(unsigned int count)
{
lodCount = count;
pieces[0]->SetLODCount(count);
}
void LocalModel::ApplyRawPieceTransformUnsynced(int piecenum) const
{
pieces[piecenum]->ApplyTransformUnsynced();
}
float3 LocalModel::GetRawPiecePos(int piecenum) const
{
return pieces[piecenum]->GetAbsolutePos();
}
CMatrix44f LocalModel::GetRawPieceMatrix(int piecenum) const
{
return pieces[piecenum]->GetMatrix();
}
void LocalModel::GetRawEmitDirPos(int piecenum, float3 &pos, float3 &dir) const
{
pieces[piecenum]->GetEmitDirPos(pos, dir);
}
//! Only useful for special pieces. Used for emit-sfx.
float3 LocalModel::GetRawPieceDirection(int piecenum) const
{
return pieces[piecenum]->GetDirection();
}
/** ****************************************************************************************************
* LocalModelPiece
*/
LocalModelPiece::LocalModelPiece(const S3DModelPiece* piece)
: numUpdatesSynced(1)
, lastMatrixUpdate(0)
{
assert(piece);
original = piece;
parent = NULL; // set later
dispListID = piece->dispListID;
visible = !piece->isEmpty;
identity = true;
pos = piece->offset;
colvol = new CollisionVolume(piece->GetCollisionVolume());
childs.reserve(piece->childs.size());
}
LocalModelPiece::~LocalModelPiece() {
delete colvol; colvol = NULL;
}
inline void LocalModelPiece::CheckUpdateMatrixUnsynced()
{
if (lastMatrixUpdate != numUpdatesSynced) {
lastMatrixUpdate = numUpdatesSynced;
identity = true;
transfMat.LoadIdentity();
if (pos.SqLength() > 0.0f) { transfMat.Translate(pos.x, pos.y, pos.z); identity = false; }
if (rot[1] != 0.0f) { transfMat.RotateY(-rot[1]); identity = false; }
if (rot[0] != 0.0f) { transfMat.RotateX(-rot[0]); identity = false; }
if (rot[2] != 0.0f) { transfMat.RotateZ(-rot[2]); identity = false; }
}
}
void LocalModelPiece::Draw()
{
if (!visible && childs.empty())
return;
CheckUpdateMatrixUnsynced();
if (!identity) {
glPushMatrix();
glMultMatrixf(transfMat);
}
if (visible)
glCallList(dispListID);
for (unsigned int i = 0; i < childs.size(); i++) {
childs[i]->Draw();
}
if (!identity) {
glPopMatrix();
}
}
void LocalModelPiece::DrawLOD(unsigned int lod)
{
if (!visible && childs.empty())
return;
CheckUpdateMatrixUnsynced();
if (!identity) {
glPushMatrix();
glMultMatrixf(transfMat);
}
if (visible)
glCallList(lodDispLists[lod]);
for (unsigned int i = 0; i < childs.size(); i++) {
childs[i]->DrawLOD(lod);
}
if (!identity) {
glPopMatrix();
}
}
void LocalModelPiece::ApplyTransformUnsynced()
{
if (parent) {
parent->ApplyTransformUnsynced();
}
CheckUpdateMatrixUnsynced();
if (!identity) {
glMultMatrixf(transfMat);
}
}
void LocalModelPiece::GetPiecePosIter(CMatrix44f* mat) const
{
if (parent) {
parent->GetPiecePosIter(mat);
}
/**/
if (pos.SqLength()) { mat->Translate(pos.x, pos.y, pos.z); }
if (rot[1]) { mat->RotateY(-rot[1]); }
if (rot[0]) { mat->RotateX(-rot[0]); }
if (rot[2]) { mat->RotateZ(-rot[2]); }
/**/
//(*mat) *= transMat; //! Translate & Rotate are faster than matrix-mul!
}
void LocalModelPiece::SetLODCount(unsigned int count)
{
const unsigned int oldCount = lodDispLists.size();
lodDispLists.resize(count);
for (unsigned int i = oldCount; i < count; i++) {
lodDispLists[i] = 0;
}
for (unsigned int i = 0; i < childs.size(); i++) {
childs[i]->SetLODCount(count);
}
}
#if defined(USE_GML) && defined(__GNUC__) && (__GNUC__ == 4)
//! This is supposed to fix some GCC crashbug related to threading
//! The MOVAPS SSE instruction is otherwise getting misaligned data
__attribute__ ((force_align_arg_pointer))
#endif
float3 LocalModelPiece::GetAbsolutePos() const
{
CMatrix44f mat;
GetPiecePosIter(&mat);
mat.Translate(original->GetPosOffset());
//! we use a 'right' vector, and the positive x axis points to the left
float3 pos = mat.GetPos();
pos.x = -pos.x;
return pos;
}
CMatrix44f LocalModelPiece::GetMatrix() const
{
CMatrix44f mat;
GetPiecePosIter(&mat);
return mat;
}
float3 LocalModelPiece::GetDirection() const
{
const S3DModelPiece* piece = original;
const unsigned int count = piece->GetVertexCount();
if (count < 2) {
return float3(1.0f, 1.0f, 1.0f);
}
return (piece->GetVertexPos(0) - piece->GetVertexPos(1));
}
bool LocalModelPiece::GetEmitDirPos(float3& pos, float3& dir) const
{
CMatrix44f mat;
GetPiecePosIter(&mat);
const S3DModelPiece* piece = original;
if (piece == NULL)
return false;
const unsigned int count = piece->GetVertexCount();
if (count == 0) {
pos = mat.GetPos();
dir = mat.Mul(float3(0.0f, 0.0f, 1.0f)) - pos;
} else if (count == 1) {
pos = mat.GetPos();
dir = mat.Mul(piece->GetVertexPos(0)) - pos;
} else if (count >= 2) {
float3 p1 = mat.Mul(piece->GetVertexPos(0));
float3 p2 = mat.Mul(piece->GetVertexPos(1));
pos = p1;
dir = p2 - p1;
} else {
return false;
}
//! we use a 'right' vector, and the positive x axis points to the left
pos.x = -pos.x;
dir.x = -dir.x;
return true;
}
/******************************************************************************/
/******************************************************************************/
|