| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| 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/>. | 
|---|
| 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" | 
|---|
| 51 | #include "Element/element.hpp" | 
|---|
| 52 | #include "Element/periodentafel.hpp" | 
|---|
| 53 | #include "World.hpp" | 
|---|
| 54 |  | 
|---|
| 55 | GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap; | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| 59 |  | 
|---|
| 60 | QGLMaterial *GLMoleculeObject::m_hoverMaterial = NULL; | 
|---|
| 61 | QGLMaterial *GLMoleculeObject::m_selectionMaterial = NULL; | 
|---|
| 62 | QGLMaterial *GLMoleculeObject::m_selectionBoxMaterial = NULL; | 
|---|
| 63 | QGLMaterial *GLMoleculeObject::m_velocityMaterial = NULL; | 
|---|
| 64 | QGLMaterial *GLMoleculeObject::m_forceMaterial = NULL; | 
|---|
| 65 |  | 
|---|
| 66 | QGLSceneNode *GLMoleculeObject::meshEmpty[GLMoleculeObject::DETAILTYPES_MAX]; | 
|---|
| 67 | QGLSceneNode *GLMoleculeObject::meshSphere[GLMoleculeObject::DETAILTYPES_MAX]; | 
|---|
| 68 | QGLSceneNode *GLMoleculeObject::meshCylinder[GLMoleculeObject::DETAILTYPES_MAX]; | 
|---|
| 69 | QGLSceneNode *GLMoleculeObject::meshArrow[GLMoleculeObject::DETAILTYPES_MAX]; | 
|---|
| 70 |  | 
|---|
| 71 | double GLMoleculeObject::detailMinDistance[GLMoleculeObject::DETAILTYPES_MAX] = {0, 15, 30, 42}; | 
|---|
| 72 |  | 
|---|
| 73 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh[], QObject *parent) | 
|---|
| 74 | : QObject(parent) | 
|---|
| 75 | { | 
|---|
| 76 | //mesh->setParent(this); | 
|---|
| 77 | for (int i=0;i<DETAILTYPES_MAX;i++) | 
|---|
| 78 | m_mesh[i] = mesh[i]; | 
|---|
| 79 | MeshFreeType = free_none; | 
|---|
| 80 | m_scaleX = 1.0f; | 
|---|
| 81 | m_scaleY = 1.0f; | 
|---|
| 82 | m_scaleZ = 1.0f; | 
|---|
| 83 | m_rotationAngle = 0.0f; | 
|---|
| 84 | m_effect = 0; | 
|---|
| 85 | m_objectId = -1; | 
|---|
| 86 | m_hovering = false; | 
|---|
| 87 | m_selected = false; | 
|---|
| 88 | m_visible = true; | 
|---|
| 89 | m_material = 0; | 
|---|
| 90 | initStaticMaterials(); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh, QObject *parent) | 
|---|
| 94 | : QObject(parent) | 
|---|
| 95 | { | 
|---|
| 96 | //mesh->setParent(this); | 
|---|
| 97 | for (int i=0;i<DETAILTYPES_MAX;i++) | 
|---|
| 98 | m_mesh[i] = mesh; | 
|---|
| 99 | MeshFreeType = free_none; | 
|---|
| 100 | m_scaleX = 1.0f; | 
|---|
| 101 | m_scaleY = 1.0f; | 
|---|
| 102 | m_scaleZ = 1.0f; | 
|---|
| 103 | m_rotationAngle = 0.0f; | 
|---|
| 104 | m_effect = 0; | 
|---|
| 105 | m_objectId = -1; | 
|---|
| 106 | m_hovering = false; | 
|---|
| 107 | m_selected = false; | 
|---|
| 108 | m_material = 0; | 
|---|
| 109 | initStaticMaterials(); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent) | 
|---|
| 113 | : QObject(parent) | 
|---|
| 114 | { | 
|---|
| 115 | for (int i=0;i<DETAILTYPES_MAX;i++) | 
|---|
| 116 | m_mesh[i] = scene->mainNode(); | 
|---|
| 117 | MeshFreeType = free_none; | 
|---|
| 118 | m_scaleX = 1.0f; | 
|---|
| 119 | m_scaleY = 1.0f; | 
|---|
| 120 | m_scaleZ = 1.0f; | 
|---|
| 121 | m_rotationAngle = 0.0f; | 
|---|
| 122 | m_effect = 0; | 
|---|
| 123 | m_objectId = -1; | 
|---|
| 124 | m_hovering = false; | 
|---|
| 125 | m_selected = false; | 
|---|
| 126 | m_material = 0; | 
|---|
| 127 | initStaticMaterials(); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | GLMoleculeObject::~GLMoleculeObject() | 
|---|
| 131 | { | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter) | 
|---|
| 135 | { | 
|---|
| 136 | Q_UNUSED(painter); | 
|---|
| 137 | if (m_objectId != -1) | 
|---|
| 138 | view->registerObject(m_objectId, this); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 |  | 
|---|
| 142 | /** Draws a box around the mesh. | 
|---|
| 143 | * | 
|---|
| 144 | */ | 
|---|
| 145 | void GLMoleculeObject::drawSelectionBox(QGLPainter *painter) | 
|---|
| 146 | { | 
|---|
| 147 | painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial); | 
|---|
| 148 | QVector3DArray array; | 
|---|
| 149 | qreal radius = 1.0; | 
|---|
| 150 | array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius); | 
|---|
| 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 |  | 
|---|
| 155 | array.append(-radius, -radius,  radius); array.append( radius, -radius,  radius); | 
|---|
| 156 | array.append( radius, -radius,  radius); array.append( radius,  radius,  radius); | 
|---|
| 157 | array.append( radius,  radius,  radius); array.append(-radius,  radius,  radius); | 
|---|
| 158 | array.append(-radius,  radius,  radius); array.append(-radius, -radius,  radius); | 
|---|
| 159 |  | 
|---|
| 160 | array.append(-radius, -radius, -radius); array.append(-radius, -radius,  radius); | 
|---|
| 161 | array.append( radius, -radius, -radius); array.append( radius, -radius,  radius); | 
|---|
| 162 | array.append(-radius,  radius, -radius); array.append(-radius,  radius,  radius); | 
|---|
| 163 | array.append( radius,  radius, -radius); array.append( radius,  radius,  radius); | 
|---|
| 164 | painter->clearAttributes(); | 
|---|
| 165 | painter->setVertexAttribute(QGL::Position, array); | 
|---|
| 166 | painter->draw(QGL::Lines, 24); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane) | 
|---|
| 170 | { | 
|---|
| 171 | // Position the model at its designated position, scale, and orientation. | 
|---|
| 172 | painter->modelViewMatrix().push(); | 
|---|
| 173 | painter->modelViewMatrix().translate(m_position); | 
|---|
| 174 | if (m_rotationAngle != 0.0f) | 
|---|
| 175 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector); | 
|---|
| 176 | if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f)) | 
|---|
| 177 | painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ); | 
|---|
| 178 |  | 
|---|
| 179 | // Apply the material and effect to the painter. | 
|---|
| 180 | QGLMaterial *material; | 
|---|
| 181 | if (m_hovering) | 
|---|
| 182 | material = m_hoverMaterial; | 
|---|
| 183 | else if (m_selected) | 
|---|
| 184 | material = m_selectionMaterial; | 
|---|
| 185 | else | 
|---|
| 186 | material = m_material; | 
|---|
| 187 |  | 
|---|
| 188 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL"); | 
|---|
| 189 |  | 
|---|
| 190 | painter->setColor(material->diffuseColor()); | 
|---|
| 191 | painter->setFaceMaterial(QGL::AllFaces, material); | 
|---|
| 192 | if (m_effect) | 
|---|
| 193 | painter->setUserEffect(m_effect); | 
|---|
| 194 | else | 
|---|
| 195 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
| 196 |  | 
|---|
| 197 | // Mark the object for object picking purposes. | 
|---|
| 198 | int prevObjectId = painter->objectPickId(); | 
|---|
| 199 | if (m_objectId != -1) | 
|---|
| 200 | painter->setObjectPickId(m_objectId); | 
|---|
| 201 |  | 
|---|
| 202 | // Draw the geometry mesh. | 
|---|
| 203 | QVector4D pos4d(m_position, -1); | 
|---|
| 204 | qreal distance = QVector4D::dotProduct(cameraPlane, pos4d); | 
|---|
| 205 |  | 
|---|
| 206 | if (distance > detailMinDistance[DETAIL_LOW]) | 
|---|
| 207 | m_mesh[DETAIL_LOW]->draw(painter); | 
|---|
| 208 | else if (distance > detailMinDistance[DETAIL_MEDIUM]) | 
|---|
| 209 | m_mesh[DETAIL_MEDIUM]->draw(painter); | 
|---|
| 210 | else if (distance > detailMinDistance[DETAIL_HIGH]) | 
|---|
| 211 | m_mesh[DETAIL_HIGH]->draw(painter); | 
|---|
| 212 | else if (distance > detailMinDistance[DETAIL_HIGHEST]) | 
|---|
| 213 | m_mesh[DETAIL_HIGHEST]->draw(painter); | 
|---|
| 214 |  | 
|---|
| 215 | // Draw a box around the mesh, if selected. | 
|---|
| 216 | if (m_selected) | 
|---|
| 217 | drawSelectionBox(painter); | 
|---|
| 218 |  | 
|---|
| 219 | // Turn off the user effect, if present. | 
|---|
| 220 | if (m_effect) | 
|---|
| 221 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
| 222 |  | 
|---|
| 223 | // Revert to the previous object identifier. | 
|---|
| 224 | painter->setObjectPickId(prevObjectId); | 
|---|
| 225 |  | 
|---|
| 226 | // Restore the modelview matrix. | 
|---|
| 227 | painter->modelViewMatrix().pop(); | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | bool GLMoleculeObject::event(QEvent *e) | 
|---|
| 231 | { | 
|---|
| 232 | // Convert the raw event into a signal representing the user's action. | 
|---|
| 233 | if (e->type() == QEvent::MouseButtonPress) { | 
|---|
| 234 | QMouseEvent *me = (QMouseEvent *)e; | 
|---|
| 235 | if (me->button() == Qt::LeftButton) | 
|---|
| 236 | emit pressed(); | 
|---|
| 237 | } else if (e->type() == QEvent::MouseButtonRelease) { | 
|---|
| 238 | QMouseEvent *me = (QMouseEvent *)e; | 
|---|
| 239 | if (me->button() == Qt::LeftButton) { | 
|---|
| 240 | emit released(); | 
|---|
| 241 | if (me->x() >= 0)   // Positive: inside object, Negative: outside. | 
|---|
| 242 | emit clicked(); | 
|---|
| 243 | } | 
|---|
| 244 | } else if (e->type() == QEvent::MouseButtonDblClick) { | 
|---|
| 245 | emit doubleClicked(); | 
|---|
| 246 | } else if (e->type() == QEvent::Enter) { | 
|---|
| 247 | m_hovering = true; | 
|---|
| 248 | emit hoverChanged(this); | 
|---|
| 249 | } else if (e->type() == QEvent::Leave) { | 
|---|
| 250 | m_hovering = false; | 
|---|
| 251 | emit hoverChanged(NULL); | 
|---|
| 252 | } | 
|---|
| 253 | return QObject::event(e); | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | /** Returns the ref to the Material for element No \a from the map. | 
|---|
| 257 | * | 
|---|
| 258 | * \note We create a new one if the element is missing. | 
|---|
| 259 | * | 
|---|
| 260 | * @param no element no | 
|---|
| 261 | * @return ref to QGLMaterial | 
|---|
| 262 | */ | 
|---|
| 263 | QGLMaterial* GLMoleculeObject::getMaterial(size_t no) | 
|---|
| 264 | { | 
|---|
| 265 | ASSERT( (no > 0) && (no < MAX_ELEMENTS), | 
|---|
| 266 | "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid."); | 
|---|
| 267 | if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){ | 
|---|
| 268 | // get present one | 
|---|
| 269 | return ElementNoMaterialMap[no]; | 
|---|
| 270 | } else { | 
|---|
| 271 | // create new one | 
|---|
| 272 | LOG(1, "Creating new material for element "+toString(no)+"."); | 
|---|
| 273 | QGLMaterial *newmaterial = new QGLMaterial(NULL); | 
|---|
| 274 |  | 
|---|
| 275 | // create material for element | 
|---|
| 276 | periodentafel *periode = World::getInstance().getPeriode(); | 
|---|
| 277 | const element *desiredelement = periode->FindElement(no); | 
|---|
| 278 | ASSERT(desiredelement != NULL, | 
|---|
| 279 | "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel."); | 
|---|
| 280 | const unsigned char* color = desiredelement->getColor(); | 
|---|
| 281 | LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << "."); | 
|---|
| 282 | newmaterial->setColor( QColor((int)color[0], (int)color[1], (int)color[2]) ); | 
|---|
| 283 | //newmaterial->setSpecularColor( QColor(60, 60, 60) ); | 
|---|
| 284 | //newmaterial->setShininess( 64 ); | 
|---|
| 285 | ElementNoMaterialMap.insert( make_pair(no, newmaterial) ); | 
|---|
| 286 |  | 
|---|
| 287 | return newmaterial; | 
|---|
| 288 | } | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | /** Create the 3 materials shared by all objects. | 
|---|
| 292 | * | 
|---|
| 293 | */ | 
|---|
| 294 | void GLMoleculeObject::initStaticMaterials() | 
|---|
| 295 | { | 
|---|
| 296 | if (!m_hoverMaterial){ | 
|---|
| 297 | m_hoverMaterial = new QGLMaterial(NULL); | 
|---|
| 298 | m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) ); | 
|---|
| 299 | m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) ); | 
|---|
| 300 | m_hoverMaterial->setShininess( 128 ); | 
|---|
| 301 | } | 
|---|
| 302 | if (!m_selectionMaterial){ | 
|---|
| 303 | m_selectionMaterial = new QGLMaterial(NULL); | 
|---|
| 304 | m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) ); | 
|---|
| 305 | m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) ); | 
|---|
| 306 | m_selectionMaterial->setShininess( 128 ); | 
|---|
| 307 | } | 
|---|
| 308 | if (!m_selectionBoxMaterial){ | 
|---|
| 309 | m_selectionBoxMaterial = new QGLMaterial(NULL); | 
|---|
| 310 | m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) ); | 
|---|
| 311 | m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) ); | 
|---|
| 312 | m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) ); | 
|---|
| 313 | } | 
|---|
| 314 | if (!m_velocityMaterial){ | 
|---|
| 315 | m_velocityMaterial = new QGLMaterial(NULL); | 
|---|
| 316 | QColor colorvelocity(50,50,255,255); | 
|---|
| 317 | m_velocityMaterial->setColor(colorvelocity); | 
|---|
| 318 | } | 
|---|
| 319 | if (!m_forceMaterial){ | 
|---|
| 320 | m_forceMaterial = new QGLMaterial(NULL); | 
|---|
| 321 | QColor colorforce(50,255,50,255); | 
|---|
| 322 | m_forceMaterial->setColor(colorforce); | 
|---|
| 323 | } | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | /** Static function to be called when Materials have to be removed. | 
|---|
| 327 | * | 
|---|
| 328 | */ | 
|---|
| 329 | void GLMoleculeObject::cleanMaterialMap() | 
|---|
| 330 | { | 
|---|
| 331 | for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin(); | 
|---|
| 332 | !ElementNoMaterialMap.empty(); | 
|---|
| 333 | iter = ElementNoMaterialMap.begin()) { | 
|---|
| 334 | delete iter->second; | 
|---|
| 335 | ElementNoMaterialMap.erase(iter); | 
|---|
| 336 | } | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 |  | 
|---|
| 340 | void GLMoleculeObject::setSelected(bool value) | 
|---|
| 341 | { | 
|---|
| 342 | if (value != m_selected){ | 
|---|
| 343 | m_selected = value; | 
|---|
| 344 | emit selectionChanged(); | 
|---|
| 345 | } | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 | void GLMoleculeObject::setVisible(bool value) | 
|---|
| 349 | { | 
|---|
| 350 | if (value != m_visible){ | 
|---|
| 351 | m_visible = value; | 
|---|
| 352 | emit changed(); | 
|---|
| 353 | } | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | void GLMoleculeObject::updateMesh(QGLSceneNode *mesh) | 
|---|
| 357 | { | 
|---|
| 358 | if (m_mesh[0] != NULL) { | 
|---|
| 359 | switch(MeshFreeType) { | 
|---|
| 360 | case free_all: | 
|---|
| 361 | for (int i=0;i<DETAILTYPES_MAX;i++) | 
|---|
| 362 | delete m_mesh[i]; | 
|---|
| 363 | break; | 
|---|
| 364 | case free_single: | 
|---|
| 365 | delete m_mesh[0]; | 
|---|
| 366 | break; | 
|---|
| 367 | default: | 
|---|
| 368 | case free_none: | 
|---|
| 369 | break; | 
|---|
| 370 | } | 
|---|
| 371 | } | 
|---|
| 372 | MeshFreeType = free_single; | 
|---|
| 373 | for (int i=0;i<DETAILTYPES_MAX;i++) | 
|---|
| 374 | m_mesh[i] = mesh; | 
|---|
| 375 | } | 
|---|