leocad/common/piece.cpp

793 lines
21 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 "pieceinf.h"
#include "piece.h"
#include "group.h"
#include "project.h"
#include "lc_model.h"
#include "lc_file.h"
2011-09-07 23:06:51 +02:00
#include "lc_application.h"
2013-08-09 06:57:18 +02:00
#include "lc_library.h"
2014-11-24 00:48:56 +01:00
#include "lc_context.h"
#include "lc_qutils.h"
2011-09-07 23:06:51 +02:00
#define LC_PIECE_CONTROL_POINT_SIZE 10.0f
2011-09-07 23:06:51 +02:00
lcPiece::lcPiece(PieceInfo* Info)
2015-12-14 19:01:17 +01:00
: lcObject(LC_OBJECT_PIECE)
2011-09-07 23:06:51 +02:00
{
2016-02-19 18:53:54 +01:00
mMesh = NULL;
SetPieceInfo(Info);
mState = 0;
mColorIndex = gDefaultColor;
mColorCode = 16;
2014-07-06 08:04:09 +02:00
mStepShow = 1;
mStepHide = LC_STEP_MAX;
2014-05-25 20:23:09 +02:00
mGroup = NULL;
2015-03-21 21:12:04 +01:00
mFileLine = -1;
2015-12-14 19:01:17 +01:00
mPivotMatrix = lcMatrix44Identity();
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, lcMatrix33Identity(), 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
{
2015-12-14 19:01:17 +01:00
if (mPieceInfo)
mPieceInfo->Release(false);
2016-02-19 18:53:54 +01:00
delete mMesh;
2011-09-07 23:06:51 +02:00
}
void lcPiece::SetPieceInfo(PieceInfo* Info)
{
mPieceInfo = Info;
if (mPieceInfo)
mPieceInfo->AddRef();
if (0) // todo: get control points from the piece info
{
mControlPoints.SetSize(2);
mControlPoints[0].Position = lcVector3(0, 0, -30);
mControlPoints[1].Position = lcVector3(0, 0, 30);
2016-02-19 18:53:54 +01:00
UpdateMesh();
}
else
{
mControlPoints.RemoveAll();
2016-02-19 18:53:54 +01:00
delete mMesh;
mMesh = NULL;
}
}
2014-09-05 02:24:28 +02:00
void lcPiece::SaveLDraw(QTextStream& Stream) const
{
2014-09-08 21:42:20 +02:00
QLatin1String LineEnding("\r\n");
if (mStepHide != LC_STEP_MAX)
2014-09-08 21:42:20 +02:00
Stream << QLatin1String("0 !LEOCAD PIECE STEP_HIDE ") << mStepHide << LineEnding;
2014-09-08 21:42:20 +02:00
if (IsHidden())
Stream << QLatin1String("0 !LEOCAD PIECE HIDDEN") << LineEnding;
2015-12-16 23:36:42 +01:00
if (mState & LC_PIECE_PIVOT_POINT_VALID)
{
const float* PivotMatrix = mPivotMatrix;
float PivotNumbers[12] = { PivotMatrix[12], -PivotMatrix[14], PivotMatrix[13], PivotMatrix[0], -PivotMatrix[8], PivotMatrix[4], -PivotMatrix[2], PivotMatrix[10], -PivotMatrix[6], PivotMatrix[1], -PivotMatrix[9], PivotMatrix[5] };
Stream << QLatin1String("0 !LEOCAD PIECE PIVOT ");
for (int NumberIdx = 0; NumberIdx < 12; NumberIdx++)
Stream << ' ' << lcFormatValue(PivotNumbers[NumberIdx]);
Stream << LineEnding;
}
if (mPositionKeys.GetSize() > 1)
2014-09-05 02:24:28 +02:00
SaveKeysLDraw(Stream, mPositionKeys, "PIECE POSITION_KEY ");
if (mRotationKeys.GetSize() > 1)
2014-09-05 02:24:28 +02:00
SaveKeysLDraw(Stream, mRotationKeys, "PIECE ROTATION_KEY ");
Stream << "1 " << mColorCode << ' ';
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] };
for (int NumberIdx = 0; NumberIdx < 12; NumberIdx++)
Stream << lcFormatValue(Numbers[NumberIdx]) << ' ';
2014-12-26 18:09:11 +01:00
Stream << mPieceInfo->GetSaveID() << LineEnding;
}
2014-09-08 21:42:20 +02:00
bool lcPiece::ParseLDrawLine(QTextStream& Stream)
{
2014-09-08 21:42:20 +02:00
while (!Stream.atEnd())
2014-09-03 16:34:53 +02:00
{
2014-09-08 21:42:20 +02:00
QString Token;
Stream >> Token;
if (Token == QLatin1String("STEP_HIDE"))
Stream >> mStepHide;
else if (Token == QLatin1String("HIDDEN"))
SetHidden(true);
2015-12-16 23:36:42 +01:00
else if (Token == QLatin1String("PIVOT"))
{
float PivotNumbers[12];
for (int TokenIdx = 0; TokenIdx < 12; TokenIdx++)
Stream >> PivotNumbers[TokenIdx];
2015-12-17 01:41:10 +01:00
lcMatrix44 PivotMatrix(lcVector4( PivotNumbers[3], PivotNumbers[9], -PivotNumbers[6], 0.0f), lcVector4(PivotNumbers[5], PivotNumbers[11], -PivotNumbers[8], 0.0f),
lcVector4(-PivotNumbers[4], -PivotNumbers[10], PivotNumbers[7], 0.0f), lcVector4(PivotNumbers[0], PivotNumbers[2], -PivotNumbers[1], 1.0f));
2015-12-16 23:36:42 +01:00
mPivotMatrix = PivotMatrix;
mState |= LC_PIECE_PIVOT_POINT_VALID;
}
2014-09-08 21:42:20 +02:00
else if (Token == QLatin1String("POSITION_KEY"))
LoadKeysLDraw(Stream, mPositionKeys);
else if (Token == QLatin1String("ROTATION_KEY"))
LoadKeysLDraw(Stream, mRotationKeys);
2014-09-03 16:34:53 +02:00
}
return false;
}
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();
2015-12-14 19:01:17 +01:00
if (version > 12) // LeoCAD 0.80
2014-08-31 02:53:12 +02:00
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, lcMatrix33FromAxisAngle(lcVector3(param[0], param[1], param[2]), param[3] * LC_DTOR), time, true);
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);
}
}
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, lcMatrix33FromAxisAngle(lcVector3(param[0], param[1], param[2]), param[3] * LC_DTOR), 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;
ChangeKey(mPositionKeys, ModelWorld.GetTranslation(), 1, true);
ChangeKey(mRotationKeys, lcMatrix33(ModelWorld), 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);
ChangeKey(mRotationKeys, lcMatrix33(ModelWorld), 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
2015-02-22 03:39:15 +01:00
PieceInfo* pInfo = lcGetPiecesLibrary()->FindPiece(name, NULL, true);
2013-08-09 06:57:18 +02:00
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.Seek(ch, SEEK_CUR);
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;
file.Seek(81, SEEK_CUR);
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);
2016-02-19 18:53:54 +01:00
mGroup = (lcGroup*)(quintptr)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
2016-02-19 18:53:54 +01:00
mGroup = (lcGroup*)(quintptr)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
}
void lcPiece::Initialize(const lcMatrix44& WorldMatrix, 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
ChangeKey(mPositionKeys, WorldMatrix.GetTranslation(), 1, true);
ChangeKey(mRotationKeys, lcMatrix33(WorldMatrix), 1, true);
2011-09-07 23:06:51 +02:00
2014-11-29 03:55:58 +01:00
UpdatePosition(Step);
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
{
2014-12-26 16:44:46 +01:00
if (mPieceInfo->MinIntersectDist(mModelWorld, ObjectRayTest.Start, ObjectRayTest.End, ObjectRayTest.Distance))
{
2014-08-07 17:22:33 +02:00
ObjectRayTest.ObjectSection.Object = const_cast<lcPiece*>(this);
ObjectRayTest.ObjectSection.Section = LC_PIECE_SECTION_POSITION;
}
2015-12-14 19:01:17 +01:00
if (AreControlPointsVisible())
{
lcMatrix44 InverseWorldMatrix = lcMatrix44AffineInverse(mModelWorld);
lcVector3 Start = lcMul31(ObjectRayTest.Start, InverseWorldMatrix);
lcVector3 End = lcMul31(ObjectRayTest.End, InverseWorldMatrix);
for (int ControlPointIdx = 0; ControlPointIdx < mControlPoints.GetSize(); ControlPointIdx++)
{
lcVector3 Min(-LC_PIECE_CONTROL_POINT_SIZE, -LC_PIECE_CONTROL_POINT_SIZE, -LC_PIECE_CONTROL_POINT_SIZE);
lcVector3 Max(LC_PIECE_CONTROL_POINT_SIZE, LC_PIECE_CONTROL_POINT_SIZE, LC_PIECE_CONTROL_POINT_SIZE);
Min += mControlPoints[ControlPointIdx].Position;
Max += mControlPoints[ControlPointIdx].Position;
float Distance;
if (!lcBoundingBoxRayIntersectDistance(Min, Max, Start, End, &Distance, NULL) || (Distance >= ObjectRayTest.Distance))
continue;
ObjectRayTest.ObjectSection.Object = const_cast<lcPiece*>(this);
ObjectRayTest.ObjectSection.Section = LC_PIECE_SECTION_CONTROL_POINT_1 + ControlPointIdx;
ObjectRayTest.Distance = Distance;
}
}
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
{
2014-12-26 16:44:46 +01:00
if (mPieceInfo->BoxTest(mModelWorld, ObjectBoxTest.Planes))
2014-11-29 03:55:58 +01:00
ObjectBoxTest.Objects.Add(const_cast<lcPiece*>(this));
2011-09-07 23:06:51 +02:00
}
2015-05-17 01:04:35 +02:00
void lcPiece::DrawInterface(lcContext* Context) const
2014-11-24 00:48:56 +01:00
{
float LineWidth = lcGetPreferences().mLineWidth;
Context->SetLineWidth(2.0f * LineWidth);
2016-02-19 18:53:54 +01:00
const lcBoundingBox& BoundingBox = GetBoundingBox();
const lcVector3& Min = BoundingBox.Min;
const lcVector3& Max = BoundingBox.Max;
2014-11-24 00:48:56 +01:00
lcVector3 Edge((Max - Min) * 0.33f);
float Verts[48][3] =
{
{ Max[0], Max[1], Max[2] }, { Max[0] - Edge[0], Max[1], Max[2] },
{ Max[0], Max[1], Max[2] }, { Max[0], Max[1] - Edge[1], Max[2] },
{ Max[0], Max[1], Max[2] }, { Max[0], Max[1], Max[2] - Edge[2] },
{ Min[0], Max[1], Max[2] }, { Min[0] + Edge[0], Max[1], Max[2] },
{ Min[0], Max[1], Max[2] }, { Min[0], Max[1] - Edge[1], Max[2] },
{ Min[0], Max[1], Max[2] }, { Min[0], Max[1], Max[2] - Edge[2] },
{ Max[0], Min[1], Max[2] }, { Max[0] - Edge[0], Min[1], Max[2] },
{ Max[0], Min[1], Max[2] }, { Max[0], Min[1] + Edge[1], Max[2] },
{ Max[0], Min[1], Max[2] }, { Max[0], Min[1], Max[2] - Edge[2] },
{ Min[0], Min[1], Max[2] }, { Min[0] + Edge[0], Min[1], Max[2] },
{ Min[0], Min[1], Max[2] }, { Min[0], Min[1] + Edge[1], Max[2] },
{ Min[0], Min[1], Max[2] }, { Min[0], Min[1], Max[2] - Edge[2] },
{ Max[0], Max[1], Min[2] }, { Max[0] - Edge[0], Max[1], Min[2] },
{ Max[0], Max[1], Min[2] }, { Max[0], Max[1] - Edge[1], Min[2] },
{ Max[0], Max[1], Min[2] }, { Max[0], Max[1], Min[2] + Edge[2] },
{ Min[0], Max[1], Min[2] }, { Min[0] + Edge[0], Max[1], Min[2] },
{ Min[0], Max[1], Min[2] }, { Min[0], Max[1] - Edge[1], Min[2] },
{ Min[0], Max[1], Min[2] }, { Min[0], Max[1], Min[2] + Edge[2] },
{ Max[0], Min[1], Min[2] }, { Max[0] - Edge[0], Min[1], Min[2] },
{ Max[0], Min[1], Min[2] }, { Max[0], Min[1] + Edge[1], Min[2] },
{ Max[0], Min[1], Min[2] }, { Max[0], Min[1], Min[2] + Edge[2] },
{ Min[0], Min[1], Min[2] }, { Min[0] + Edge[0], Min[1], Min[2] },
{ Min[0], Min[1], Min[2] }, { Min[0], Min[1] + Edge[1], Min[2] },
{ Min[0], Min[1], Min[2] }, { Min[0], Min[1], Min[2] + Edge[2] },
};
Context->SetProgram(LC_PROGRAM_SIMPLE);
2015-05-17 01:04:35 +02:00
Context->SetWorldMatrix(mModelWorld);
2014-11-24 00:48:56 +01:00
if (IsFocused(LC_PIECE_SECTION_POSITION))
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_FOCUSED);
2014-11-24 00:48:56 +01:00
else
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_SELECTED);
2014-11-24 00:48:56 +01:00
2015-04-19 03:10:01 +02:00
Context->SetVertexBufferPointer(Verts);
Context->SetVertexFormat(0, 3, 0, 0);
Context->DrawPrimitives(GL_LINES, 0, 48);
2015-12-14 19:01:17 +01:00
if (IsPivotPointVisible())
{
2015-12-15 02:57:22 +01:00
const float Size = 5.0f;
const float Verts[8 * 3] =
2015-12-14 19:01:17 +01:00
{
2015-12-15 02:57:22 +01:00
-Size, -Size, -Size, -Size, Size, -Size, Size, Size, -Size, Size, -Size, -Size,
-Size, -Size, Size, -Size, Size, Size, Size, Size, Size, Size, -Size, Size
2015-12-14 19:01:17 +01:00
};
2015-12-15 02:57:22 +01:00
const GLushort Indices[24] =
{
0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7
};
Context->SetWorldMatrix(lcMul(mPivotMatrix, mModelWorld));
2015-12-14 19:01:17 +01:00
Context->SetVertexBufferPointer(Verts);
Context->SetVertexFormat(0, 3, 0, 0);
Context->SetIndexBufferPointer(Indices);
2015-12-15 02:57:22 +01:00
Context->DrawIndexedPrimitives(GL_LINES, 24, GL_UNSIGNED_SHORT, 0);
2015-12-14 19:01:17 +01:00
}
if (AreControlPointsVisible())
{
for (int ControlPointIdx = 0; ControlPointIdx < mControlPoints.GetSize(); ControlPointIdx++)
{
float Verts[8 * 3];
float* CurVert = Verts;
lcVector3 Min(-LC_PIECE_CONTROL_POINT_SIZE, -LC_PIECE_CONTROL_POINT_SIZE, -LC_PIECE_CONTROL_POINT_SIZE);
lcVector3 Max(LC_PIECE_CONTROL_POINT_SIZE, LC_PIECE_CONTROL_POINT_SIZE, LC_PIECE_CONTROL_POINT_SIZE);
Min += mControlPoints[ControlPointIdx].Position;
Max += mControlPoints[ControlPointIdx].Position;
*CurVert++ = Min[0]; *CurVert++ = Min[1]; *CurVert++ = Min[2];
*CurVert++ = Min[0]; *CurVert++ = Max[1]; *CurVert++ = Min[2];
*CurVert++ = Max[0]; *CurVert++ = Max[1]; *CurVert++ = Min[2];
*CurVert++ = Max[0]; *CurVert++ = Min[1]; *CurVert++ = Min[2];
*CurVert++ = Min[0]; *CurVert++ = Min[1]; *CurVert++ = Max[2];
*CurVert++ = Min[0]; *CurVert++ = Max[1]; *CurVert++ = Max[2];
*CurVert++ = Max[0]; *CurVert++ = Max[1]; *CurVert++ = Max[2];
*CurVert++ = Max[0]; *CurVert++ = Min[1]; *CurVert++ = Max[2];
const GLushort Indices[36] =
{
0, 1, 2, 0, 2, 3, 7, 6, 5, 7, 5, 4, 0, 1, 5, 0, 5, 4,
2, 3, 7, 2, 7, 6, 0, 3, 7, 0, 7, 4, 1, 2, 6, 1, 6, 5
};
Context->SetVertexBufferPointer(Verts);
Context->SetVertexFormat(0, 3, 0, 0);
Context->SetIndexBufferPointer(Indices);
if (IsFocused(LC_PIECE_SECTION_CONTROL_POINT_1 + ControlPointIdx))
Context->SetInterfaceColor(LC_COLOR_FOCUSED);
else
Context->SetInterfaceColor(LC_COLOR_CAMERA);
Context->DrawIndexedPrimitives(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
}
}
2014-11-24 00:48:56 +01:00
}
2016-02-19 18:53:54 +01:00
void lcPiece::AddRenderMeshes(lcScene& Scene, bool DrawInterface) const
{
bool Focused, Selected;
if (DrawInterface)
{
Focused = IsFocused();
Selected = IsSelected();
}
else
{
Focused = false;
Selected = false;
}
if (!mMesh)
mPieceInfo->AddRenderMeshes(Scene, mModelWorld, mColorIndex, Focused, Selected);
else
{
lcRenderMesh RenderMesh;
RenderMesh.WorldMatrix = mModelWorld;
RenderMesh.Mesh = mMesh;
RenderMesh.ColorIndex = mColorIndex;
RenderMesh.State = Focused ? LC_RENDERMESH_FOCUSED : (Selected ? LC_RENDERMESH_SELECTED : LC_RENDERMESH_NONE);
RenderMesh.Distance = fabsf(lcMul31(mModelWorld[3], Scene.mViewMatrix).z);
RenderMesh.LodIndex = RenderMesh.Mesh->GetLodIndex(RenderMesh.Distance);
// bool Translucent = lcIsColorTranslucent(mColorIndex);
// if ((mFlags & (LC_PIECE_HAS_SOLID | LC_PIECE_HAS_LINES)) || ((mFlags & LC_PIECE_HAS_DEFAULT) && !Translucent))
Scene.mOpaqueMeshes.Add(RenderMesh);
// if ((mFlags & LC_PIECE_HAS_TRANSLUCENT) || ((mFlags & LC_PIECE_HAS_DEFAULT) && Translucent))
// Scene.mTranslucentMeshes.Add(RenderMesh);
}
if (Selected)
Scene.mInterfaceObjects.Add(this);
}
2015-12-16 01:28:40 +01:00
void lcPiece::Move(lcStep Step, bool AddKey, const lcVector3& Distance)
2011-09-07 23:06:51 +02:00
{
lcuint32 Section = GetFocusSection();
if (Section == LC_PIECE_SECTION_POSITION || Section == LC_PIECE_SECTION_INVALID)
{
2015-12-16 01:28:40 +01:00
lcVector3 Position = mModelWorld.GetTranslation() + Distance;
2011-09-07 23:06:51 +02:00
2015-12-16 01:28:40 +01:00
SetPosition(Position, Step, AddKey);
2015-12-16 01:28:40 +01:00
mModelWorld.SetTranslation(Position);
}
else
{
int ControlPointIndex = Section - LC_PIECE_SECTION_CONTROL_POINT_1;
if (ControlPointIndex >= 0 && ControlPointIndex < mControlPoints.GetSize())
mControlPoints[ControlPointIndex].Position += Distance;
2016-02-19 18:53:54 +01:00
UpdateMesh();
}
2011-09-07 23:06:51 +02:00
}
2015-12-16 01:28:40 +01:00
void lcPiece::Rotate(lcStep Step, bool AddKey, const lcMatrix33& RotationMatrix, const lcVector3& Center, const lcMatrix33& RotationFrame)
2015-12-15 02:57:22 +01:00
{
2015-12-16 01:28:40 +01:00
lcVector3 Distance = mModelWorld.GetTranslation() - Center;
lcMatrix33 LocalToWorldMatrix = lcMatrix33(mModelWorld);
lcMatrix33 LocalToFocusMatrix = lcMul(LocalToWorldMatrix, RotationFrame);
lcMatrix33 NewLocalToWorldMatrix = lcMul(LocalToFocusMatrix, RotationMatrix);
lcMatrix33 WorldToLocalMatrix = lcMatrix33AffineInverse(LocalToWorldMatrix);
Distance = lcMul(Distance, WorldToLocalMatrix);
Distance = lcMul(Distance, NewLocalToWorldMatrix);
NewLocalToWorldMatrix.Orthonormalize();
SetPosition(Center + Distance, Step, AddKey);
SetRotation(NewLocalToWorldMatrix, Step, AddKey);
}
void lcPiece::MovePivotPoint(const lcVector3& Distance)
{
if (!IsFocused(LC_PIECE_SECTION_POSITION))
return;
mPivotMatrix.SetTranslation(mPivotMatrix.GetTranslation() + Distance);
mState |= LC_PIECE_PIVOT_POINT_VALID;
}
void lcPiece::RotatePivotPoint(const lcMatrix33& RotationMatrix)
{
if (!IsFocused(LC_PIECE_SECTION_POSITION))
return;
2015-12-15 02:57:22 +01:00
lcMatrix33 NewPivotRotationMatrix = lcMul(RotationMatrix, lcMatrix33(mPivotMatrix));
NewPivotRotationMatrix.Orthonormalize();
mPivotMatrix = lcMatrix44(NewPivotRotationMatrix, mPivotMatrix.GetTranslation());
mState |= LC_PIECE_PIVOT_POINT_VALID;
}
const char* lcPiece::GetName() const
{
return mPieceInfo->m_strDescription;
}
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
}
2016-02-19 18:53:54 +01:00
const lcBoundingBox& lcPiece::GetBoundingBox() const
2011-09-07 23:06:51 +02:00
{
2016-02-19 18:53:54 +01:00
if (!mMesh)
return mPieceInfo->GetBoundingBox();
else
return mMesh->mBoundingBox;
}
void lcPiece::CompareBoundingBox(lcVector3& Min, lcVector3& Max) const
{
lcVector3 Points[8];
if (!mMesh)
lcGetBoxCorners(GetBoundingBox(), Points);
else
lcGetBoxCorners(mMesh->mBoundingBox, Points);
for (int i = 0; i < 8; i++)
{
lcVector3 Point = lcMul31(Points[i], mModelWorld);
2016-02-19 18:53:54 +01:00
Min = lcMin(Point, Min);
Max = lcMax(Point, Max);
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
{
lcVector3 Position = CalculateKey(mPositionKeys, Step);
lcMatrix33 Rotation = CalculateKey(mRotationKeys, Step);
mModelWorld = lcMatrix44(Rotation, Position);
2011-09-07 23:06:51 +02:00
}
2016-02-19 18:53:54 +01:00
void lcPiece::UpdateMesh()
{
delete mMesh;
lcuint16 NumSections[LC_NUM_MESH_LODS] = { 1, 1 };
mMesh = new lcMesh();
mMesh->Create(NumSections, 2, 0, 2);
NumSections[0] = 2;
NumSections[1] = 2;
for (int LodIdx = 0; LodIdx < LC_NUM_MESH_LODS; LodIdx++)
{
lcMeshSection& Section = mMesh->mLods[LodIdx].Sections[0];
Section.ColorIndex = gDefaultColor;
Section.PrimitiveType = GL_LINES;
Section.NumIndices = 2;
Section.Texture = NULL;
Section.IndexOffset = 0;
}
lcuint16* Index = (lcuint16*)mMesh->mIndexData;
*Index++ = 0;
*Index++ = 1;
lcVertex* Verts = (lcVertex*)mMesh->mVertexData;
Verts[0].Position = mControlPoints[0].Position;
Verts[1].Position = mControlPoints[1].Position;
lcVector3 Min(FLT_MAX, FLT_MAX, FLT_MAX), Max(-FLT_MAX, -FLT_MAX, -FLT_MAX);
Min = lcMin(mControlPoints[0].Position, Min);
Min = lcMin(mControlPoints[1].Position, Min);
Max = lcMax(mControlPoints[0].Position, Max);
Max = lcMax(mControlPoints[1].Position, Max);
Min -= lcVector3(5.0f, 5.0f, 5.0f);
Max += lcVector3(5.0f, 5.0f, 5.0f);
mMesh->mRadius = lcLength(Max - Min) / 2.0f;
mMesh->mBoundingBox.Min = Min;
mMesh->mBoundingBox.Max = Max;
}