| [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 | 
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *GLMoleculeObject, QObject *parent)
 | 
|---|
 | 46 |    : QObject(parent)
 | 
|---|
 | 47 | {
 | 
|---|
 | 48 |    m_mesh = 0;
 | 
|---|
 | 49 |    m_GLMoleculeObject = GLMoleculeObject;
 | 
|---|
 | 50 |    m_scale = 1.0f;
 | 
|---|
 | 51 |    m_rotationAngle = 0.0f;
 | 
|---|
 | 52 |    m_effect = 0;
 | 
|---|
 | 53 |    m_objectId = -1;
 | 
|---|
 | 54 |    m_hovering = false;
 | 
|---|
 | 55 |    m_material = 0;
 | 
|---|
 | 56 |    m_hoverMaterial = 0;
 | 
|---|
 | 57 | }
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
 | 
|---|
 | 60 |    : QObject(parent)
 | 
|---|
 | 61 | {
 | 
|---|
 | 62 |    scene->setParent(this);
 | 
|---|
 | 63 |    m_mesh = 0;
 | 
|---|
 | 64 |    m_GLMoleculeObject = scene->mainNode();
 | 
|---|
 | 65 |    m_scale = 1.0f;
 | 
|---|
 | 66 |    m_rotationAngle = 0.0f;
 | 
|---|
 | 67 |    m_effect = 0;
 | 
|---|
 | 68 |    m_objectId = -1;
 | 
|---|
 | 69 |    m_hovering = false;
 | 
|---|
 | 70 |    m_material = 0;
 | 
|---|
 | 71 |    m_hoverMaterial = 0;
 | 
|---|
 | 72 | }
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 | GLMoleculeObject::~GLMoleculeObject()
 | 
|---|
 | 75 | {
 | 
|---|
 | 76 |    delete m_mesh;
 | 
|---|
 | 77 | }
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 | void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
 | 
|---|
 | 80 | {
 | 
|---|
 | 81 |    Q_UNUSED(painter);
 | 
|---|
 | 82 |    if (m_objectId != -1)
 | 
|---|
 | 83 |        view->registerObject(m_objectId, this);
 | 
|---|
 | 84 | }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | void GLMoleculeObject::draw(QGLPainter *painter)
 | 
|---|
 | 87 | {
 | 
|---|
 | 88 |    // Position the model at its designated position, scale, and orientation.
 | 
|---|
 | 89 |    painter->modelViewMatrix().push();
 | 
|---|
 | 90 |    painter->modelViewMatrix().translate(m_position);
 | 
|---|
 | 91 |    if (m_scale != 1.0f)
 | 
|---|
 | 92 |        painter->modelViewMatrix().scale(m_scale);
 | 
|---|
 | 93 |    if (m_rotationAngle != 0.0f)
 | 
|---|
 | 94 |        painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 |    // Apply the material and effect to the painter.
 | 
|---|
 | 97 |    QGLMaterial *material;
 | 
|---|
 | 98 |    if (m_hovering)
 | 
|---|
 | 99 |        material = m_hoverMaterial;
 | 
|---|
 | 100 |    else
 | 
|---|
 | 101 |        material = m_material;
 | 
|---|
 | 102 |    painter->setColor(material->diffuseColor());
 | 
|---|
 | 103 |    painter->setFaceMaterial(QGL::AllFaces, material);
 | 
|---|
 | 104 |    if (m_effect)
 | 
|---|
 | 105 |        painter->setUserEffect(m_effect);
 | 
|---|
 | 106 |    else
 | 
|---|
 | 107 |        painter->setStandardEffect(QGL::LitMaterial);
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |    // Mark the object for object picking purposes.
 | 
|---|
 | 110 |    int prevObjectId = painter->objectPickId();
 | 
|---|
 | 111 |    if (m_objectId != -1)
 | 
|---|
 | 112 |        painter->setObjectPickId(m_objectId);
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 |    // Draw the geometry mesh.
 | 
|---|
 | 115 |    if (m_GLMoleculeObject)
 | 
|---|
 | 116 |        m_GLMoleculeObject->draw(painter);
 | 
|---|
 | 117 |    else
 | 
|---|
 | 118 |        m_mesh->draw(painter);
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |    // Turn off the user effect, if present.
 | 
|---|
 | 121 |    if (m_effect)
 | 
|---|
 | 122 |        painter->setStandardEffect(QGL::LitMaterial);
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 |    // Revert to the previous object identifier.
 | 
|---|
 | 125 |    painter->setObjectPickId(prevObjectId);
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |    // Restore the modelview matrix.
 | 
|---|
 | 128 |    painter->modelViewMatrix().pop();
 | 
|---|
 | 129 | }
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 | bool GLMoleculeObject::event(QEvent *e)
 | 
