source: projectionDesigner/trunk/projdesignerplugins/defaultplugin/DefaultCubemapSceneContent.cpp @ 368

Last change on this file since 368 was 4, checked in by Torben Dannhauer, 15 years ago
File size: 9.0 KB
Line 
1#include "DefaultCubemapSceneContent.h"
2
3
4const QString DefaultCubemapSceneContent::g_scene_name=QObject::tr("Cubemap");
5
6
7
8/**
9 * Default constructor
10 *
11 */
12DefaultCubemapSceneContent::DefaultCubemapSceneContent(SceneInterface* plugin):
13    SceneContent(plugin)
14{
15    for (unsigned int i=0; i<6; ++i)
16        m_textureIndex[i] = 0;
17}
18
19
20/**
21 * Destructor.
22 *
23 */
24DefaultCubemapSceneContent::~DefaultCubemapSceneContent()
25{
26    for (unsigned int i=0; i<6; ++i)
27        if (glIsTexture(m_textureIndex[i]))
28            glDeleteTextures(1, &m_textureIndex[i]);
29}
30
31
32/**
33 * Implements the copy from other contents.
34 *
35 * This method is used by the copy constructor.
36 *
37 * @param content The source content to copy from.
38 *
39 * @return true if the copy is ok, false otherwise. The copy can fail if
40 *         SceneContent is not a DefaultCubemapSceneContent.
41 *
42 * @seealso is_equal_to
43 *
44 */
45bool DefaultCubemapSceneContent::copy_from(const SceneContent& data) 
46{
47    if (!SceneContent::copy_from(data)) return false;
48    Q_ASSERT(NULL!=plugin());
49    Q_ASSERT(NULL!=data.plugin());
50    const DefaultCubemapSceneContent& _data = static_cast<const DefaultCubemapSceneContent&>(data);
51    setTexture (CUBEMAP_FACE_FRONT,  _data.getTexture(CUBEMAP_FACE_FRONT));
52    setTexture (CUBEMAP_FACE_BACK,   _data.getTexture(CUBEMAP_FACE_BACK));
53    setTexture (CUBEMAP_FACE_LEFT,   _data.getTexture(CUBEMAP_FACE_LEFT));
54    setTexture (CUBEMAP_FACE_RIGHT,  _data.getTexture(CUBEMAP_FACE_RIGHT));
55    setTexture (CUBEMAP_FACE_TOP,    _data.getTexture(CUBEMAP_FACE_TOP));
56    setTexture (CUBEMAP_FACE_BOTTOM, _data.getTexture(CUBEMAP_FACE_BOTTOM));
57    return true;
58}
59
60
61/**
62 * Implements the egality-test with other contents.
63 *
64 * This could be an operator==.
65 *
66 * @param content The content to compare with.
67 *
68 * @return true if the content are equal, false otherwise. The test can
69 *         fail if the contents have different parameters, but also it
70 *         SceneContent is not a DefaultCubemapSceneContent.
71 *
72 * @seealso copy_from
73 *
74 */
75bool DefaultCubemapSceneContent::is_equal_to(const SceneContent& data) const
76{
77    if (!SceneContent::is_equal_to(data)) return false;
78    const DefaultCubemapSceneContent& _data = static_cast<const DefaultCubemapSceneContent&>(data);
79    return getTexture(CUBEMAP_FACE_FRONT) ==_data.getTexture(CUBEMAP_FACE_FRONT)
80        && getTexture(CUBEMAP_FACE_BACK)  ==_data.getTexture(CUBEMAP_FACE_BACK)
81        && getTexture(CUBEMAP_FACE_LEFT)  ==_data.getTexture(CUBEMAP_FACE_LEFT)
82        && getTexture(CUBEMAP_FACE_RIGHT) ==_data.getTexture(CUBEMAP_FACE_RIGHT)
83        && getTexture(CUBEMAP_FACE_TOP)   ==_data.getTexture(CUBEMAP_FACE_TOP)
84        && getTexture(CUBEMAP_FACE_BOTTOM)==_data.getTexture(CUBEMAP_FACE_BOTTOM);
85}
86
87
88/**
89 * Draws the cubemap.
90 *
91 * @param glWidget The QGLWidget that own the OpenGL context.
92 * @param cameraMatrix The camera matrix that must be use.
93 *
94 */
95void DefaultCubemapSceneContent::draw(QGLWidget* glWidget, const float* cameraMatrix)
96{
97    Q_UNUSED(cameraMatrix);
98    glPushMatrix();
99
100    glPushAttrib(GL_ALL_ATTRIB_BITS);
101    glEnable(GL_TEXTURE_2D);
102    glDisable(GL_LIGHTING);
103
104    for (unsigned int i=0; i<6; ++i) {
105        if (!glIsTexture(m_textureIndex[i]) && !m_textureFileNames[i].isEmpty())
106        {
107            m_textureIndex[i] = glWidget->bindTexture(m_textureFileNames[i]);
108        }
109    }
110
111    glBindTexture(GL_TEXTURE_2D, m_textureIndex[(int)CUBEMAP_FACE_FRONT]);
112    glBegin(GL_TRIANGLE_STRIP);
113    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MAX); glVertex3f(-SIZE, -SIZE, -SIZE);
114    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MAX); glVertex3f( SIZE, -SIZE, -SIZE);
115    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MIN); glVertex3f(-SIZE,  SIZE, -SIZE);
116    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MIN); glVertex3f( SIZE,  SIZE, -SIZE);
117    glEnd();
118
119    glBindTexture(GL_TEXTURE_2D, m_textureIndex[(int)CUBEMAP_FACE_BACK]);
120    glBegin(GL_TRIANGLE_STRIP);
121    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MAX); glVertex3f(-SIZE, -SIZE,  SIZE);
122    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MIN); glVertex3f(-SIZE,  SIZE,  SIZE);
123    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MAX); glVertex3f( SIZE, -SIZE,  SIZE);
124    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MIN); glVertex3f( SIZE,  SIZE,  SIZE);
125    glEnd();
126
127    glBindTexture(GL_TEXTURE_2D, m_textureIndex[(int)CUBEMAP_FACE_LEFT]);
128    glBegin(GL_TRIANGLE_STRIP);
129    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MAX); glVertex3f(-SIZE, -SIZE, -SIZE);
130    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MAX); glVertex3f(-SIZE, -SIZE,  SIZE);
131    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MIN); glVertex3f(-SIZE,  SIZE, -SIZE);
132    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MIN); glVertex3f(-SIZE,  SIZE,  SIZE);
133    glEnd();
134
135    glBindTexture(GL_TEXTURE_2D, m_textureIndex[(int)CUBEMAP_FACE_RIGHT]);
136    glBegin(GL_TRIANGLE_STRIP);
137    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MAX); glVertex3f( SIZE, -SIZE, -SIZE);
138    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MIN); glVertex3f( SIZE,  SIZE, -SIZE);
139    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MAX); glVertex3f( SIZE, -SIZE,  SIZE);
140    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MIN); glVertex3f( SIZE,  SIZE,  SIZE);
141    glEnd();
142
143    glBindTexture(GL_TEXTURE_2D, m_textureIndex[(int)CUBEMAP_FACE_TOP]);
144    glBegin(GL_TRIANGLE_STRIP);
145    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MAX); glVertex3f(-SIZE,  SIZE, -SIZE);
146    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MAX); glVertex3f( SIZE,  SIZE, -SIZE);
147    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MIN); glVertex3f(-SIZE,  SIZE,  SIZE);
148    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MIN); glVertex3f( SIZE,  SIZE,  SIZE);
149    glEnd();
150
151    glBindTexture(GL_TEXTURE_2D, m_textureIndex[(int)CUBEMAP_FACE_BOTTOM]);
152    glBegin(GL_TRIANGLE_STRIP);
153    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MIN); glVertex3f(-SIZE, -SIZE, -SIZE);
154    glTexCoord2f(TEXCOORD_MIN, TEXCOORD_MAX); glVertex3f(-SIZE, -SIZE,  SIZE);
155    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MIN); glVertex3f( SIZE, -SIZE, -SIZE);
156    glTexCoord2f(TEXCOORD_MAX, TEXCOORD_MAX); glVertex3f( SIZE, -SIZE,  SIZE);
157    glEnd();
158
159    glPopAttrib();
160
161    glPopMatrix();
162}
163
164
165/**
166 * Sets the name of the texture for a face of the cubemap.
167 *
168 * @param face The face of which we want to set the name of the texture.
169 * @param fileName The new name of the texture.
170 *
171 */
172void DefaultCubemapSceneContent::setTexture(CUBEMAP_FACE face, const QString& fileName)
173{
174    if ((int)face < 0 || (int)face > 5) return;
175    if (m_textureFileNames[(int)face] == fileName) return;
176
177    if (glIsTexture(m_textureIndex[(int)face]))
178        glDeleteTextures(1, &m_textureIndex[(int)face]);
179    m_textureIndex[(int)face] = 0;
180    m_textureFileNames[(int)face] = fileName;
181}
182
183
184/**
185 * Gets the name of the texture for a face.
186 *
187 * @param face The face of which we want to know the name of the texture.
188 *
189 * @return A QString with the name of the texture.
190 *
191 */
192QString DefaultCubemapSceneContent::getTexture(CUBEMAP_FACE face) const
193{
194    if ((int)face < 0 || (int)face > 5) return QString::null;
195    return m_textureFileNames[(int)face];
196}
197
198
199/**
200 * Loads the cubemap from a QDomElement.
201 *
202 * @param element The QDomElement to read from.
203 *
204 */
205void DefaultCubemapSceneContent::initFromDOMElement(const QDomElement& element)
206{
207    if (!element.isNull())
208    {
209        SceneContent::initFromDOMElement(element);
210        QDomElement texture = element.firstChildElement("texture");
211        while (!texture.isNull())
212        {
213            if (texture.attribute("face") == "front")
214                setTexture(CUBEMAP_FACE_FRONT, texture.attribute("fileName"));
215            else if (texture.attribute("face") == "back")
216                setTexture(CUBEMAP_FACE_BACK, texture.attribute("fileName"));
217            else if (texture.attribute("face") == "left")
218                setTexture(CUBEMAP_FACE_LEFT, texture.attribute("fileName"));
219            else if (texture.attribute("face") == "right")
220                setTexture(CUBEMAP_FACE_RIGHT, texture.attribute("fileName"));
221            else if (texture.attribute("face") == "top")
222                setTexture(CUBEMAP_FACE_TOP, texture.attribute("fileName"));
223            else if (texture.attribute("face") == "bottom")
224                setTexture(CUBEMAP_FACE_BOTTOM, texture.attribute("fileName"));
225            texture = texture.nextSiblingElement("texture");
226        }
227    }
228}
229
230
231/**
232 * Writes the cubemap in a QDomDocument.
233 *
234 * @param name The name of the element.
235 * @param doc The document whom belongs the element.
236 *
237 * @return The QDomElement to add.
238 *
239 */
240QDomElement DefaultCubemapSceneContent::domElement(const QString& name, QDomDocument& doc) const
241{
242    QDomElement de = SceneContent::domElement(name, doc);
243    for (unsigned int i=0; i<6; ++i)
244    {
245        QDomElement texture = doc.createElement("texture");
246        switch (i) {
247            case 0: texture.setAttribute("face", "front"); break;
248            case 1: texture.setAttribute("face", "back"); break;
249            case 2: texture.setAttribute("face", "left"); break;
250            case 3: texture.setAttribute("face", "right"); break;
251            case 4: texture.setAttribute("face", "top"); break;
252            case 5: texture.setAttribute("face", "bottom"); break;
253        }
254        texture.setAttribute("fileName", m_textureFileNames[i]);
255        de.appendChild(texture);
256    }
257    return de;
258}
Note: See TracBrowser for help on using the repository browser.