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