#include "screen/ScreenModel.h"
using namespace projection;
/**
* Constructor.
*
* @param pScreen Screen data.
*/
ScreenModel::ScreenModel(Screen* pScreen) : ScreenShape(pScreen)
{
m_pModel = NULL;
m_modelIndex = 0;
m_pScreen = pScreen;
}
/**
* Destructor.
*/
ScreenModel::~ScreenModel()
{
if (m_pModel)
glmDelete(m_pModel);
if (glIsList(m_modelIndex))
glDeleteLists(m_modelIndex, 1);
}
/**
* Load a model file.
*
* @param fileName File name of a model to load.
*/
void ScreenModel::setFileName(const QString& fileName)
{
m_fileName = fileName;
if (m_pModel)
glmDelete(m_pModel);
if (glIsList(m_modelIndex))
glDeleteLists(m_modelIndex, 1);
m_pModel = glmReadOBJ(fileName.toLocal8Bit().data());
glmFacetNormals(m_pModel);
glmVertexNormals(m_pModel, 89.5f);
m_modelIndex = glGenLists(1);
glNewList(m_modelIndex, GL_COMPILE);
glmDraw(m_pModel, GLM_SMOOTH);
glEndList();
notifyRedraw();
}
/**
* Retrieve file name of the model.
*
* @return File name of the model.
*/
QString ScreenModel::getFileName() const
{
return m_fileName;
}
/**
* Retrieve bounding box of the screen shape.
*
* @param min One corner of the screen shape.
* @param max Another corner of the screen shape.
*/
void ScreenModel::getBoundingBox(gmtl::Vec3f& min, gmtl::Vec3f& max)
{
if (m_pModel)
{
GLfloat scale[3];
glmDimensions(m_pModel, scale);
min.set(-scale[0]/2.0f, -scale[1]/2.0f, -scale[2]/2.0f);
max.set( scale[0]/2.0f, scale[1]/2.0f, scale[2]/2.0f);
}
}
/**
* Restore the screen shape from XML data.
*
* @param element Parent XML element of the screen shape data.
*/
bool ScreenModel::initFromDOMElement(const QDomElement& element)
{
if (!element.isNull())
{
if( element.attribute("fileName").isEmpty() )
{
return true;
}
else if( QFile::exists(element.attribute("fileName")) )
{
setFileName(element.attribute("fileName"));
}
else
{
QString tmp = element.attribute("fileName");
QMessageBox::critical( m_pScreen->getProjectionModel()->getGUI()->getMainWindow(), "Screen model file not found", "Error loading screen model: '"
+element.attribute("fileName")+"' does not exist.
Please put the model file on the appropriate location or modify the project file!" );
return false;
}
notifyRedraw();
}
return true; // todo: secure this function and return false on any critical error
}
/**
* Store the current screen shape as XML data.
*
* @param name XML node name of the data.
* @param doc XML document to store the data.
* @return Current screen shape data as XML data.
*/
QDomElement ScreenModel::domElement(const QString& name, QDomDocument& doc) const
{
QDomElement de = doc.createElement(name);
de.setAttribute("name", getName());
de.setAttribute("fileName", m_fileName);
return de;
}
/**
* Draw the shape model in inherited class.
*
* @param bFrame True to draw a wire frame mesh. False to draw as a polygon model.
*/
void ScreenModel::drawShape(bool)
{
glPushAttrib(GL_ENABLE_BIT);
if (m_modelIndex)
{
glDisable(GL_CULL_FACE);
glCallList(m_modelIndex);
}
glPopAttrib();
}