[907636] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
[907636] | 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * GLMoleculeObject.cpp
|
---|
| 25 | *
|
---|
| 26 | * This is based on the Qt3D example "teaservice", specifically meshobject.cpp.
|
---|
| 27 | *
|
---|
| 28 | * Created on: Aug 17, 2011
|
---|
| 29 | * Author: heber
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | // include config.h
|
---|
| 33 | #ifdef HAVE_CONFIG_H
|
---|
| 34 | #include <config.h>
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | #include "GLMoleculeObject.hpp"
|
---|
| 38 |
|
---|
| 39 | #include <Qt3D/qglview.h>
|
---|
| 40 | #include <Qt3D/qglscenenode.h>
|
---|
| 41 | #include <Qt3D/qglpainter.h>
|
---|
| 42 | #include <Qt3D/qglmaterial.h>
|
---|
| 43 |
|
---|
| 44 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 45 |
|
---|
| 46 | #include "CodePatterns/Assert.hpp"
|
---|
| 47 | #include "CodePatterns/Log.hpp"
|
---|
| 48 |
|
---|
| 49 | #include "Helpers/defs.hpp"
|
---|
[3bdb6d] | 50 | #include "Element/element.hpp"
|
---|
| 51 | #include "Element/periodentafel.hpp"
|
---|
[907636] | 52 | #include "World.hpp"
|
---|
| 53 |
|
---|
| 54 | GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap;
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 58 |
|
---|
[3b229e] | 59 | QGLMaterial *GLMoleculeObject::m_hoverMaterial = NULL;
|
---|
| 60 | QGLMaterial *GLMoleculeObject::m_selectionMaterial = NULL;
|
---|
| 61 | QGLMaterial *GLMoleculeObject::m_selectionBoxMaterial = NULL;
|
---|
| 62 |
|
---|
[907636] | 63 |
|
---|
[72a4c1] | 64 | double GLMoleculeObject::detailMinDistance[GLMoleculeObject::DETAILTYPES_MAX] = {0, 15, 30, 42};
|
---|
| 65 |
|
---|
| 66 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh[], QObject *parent)
|
---|
[907636] | 67 | : QObject(parent)
|
---|
| 68 | {
|
---|
[bca99d] | 69 | //mesh->setParent(this);
|
---|
[72a4c1] | 70 | for (int i=0;i<DETAILTYPES_MAX;i++)
|
---|
| 71 | m_mesh[i] = mesh[i];
|
---|
[907636] | 72 | m_scale = 1.0f;
|
---|
[fbb1f1] | 73 | m_scaleZ = 1.0f;
|
---|
[907636] | 74 | m_rotationAngle = 0.0f;
|
---|
| 75 | m_effect = 0;
|
---|
| 76 | m_objectId = -1;
|
---|
| 77 | m_hovering = false;
|
---|
[8a77ac] | 78 | m_selected = false;
|
---|
[907636] | 79 | m_material = 0;
|
---|
[3b229e] | 80 | initStaticMaterials();
|
---|
[907636] | 81 | }
|
---|
| 82 |
|
---|
[615eee] | 83 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh, QObject *parent)
|
---|
| 84 | : QObject(parent)
|
---|
| 85 | {
|
---|
| 86 | //mesh->setParent(this);
|
---|
| 87 | for (int i=0;i<DETAILTYPES_MAX;i++)
|
---|
| 88 | m_mesh[i] = mesh;
|
---|
| 89 | m_scale = 1.0f;
|
---|
| 90 | m_scaleZ = 1.0f;
|
---|
| 91 | m_rotationAngle = 0.0f;
|
---|
| 92 | m_effect = 0;
|
---|
| 93 | m_objectId = -1;
|
---|
| 94 | m_hovering = false;
|
---|
| 95 | m_selected = false;
|
---|
| 96 | m_material = 0;
|
---|
| 97 | initStaticMaterials();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[907636] | 100 | GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
|
---|
| 101 | : QObject(parent)
|
---|
| 102 | {
|
---|
| 103 | scene->setParent(this);
|
---|
[615eee] | 104 | for (int i=0;i<DETAILTYPES_MAX;i++)
|
---|
| 105 | m_mesh[i] = scene->mainNode();
|
---|
[907636] | 106 | m_scale = 1.0f;
|
---|
[fbb1f1] | 107 | m_scaleZ = 1.0f;
|
---|
[907636] | 108 | m_rotationAngle = 0.0f;
|
---|
| 109 | m_effect = 0;
|
---|
| 110 | m_objectId = -1;
|
---|
| 111 | m_hovering = false;
|
---|
[8a77ac] | 112 | m_selected = false;
|
---|
[907636] | 113 | m_material = 0;
|
---|
[3b229e] | 114 | initStaticMaterials();
|
---|
[907636] | 115 | }
|
---|
| 116 |
|
---|
| 117 | GLMoleculeObject::~GLMoleculeObject()
|
---|
| 118 | {
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
|
---|
| 122 | {
|
---|
| 123 | Q_UNUSED(painter);
|
---|
| 124 | if (m_objectId != -1)
|
---|
| 125 | view->registerObject(m_objectId, this);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[225cf5] | 128 |
|
---|
| 129 | /** Draws a box around the mesh.
|
---|
| 130 | *
|
---|
| 131 | */
|
---|
| 132 | void GLMoleculeObject::drawSelectionBox(QGLPainter *painter)
|
---|
| 133 | {
|
---|
| 134 | painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial);
|
---|
| 135 | QVector3DArray array;
|
---|
[3927ef] | 136 | qreal radius = 1.0;
|
---|
[225cf5] | 137 | array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
|
---|
| 138 | array.append( radius, -radius, -radius); array.append( radius, radius, -radius);
|
---|
| 139 | array.append( radius, radius, -radius); array.append(-radius, radius, -radius);
|
---|
| 140 | array.append(-radius, radius, -radius); array.append(-radius, -radius, -radius);
|
---|
| 141 |
|
---|
| 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 | array.append(-radius, radius, radius); array.append(-radius, -radius, radius);
|
---|
| 146 |
|
---|
| 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 | array.append( radius, radius, -radius); array.append( radius, radius, radius);
|
---|
| 151 | painter->clearAttributes();
|
---|
| 152 | painter->setVertexAttribute(QGL::Position, array);
|
---|
| 153 | painter->draw(QGL::Lines, 24);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[72a4c1] | 156 | void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane)
|
---|
[907636] | 157 | {
|
---|
| 158 | // Position the model at its designated position, scale, and orientation.
|
---|
| 159 | painter->modelViewMatrix().push();
|
---|
| 160 | painter->modelViewMatrix().translate(m_position);
|
---|
| 161 | if (m_scale != 1.0f)
|
---|
| 162 | painter->modelViewMatrix().scale(m_scale);
|
---|
| 163 | if (m_rotationAngle != 0.0f)
|
---|
| 164 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
|
---|
[fbb1f1] | 165 | if (m_scaleZ != 1.0f)
|
---|
| 166 | painter->modelViewMatrix().scale(1.0f, 1.0f, m_scaleZ);
|
---|
[907636] | 167 |
|
---|
| 168 | // Apply the material and effect to the painter.
|
---|
| 169 | QGLMaterial *material;
|
---|
| 170 | if (m_hovering)
|
---|
| 171 | material = m_hoverMaterial;
|
---|
[8a77ac] | 172 | else if (m_selected)
|
---|
| 173 | material = m_selectionMaterial;
|
---|
[907636] | 174 | else
|
---|
| 175 | material = m_material;
|
---|
[3b229e] | 176 |
|
---|
| 177 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
|
---|
| 178 |
|
---|
[907636] | 179 | painter->setColor(material->diffuseColor());
|
---|
| 180 | painter->setFaceMaterial(QGL::AllFaces, material);
|
---|
| 181 | if (m_effect)
|
---|
| 182 | painter->setUserEffect(m_effect);
|
---|
| 183 | else
|
---|
| 184 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
| 185 |
|
---|
| 186 | // Mark the object for object picking purposes.
|
---|
| 187 | int prevObjectId = painter->objectPickId();
|
---|
| 188 | if (m_objectId != -1)
|
---|
| 189 | painter->setObjectPickId(m_objectId);
|
---|
| 190 |
|
---|
| 191 | // Draw the geometry mesh.
|
---|
[72a4c1] | 192 | QVector4D pos4d(m_position, -1);
|
---|
| 193 | qreal distance = QVector4D::dotProduct(cameraPlane, pos4d);
|
---|
| 194 |
|
---|
| 195 | if (distance > detailMinDistance[DETAIL_LOW])
|
---|
| 196 | m_mesh[DETAIL_LOW]->draw(painter);
|
---|
| 197 | else if (distance > detailMinDistance[DETAIL_MEDIUM])
|
---|
| 198 | m_mesh[DETAIL_MEDIUM]->draw(painter);
|
---|
| 199 | else if (distance > detailMinDistance[DETAIL_HIGH])
|
---|
| 200 | m_mesh[DETAIL_HIGH]->draw(painter);
|
---|
| 201 | else if (distance > detailMinDistance[DETAIL_HIGHEST])
|
---|
| 202 | m_mesh[DETAIL_HIGHEST]->draw(painter);
|
---|
[907636] | 203 |
|
---|
[225cf5] | 204 | // Draw a box around the mesh, if selected.
|
---|
| 205 | if (m_selected)
|
---|
| 206 | drawSelectionBox(painter);
|
---|
[8a77ac] | 207 |
|
---|
[907636] | 208 | // Turn off the user effect, if present.
|
---|
| 209 | if (m_effect)
|
---|
| 210 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
| 211 |
|
---|
| 212 | // Revert to the previous object identifier.
|
---|
| 213 | painter->setObjectPickId(prevObjectId);
|
---|
| 214 |
|
---|
| 215 | // Restore the modelview matrix.
|
---|
| 216 | painter->modelViewMatrix().pop();
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | bool GLMoleculeObject::event(QEvent *e)
|
---|
| 220 | {
|
---|
| 221 | // Convert the raw event into a signal representing the user's action.
|
---|
| 222 | if (e->type() == QEvent::MouseButtonPress) {
|
---|
| 223 | QMouseEvent *me = (QMouseEvent *)e;
|
---|
| 224 | if (me->button() == Qt::LeftButton)
|
---|
| 225 | emit pressed();
|
---|
| 226 | } else if (e->type() == QEvent::MouseButtonRelease) {
|
---|
| 227 | QMouseEvent *me = (QMouseEvent *)e;
|
---|
| 228 | if (me->button() == Qt::LeftButton) {
|
---|
| 229 | emit released();
|
---|
| 230 | if (me->x() >= 0) // Positive: inside object, Negative: outside.
|
---|
| 231 | emit clicked();
|
---|
| 232 | }
|
---|
| 233 | } else if (e->type() == QEvent::MouseButtonDblClick) {
|
---|
| 234 | emit doubleClicked();
|
---|
| 235 | } else if (e->type() == QEvent::Enter) {
|
---|
| 236 | m_hovering = true;
|
---|
[407638e] | 237 | emit hoverChanged(this);
|
---|
[907636] | 238 | } else if (e->type() == QEvent::Leave) {
|
---|
| 239 | m_hovering = false;
|
---|
[407638e] | 240 | emit hoverChanged(NULL);
|
---|
[907636] | 241 | }
|
---|
| 242 | return QObject::event(e);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /** Returns the ref to the Material for element No \a from the map.
|
---|
| 246 | *
|
---|
| 247 | * \note We create a new one if the element is missing.
|
---|
| 248 | *
|
---|
| 249 | * @param no element no
|
---|
| 250 | * @return ref to QGLMaterial
|
---|
| 251 | */
|
---|
| 252 | QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
|
---|
| 253 | {
|
---|
[3b229e] | 254 | ASSERT( (no > 0) && (no < MAX_ELEMENTS),
|
---|
[907636] | 255 | "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
|
---|
| 256 | if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
|
---|
| 257 | // get present one
|
---|
| 258 | return ElementNoMaterialMap[no];
|
---|
| 259 | } else {
|
---|
| 260 | // create new one
|
---|
| 261 | LOG(1, "Creating new material for element "+toString(no)+".");
|
---|
| 262 | QGLMaterial *newmaterial = new QGLMaterial(NULL);
|
---|
| 263 |
|
---|
[3b229e] | 264 | // create material for element
|
---|
| 265 | periodentafel *periode = World::getInstance().getPeriode();
|
---|
| 266 | const element *desiredelement = periode->FindElement(no);
|
---|
| 267 | ASSERT(desiredelement != NULL,
|
---|
| 268 | "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
|
---|
| 269 | const unsigned char* color = desiredelement->getColor();
|
---|
| 270 | LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
|
---|
| 271 | newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
|
---|
[907636] | 272 | newmaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
| 273 | newmaterial->setShininess( 128 );
|
---|
| 274 | ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
|
---|
| 275 |
|
---|
| 276 | return newmaterial;
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[3b229e] | 280 | /** Create the 3 materials shared by all objects.
|
---|
| 281 | *
|
---|
| 282 | */
|
---|
| 283 | void GLMoleculeObject::initStaticMaterials()
|
---|
| 284 | {
|
---|
| 285 | if (!m_hoverMaterial){
|
---|
| 286 | m_hoverMaterial = new QGLMaterial(NULL);
|
---|
| 287 | m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) );
|
---|
| 288 | m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
| 289 | m_hoverMaterial->setShininess( 128 );
|
---|
| 290 | }
|
---|
| 291 | if (!m_selectionMaterial){
|
---|
| 292 | m_selectionMaterial = new QGLMaterial(NULL);
|
---|
| 293 | m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) );
|
---|
| 294 | m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
| 295 | m_selectionMaterial->setShininess( 128 );
|
---|
| 296 | }
|
---|
| 297 | if (!m_selectionBoxMaterial){
|
---|
| 298 | m_selectionBoxMaterial = new QGLMaterial(NULL);
|
---|
| 299 | m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) );
|
---|
| 300 | m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) );
|
---|
| 301 | m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) );
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[907636] | 305 | /** Static function to be called when Materials have to be removed.
|
---|
| 306 | *
|
---|
| 307 | */
|
---|
| 308 | void GLMoleculeObject::cleanMaterialMap()
|
---|
| 309 | {
|
---|
| 310 | for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
|
---|
| 311 | !ElementNoMaterialMap.empty();
|
---|
| 312 | iter = ElementNoMaterialMap.begin()) {
|
---|
| 313 | delete iter->second;
|
---|
| 314 | ElementNoMaterialMap.erase(iter);
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
[c67518] | 317 |
|
---|
| 318 |
|
---|
| 319 | void GLMoleculeObject::setSelected(bool value)
|
---|
| 320 | {
|
---|
| 321 | if (value != m_selected){
|
---|
| 322 | m_selected = value;
|
---|
| 323 | emit selectionChanged();
|
---|
| 324 | }
|
---|
| 325 | }
|
---|