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 | QGLMaterial *GLMoleculeObject::m_hoverMaterial = NULL;
|
---|
45 | QGLMaterial *GLMoleculeObject::m_selectionMaterial = NULL;
|
---|
46 | QGLMaterial *GLMoleculeObject::m_selectionBoxMaterial = NULL;
|
---|
47 |
|
---|
48 |
|
---|
49 | double GLMoleculeObject::detailMinDistance[GLMoleculeObject::DETAILTYPES_MAX] = {0, 15, 30, 42};
|
---|
50 |
|
---|
51 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh[], QObject *parent)
|
---|
52 | : QObject(parent)
|
---|
53 | {
|
---|
54 | //mesh->setParent(this);
|
---|
55 | for (int i=0;i<DETAILTYPES_MAX;i++)
|
---|
56 | m_mesh[i] = mesh[i];
|
---|
57 | m_scale = 1.0f;
|
---|
58 | m_scaleZ = 1.0f;
|
---|
59 | m_rotationAngle = 0.0f;
|
---|
60 | m_effect = 0;
|
---|
61 | m_objectId = -1;
|
---|
62 | m_hovering = false;
|
---|
63 | m_selected = false;
|
---|
64 | m_material = 0;
|
---|
65 | initStaticMaterials();
|
---|
66 | }
|
---|
67 |
|
---|
68 | GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
|
---|
69 | : QObject(parent)
|
---|
70 | {
|
---|
71 | scene->setParent(this);
|
---|
72 | m_mesh[0] = scene->mainNode();
|
---|
73 | m_mesh[1] = scene->mainNode();
|
---|
74 | m_mesh[2] = scene->mainNode();
|
---|
75 | m_scale = 1.0f;
|
---|
76 | m_scaleZ = 1.0f;
|
---|
77 | m_rotationAngle = 0.0f;
|
---|
78 | m_effect = 0;
|
---|
79 | m_objectId = -1;
|
---|
80 | m_hovering = false;
|
---|
81 | m_selected = false;
|
---|
82 | m_material = 0;
|
---|
83 | initStaticMaterials();
|
---|
84 | }
|
---|
85 |
|
---|
86 | GLMoleculeObject::~GLMoleculeObject()
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
|
---|
91 | {
|
---|
92 | Q_UNUSED(painter);
|
---|
93 | if (m_objectId != -1)
|
---|
94 | view->registerObject(m_objectId, this);
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /** Draws a box around the mesh.
|
---|
99 | *
|
---|
100 | */
|
---|
101 | void GLMoleculeObject::drawSelectionBox(QGLPainter *painter)
|
---|
102 | {
|
---|
103 | painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial);
|
---|
104 | QVector3DArray array;
|
---|
105 | qreal radius = 1.0;
|
---|
106 | array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
|
---|
107 | array.append( radius, -radius, -radius); array.append( radius, radius, -radius);
|
---|
108 | array.append( radius, radius, -radius); array.append(-radius, radius, -radius);
|
---|
109 | array.append(-radius, radius, -radius); array.append(-radius, -radius, -radius);
|
---|
110 |
|
---|
111 | array.append(-radius, -radius, radius); array.append( radius, -radius, radius);
|
---|
112 | array.append( radius, -radius, radius); array.append( radius, radius, radius);
|
---|
113 | array.append( radius, radius, radius); array.append(-radius, radius, radius);
|
---|
114 | array.append(-radius, radius, radius); array.append(-radius, -radius, radius);
|
---|
115 |
|
---|
116 | array.append(-radius, -radius, -radius); array.append(-radius, -radius, radius);
|
---|
117 | array.append( radius, -radius, -radius); array.append( radius, -radius, radius);
|
---|
118 | array.append(-radius, radius, -radius); array.append(-radius, radius, radius);
|
---|
119 | array.append( radius, radius, -radius); array.append( radius, radius, radius);
|
---|
120 | painter->clearAttributes();
|
---|
121 | painter->setVertexAttribute(QGL::Position, array);
|
---|
122 | painter->draw(QGL::Lines, 24);
|
---|
123 | }
|
---|
124 |
|
---|
125 | void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane)
|
---|
126 | {
|
---|
127 | // Position the model at its designated position, scale, and orientation.
|
---|
128 | painter->modelViewMatrix().push();
|
---|
129 | painter->modelViewMatrix().translate(m_position);
|
---|
130 | if (m_scale != 1.0f)
|
---|
131 | painter->modelViewMatrix().scale(m_scale);
|
---|
132 | if (m_rotationAngle != 0.0f)
|
---|
133 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
|
---|
134 | if (m_scaleZ != 1.0f)
|
---|
135 | painter->modelViewMatrix().scale(1.0f, 1.0f, m_scaleZ);
|
---|
136 |
|
---|
137 | // Apply the material and effect to the painter.
|
---|
138 | QGLMaterial *material;
|
---|
139 | if (m_hovering)
|
---|
140 | material = m_hoverMaterial;
|
---|
141 | else if (m_selected)
|
---|
142 | material = m_selectionMaterial;
|
---|
143 | else
|
---|
144 | material = m_material;
|
---|
145 |
|
---|
146 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
|
---|
147 |
|
---|
148 | painter->setColor(material->diffuseColor());
|
---|
149 | painter->setFaceMaterial(QGL::AllFaces, material);
|
---|
150 | if (m_effect)
|
---|
151 | painter->setUserEffect(m_effect);
|
---|
152 | else
|
---|
153 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
154 |
|
---|
155 | // Mark the object for object picking purposes.
|
---|
156 | int prevObjectId = painter->objectPickId();
|
---|
157 | if (m_objectId != -1)
|
---|
158 | painter->setObjectPickId(m_objectId);
|
---|
159 |
|
---|
160 | // Draw the geometry mesh.
|
---|
161 | QVector4D pos4d(m_position, -1);
|
---|
162 | qreal distance = QVector4D::dotProduct(cameraPlane, pos4d);
|
---|
163 |
|
---|
164 | if (distance > detailMinDistance[DETAIL_LOW])
|
---|
165 | m_mesh[DETAIL_LOW]->draw(painter);
|
---|
166 | else if (distance > detailMinDistance[DETAIL_MEDIUM])
|
---|
167 | m_mesh[DETAIL_MEDIUM]->draw(painter);
|
---|
168 | else if (distance > detailMinDistance[DETAIL_HIGH])
|
---|
169 | m_mesh[DETAIL_HIGH]->draw(painter);
|
---|
170 | else if (distance > detailMinDistance[DETAIL_HIGHEST])
|
---|
171 | m_mesh[DETAIL_HIGHEST]->draw(painter);
|
---|
172 |
|
---|
173 | // Draw a box around the mesh, if selected.
|
---|
174 | if (m_selected)
|
---|
175 | drawSelectionBox(painter);
|
---|
176 |
|
---|
177 | // Turn off the user effect, if present.
|
---|
178 | if (m_effect)
|
---|
179 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
180 |
|
---|
181 | // Revert to the previous object identifier.
|
---|
182 | painter->setObjectPickId(prevObjectId);
|
---|
183 |
|
---|
184 | // Restore the modelview matrix.
|
---|
185 | painter->modelViewMatrix().pop();
|
---|
186 | }
|
---|
187 |
|
---|
188 | bool GLMoleculeObject::event(QEvent *e)
|
---|
189 | {
|
---|
190 | // Convert the raw event into a signal representing the user's action.
|
---|
191 | if (e->type() == QEvent::MouseButtonPress) {
|
---|
192 | QMouseEvent *me = (QMouseEvent *)e;
|
---|
193 | if (me->button() == Qt::LeftButton)
|
---|
194 | emit pressed();
|
---|
195 | } else if (e->type() == QEvent::MouseButtonRelease) {
|
---|
196 | QMouseEvent *me = (QMouseEvent *)e;
|
---|
197 | if (me->button() == Qt::LeftButton) {
|
---|
198 | emit released();
|
---|
199 | if (me->x() >= 0) // Positive: inside object, Negative: outside.
|
---|
200 | emit clicked();
|
---|
201 | }
|
---|
202 | } else if (e->type() == QEvent::MouseButtonDblClick) {
|
---|
203 | emit doubleClicked();
|
---|
204 | } else if (e->type() == QEvent::Enter) {
|
---|
205 | m_hovering = true;
|
---|
206 | emit hoverChanged(this);
|
---|
207 | } else if (e->type() == QEvent::Leave) {
|
---|
208 | m_hovering = false;
|
---|
209 | emit hoverChanged(NULL);
|
---|
210 | }
|
---|
211 | return QObject::event(e);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Returns the ref to the Material for element No \a from the map.
|
---|
215 | *
|
---|
216 | * \note We create a new one if the element is missing.
|
---|
217 | *
|
---|
218 | * @param no element no
|
---|
219 | * @return ref to QGLMaterial
|
---|
220 | */
|
---|
221 | QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
|
---|
222 | {
|
---|
223 | ASSERT( (no > 0) && (no < MAX_ELEMENTS),
|
---|
224 | "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
|
---|
225 | if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
|
---|
226 | // get present one
|
---|
227 | return ElementNoMaterialMap[no];
|
---|
228 | } else {
|
---|
229 | // create new one
|
---|
230 | LOG(1, "Creating new material for element "+toString(no)+".");
|
---|
231 | QGLMaterial *newmaterial = new QGLMaterial(NULL);
|
---|
232 |
|
---|
233 | // create material for element
|
---|
234 | periodentafel *periode = World::getInstance().getPeriode();
|
---|
235 | const element *desiredelement = periode->FindElement(no);
|
---|
236 | ASSERT(desiredelement != NULL,
|
---|
237 | "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
|
---|
238 | const unsigned char* color = desiredelement->getColor();
|
---|
239 | LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
|
---|
240 | newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
|
---|
241 | newmaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
242 | newmaterial->setShininess( 128 );
|
---|
243 | ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
|
---|
244 |
|
---|
245 | return newmaterial;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | /** Create the 3 materials shared by all objects.
|
---|
250 | *
|
---|
251 | */
|
---|
252 | void GLMoleculeObject::initStaticMaterials()
|
---|
253 | {
|
---|
254 | if (!m_hoverMaterial){
|
---|
255 | m_hoverMaterial = new QGLMaterial(NULL);
|
---|
256 | m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) );
|
---|
257 | m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
258 | m_hoverMaterial->setShininess( 128 );
|
---|
259 | }
|
---|
260 | if (!m_selectionMaterial){
|
---|
261 | m_selectionMaterial = new QGLMaterial(NULL);
|
---|
262 | m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) );
|
---|
263 | m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
264 | m_selectionMaterial->setShininess( 128 );
|
---|
265 | }
|
---|
266 | if (!m_selectionBoxMaterial){
|
---|
267 | m_selectionBoxMaterial = new QGLMaterial(NULL);
|
---|
268 | m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) );
|
---|
269 | m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) );
|
---|
270 | m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) );
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** Static function to be called when Materials have to be removed.
|
---|
275 | *
|
---|
276 | */
|
---|
277 | void GLMoleculeObject::cleanMaterialMap()
|
---|
278 | {
|
---|
279 | for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
|
---|
280 | !ElementNoMaterialMap.empty();
|
---|
281 | iter = ElementNoMaterialMap.begin()) {
|
---|
282 | delete iter->second;
|
---|
283 | ElementNoMaterialMap.erase(iter);
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | void GLMoleculeObject::setSelected(bool value)
|
---|
289 | {
|
---|
290 | if (value != m_selected){
|
---|
291 | m_selected = value;
|
---|
292 | emit selectionChanged();
|
---|
293 | }
|
---|
294 | }
|
---|