leocad/common/pieceinf.cpp

116 lines
2.8 KiB
C++
Raw Normal View History

#include "lc_global.h"
2012-03-29 03:10:55 +02:00
#include "lc_math.h"
2012-04-14 01:41:58 +02:00
#include "lc_mesh.h"
#include "lc_colors.h"
2012-10-12 01:55:55 +02:00
#include "lc_texture.h"
2011-09-07 23:06:51 +02:00
#include "opengl.h"
#include "pieceinf.h"
2012-10-02 03:23:44 +02:00
#include "lc_library.h"
2011-09-07 23:06:51 +02:00
#include "lc_application.h"
PieceInfo::PieceInfo(int ZipFileIndex)
2011-09-07 23:06:51 +02:00
{
mZipFileIndex = ZipFileIndex;
mFlags = 0;
mMesh = NULL;
2012-10-12 01:55:55 +02:00
mRefCount = 0;
2011-09-07 23:06:51 +02:00
}
2012-04-14 01:41:58 +02:00
PieceInfo::~PieceInfo()
2011-09-07 23:06:51 +02:00
{
2012-10-12 02:21:04 +02:00
if (mRefCount)
Unload();
2011-09-07 23:06:51 +02:00
}
void PieceInfo::CreatePlaceholder(const char* Name)
{
strncpy(m_strName, Name, sizeof(m_strName));
m_strName[sizeof(m_strName)-1] = 0;
strncpy(m_strDescription, Name, sizeof(m_strDescription));
m_strDescription[sizeof(m_strDescription)-1] = 0;
mFlags = LC_PIECE_PLACEHOLDER;
}
2012-10-12 01:55:55 +02:00
void PieceInfo::Load()
2011-09-07 23:06:51 +02:00
{
if (mFlags & LC_PIECE_PLACEHOLDER)
{
mMesh = new lcMesh();
mMesh->CreateBox();
mFlags |= LC_PIECE_HAS_DEFAULT | LC_PIECE_HAS_LINES;
m_fDimensions[0] = 0.4f;
m_fDimensions[1] = 0.4f;
m_fDimensions[2] = 0.16f;
m_fDimensions[3] = -0.4f;
m_fDimensions[4] = -0.4f;
m_fDimensions[5] = -0.96f;
2012-04-14 01:41:58 +02:00
}
else if (mFlags & LC_PIECE_GENERATED)
lcGetPiecesLibrary()->GeneratePiece(this);
2012-10-02 03:23:44 +02:00
else
lcGetPiecesLibrary()->LoadPiece(this);
2013-08-09 06:57:18 +02:00
}
2011-09-07 23:06:51 +02:00
2012-10-12 01:55:55 +02:00
void PieceInfo::Unload()
2011-09-07 23:06:51 +02:00
{
2012-10-12 01:55:55 +02:00
for (int SectionIdx = 0; SectionIdx < mMesh->mNumSections; SectionIdx++)
{
lcMeshSection& Section = mMesh->mSections[SectionIdx];
if (Section.Texture)
Section.Texture->Release();
}
2012-04-14 01:41:58 +02:00
delete mMesh;
mMesh = NULL;
2011-09-07 23:06:51 +02:00
}
// Zoom extents for the preview window and print catalog
void PieceInfo::ZoomExtents(float Fov, float Aspect, float* EyePos) const
{
2012-06-21 02:41:53 +02:00
lcVector3 Points[8] =
2011-09-07 23:06:51 +02:00
{
2012-06-21 02:41:53 +02:00
lcVector3(m_fDimensions[0], m_fDimensions[1], m_fDimensions[5]),
lcVector3(m_fDimensions[3], m_fDimensions[1], m_fDimensions[5]),
lcVector3(m_fDimensions[0], m_fDimensions[1], m_fDimensions[2]),
lcVector3(m_fDimensions[3], m_fDimensions[4], m_fDimensions[5]),
lcVector3(m_fDimensions[3], m_fDimensions[4], m_fDimensions[2]),
lcVector3(m_fDimensions[0], m_fDimensions[4], m_fDimensions[2]),
lcVector3(m_fDimensions[0], m_fDimensions[4], m_fDimensions[5]),
lcVector3(m_fDimensions[3], m_fDimensions[1], m_fDimensions[2])
};
2011-09-07 23:06:51 +02:00
2012-06-21 02:41:53 +02:00
lcVector3 Center(GetCenter());
lcVector3 Position;
2011-09-07 23:06:51 +02:00
2012-06-21 02:41:53 +02:00
if (EyePos)
Position = lcVector3(EyePos[0], EyePos[1], EyePos[2]);
else
Position = lcVector3(-10.0f, -10.0f, 5.0f);
Position += Center;
2011-09-07 23:06:51 +02:00
2012-06-21 02:41:53 +02:00
lcMatrix44 Projection = lcMatrix44Perspective(30.0f, Aspect, 1.0f, 100.0f);
lcMatrix44 ModelView = lcMatrix44LookAt(Position, Center, lcVector3(0, 0, 1));
Position = lcZoomExtents(Position, ModelView, Projection, Points, 8);
2011-09-07 23:06:51 +02:00
2012-06-21 02:41:53 +02:00
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(Projection);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(lcMatrix44LookAt(Position, Center, lcVector3(0, 0, 1)));
2011-09-07 23:06:51 +02:00
if (EyePos)
{
2012-06-21 02:41:53 +02:00
EyePos[0] = Position[0];
EyePos[1] = Position[1];
EyePos[2] = Position[2];
2011-09-07 23:06:51 +02:00
}
}
void PieceInfo::RenderPiece(int nColor)
{
2012-04-14 01:41:58 +02:00
mMesh->Render(nColor, false, false);
2011-09-07 23:06:51 +02:00
}