[4] | 1 | #include "screen/ScreenModel.h" |
---|
| 2 | |
---|
| 3 | using namespace projection; |
---|
| 4 | |
---|
| 5 | /** |
---|
| 6 | * Constructor. |
---|
| 7 | * |
---|
| 8 | * @param pScreen Screen data. |
---|
| 9 | */ |
---|
| 10 | ScreenModel::ScreenModel(Screen* pScreen) : ScreenShape(pScreen) |
---|
| 11 | { |
---|
| 12 | m_pModel = NULL; |
---|
| 13 | m_modelIndex = 0; |
---|
| 14 | m_pScreen = pScreen; |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | * Destructor. |
---|
| 19 | */ |
---|
| 20 | ScreenModel::~ScreenModel() |
---|
| 21 | { |
---|
| 22 | if (m_pModel) |
---|
| 23 | glmDelete(m_pModel); |
---|
| 24 | if (glIsList(m_modelIndex)) |
---|
| 25 | glDeleteLists(m_modelIndex, 1); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Load a model file. |
---|
| 30 | * |
---|
| 31 | * @param fileName File name of a model to load. |
---|
| 32 | */ |
---|
| 33 | void ScreenModel::setFileName(const QString& fileName) |
---|
| 34 | { |
---|
| 35 | m_fileName = fileName; |
---|
| 36 | |
---|
| 37 | if (m_pModel) |
---|
| 38 | glmDelete(m_pModel); |
---|
| 39 | if (glIsList(m_modelIndex)) |
---|
| 40 | glDeleteLists(m_modelIndex, 1); |
---|
| 41 | |
---|
| 42 | m_pModel = glmReadOBJ(fileName.toLocal8Bit().data()); |
---|
| 43 | glmFacetNormals(m_pModel); |
---|
| 44 | glmVertexNormals(m_pModel, 89.5f); |
---|
| 45 | m_modelIndex = glGenLists(1); |
---|
| 46 | glNewList(m_modelIndex, GL_COMPILE); |
---|
| 47 | glmDraw(m_pModel, GLM_SMOOTH); |
---|
| 48 | glEndList(); |
---|
| 49 | |
---|
| 50 | notifyRedraw(); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Retrieve file name of the model. |
---|
| 55 | * |
---|
| 56 | * @return File name of the model. |
---|
| 57 | */ |
---|
| 58 | QString ScreenModel::getFileName() const |
---|
| 59 | { |
---|
| 60 | return m_fileName; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Retrieve bounding box of the screen shape. |
---|
| 65 | * |
---|
| 66 | * @param min One corner of the screen shape. |
---|
| 67 | * @param max Another corner of the screen shape. |
---|
| 68 | */ |
---|
| 69 | void ScreenModel::getBoundingBox(gmtl::Vec3f& min, gmtl::Vec3f& max) |
---|
| 70 | { |
---|
| 71 | if (m_pModel) |
---|
| 72 | { |
---|
| 73 | GLfloat scale[3]; |
---|
| 74 | glmDimensions(m_pModel, scale); |
---|
| 75 | min.set(-scale[0]/2.0f, -scale[1]/2.0f, -scale[2]/2.0f); |
---|
| 76 | max.set( scale[0]/2.0f, scale[1]/2.0f, scale[2]/2.0f); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Restore the screen shape from XML data. |
---|
| 82 | * |
---|
| 83 | * @param element Parent XML element of the screen shape data. |
---|
| 84 | */ |
---|
| 85 | bool ScreenModel::initFromDOMElement(const QDomElement& element) |
---|
| 86 | { |
---|
| 87 | if (!element.isNull()) |
---|
| 88 | { |
---|
| 89 | if( element.attribute("fileName").isEmpty() ) |
---|
| 90 | { |
---|
| 91 | return true; |
---|
| 92 | } |
---|
| 93 | else if( QFile::exists(element.attribute("fileName")) ) |
---|
| 94 | { |
---|
| 95 | setFileName(element.attribute("fileName")); |
---|
| 96 | } |
---|
| 97 | else |
---|
| 98 | { |
---|
| 99 | QString tmp = element.attribute("fileName"); |
---|
| 100 | QMessageBox::critical( m_pScreen->getProjectionModel()->getGUI()->getMainWindow(), "Screen model file not found", "Error loading screen model: '" |
---|
| 101 | +element.attribute("fileName")+"' does not exist.<br><br>Please put the model file on the appropriate location or modify the project file!" ); |
---|
| 102 | |
---|
| 103 | return false; |
---|
| 104 | } |
---|
| 105 | notifyRedraw(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | return true; // todo: secure this function and return false on any critical error |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Store the current screen shape as XML data. |
---|
| 113 | * |
---|
| 114 | * @param name XML node name of the data. |
---|
| 115 | * @param doc XML document to store the data. |
---|
| 116 | * @return Current screen shape data as XML data. |
---|
| 117 | */ |
---|
| 118 | QDomElement ScreenModel::domElement(const QString& name, QDomDocument& doc) const |
---|
| 119 | { |
---|
| 120 | QDomElement de = doc.createElement(name); |
---|
| 121 | de.setAttribute("name", getName()); |
---|
| 122 | de.setAttribute("fileName", m_fileName); |
---|
| 123 | |
---|
| 124 | return de; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * Draw the shape model in inherited class. |
---|
| 129 | * |
---|
| 130 | * @param bFrame True to draw a wire frame mesh. False to draw as a polygon model. |
---|
| 131 | */ |
---|
| 132 | void ScreenModel::drawShape(bool) |
---|
| 133 | { |
---|
| 134 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 135 | |
---|
| 136 | if (m_modelIndex) |
---|
| 137 | { |
---|
| 138 | glDisable(GL_CULL_FACE); |
---|
| 139 | glCallList(m_modelIndex); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | glPopAttrib(); |
---|
| 143 | } |
---|