|---|
 | 132 | {
 | 
|---|
 | 133 |    // Convert the raw event into a signal representing the user's action.
 | 
|---|
 | 134 |    if (e->type() == QEvent::MouseButtonPress) {
 | 
|---|
 | 135 |        QMouseEvent *me = (QMouseEvent *)e;
 | 
|---|
 | 136 |        if (me->button() == Qt::LeftButton)
 | 
|---|
 | 137 |            emit pressed();
 | 
|---|
 | 138 |    } else if (e->type() == QEvent::MouseButtonRelease) {
 | 
|---|
 | 139 |        QMouseEvent *me = (QMouseEvent *)e;
 | 
|---|
 | 140 |        if (me->button() == Qt::LeftButton) {
 | 
|---|
 | 141 |            emit released();
 | 
|---|
 | 142 |            if (me->x() >= 0)   // Positive: inside object, Negative: outside.
 | 
|---|
 | 143 |                emit clicked();
 | 
|---|
 | 144 |        }
 | 
|---|
 | 145 |    } else if (e->type() == QEvent::MouseButtonDblClick) {
 | 
|---|
 | 146 |        emit doubleClicked();
 | 
|---|
 | 147 |    } else if (e->type() == QEvent::Enter) {
 | 
|---|
 | 148 |        m_hovering = true;
 | 
|---|
 | 149 |        emit hoverChanged();
 | 
|---|
 | 150 |    } else if (e->type() == QEvent::Leave) {
 | 
|---|
 | 151 |        m_hovering = false;
 | 
|---|
 | 152 |        emit hoverChanged();
 | 
|---|
 | 153 |    }
 | 
|---|
 | 154 |    return QObject::event(e);
 | 
|---|
 | 155 | }
 | 
|---|
 | 156 | 
 | 
|---|
 | 157 | /** Returns the ref to the Material for element No \a from the map.
 | 
|---|
 | 158 |  *
 | 
|---|
 | 159 |  * \note We create a new one if the element is missing.
 | 
|---|
 | 160 |  *
 | 
|---|
 | 161 |  * @param no element no
 | 
|---|
 | 162 |  * @return ref to QGLMaterial
 | 
|---|
 | 163 |  */
 | 
|---|
 | 164 | QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
 | 
|---|
 | 165 | {
 | 
|---|
 | 166 |   ASSERT( (no >= 0) && (no < MAX_ELEMENTS),
 | 
|---|
 | 167 |       "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
 | 
|---|
 | 168 |   if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
 | 
|---|
 | 169 |     // get present one
 | 
|---|
 | 170 |     return ElementNoMaterialMap[no];
 | 
|---|
 | 171 |   } else {
 | 
|---|
 | 172 |     // create new one
 | 
|---|
 | 173 |     LOG(1, "Creating new material for element "+toString(no)+".");
 | 
|---|
 | 174 |     QGLMaterial *newmaterial = new QGLMaterial(NULL);
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 |     if (no == 0) { // create hover material
 | 
|---|
 | 177 |       newmaterial->setAmbientColor( QColor(0, 128, 128) );
 | 
|---|
 | 178 |     } else { // create material for element
 | 
|---|
 | 179 |       periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
 | 180 |       const element *desiredelement = periode->FindElement(no);
 | 
|---|
 | 181 |       ASSERT(desiredelement != NULL,
 | 
|---|
 | 182 |           "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
 | 
|---|
 | 183 |       const unsigned char* color = desiredelement->getColor();
 | 
|---|
 | 184 |       LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
 | 
|---|
 | 185 |       newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
 | 
|---|
 | 186 |     }
 | 
|---|
 | 187 |     newmaterial->setSpecularColor( QColor(60, 60, 60) );
 | 
|---|
 | 188 |     newmaterial->setShininess( 128 );
 | 
|---|
 | 189 |     ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 |     return newmaterial;
 | 
|---|
 | 192 |   }
 | 
|---|
 | 193 | }
 | 
|---|
 | 194 | 
 | 
|---|
 | 195 | /** Static function to be called when Materials have to be removed.
 | 
|---|
 | 196 |  *
 | 
|---|
 | 197 |  */
 | 
|---|
 | 198 | void GLMoleculeObject::cleanMaterialMap()
 | 
|---|
 | 199 | {
 | 
|---|
 | 200 |   for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
 | 
|---|
 | 201 |       !ElementNoMaterialMap.empty();
 | 
|---|
 | 202 |       iter = ElementNoMaterialMap.begin()) {
 | 
|---|
 | 203 |     delete iter->second;
 | 
|---|
 | 204 |     ElementNoMaterialMap.erase(iter);
 | 
|---|
 | 205 |   }
 | 
|---|
 | 206 | }
 | 
|---|