| 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 | 
 | 
|---|
| 64 | 
 | 
|---|
| 65 | double GLMoleculeObject::detailMinDistance[GLMoleculeObject::DETAILTYPES_MAX] = {0, 15, 30, 42};
 | 
|---|
| 66 | 
 | 
|---|
| 67 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh[], QObject *parent)
 | 
|---|
| 68 |    : QObject(parent)
 | 
|---|
| 69 | {
 | 
|---|
| 70 |    //mesh->setParent(this);
 | 
|---|
| 71 |    for (int i=0;i<DETAILTYPES_MAX;i++)
 | 
|---|
| 72 |      m_mesh[i] = mesh[i];
 | 
|---|
| 73 |    m_scaleX = 1.0f;
 | 
|---|
| 74 |    m_scaleY = 1.0f;
 | 
|---|
| 75 |    m_scaleZ = 1.0f;
 | 
|---|
| 76 |    m_rotationAngle = 0.0f;
 | 
|---|
| 77 |    m_effect = 0;
 | 
|---|
| 78 |    m_objectId = -1;
 | 
|---|
| 79 |    m_hovering = false;
 | 
|---|
| 80 |    m_selected = false;
 | 
|---|
| 81 |    m_material = 0;
 | 
|---|
| 82 |    initStaticMaterials();
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 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;
 | 
|---|
| 91 |    m_scaleX = 1.0f;
 | 
|---|
| 92 |    m_scaleY = 1.0f;
 | 
|---|
| 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 | 
 | 
|---|
| 103 | GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
 | 
|---|
| 104 |    : QObject(parent)
 | 
|---|
| 105 | {
 | 
|---|
| 106 |    scene->setParent(this);
 | 
|---|
| 107 |    for (int i=0;i<DETAILTYPES_MAX;i++)
 | 
|---|
| 108 |      m_mesh[i] = scene->mainNode();
 | 
|---|
| 109 |    m_scaleX = 1.0f;
 | 
|---|
| 110 |    m_scaleY = 1.0f;
 | 
|---|
| 111 |    m_scaleZ = 1.0f;
 | 
|---|
| 112 |    m_rotationAngle = 0.0f;
 | 
|---|
| 113 |    m_effect = 0;
 | 
|---|
| 114 |    m_objectId = -1;
 | 
|---|
| 115 |    m_hovering = false;
 | 
|---|
| 116 |    m_selected = false;
 | 
|---|
| 117 |    m_material = 0;
 | 
|---|
| 118 |    initStaticMaterials();
 | 
|---|
| 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 | 
 | 
|---|
| 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;
 | 
|---|
| 140 |   qreal radius = 1.0;
 | 
|---|
| 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 | 
 | 
|---|
| 160 | void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane)
 | 
|---|
| 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)
 | 
|---|
| 166 |      painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
 | 
|---|
| 167 |    painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
 | 
|---|
| 168 | 
 | 
|---|
| 169 |    // Apply the material and effect to the painter.
 | 
|---|
| 170 |    QGLMaterial *material;
 | 
|---|
| 171 |    if (m_hovering)
 | 
|---|
| 172 |        material = m_hoverMaterial;
 | 
|---|
| 173 |    else if (m_selected)
 | 
|---|
| 174 |        material = m_selectionMaterial;
 | 
|---|
| 175 |    else
 | 
|---|
| 176 |        material = m_material;
 | 
|---|
| 177 | 
 | 
|---|
| 178 |    ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
 | 
|---|
| 179 | 
 | 
|---|
| 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.
 | 
|---|
| 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);
 | 
|---|
| 204 | 
 | 
|---|
| 205 |    // Draw a box around the mesh, if selected.
 | 
|---|
| 206 |    if (m_selected)
 | 
|---|
| 207 |      drawSelectionBox(painter);
 | 
|---|
| 208 | 
 | 
|---|
| 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;
 | 
|---|
| 238 |        emit hoverChanged(this);
 | 
|---|
| 239 |    } else if (e->type() == QEvent::Leave) {
 | 
|---|
| 240 |        m_hovering = false;
 | 
|---|
| 241 |        emit hoverChanged(NULL);
 | 
|---|
| 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 | {
 | 
|---|
| 255 |   ASSERT( (no > 0) && (no < MAX_ELEMENTS),
 | 
|---|
| 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 | 
 | 
|---|
| 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]) );
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | }
 | 
|---|
| 318 | 
 | 
|---|
| 319 | 
 | 
|---|
| 320 | void GLMoleculeObject::setSelected(bool value)
 | 
|---|
| 321 | {
 | 
|---|
| 322 |   if (value != m_selected){
 | 
|---|
| 323 |     m_selected = value;
 | 
|---|
| 324 |     emit selectionChanged();
 | 
|---|
| 325 |   }
 | 
|---|
| 326 | }
 | 
|---|