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