| [907636] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [5aaa43] | 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| [94d5ac6] | 6 | * | 
|---|
|  | 7 | * | 
|---|
|  | 8 | *   This file is part of MoleCuilder. | 
|---|
|  | 9 | * | 
|---|
|  | 10 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
|  | 11 | *    it under the terms of the GNU General Public License as published by | 
|---|
|  | 12 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
|  | 13 | *    (at your option) any later version. | 
|---|
|  | 14 | * | 
|---|
|  | 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
|  | 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 18 | *    GNU General Public License for more details. | 
|---|
|  | 19 | * | 
|---|
|  | 20 | *    You should have received a copy of the GNU General Public License | 
|---|
|  | 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| [907636] | 22 | */ | 
|---|
|  | 23 |  | 
|---|
|  | 24 | /* | 
|---|
|  | 25 | * GLMoleculeObject.cpp | 
|---|
|  | 26 | * | 
|---|
|  | 27 | *  This is based on the Qt3D example "teaservice", specifically meshobject.cpp. | 
|---|
|  | 28 | * | 
|---|
|  | 29 | *  Created on: Aug 17, 2011 | 
|---|
|  | 30 | *      Author: heber | 
|---|
|  | 31 | */ | 
|---|
|  | 32 |  | 
|---|
|  | 33 | // include config.h | 
|---|
|  | 34 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 35 | #include <config.h> | 
|---|
|  | 36 | #endif | 
|---|
|  | 37 |  | 
|---|
|  | 38 | #include "GLMoleculeObject.hpp" | 
|---|
|  | 39 |  | 
|---|
|  | 40 | #include <Qt3D/qglview.h> | 
|---|
|  | 41 | #include <Qt3D/qglscenenode.h> | 
|---|
|  | 42 | #include <Qt3D/qglpainter.h> | 
|---|
|  | 43 | #include <Qt3D/qglmaterial.h> | 
|---|
|  | 44 |  | 
|---|
|  | 45 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 46 |  | 
|---|
|  | 47 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 48 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 49 |  | 
|---|
|  | 50 | #include "Helpers/defs.hpp" | 
|---|
| [3bdb6d] | 51 | #include "Element/element.hpp" | 
|---|
|  | 52 | #include "Element/periodentafel.hpp" | 
|---|
| [907636] | 53 | #include "World.hpp" | 
|---|
|  | 54 |  | 
|---|
|  | 55 | GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap; | 
|---|
|  | 56 |  | 
|---|
|  | 57 |  | 
|---|
|  | 58 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 59 |  | 
|---|
| [3b229e] | 60 | QGLMaterial *GLMoleculeObject::m_hoverMaterial = NULL; | 
|---|
|  | 61 | QGLMaterial *GLMoleculeObject::m_selectionMaterial = NULL; | 
|---|
|  | 62 | QGLMaterial *GLMoleculeObject::m_selectionBoxMaterial = NULL; | 
|---|
|  | 63 |  | 
|---|
| [907636] | 64 |  | 
|---|
| [72a4c1] | 65 | double GLMoleculeObject::detailMinDistance[GLMoleculeObject::DETAILTYPES_MAX] = {0, 15, 30, 42}; | 
|---|
|  | 66 |  | 
|---|
|  | 67 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh[], QObject *parent) | 
|---|
| [907636] | 68 | : QObject(parent) | 
|---|
|  | 69 | { | 
|---|
| [bca99d] | 70 | //mesh->setParent(this); | 
|---|
| [72a4c1] | 71 | for (int i=0;i<DETAILTYPES_MAX;i++) | 
|---|
|  | 72 | m_mesh[i] = mesh[i]; | 
|---|
| [2d5137] | 73 | m_scaleX = 1.0f; | 
|---|
|  | 74 | m_scaleY = 1.0f; | 
|---|
| [fbb1f1] | 75 | m_scaleZ = 1.0f; | 
|---|
| [907636] | 76 | m_rotationAngle = 0.0f; | 
|---|
|  | 77 | m_effect = 0; | 
|---|
|  | 78 | m_objectId = -1; | 
|---|
|  | 79 | m_hovering = false; | 
|---|
| [8a77ac] | 80 | m_selected = false; | 
|---|
| [907636] | 81 | m_material = 0; | 
|---|
| [3b229e] | 82 | initStaticMaterials(); | 
|---|
| [907636] | 83 | } | 
|---|
|  | 84 |  | 
|---|
| [615eee] | 85 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh, QObject *parent) | 
|---|
|  | 86 | : QObject(parent) | 
|---|
|  | 87 | { | 
|---|
|  | 88 | //mesh->setParent(this); | 
|---|
|  | 89 | for (int i=0;i<DETAILTYPES_MAX;i++) | 
|---|
|  | 90 | m_mesh[i] = mesh; | 
|---|
| [2d5137] | 91 | m_scaleX = 1.0f; | 
|---|
|  | 92 | m_scaleY = 1.0f; | 
|---|
| [615eee] | 93 | m_scaleZ = 1.0f; | 
|---|
|  | 94 | m_rotationAngle = 0.0f; | 
|---|
|  | 95 | m_effect = 0; | 
|---|
|  | 96 | m_objectId = -1; | 
|---|
|  | 97 | m_hovering = false; | 
|---|
|  | 98 | m_selected = false; | 
|---|
|  | 99 | m_material = 0; | 
|---|
|  | 100 | initStaticMaterials(); | 
|---|
|  | 101 | } | 
|---|
|  | 102 |  | 
|---|
| [907636] | 103 | GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent) | 
|---|
|  | 104 | : QObject(parent) | 
|---|
|  | 105 | { | 
|---|
|  | 106 | scene->setParent(this); | 
|---|
| [615eee] | 107 | for (int i=0;i<DETAILTYPES_MAX;i++) | 
|---|
|  | 108 | m_mesh[i] = scene->mainNode(); | 
|---|
| [2d5137] | 109 | m_scaleX = 1.0f; | 
|---|
|  | 110 | m_scaleY = 1.0f; | 
|---|
| [fbb1f1] | 111 | m_scaleZ = 1.0f; | 
|---|
| [907636] | 112 | m_rotationAngle = 0.0f; | 
|---|
|  | 113 | m_effect = 0; | 
|---|
|  | 114 | m_objectId = -1; | 
|---|
|  | 115 | m_hovering = false; | 
|---|
| [8a77ac] | 116 | m_selected = false; | 
|---|
| [907636] | 117 | m_material = 0; | 
|---|
| [3b229e] | 118 | initStaticMaterials(); | 
|---|
| [907636] | 119 | } | 
|---|
|  | 120 |  | 
|---|
|  | 121 | GLMoleculeObject::~GLMoleculeObject() | 
|---|
|  | 122 | { | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
|  | 125 | void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter) | 
|---|
|  | 126 | { | 
|---|
|  | 127 | Q_UNUSED(painter); | 
|---|
|  | 128 | if (m_objectId != -1) | 
|---|
|  | 129 | view->registerObject(m_objectId, this); | 
|---|
|  | 130 | } | 
|---|
|  | 131 |  | 
|---|
| [225cf5] | 132 |  | 
|---|
|  | 133 | /** Draws a box around the mesh. | 
|---|
|  | 134 | * | 
|---|
|  | 135 | */ | 
|---|
|  | 136 | void GLMoleculeObject::drawSelectionBox(QGLPainter *painter) | 
|---|
|  | 137 | { | 
|---|
|  | 138 | painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial); | 
|---|
|  | 139 | QVector3DArray array; | 
|---|
| [3927ef] | 140 | qreal radius = 1.0; | 
|---|
| [225cf5] | 141 | array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius); | 
|---|
|  | 142 | array.append( radius, -radius, -radius); array.append( radius,  radius, -radius); | 
|---|
|  | 143 | array.append( radius,  radius, -radius); array.append(-radius,  radius, -radius); | 
|---|
|  | 144 | array.append(-radius,  radius, -radius); array.append(-radius, -radius, -radius); | 
|---|
|  | 145 |  | 
|---|
|  | 146 | array.append(-radius, -radius,  radius); array.append( radius, -radius,  radius); | 
|---|
|  | 147 | array.append( radius, -radius,  radius); array.append( radius,  radius,  radius); | 
|---|
|  | 148 | array.append( radius,  radius,  radius); array.append(-radius,  radius,  radius); | 
|---|
|  | 149 | array.append(-radius,  radius,  radius); array.append(-radius, -radius,  radius); | 
|---|
|  | 150 |  | 
|---|
|  | 151 | array.append(-radius, -radius, -radius); array.append(-radius, -radius,  radius); | 
|---|
|  | 152 | array.append( radius, -radius, -radius); array.append( radius, -radius,  radius); | 
|---|
|  | 153 | array.append(-radius,  radius, -radius); array.append(-radius,  radius,  radius); | 
|---|
|  | 154 | array.append( radius,  radius, -radius); array.append( radius,  radius,  radius); | 
|---|
|  | 155 | painter->clearAttributes(); | 
|---|
|  | 156 | painter->setVertexAttribute(QGL::Position, array); | 
|---|
|  | 157 | painter->draw(QGL::Lines, 24); | 
|---|
|  | 158 | } | 
|---|
|  | 159 |  | 
|---|
| [72a4c1] | 160 | void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane) | 
|---|
| [907636] | 161 | { | 
|---|
|  | 162 | // Position the model at its designated position, scale, and orientation. | 
|---|
|  | 163 | painter->modelViewMatrix().push(); | 
|---|
|  | 164 | painter->modelViewMatrix().translate(m_position); | 
|---|
|  | 165 | if (m_rotationAngle != 0.0f) | 
|---|
| [2d5137] | 166 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector); | 
|---|
|  | 167 | painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ); | 
|---|
| [907636] | 168 |  | 
|---|
|  | 169 | // Apply the material and effect to the painter. | 
|---|
|  | 170 | QGLMaterial *material; | 
|---|
|  | 171 | if (m_hovering) | 
|---|
|  | 172 | material = m_hoverMaterial; | 
|---|
| [8a77ac] | 173 | else if (m_selected) | 
|---|
|  | 174 | material = m_selectionMaterial; | 
|---|
| [907636] | 175 | else | 
|---|
|  | 176 | material = m_material; | 
|---|
| [3b229e] | 177 |  | 
|---|
|  | 178 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL"); | 
|---|
|  | 179 |  | 
|---|
| [907636] | 180 | painter->setColor(material->diffuseColor()); | 
|---|
|  | 181 | painter->setFaceMaterial(QGL::AllFaces, material); | 
|---|
|  | 182 | if (m_effect) | 
|---|
|  | 183 | painter->setUserEffect(m_effect); | 
|---|
|  | 184 | else | 
|---|
|  | 185 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
|  | 186 |  | 
|---|
|  | 187 | // Mark the object for object picking purposes. | 
|---|
|  | 188 | int prevObjectId = painter->objectPickId(); | 
|---|
|  | 189 | if (m_objectId != -1) | 
|---|
|  | 190 | painter->setObjectPickId(m_objectId); | 
|---|
|  | 191 |  | 
|---|
|  | 192 | // Draw the geometry mesh. | 
|---|
| [72a4c1] | 193 | QVector4D pos4d(m_position, -1); | 
|---|
|  | 194 | qreal distance = QVector4D::dotProduct(cameraPlane, pos4d); | 
|---|
|  | 195 |  | 
|---|
|  | 196 | if (distance > detailMinDistance[DETAIL_LOW]) | 
|---|
|  | 197 | m_mesh[DETAIL_LOW]->draw(painter); | 
|---|
|  | 198 | else if (distance > detailMinDistance[DETAIL_MEDIUM]) | 
|---|
|  | 199 | m_mesh[DETAIL_MEDIUM]->draw(painter); | 
|---|
|  | 200 | else if (distance > detailMinDistance[DETAIL_HIGH]) | 
|---|
|  | 201 | m_mesh[DETAIL_HIGH]->draw(painter); | 
|---|
|  | 202 | else if (distance > detailMinDistance[DETAIL_HIGHEST]) | 
|---|
|  | 203 | m_mesh[DETAIL_HIGHEST]->draw(painter); | 
|---|
| [907636] | 204 |  | 
|---|
| [225cf5] | 205 | // Draw a box around the mesh, if selected. | 
|---|
|  | 206 | if (m_selected) | 
|---|
|  | 207 | drawSelectionBox(painter); | 
|---|
| [8a77ac] | 208 |  | 
|---|
| [907636] | 209 | // Turn off the user effect, if present. | 
|---|
|  | 210 | if (m_effect) | 
|---|
|  | 211 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
|  | 212 |  | 
|---|
|  | 213 | // Revert to the previous object identifier. | 
|---|
|  | 214 | painter->setObjectPickId(prevObjectId); | 
|---|
|  | 215 |  | 
|---|
|  | 216 | // Restore the modelview matrix. | 
|---|
|  | 217 | painter->modelViewMatrix().pop(); | 
|---|
|  | 218 | } | 
|---|
|  | 219 |  | 
|---|
|  | 220 | bool GLMoleculeObject::event(QEvent *e) | 
|---|
|  | 221 | { | 
|---|
|  | 222 | // Convert the raw event into a signal representing the user's action. | 
|---|
|  | 223 | if (e->type() == QEvent::MouseButtonPress) { | 
|---|
|  | 224 | QMouseEvent *me = (QMouseEvent *)e; | 
|---|
|  | 225 | if (me->button() == Qt::LeftButton) | 
|---|
|  | 226 | emit pressed(); | 
|---|
|  | 227 | } else if (e->type() == QEvent::MouseButtonRelease) { | 
|---|
|  | 228 | QMouseEvent *me = (QMouseEvent *)e; | 
|---|
|  | 229 | if (me->button() == Qt::LeftButton) { | 
|---|
|  | 230 | emit released(); | 
|---|
|  | 231 | if (me->x() >= 0)   // Positive: inside object, Negative: outside. | 
|---|
|  | 232 | emit clicked(); | 
|---|
|  | 233 | } | 
|---|
|  | 234 | } else if (e->type() == QEvent::MouseButtonDblClick) { | 
|---|
|  | 235 | emit doubleClicked(); | 
|---|
|  | 236 | } else if (e->type() == QEvent::Enter) { | 
|---|
|  | 237 | m_hovering = true; | 
|---|
| [407638e] | 238 | emit hoverChanged(this); | 
|---|
| [907636] | 239 | } else if (e->type() == QEvent::Leave) { | 
|---|
|  | 240 | m_hovering = false; | 
|---|
| [407638e] | 241 | emit hoverChanged(NULL); | 
|---|
| [907636] | 242 | } | 
|---|
|  | 243 | return QObject::event(e); | 
|---|
|  | 244 | } | 
|---|
|  | 245 |  | 
|---|
|  | 246 | /** Returns the ref to the Material for element No \a from the map. | 
|---|
|  | 247 | * | 
|---|
|  | 248 | * \note We create a new one if the element is missing. | 
|---|
|  | 249 | * | 
|---|
|  | 250 | * @param no element no | 
|---|
|  | 251 | * @return ref to QGLMaterial | 
|---|
|  | 252 | */ | 
|---|
|  | 253 | QGLMaterial* GLMoleculeObject::getMaterial(size_t no) | 
|---|
|  | 254 | { | 
|---|
| [3b229e] | 255 | ASSERT( (no > 0) && (no < MAX_ELEMENTS), | 
|---|
| [907636] | 256 | "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid."); | 
|---|
|  | 257 | if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){ | 
|---|
|  | 258 | // get present one | 
|---|
|  | 259 | return ElementNoMaterialMap[no]; | 
|---|
|  | 260 | } else { | 
|---|
|  | 261 | // create new one | 
|---|
|  | 262 | LOG(1, "Creating new material for element "+toString(no)+"."); | 
|---|
|  | 263 | QGLMaterial *newmaterial = new QGLMaterial(NULL); | 
|---|
|  | 264 |  | 
|---|
| [3b229e] | 265 | // create material for element | 
|---|
|  | 266 | periodentafel *periode = World::getInstance().getPeriode(); | 
|---|
|  | 267 | const element *desiredelement = periode->FindElement(no); | 
|---|
|  | 268 | ASSERT(desiredelement != NULL, | 
|---|
|  | 269 | "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel."); | 
|---|
|  | 270 | const unsigned char* color = desiredelement->getColor(); | 
|---|
|  | 271 | LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << "."); | 
|---|
|  | 272 | newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) ); | 
|---|
| [907636] | 273 | newmaterial->setSpecularColor( QColor(60, 60, 60) ); | 
|---|
|  | 274 | newmaterial->setShininess( 128 ); | 
|---|
|  | 275 | ElementNoMaterialMap.insert( make_pair(no, newmaterial) ); | 
|---|
|  | 276 |  | 
|---|
|  | 277 | return newmaterial; | 
|---|
|  | 278 | } | 
|---|
|  | 279 | } | 
|---|
|  | 280 |  | 
|---|
| [3b229e] | 281 | /** Create the 3 materials shared by all objects. | 
|---|
|  | 282 | * | 
|---|
|  | 283 | */ | 
|---|
|  | 284 | void GLMoleculeObject::initStaticMaterials() | 
|---|
|  | 285 | { | 
|---|
|  | 286 | if (!m_hoverMaterial){ | 
|---|
|  | 287 | m_hoverMaterial = new QGLMaterial(NULL); | 
|---|
|  | 288 | m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) ); | 
|---|
|  | 289 | m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) ); | 
|---|
|  | 290 | m_hoverMaterial->setShininess( 128 ); | 
|---|
|  | 291 | } | 
|---|
|  | 292 | if (!m_selectionMaterial){ | 
|---|
|  | 293 | m_selectionMaterial = new QGLMaterial(NULL); | 
|---|
|  | 294 | m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) ); | 
|---|
|  | 295 | m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) ); | 
|---|
|  | 296 | m_selectionMaterial->setShininess( 128 ); | 
|---|
|  | 297 | } | 
|---|
|  | 298 | if (!m_selectionBoxMaterial){ | 
|---|
|  | 299 | m_selectionBoxMaterial = new QGLMaterial(NULL); | 
|---|
|  | 300 | m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) ); | 
|---|
|  | 301 | m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) ); | 
|---|
|  | 302 | m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) ); | 
|---|
|  | 303 | } | 
|---|
|  | 304 | } | 
|---|
|  | 305 |  | 
|---|
| [907636] | 306 | /** Static function to be called when Materials have to be removed. | 
|---|
|  | 307 | * | 
|---|
|  | 308 | */ | 
|---|
|  | 309 | void GLMoleculeObject::cleanMaterialMap() | 
|---|
|  | 310 | { | 
|---|
|  | 311 | for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin(); | 
|---|
|  | 312 | !ElementNoMaterialMap.empty(); | 
|---|
|  | 313 | iter = ElementNoMaterialMap.begin()) { | 
|---|
|  | 314 | delete iter->second; | 
|---|
|  | 315 | ElementNoMaterialMap.erase(iter); | 
|---|
|  | 316 | } | 
|---|
|  | 317 | } | 
|---|
| [c67518] | 318 |  | 
|---|
|  | 319 |  | 
|---|
|  | 320 | void GLMoleculeObject::setSelected(bool value) | 
|---|
|  | 321 | { | 
|---|
|  | 322 | if (value != m_selected){ | 
|---|
|  | 323 | m_selected = value; | 
|---|
|  | 324 | emit selectionChanged(); | 
|---|
|  | 325 | } | 
|---|
|  | 326 | } | 
|---|