#include "DefaultModelSceneContent.h" const QString DefaultModelSceneContent::g_scene_name=QObject::tr("Model"); /** * Default constructor * */ DefaultModelSceneContent::DefaultModelSceneContent(SceneInterface* plugin): SceneContent(plugin), m_backFaceCulling(true), m_smoothing(true), m_pModel(NULL), m_modelIndex(0) { setFileName(); } /** * Destructor. * */ DefaultModelSceneContent::~DefaultModelSceneContent() { } /** * Implements the copy from other contents. * * This method is used by the copy constructor. * * @param content The source content to copy from. * * @return true if the copy is ok, false otherwise. The copy can fail if * SceneContent is not a DefaultModelSceneContent. * * @seealso is_equal_to * */ bool DefaultModelSceneContent::copy_from(const SceneContent& data) { if (!SceneContent::copy_from(data)) return false; Q_ASSERT(NULL!=plugin()); Q_ASSERT(NULL!=data.plugin()); const DefaultModelSceneContent& _data = static_cast(data); setFileName(_data.fileName()); setBackFaceCulling(_data.backFaceCulling()); setSmoothing(_data.smoothing()); return true; } /** * Implements the egality-test with other contents. * * This could be an operator==. * * @param content The content to compare with. * * @return true if the content are equal, false otherwise. The test can * fail if the contents have different parameters, but also it * SceneContent is not a DefaultModelSceneContent. * * @seealso copy_from * */ bool DefaultModelSceneContent::is_equal_to(const SceneContent& data) const { if (!SceneContent::is_equal_to(data)) return false; const DefaultModelSceneContent& _data = static_cast(data); return fileName()==_data.fileName() && backFaceCulling()==_data.backFaceCulling() && smoothing()==_data.smoothing(); } /** * Draws the GLM scene. * * @param glWidget The QGLWidget that own the OpenGL context. * @param cameraMatrix The camera matrix that must be use. * */ void DefaultModelSceneContent::draw(QGLWidget *glWidget, const float* cameraMatrix) { Q_UNUSED (glWidget); Q_UNUSED(cameraMatrix); glPushAttrib(GL_ENABLE_BIT); if (m_modelIndex) { if (m_backFaceCulling) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); glCallList(m_modelIndex); } glPopAttrib(); } /** * Sets the filename of the GLM scene. * * @param filename The new filename of the GLM scene. * */ void DefaultModelSceneContent::setFileName(const QString& fileName) { if (m_fileName != 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, (m_smoothing?GLM_SMOOTH:GLM_FLAT) | GLM_MATERIAL); glEndList(); } } /** * Loads the GLM scene from a QDomElement. * * Only the contents are loaded. After this, the model is loaded from the * filename. The model is not in the QDomElement. * * @param element The QDomElement to read from. * */ void DefaultModelSceneContent::initFromDOMElement(const QDomElement& element) { if (!element.isNull()) { setFileName(element.attribute("fileName")); setBackFaceCulling(element.attribute("backFaceCulling").toLower()=="true"); setSmoothing(element.attribute("smoothing").toLower()=="true"); SceneContent::initFromDOMElement(element); } } /** * Writes the contents in a QDomDocument. * * Only the contents are stored in the QDomElement. The model is not stored * in it. * * @param name The name of the element. * @param doc The document whom belongs the element. * * @return The QDomElement to add. * */ QDomElement DefaultModelSceneContent::domElement(const QString& name, QDomDocument& doc) const { QDomElement de = SceneContent::domElement(name, doc); de.setAttribute("fileName", fileName()); de.setAttribute("backFaceCulling", backFaceCulling()); de.setAttribute("smoothing", smoothing()); return de; }