1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * GLMoleculeObject.cpp
|
---|
10 | *
|
---|
11 | * This is based on the Qt3D example "teaservice", specifically meshobject.cpp.
|
---|
12 | *
|
---|
13 | * Created on: Aug 17, 2011
|
---|
14 | * Author: heber
|
---|
15 | */
|
---|
16 |
|
---|
17 | // include config.h
|
---|
18 | #ifdef HAVE_CONFIG_H
|
---|
19 | #include <config.h>
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #include "GLMoleculeObject.hpp"
|
---|
23 |
|
---|
24 | #include <Qt3D/qglview.h>
|
---|
25 | #include <Qt3D/qglscenenode.h>
|
---|
26 | #include <Qt3D/qglpainter.h>
|
---|
27 | #include <Qt3D/qglmaterial.h>
|
---|
28 |
|
---|
29 | #include "CodePatterns/MemDebug.hpp"
|
---|
30 |
|
---|
31 | #include "CodePatterns/Assert.hpp"
|
---|
32 | #include "CodePatterns/Log.hpp"
|
---|
33 |
|
---|
34 | #include "Helpers/defs.hpp"
|
---|
35 | #include "Element/element.hpp"
|
---|
36 | #include "Element/periodentafel.hpp"
|
---|
37 | #include "World.hpp"
|
---|
38 |
|
---|
39 | GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap;
|
---|
40 |
|
---|
41 |
|
---|
42 | #include "CodePatterns/MemDebug.hpp"
|
---|
43 |
|
---|
44 |
|
---|
45 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *GLMoleculeObject, QObject *parent)
|
---|
46 | : QObject(parent)
|
---|
47 | {
|
---|
48 | m_mesh = 0;
|
---|
49 | m_GLMoleculeObject = GLMoleculeObject;
|
---|
50 | m_scale = 1.0f;
|
---|
51 | m_rotationAngle = 0.0f;
|
---|
52 | m_effect = 0;
|
---|
53 | m_objectId = -1;
|
---|
54 | m_hovering = false;
|
---|
55 | m_selected = false;
|
---|
56 | m_material = 0;
|
---|
57 | m_hoverMaterial = 0;
|
---|
58 | m_selectionMaterial = 0;
|
---|
59 | }
|
---|
60 |
|
---|
61 | GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
|
---|
62 | : QObject(parent)
|
---|
63 | {
|
---|
64 | scene->setParent(this);
|
---|
65 | m_mesh = 0;
|
---|
66 | m_GLMoleculeObject = scene->mainNode();
|
---|
67 | m_scale = 1.0f;
|
---|
68 | m_rotationAngle = 0.0f;
|
---|
69 | m_effect = 0;
|
---|
70 | m_objectId = -1;
|
---|
71 | m_hovering = false;
|
---|
72 | m_selected = false;
|
---|
73 | m_material = 0;
|
---|
74 | m_hoverMaterial = 0;
|
---|
75 | m_selectionMaterial = 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | GLMoleculeObject::~GLMoleculeObject()
|
---|
79 | {
|
---|
80 | delete m_mesh;
|
---|
81 | }
|
---|
82 |
|
---|
83 | void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
|
---|
84 | {
|
---|
85 | Q_UNUSED(painter);
|
---|
86 | if (m_objectId != -1)
|
---|
87 | view->registerObject(m_objectId, this);
|
---|
88 | }
|
---|
89 |
|
---|
90 | void GLMoleculeObject::draw(QGLPainter *painter)
|
---|
91 | {
|
---|
92 | // Position the model at its designated position, scale, and orientation.
|
---|
93 | painter->modelViewMatrix().push();
|
---|
94 | painter->modelViewMatrix().translate(m_position);
|
---|
95 | if (m_scale != 1.0f)
|
---|
96 | painter->modelViewMatrix().scale(m_scale);
|
---|
97 | if (m_rotationAngle != 0.0f)
|
---|
98 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
|
---|
99 |
|
---|
100 | // Apply the material and effect to the painter.
|
---|
101 | QGLMaterial *material;
|
---|
102 | if (m_hovering)
|
---|
103 | material = m_hoverMaterial;
|
---|
104 | else if (m_selected)
|
---|
105 | material = m_selectionMaterial;
|
---|
106 | else
|
---|
107 | material = m_material;
|
---|
108 | painter->setColor(material->diffuseColor());
|
---|
109 | painter->setFaceMaterial(QGL::AllFaces, material);
|
---|
110 | if (m_effect)
|
---|
111 | painter->setUserEffect(m_effect);
|
---|
112 | else
|
---|
113 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
114 |
|
---|
115 | // Mark the object for object picking purposes.
|
---|
116 | int prevObjectId = painter->objectPickId();
|
---|
117 | if (m_objectId != -1)
|
---|
118 | painter->setObjectPickId(m_objectId);
|
---|
119 |
|
---|
120 | // Draw the geometry mesh.
|
---|
121 | if (m_GLMoleculeObject)
|
---|
122 | m_GLMoleculeObject->draw(painter);
|
---|
123 | else
|
---|
124 | m_mesh->draw(painter);
|
---|
125 |
|
---|
126 | QVector3DArray array;
|
---|
127 | qreal radius = 0.3f;
|
---|
128 | array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
|
---|
129 | array.append( radius, -radius, -radius); array.append( radius, radius, -radius);
|
---|
130 | array.append( radius, radius, -radius); array.append(-radius, radius, -radius);
|
---|
131 | array.append(-radius, radius, -radius); array.append(-radius, -radius, -radius);
|
---|
132 |
|
---|
133 | array.append(-radius, -radius, radius); array.append( radius, -radius, radius);
|
---|
134 | array.append( radius, -radius, radius); array.append( radius, radius, radius);
|
---|
135 | array.append( radius, radius, radius); array.append(-radius, radius, radius);
|
---|
136 | array.append(-radius, radius, radius); array.append(-radius, -radius, radius);
|
---|
137 |
|
---|
138 | array.append(-radius, -radius, -radius); array.append(-radius, -radius, radius);
|
---|
139 | array.append( radius, -radius, -radius); array.append( radius, -radius, radius);
|
---|
140 | array.append(-radius, radius, -radius); array.append(-radius, radius, radius);
|
---|
141 | array.append( radius, radius, -radius); array.append( radius, radius, radius);
|
---|
142 | painter->clearAttributes();
|
---|
143 | painter->setVertexAttribute(QGL::Position, array);
|
---|
144 | painter->draw(QGL::Lines, 24);
|
---|
145 |
|
---|
146 | // Turn off the user effect, if present.
|
---|
147 | if (m_effect)
|
---|
148 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
149 |
|
---|
150 | // Revert to the previous object identifier.
|
---|
151 | painter->setObjectPickId(prevObjectId);
|
---|
152 |
|
---|
153 | // Restore the modelview matrix.
|
---|
154 | painter->modelViewMatrix().pop();
|
---|
155 | }
|
---|
156 |
|
---|
157 | bool GLMoleculeObject::event(QEvent *e)
|
---|
158 | {
|
---|
159 | // Convert the raw event into a signal representing the user's action.
|
---|
160 | if (e->type() == QEvent::MouseButtonPress) {
|
---|
161 | QMouseEvent *me = (QMouseEvent *)e;
|
---|
162 | if (me->button() == Qt::LeftButton)
|
---|
163 | emit pressed();
|
---|
164 | } else if (e->type() == QEvent::MouseButtonRelease) {
|
---|
165 | QMouseEvent *me = (QMouseEvent *)e;
|
---|
166 | if (me->button() == Qt::LeftButton) {
|
---|
167 | emit released();
|
---|
168 | if (me->x() >= 0) // Positive: inside object, Negative: outside.
|
---|
169 | emit clicked();
|
---|
170 | }
|
---|
171 | } else if (e->type() == QEvent::MouseButtonDblClick) {
|
---|
172 | emit doubleClicked();
|
---|
173 | } else if (e->type() == QEvent::Enter) {
|
---|
174 | m_hovering = true;
|
---|
175 | emit hoverChanged();
|
---|
176 | } else if (e->type() == QEvent::Leave) {
|
---|
177 | m_hovering = false;
|
---|
178 | emit hoverChanged();
|
---|
179 | }
|
---|
180 | return QObject::event(e);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Returns the ref to the Material for element No \a from the map.
|
---|
184 | *
|
---|
185 | * \note We create a new one if the element is missing.
|
---|
186 | *
|
---|
187 | * @param no element no
|
---|
188 | * @return ref to QGLMaterial
|
---|
189 | */
|
---|
190 | QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
|
---|
191 | {
|
---|
192 | ASSERT( (no >= 0) && (no < MAX_ELEMENTS) || (no == 65536),
|
---|
193 | "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
|
---|
194 | if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
|
---|
195 | // get present one
|
---|
196 | return ElementNoMaterialMap[no];
|
---|
197 | } else {
|
---|
198 | // create new one
|
---|
199 | LOG(1, "Creating new material for element "+toString(no)+".");
|
---|
200 | QGLMaterial *newmaterial = new QGLMaterial(NULL);
|
---|
201 |
|
---|
202 | if (no == 0) { // create hover material
|
---|
203 | newmaterial->setAmbientColor( QColor(0, 128, 128) );
|
---|
204 | } else if (no == 65536) { // create selection material
|
---|
205 | newmaterial->setAmbientColor( QColor(200, 200, 200) );
|
---|
206 | } else { // create material for element
|
---|
207 | periodentafel *periode = World::getInstance().getPeriode();
|
---|
208 | const element *desiredelement = periode->FindElement(no);
|
---|
209 | ASSERT(desiredelement != NULL,
|
---|
210 | "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
|
---|
211 | const unsigned char* color = desiredelement->getColor();
|
---|
212 | LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
|
---|
213 | newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
|
---|
214 | }
|
---|
215 | newmaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
216 | newmaterial->setShininess( 128 );
|
---|
217 | ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
|
---|
218 |
|
---|
219 | return newmaterial;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Static function to be called when Materials have to be removed.
|
---|
224 | *
|
---|
225 | */
|
---|
226 | void GLMoleculeObject::cleanMaterialMap()
|
---|
227 | {
|
---|
228 | for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
|
---|
229 | !ElementNoMaterialMap.empty();
|
---|
230 | iter = ElementNoMaterialMap.begin()) {
|
---|
231 | delete iter->second;
|
---|
232 | ElementNoMaterialMap.erase(iter);
|
---|
233 | }
|
---|
234 | }
|
---|