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