| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
|---|
| 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/>.
|
|---|
| 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"
|
|---|
| 50 | #include "Element/element.hpp"
|
|---|
| 51 | #include "Element/periodentafel.hpp"
|
|---|
| 52 | #include "World.hpp"
|
|---|
| 53 |
|
|---|
| 54 | GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap;
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 58 |
|
|---|
| 59 | QGLMaterial *GLMoleculeObject::m_hoverMaterial = NULL;
|
|---|
| 60 | QGLMaterial *GLMoleculeObject::m_selectionMaterial = NULL;
|
|---|
| 61 | QGLMaterial *GLMoleculeObject::m_selectionBoxMaterial = NULL;
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | double GLMoleculeObject::detailMinDistance[GLMoleculeObject::DETAILTYPES_MAX] = {0, 15, 30, 42};
|
|---|
| 65 |
|
|---|
| 66 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh[], QObject *parent)
|
|---|
| 67 | : QObject(parent)
|
|---|
| 68 | {
|
|---|
| 69 | //mesh->setParent(this);
|
|---|
| 70 | for (int i=0;i<DETAILTYPES_MAX;i++)
|
|---|
| 71 | m_mesh[i] = mesh[i];
|
|---|
| 72 | m_scaleX = 1.0f;
|
|---|
| 73 | m_scaleY = 1.0f;
|
|---|
| 74 | m_scaleZ = 1.0f;
|
|---|
| 75 | m_rotationAngle = 0.0f;
|
|---|
| 76 | m_effect = 0;
|
|---|
| 77 | m_objectId = -1;
|
|---|
| 78 | m_hovering = false;
|
|---|
| 79 | m_selected = false;
|
|---|
| 80 | m_material = 0;
|
|---|
| 81 | initStaticMaterials();
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh, QObject *parent)
|
|---|
| 85 | : QObject(parent)
|
|---|
| 86 | {
|
|---|
| 87 | //mesh->setParent(this);
|
|---|
| 88 | for (int i=0;i<DETAILTYPES_MAX;i++)
|
|---|
| 89 | m_mesh[i] = mesh;
|
|---|
| 90 | m_scaleX = 1.0f;
|
|---|
| 91 | m_scaleY = 1.0f;
|
|---|
| 92 | m_scaleZ = 1.0f;
|
|---|
| 93 | m_rotationAngle = 0.0f;
|
|---|
| 94 | m_effect = 0;
|
|---|
| 95 | m_objectId = -1;
|
|---|
| 96 | m_hovering = false;
|
|---|
| 97 | m_selected = false;
|
|---|
| 98 | m_material = 0;
|
|---|
| 99 | initStaticMaterials();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
|
|---|
| 103 | : QObject(parent)
|
|---|
| 104 | {
|
|---|
| 105 | scene->setParent(this);
|
|---|
| 106 | for (int i=0;i<DETAILTYPES_MAX;i++)
|
|---|
| 107 | m_mesh[i] = scene->mainNode();
|
|---|
| 108 | m_scaleX = 1.0f;
|
|---|
| 109 | m_scaleY = 1.0f;
|
|---|
| 110 | m_scaleZ = 1.0f;
|
|---|
| 111 | m_rotationAngle = 0.0f;
|
|---|
| 112 | m_effect = 0;
|
|---|
| 113 | m_objectId = -1;
|
|---|
| 114 | m_hovering = false;
|
|---|
| 115 | m_selected = false;
|
|---|
| 116 | m_material = 0;
|
|---|
| 117 | initStaticMaterials();
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | GLMoleculeObject::~GLMoleculeObject()
|
|---|
| 121 | {
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
|
|---|
| 125 | {
|
|---|
| 126 | Q_UNUSED(painter);
|
|---|
| 127 | if (m_objectId != -1)
|
|---|
| 128 | view->registerObject(m_objectId, this);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 | /** Draws a box around the mesh.
|
|---|
| 133 | *
|
|---|
| 134 | */
|
|---|
| 135 | void GLMoleculeObject::drawSelectionBox(QGLPainter *painter)
|
|---|
| 136 | {
|
|---|
| 137 | painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial);
|
|---|
| 138 | QVector3DArray array;
|
|---|
| 139 | qreal radius = 1.0;
|
|---|
| 140 | array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
|
|---|
| 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 |
|
|---|
| 145 | array.append(-radius, -radius, radius); array.append( radius, -radius, radius);
|
|---|
| 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 |
|
|---|
| 150 | array.append(-radius, -radius, -radius); array.append(-radius, -radius, radius);
|
|---|
| 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 | painter->clearAttributes();
|
|---|
| 155 | painter->setVertexAttribute(QGL::Position, array);
|
|---|
| 156 | painter->draw(QGL::Lines, 24);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane)
|
|---|
| 160 | {
|
|---|
| 161 | // Position the model at its designated position, scale, and orientation.
|
|---|
| 162 | painter->modelViewMatrix().push();
|
|---|
| 163 | painter->modelViewMatrix().translate(m_position);
|
|---|
| 164 | if (m_rotationAngle != 0.0f)
|
|---|
| 165 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
|
|---|
| 166 | painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
|
|---|
| 167 |
|
|---|
| 168 | // Apply the material and effect to the painter.
|
|---|
| 169 | QGLMaterial *material;
|
|---|
| 170 | if (m_hovering)
|
|---|
| 171 | material = m_hoverMaterial;
|
|---|
| 172 | else if (m_selected)
|
|---|
| 173 | material = m_selectionMaterial;
|
|---|
| 174 | else
|
|---|
| 175 | material = m_material;
|
|---|
| 176 |
|
|---|
| 177 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
|
|---|
| 178 |
|
|---|
| 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.
|
|---|
| 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);
|
|---|
| 203 |
|
|---|
| 204 | // Draw a box around the mesh, if selected.
|
|---|
| 205 | if (m_selected)
|
|---|
| 206 | drawSelectionBox(painter);
|
|---|
| 207 |
|
|---|
| 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;
|
|---|
| 237 | emit hoverChanged(this);
|
|---|
| 238 | } else if (e->type() == QEvent::Leave) {
|
|---|
| 239 | m_hovering = false;
|
|---|
| 240 | emit hoverChanged(NULL);
|
|---|
| 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 | {
|
|---|
| 254 | ASSERT( (no > 0) && (no < MAX_ELEMENTS),
|
|---|
| 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 |
|
|---|
| 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]) );
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 | }
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 | void GLMoleculeObject::setSelected(bool value)
|
|---|
| 320 | {
|
|---|
| 321 | if (value != m_selected){
|
|---|
| 322 | m_selected = value;
|
|---|
| 323 | emit selectionChanged();
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|