leocad/common/piece.cpp

663 lines
16 KiB
C++
Raw Normal View History

#include "lc_global.h"
2012-04-14 01:41:58 +02:00
#include "lc_mesh.h"
#include "lc_colors.h"
2011-09-07 23:06:51 +02:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "opengl.h"
#include "pieceinf.h"
#include "piece.h"
#include "group.h"
#include "project.h"
#include "lc_application.h"
2013-08-09 06:57:18 +02:00
#include "lc_library.h"
2011-09-07 23:06:51 +02:00
2014-08-30 21:48:36 +02:00
#define LC_PIECE_SAVE_VERSION 12 // LeoCAD 0.80
2011-09-07 23:06:51 +02:00
/////////////////////////////////////////////////////////////////////////////
// Piece construction/destruction
2014-05-01 20:42:11 +02:00
lcPiece::lcPiece(PieceInfo* pPieceInfo)
2014-08-07 17:22:33 +02:00
: lcObject (LC_OBJECT_PIECE)
2011-09-07 23:06:51 +02:00
{
mPieceInfo = pPieceInfo;
mState = 0;
mColorIndex = gDefaultColor;
mColorCode = 16;
2014-07-06 08:04:09 +02:00
mStepShow = 1;
mStepHide = LC_STEP_MAX;
2011-09-07 23:06:51 +02:00
memset(m_strName, 0, sizeof(m_strName));
2014-05-25 20:23:09 +02:00
mGroup = NULL;
2011-09-07 23:06:51 +02:00
if (mPieceInfo != NULL)
mPieceInfo->AddRef();
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, lcVector3(0.0f, 0.0f, 0.0f), 1, true);
ChangeKey(mRotationKeys, lcVector4(0.0f, 0.0f, 1.0f, 0.0f), 1, true);
2011-09-07 23:06:51 +02:00
}
2014-05-01 20:42:11 +02:00
lcPiece::~lcPiece()
2011-09-07 23:06:51 +02:00
{
if (mPieceInfo != NULL)
2012-10-12 01:55:55 +02:00
mPieceInfo->Release();
2011-09-07 23:06:51 +02:00
}
void lcPiece::SaveLDraw(lcFile& File) const
{
char Line[1024];
if (mStepHide != LC_STEP_MAX)
{
sprintf(Line, "0 !LEOCAD PIECE STEP_HIDE %d\r\n", mStepHide);
File.WriteBuffer(Line, strlen(Line));
}
sprintf(Line, "0 !LEOCAD PIECE NAME %s\r\n", m_strName);
File.WriteBuffer(Line, strlen(Line));
if (IsHidden())
{
strcpy(Line, "0 !LEOCAD PIECE HIDDEN\r\n");
File.WriteBuffer(Line, strlen(Line));
}
if (mGroup)
{
sprintf(Line, "0 !LEOCAD PIECE GROUP %s\r\n", mGroup->m_strName);
File.WriteBuffer(Line, strlen(Line));
}
if (mPositionKeys.GetSize() > 1)
SaveKeysLDraw(mPositionKeys, "PIECE POSITION_KEY", File);
if (mRotationKeys.GetSize() > 1)
SaveKeysLDraw(mRotationKeys, "PIECE ROTATION_KEY", File);
const float* Matrix = mModelWorld;
float Numbers[12] = { Matrix[12], -Matrix[14], Matrix[13], Matrix[0], -Matrix[8], Matrix[4], -Matrix[2], Matrix[10], -Matrix[6], Matrix[1], -Matrix[9], Matrix[5] };
char Strings[12][32];
for (int NumberIdx = 0; NumberIdx < 12; NumberIdx++)
{
sprintf(Strings[NumberIdx], "%f", Numbers[NumberIdx]);
char* Dot = strchr(Strings[NumberIdx], '.');
if (Dot)
{
char* Last;
for (Last = Dot + strlen(Dot) - 1; *Last == '0'; Last--)
*Last = 0;
if (Last == Dot)
*Dot = 0;
}
}
sprintf(Line, "1 %d %s %s %s %s %s %s %s %s %s %s %s %s %s.DAT\r\n",
mColorCode, Strings[0], Strings[1], Strings[2], Strings[3], Strings[4], Strings[5], Strings[6], Strings[7], Strings[8], Strings[9], Strings[10], Strings[11], mPieceInfo->m_strName);
File.WriteBuffer(Line, strlen(Line));
}
2014-09-03 16:34:53 +02:00
bool lcPiece::ParseLDrawLine(QTextStream& Stream, lcModel* Model)
{
2014-09-03 16:34:53 +02:00
QString Token;
Stream >> Token;
2014-09-04 16:27:37 +02:00
if (Token == QLatin1String("STEP_HIDE"))
2014-09-03 16:34:53 +02:00
Stream >> mStepHide;
2014-09-04 16:27:37 +02:00
else if (Token == QLatin1String("NAME"))
{
2014-09-03 16:34:53 +02:00
QString Name = Stream.readAll().trimmed();
QByteArray NameUtf = Name.toUtf8(); // todo: replace with qstring
strncpy(m_strName, NameUtf.constData(), sizeof(m_strName));
m_strName[sizeof(m_strName) - 1] = 0;
}
2014-09-04 16:27:37 +02:00
else if (Token == QLatin1String("HIDDEN"))
SetHidden(true);
2014-09-04 16:27:37 +02:00
else if (Token == QLatin1String("GROUP"))
2014-09-03 16:34:53 +02:00
{
QString Name = Stream.readAll().trimmed();
QByteArray NameUtf = Name.toUtf8(); // todo: replace with qstring
mGroup = Model->GetGroup(NameUtf.constData(), true);
}
2014-09-04 16:27:37 +02:00
else if (Token == QLatin1String("POSITION_KEY"))
2014-09-03 16:34:53 +02:00
LoadKeyLDraw(mPositionKeys, Stream);
2014-09-04 16:27:37 +02:00
else if (Token == QLatin1String("ROTATION_KEY"))
2014-09-03 16:34:53 +02:00
LoadKeyLDraw(mRotationKeys, Stream);
return false;
}
2011-09-07 23:06:51 +02:00
/////////////////////////////////////////////////////////////////////////////
// Piece save/load
// Use only when loading from a file
2014-05-01 20:42:11 +02:00
void lcPiece::SetPieceInfo(PieceInfo* pPieceInfo)
2011-09-07 23:06:51 +02:00
{
mPieceInfo = pPieceInfo;
mPieceInfo->AddRef();
2011-09-07 23:06:51 +02:00
}
2014-05-01 20:42:11 +02:00
bool lcPiece::FileLoad(lcFile& file)
2011-09-07 23:06:51 +02:00
{
2014-08-31 02:53:12 +02:00
lcuint8 version, ch;
version = file.ReadU8();
if (version > LC_PIECE_SAVE_VERSION)
return false;
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
if (version > 8)
{
if (file.ReadU8() != 1)
return false;
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
lcuint16 time;
float param[4];
lcuint8 type;
lcuint32 n;
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
file.ReadU32(&n, 1);
while (n--)
{
file.ReadU16(&time, 1);
file.ReadFloats(param, 4);
file.ReadU8(&type, 1);
if (type == 0)
ChangeKey(mPositionKeys, lcVector3(param[0], param[1], param[2]), time, true);
else if (type == 1)
ChangeKey(mRotationKeys, lcVector4(param[0], param[1], param[2], param[3]), time, true);
}
file.ReadU32(&n, 1);
while (n--)
{
file.ReadU16(&time, 1);
file.ReadFloats(param, 4);
file.ReadU8(&type, 1);
}
}
2011-09-07 23:06:51 +02:00
if (version < 9)
{
lcuint16 time;
2012-03-23 00:44:56 +01:00
lcuint8 type;
2011-09-07 23:06:51 +02:00
if (version > 5)
{
lcuint32 keys;
2012-10-18 20:57:21 +02:00
float param[4];
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
file.ReadU32(&keys, 1);
2011-09-07 23:06:51 +02:00
while (keys--)
{
2012-03-23 00:44:56 +01:00
file.ReadFloats(param, 4);
file.ReadU16(&time, 1);
file.ReadU8(&type, 1);
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
if (type == 0)
ChangeKey(mPositionKeys, lcVector3(param[0], param[1], param[2]), time, true);
else if (type == 1)
ChangeKey(mRotationKeys, lcVector4(param[0], param[1], param[2], param[3]), time, true);
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
file.ReadU32(&keys, 1);
2011-09-07 23:06:51 +02:00
while (keys--)
{
2012-03-23 00:44:56 +01:00
file.ReadFloats(param, 4);
file.ReadU16(&time, 1);
file.ReadU8(&type, 1);
2011-09-07 23:06:51 +02:00
}
}
else
{
if (version > 2)
{
2012-03-23 00:44:56 +01:00
file.ReadU8(&ch, 1);
2011-09-07 23:06:51 +02:00
while (ch--)
{
2012-10-18 20:57:21 +02:00
lcMatrix44 ModelWorld;
if (version > 3)
{
file.ReadFloats(ModelWorld, 16);
}
else
{
lcVector3 Translation;
float Rotation[3];
file.ReadFloats(Translation, 3);
file.ReadFloats(Rotation, 3);
ModelWorld = lcMatrix44Translation(Translation);
ModelWorld = lcMul(lcMatrix44RotationZ(Rotation[2] * LC_DTOR), lcMul(lcMatrix44RotationY(Rotation[1] * LC_DTOR), lcMul(lcMatrix44RotationX(Rotation[0] * LC_DTOR), ModelWorld)));
}
lcuint8 b;
file.ReadU8(&b, 1);
time = b;
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, lcVector3(ModelWorld.r[3][0], ModelWorld.r[3][1], ModelWorld.r[3][2]), 1, true);
2012-10-18 20:57:21 +02:00
lcVector4 AxisAngle = lcMatrix44ToAxisAngle(ModelWorld);
AxisAngle[3] *= LC_RTOD;
2014-08-31 02:53:12 +02:00
ChangeKey(mRotationKeys, AxisAngle, time, true);
2012-10-18 20:57:21 +02:00
lcint32 bl;
file.ReadS32(&bl, 1);
2011-09-07 23:06:51 +02:00
}
}
else
{
2012-10-18 20:57:21 +02:00
lcVector3 Translation;
float Rotation[3];
file.ReadFloats(Translation, 3);
file.ReadFloats(Rotation, 3);
lcMatrix44 ModelWorld = lcMatrix44Translation(Translation);
ModelWorld = lcMul(lcMatrix44RotationZ(Rotation[2] * LC_DTOR), lcMul(lcMatrix44RotationY(Rotation[1] * LC_DTOR), lcMul(lcMatrix44RotationX(Rotation[0] * LC_DTOR), ModelWorld)));
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, lcVector3(ModelWorld.r[3][0], ModelWorld.r[3][1], ModelWorld.r[3][2]), 1, true);
2012-10-18 20:57:21 +02:00
lcVector4 AxisAngle = lcMatrix44ToAxisAngle(ModelWorld);
AxisAngle[3] *= LC_RTOD;
2014-08-31 02:53:12 +02:00
ChangeKey(mRotationKeys, AxisAngle, 1, true);
2014-01-30 04:13:34 +01:00
}
2011-09-07 23:06:51 +02:00
}
}
// Common to all versions.
2013-08-09 06:57:18 +02:00
char name[LC_PIECE_NAME_LEN];
2011-09-07 23:06:51 +02:00
if (version < 10)
{
memset(name, 0, LC_PIECE_NAME_LEN);
2012-03-23 00:44:56 +01:00
file.ReadBuffer(name, 9);
2011-09-07 23:06:51 +02:00
}
else
2012-03-23 00:44:56 +01:00
file.ReadBuffer(name, LC_PIECE_NAME_LEN);
2011-09-07 23:06:51 +02:00
2013-08-09 06:57:18 +02:00
PieceInfo* pInfo = lcGetPiecesLibrary()->FindPiece(name, true);
SetPieceInfo(pInfo);
2012-04-21 03:30:02 +02:00
// 11 (0.77)
2012-03-28 03:07:18 +02:00
if (version < 11)
{
lcuint8 Color;
file.ReadU8(&Color, 1);
if (version < 5)
2012-04-21 03:30:02 +02:00
mColorCode = lcGetColorCodeFromOriginalColor(Color);
else
mColorCode = lcGetColorCodeFromExtendedColor(Color);
2012-03-28 03:07:18 +02:00
}
else
file.ReadU32(&mColorCode, 1);
mColorIndex = lcGetColorIndex(mColorCode);
2011-09-07 23:06:51 +02:00
2014-07-06 08:04:09 +02:00
lcuint8 Step;
file.ReadU8(&Step, 1);
mStepShow = Step;
2011-09-07 23:06:51 +02:00
if (version > 1)
2014-07-06 08:04:09 +02:00
{
file.ReadU8(&Step, 1);
mStepHide = Step == 255 ? LC_STEP_MAX : Step;
}
2011-09-07 23:06:51 +02:00
else
2014-07-06 08:04:09 +02:00
mStepHide = LC_STEP_MAX;
2011-09-07 23:06:51 +02:00
if (version > 5)
{
2014-01-30 04:13:34 +01:00
file.ReadU16(); // m_nFrameShow
file.ReadU16(); // m_nFrameHide
2011-09-07 23:06:51 +02:00
if (version > 7)
{
lcuint8 Hidden;
file.ReadU8(&Hidden, 1);
if (Hidden & 1)
mState |= LC_PIECE_HIDDEN;
2012-03-23 00:44:56 +01:00
file.ReadU8(&ch, 1);
file.ReadBuffer(m_strName, ch);
2011-09-07 23:06:51 +02:00
}
else
{
2012-03-23 00:44:56 +01:00
lcint32 hide;
file.ReadS32(&hide, 1);
2011-09-07 23:06:51 +02:00
if (hide != 0)
mState |= LC_PIECE_HIDDEN;
2012-03-23 00:44:56 +01:00
file.ReadBuffer(m_strName, 81);
2011-09-07 23:06:51 +02:00
}
// 7 (0.64)
2012-03-23 00:44:56 +01:00
lcint32 i = -1;
2011-09-07 23:06:51 +02:00
if (version > 6)
2012-03-23 00:44:56 +01:00
file.ReadS32(&i, 1);
2014-08-07 17:22:33 +02:00
mGroup = (lcGroup*)(long)i;
2011-09-07 23:06:51 +02:00
}
else
{
2012-03-23 00:44:56 +01:00
file.ReadU8(&ch, 1);
2011-09-07 23:06:51 +02:00
if (ch == 0)
2014-08-07 17:22:33 +02:00
mGroup = (lcGroup*)-1;
2011-09-07 23:06:51 +02:00
else
2014-08-07 17:22:33 +02:00
mGroup = (lcGroup*)(long)ch;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
file.ReadU8(&ch, 1);
2011-09-07 23:06:51 +02:00
if (ch & 0x01)
mState |= LC_PIECE_HIDDEN;
2011-09-07 23:06:51 +02:00
}
2014-08-30 21:48:36 +02:00
if (version < 12)
{
2014-08-31 02:53:12 +02:00
for (int KeyIdx = 0; KeyIdx < mPositionKeys.GetSize(); KeyIdx++)
mPositionKeys[KeyIdx].Value *= 25.0f;
2014-08-30 21:48:36 +02:00
}
return true;
2011-09-07 23:06:51 +02:00
}
2014-05-01 20:42:11 +02:00
void lcPiece::FileSave(lcFile& file) const
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
file.WriteU8(LC_PIECE_SAVE_VERSION);
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
file.WriteU8(1);
file.WriteU32(mPositionKeys.GetSize() + mRotationKeys.GetSize());
for (int KeyIdx = 0; KeyIdx < mPositionKeys.GetSize(); KeyIdx++)
{
lcObjectKey<lcVector3>& Key = mPositionKeys[KeyIdx];
lcuint16 Step = lcMin(Key.Step, 0xFFFFU);
file.WriteU16(Step);
file.WriteFloats(Key.Value, 3);
file.WriteFloat(0);
file.WriteU8(0);
}
for (int KeyIdx = 0; KeyIdx < mRotationKeys.GetSize(); KeyIdx++)
{
lcObjectKey<lcVector4>& Key = mRotationKeys[KeyIdx];
lcuint16 Step = lcMin(Key.Step, 0xFFFFU);
file.WriteU16(Step);
file.WriteFloats(Key.Value, 4);
file.WriteU8(1);
}
file.WriteU32(0);
2011-09-07 23:06:51 +02:00
file.WriteBuffer(mPieceInfo->m_strName, LC_PIECE_NAME_LEN);
2012-03-28 03:07:18 +02:00
file.WriteU32(mColorCode);
2014-07-06 08:04:09 +02:00
file.WriteU8(lcMin(mStepShow, 254U));
file.WriteU8(lcMin(mStepHide, 255U));
2014-01-30 04:13:34 +01:00
file.WriteU16(1); // m_nFrameShow
file.WriteU16(100); // m_nFrameHide
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
// version 8
file.WriteU8(mState & LC_PIECE_HIDDEN ? 1 : 0);
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
lcuint8 Length = strlen(m_strName);
file.WriteU8(Length);
file.WriteBuffer(m_strName, Length);
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
// version 7
2014-05-25 20:23:09 +02:00
lcint32 GroupIndex = lcGetActiveProject()->GetGroupIndex(mGroup);
2014-05-25 03:45:19 +02:00
file.WriteS32(GroupIndex);
2011-09-07 23:06:51 +02:00
}
2014-08-31 02:53:12 +02:00
void lcPiece::Initialize(const lcVector3& Position, const lcVector4& AxisAngle, lcStep Step)
2011-09-07 23:06:51 +02:00
{
2014-07-06 08:04:09 +02:00
mStepShow = Step;
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, Position, 1, true);
ChangeKey(mRotationKeys, AxisAngle, 1, true);
2011-09-07 23:06:51 +02:00
2014-01-30 04:13:34 +01:00
UpdatePosition(1);
2011-09-07 23:06:51 +02:00
}
2014-08-07 17:22:33 +02:00
void lcPiece::CreateName(const lcArray<lcPiece*>& Pieces)
2011-09-07 23:06:51 +02:00
{
if (m_strName[0])
{
bool Found = false;
for (int PieceIdx = 0; PieceIdx < Pieces.GetSize(); PieceIdx++)
{
if (!strcmp(Pieces[PieceIdx]->m_strName, m_strName))
{
Found = true;
break;
}
}
if (!Found)
return;
}
const char* Prefix = mPieceInfo->m_strDescription;
int Length = strlen(Prefix);
2011-09-07 23:06:51 +02:00
int i, max = 0;
for (int PieceIdx = 0; PieceIdx < Pieces.GetSize(); PieceIdx++)
{
2014-08-07 17:22:33 +02:00
lcPiece* Piece = Pieces[PieceIdx];
if (strncmp(Piece->m_strName, Prefix, Length) == 0)
if (sscanf(Piece->m_strName + Length, " #%d", &i) == 1)
2013-01-06 20:24:25 +01:00
if (i > max)
2011-09-07 23:06:51 +02:00
max = i;
}
2011-09-07 23:06:51 +02:00
snprintf(m_strName, sizeof(m_strName), "%s #%.2d", Prefix, max + 1);
m_strName[sizeof(m_strName) - 1] = 0;
2011-09-07 23:06:51 +02:00
}
2014-07-06 08:04:09 +02:00
void lcPiece::InsertTime(lcStep Start, lcStep Time)
2011-09-07 23:06:51 +02:00
{
2014-07-06 08:04:09 +02:00
if (mStepShow >= Start)
{
if (mStepShow < LC_STEP_MAX - Time)
mStepShow += Time;
else
mStepShow = LC_STEP_MAX;
}
2011-09-07 23:06:51 +02:00
2014-07-06 08:04:09 +02:00
if (mStepHide >= Start)
{
if (mStepHide < LC_STEP_MAX - Time)
mStepHide += Time;
else
mStepHide = LC_STEP_MAX;
}
2011-09-07 23:06:51 +02:00
2014-07-06 08:04:09 +02:00
if (mStepShow >= mStepHide)
{
if (mStepShow != LC_STEP_MAX)
mStepHide = mStepShow + 1;
else
{
mStepShow = LC_STEP_MAX - 1;
mStepHide = LC_STEP_MAX;
}
}
2014-08-31 02:53:12 +02:00
lcObject::InsertTime(mPositionKeys, Start, Time);
lcObject::InsertTime(mRotationKeys, Start, Time);
2011-09-07 23:06:51 +02:00
}
2014-07-06 08:04:09 +02:00
void lcPiece::RemoveTime(lcStep Start, lcStep Time)
2011-09-07 23:06:51 +02:00
{
2014-07-06 08:04:09 +02:00
if (mStepShow >= Start)
{
if (mStepShow > Time)
mStepShow -= Time;
else
mStepShow = 1;
}
2011-09-07 23:06:51 +02:00
2014-07-06 08:04:09 +02:00
if (mStepHide != LC_STEP_MAX)
{
if (mStepHide > Time)
mStepHide -= Time;
else
mStepHide = 1;
}
if (mStepShow >= mStepHide)
{
if (mStepShow != LC_STEP_MAX)
mStepHide = mStepShow + 1;
else
{
mStepShow = LC_STEP_MAX - 1;
mStepHide = LC_STEP_MAX;
}
}
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
lcObject::RemoveTime(mPositionKeys, Start, Time);
lcObject::RemoveTime(mRotationKeys, Start, Time);
2011-09-07 23:06:51 +02:00
}
2014-05-01 20:42:11 +02:00
void lcPiece::RayTest(lcObjectRayTest& ObjectRayTest) const
2011-09-07 23:06:51 +02:00
{
2012-08-17 01:50:40 +02:00
lcVector3 Min(mPieceInfo->m_fDimensions[3], mPieceInfo->m_fDimensions[4], mPieceInfo->m_fDimensions[5]);
lcVector3 Max(mPieceInfo->m_fDimensions[0], mPieceInfo->m_fDimensions[1], mPieceInfo->m_fDimensions[2]);
lcMatrix44 WorldModel = lcMatrix44AffineInverse(mModelWorld);
lcVector3 Start = lcMul31(ObjectRayTest.Start, WorldModel);
lcVector3 End = lcMul31(ObjectRayTest.End, WorldModel);
float Distance;
if (!lcBoundingBoxRayMinIntersectDistance(Min, Max, Start, End, &Distance, NULL) || (Distance >= ObjectRayTest.Distance))
2012-08-17 01:50:40 +02:00
return;
2011-09-07 23:06:51 +02:00
2012-06-16 02:17:52 +02:00
lcVector3 Intersection;
2011-09-07 23:06:51 +02:00
if (mPieceInfo->mMesh->MinIntersectDist(Start, End, ObjectRayTest.Distance, Intersection))
{
2014-08-07 17:22:33 +02:00
ObjectRayTest.ObjectSection.Object = const_cast<lcPiece*>(this);
ObjectRayTest.ObjectSection.Section = 0;
}
2011-09-07 23:06:51 +02:00
}
2014-05-01 20:42:11 +02:00
void lcPiece::BoxTest(lcObjectBoxTest& ObjectBoxTest) const
2011-09-07 23:06:51 +02:00
{
lcVector3 Box[8] =
2011-09-07 23:06:51 +02:00
{
lcVector3(mPieceInfo->m_fDimensions[0], mPieceInfo->m_fDimensions[1], mPieceInfo->m_fDimensions[5]),
lcVector3(mPieceInfo->m_fDimensions[3], mPieceInfo->m_fDimensions[1], mPieceInfo->m_fDimensions[5]),
lcVector3(mPieceInfo->m_fDimensions[0], mPieceInfo->m_fDimensions[1], mPieceInfo->m_fDimensions[2]),
lcVector3(mPieceInfo->m_fDimensions[3], mPieceInfo->m_fDimensions[4], mPieceInfo->m_fDimensions[5]),
lcVector3(mPieceInfo->m_fDimensions[3], mPieceInfo->m_fDimensions[4], mPieceInfo->m_fDimensions[2]),
lcVector3(mPieceInfo->m_fDimensions[0], mPieceInfo->m_fDimensions[4], mPieceInfo->m_fDimensions[2]),
lcVector3(mPieceInfo->m_fDimensions[0], mPieceInfo->m_fDimensions[4], mPieceInfo->m_fDimensions[5]),
lcVector3(mPieceInfo->m_fDimensions[3], mPieceInfo->m_fDimensions[1], mPieceInfo->m_fDimensions[2])
2011-09-07 23:06:51 +02:00
};
lcMatrix44 WorldToLocal = lcMatrix44FromAxisAngle(lcVector3(mRotation[0], mRotation[1], mRotation[2]), -mRotation[3] * LC_DTOR);
WorldToLocal.SetTranslation(lcMul31(lcVector3(-mPosition[0], -mPosition[1], -mPosition[2]), WorldToLocal));
2011-09-07 23:06:51 +02:00
const int NumPlanes = 6;
lcVector4 LocalPlanes[NumPlanes];
2011-09-07 23:06:51 +02:00
int i;
for (i = 0; i < NumPlanes; i++)
{
lcVector3 PlaneNormal = lcMul30(ObjectBoxTest.Planes[i], WorldToLocal);
LocalPlanes[i] = lcVector4(PlaneNormal, ObjectBoxTest.Planes[i][3] - lcDot3(WorldToLocal[3], PlaneNormal));
2011-09-07 23:06:51 +02:00
}
int Outcodes[8];
for (i = 0; i < 8; i++)
{
Outcodes[i] = 0;
for (int j = 0; j < NumPlanes; j++)
{
if (lcDot3(Box[i], LocalPlanes[j]) + LocalPlanes[j][3] > 0)
2011-09-07 23:06:51 +02:00
Outcodes[i] |= 1 << j;
}
}
int OutcodesOR = 0, OutcodesAND = 0x3f;
for (i = 0; i < 8; i++)
{
OutcodesAND &= Outcodes[i];
OutcodesOR |= Outcodes[i];
}
if (OutcodesAND != 0)
return;
2011-09-07 23:06:51 +02:00
if (OutcodesOR == 0 || mPieceInfo->mMesh->IntersectsPlanes(LocalPlanes))
{
lcObjectSection& ObjectSection = ObjectBoxTest.ObjectSections.Add();
2014-08-07 17:22:33 +02:00
ObjectSection.Object = const_cast<lcPiece*>(this);
ObjectSection.Section = 0;
}
2011-09-07 23:06:51 +02:00
}
2014-07-06 08:04:09 +02:00
void lcPiece::Move(lcStep Step, bool AddKey, const lcVector3& Distance)
2011-09-07 23:06:51 +02:00
{
2014-06-22 19:39:15 +02:00
mPosition += Distance;
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, mPosition, Step, AddKey);
mModelWorld.SetTranslation(mPosition);
2011-09-07 23:06:51 +02:00
}
2014-07-06 08:04:09 +02:00
bool lcPiece::IsVisible(lcStep Step)
2011-09-07 23:06:51 +02:00
{
if (mState & LC_PIECE_HIDDEN)
2011-09-07 23:06:51 +02:00
return false;
2014-07-06 08:04:09 +02:00
return (mStepShow <= Step) && (mStepHide > Step);
2011-09-07 23:06:51 +02:00
}
2014-05-01 20:42:11 +02:00
void lcPiece::CompareBoundingBox(float box[6])
2011-09-07 23:06:51 +02:00
{
lcVector3 Points[8] =
2011-09-07 23:06:51 +02:00
{
lcVector3(mPieceInfo->m_fDimensions[0], mPieceInfo->m_fDimensions[1], mPieceInfo->m_fDimensions[5]),
lcVector3(mPieceInfo->m_fDimensions[3], mPieceInfo->m_fDimensions[1], mPieceInfo->m_fDimensions[5]),
lcVector3(mPieceInfo->m_fDimensions[0], mPieceInfo->m_fDimensions[1], mPieceInfo->m_fDimensions[2]),
lcVector3(mPieceInfo->m_fDimensions[3], mPieceInfo->m_fDimensions[4], mPieceInfo->m_fDimensions[5]),
lcVector3(mPieceInfo->m_fDimensions[3], mPieceInfo->m_fDimensions[4], mPieceInfo->m_fDimensions[2]),
lcVector3(mPieceInfo->m_fDimensions[0], mPieceInfo->m_fDimensions[4], mPieceInfo->m_fDimensions[2]),
lcVector3(mPieceInfo->m_fDimensions[0], mPieceInfo->m_fDimensions[4], mPieceInfo->m_fDimensions[5]),
lcVector3(mPieceInfo->m_fDimensions[3], mPieceInfo->m_fDimensions[1], mPieceInfo->m_fDimensions[2])
};
for (int i = 0; i < 8; i++)
{
lcVector3 Point = lcMul31(Points[i], mModelWorld);
if (Point[0] < box[0]) box[0] = Point[0];
if (Point[1] < box[1]) box[1] = Point[1];
if (Point[2] < box[2]) box[2] = Point[2];
2012-07-21 00:57:43 +02:00
if (Point[0] > box[3]) box[3] = Point[0];
if (Point[1] > box[4]) box[4] = Point[1];
if (Point[2] > box[5]) box[5] = Point[2];
2011-09-07 23:06:51 +02:00
}
}
2014-05-25 20:23:09 +02:00
lcGroup* lcPiece::GetTopGroup()
2011-09-07 23:06:51 +02:00
{
2014-05-25 20:23:09 +02:00
return mGroup ? mGroup->GetTopGroup() : NULL;
2011-09-07 23:06:51 +02:00
}
2014-07-06 08:04:09 +02:00
void lcPiece::UpdatePosition(lcStep Step)
2011-09-07 23:06:51 +02:00
{
2014-08-31 02:53:12 +02:00
mPosition = CalculateKey(mPositionKeys, Step);
mRotation = CalculateKey(mRotationKeys, Step);
mModelWorld = lcMatrix44FromAxisAngle(lcVector3(mRotation[0], mRotation[1], mRotation[2]), mRotation[3] * LC_DTOR);
mModelWorld.SetTranslation(mPosition);
2011-09-07 23:06:51 +02:00
}