source: projectionDesigner/trunk/projdesignerplugins/defaultplugin/DefaultModelSceneContent.cpp @ 347

Last change on this file since 347 was 4, checked in by Torben Dannhauer, 15 years ago
File size: 4.3 KB
Line 
1#include "DefaultModelSceneContent.h"
2
3
4const QString DefaultModelSceneContent::g_scene_name=QObject::tr("Model");
5
6
7/**
8 * Default constructor
9 *
10 */
11DefaultModelSceneContent::DefaultModelSceneContent(SceneInterface* plugin):
12    SceneContent(plugin),
13    m_backFaceCulling(true),
14    m_smoothing(true),
15    m_pModel(NULL),
16    m_modelIndex(0)
17{
18    setFileName();
19}
20
21
22/**
23 * Destructor.
24 *
25 */
26DefaultModelSceneContent::~DefaultModelSceneContent()
27{
28}
29
30
31/**
32 * Implements the copy from other contents.
33 *
34 * This method is used by the copy constructor.
35 *
36 * @param content The source content to copy from.
37 *
38 * @return true if the copy is ok, false otherwise. The copy can fail if
39 *         SceneContent is not a DefaultModelSceneContent.
40 *
41 * @seealso is_equal_to
42 *
43 */
44bool DefaultModelSceneContent::copy_from(const SceneContent& data) 
45{
46    if (!SceneContent::copy_from(data)) return false;
47    Q_ASSERT(NULL!=plugin());
48    Q_ASSERT(NULL!=data.plugin());
49    const DefaultModelSceneContent& _data = static_cast<const DefaultModelSceneContent&>(data);
50    setFileName(_data.fileName());
51    setBackFaceCulling(_data.backFaceCulling());
52    setSmoothing(_data.smoothing());
53    return true;
54}
55
56
57/**
58 * Implements the egality-test with other contents.
59 *
60 * This could be an operator==.
61 *
62 * @param content The content to compare with.
63 *
64 * @return true if the content are equal, false otherwise. The test can
65 *         fail if the contents have different parameters, but also it
66 *         SceneContent is not a DefaultModelSceneContent.
67 *
68 * @seealso copy_from
69 *
70 */
71bool DefaultModelSceneContent::is_equal_to(const SceneContent& data) const
72{
73    if (!SceneContent::is_equal_to(data)) return false;
74    const DefaultModelSceneContent& _data = static_cast<const DefaultModelSceneContent&>(data);
75    return fileName()==_data.fileName()
76        && backFaceCulling()==_data.backFaceCulling()
77        && smoothing()==_data.smoothing();
78}
79
80
81/**
82 * Draws the GLM scene.
83 *
84 * @param glWidget The QGLWidget that own the OpenGL context.
85 * @param cameraMatrix The camera matrix that must be use.
86 *
87 */
88void DefaultModelSceneContent::draw(QGLWidget *glWidget, const float* cameraMatrix)
89{
90    Q_UNUSED (glWidget);
91    Q_UNUSED(cameraMatrix);
92    glPushAttrib(GL_ENABLE_BIT);
93    if (m_modelIndex)
94    {
95        if (m_backFaceCulling)
96            glEnable(GL_CULL_FACE);
97        else
98            glDisable(GL_CULL_FACE);
99        glCallList(m_modelIndex);
100    }
101    glPopAttrib();
102}
103
104
105/**
106 * Sets the filename of the GLM scene.
107 *
108 * @param filename The new filename of the GLM scene.
109 *
110 */
111void DefaultModelSceneContent::setFileName(const QString& fileName)
112{
113    if (m_fileName != fileName)
114    {
115        m_fileName = fileName;
116        if (m_pModel)
117            glmDelete(m_pModel);
118        if (glIsList(m_modelIndex))
119            glDeleteLists(m_modelIndex, 1);
120        m_pModel = glmReadOBJ(fileName.toLocal8Bit().data());
121        glmFacetNormals(m_pModel);
122        glmVertexNormals(m_pModel, 89.5f);
123        m_modelIndex = glGenLists(1);
124        glNewList(m_modelIndex, GL_COMPILE);
125        glmDraw(m_pModel, (m_smoothing?GLM_SMOOTH:GLM_FLAT) | GLM_MATERIAL);
126        glEndList();
127    }
128}
129
130
131/**
132 * Loads the GLM scene from a QDomElement.
133 *
134 * Only the contents are loaded. After this, the model is loaded from the
135 * filename. The model is not in the QDomElement.
136 *
137 * @param element The QDomElement to read from.
138 *
139 */
140void DefaultModelSceneContent::initFromDOMElement(const QDomElement& element)
141{
142    if (!element.isNull())
143    {
144        setFileName(element.attribute("fileName"));
145        setBackFaceCulling(element.attribute("backFaceCulling").toLower()=="true");
146        setSmoothing(element.attribute("smoothing").toLower()=="true");
147        SceneContent::initFromDOMElement(element);
148    }
149}
150
151
152/**
153 * Writes the contents in a QDomDocument.
154 *
155 * Only the contents are stored in the QDomElement. The model is not stored
156 * in it.
157 *
158 * @param name The name of the element.
159 * @param doc The document whom belongs the element.
160 *
161 * @return The QDomElement to add.
162 *
163 */
164QDomElement DefaultModelSceneContent::domElement(const QString& name, QDomDocument& doc) const
165{
166    QDomElement de = SceneContent::domElement(name, doc);
167    de.setAttribute("fileName", fileName());
168    de.setAttribute("backFaceCulling", backFaceCulling());
169    de.setAttribute("smoothing", smoothing());
170    return de;
171}
Note: See TracBrowser for help on using the repository browser.