[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 |
|
---|
| 83 | GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
|
---|
| 84 | : QObject(parent)
|
---|
| 85 | {
|
---|
| 86 | scene->setParent(this);
|
---|
[72a4c1] | 87 | m_mesh[0] = scene->mainNode();
|
---|
| 88 | m_mesh[1] = scene->mainNode();
|
---|
| 89 | m_mesh[2] = scene->mainNode();
|
---|
[907636] | 90 | m_scale = 1.0f;
|
---|
[fbb1f1] | 91 | m_scaleZ = 1.0f;
|
---|
[907636] | 92 | m_rotationAngle = 0.0f;
|
---|
| 93 | m_effect = 0;
|
---|
| 94 | m_objectId = -1;
|
---|
| 95 | m_hovering = false;
|
---|
[8a77ac] | 96 | m_selected = false;
|
---|
[907636] | 97 | m_material = 0;
|
---|
[3b229e] | 98 | initStaticMaterials();
|
---|
[907636] | 99 | }
|
---|
| 100 |
|
---|
| 101 | GLMoleculeObject::~GLMoleculeObject()
|
---|
| 102 | {
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
|
---|
| 106 | {
|
---|
| 107 | Q_UNUSED(painter);
|
---|
| 108 | if (m_objectId != -1)
|
---|
| 109 | view->registerObject(m_objectId, this);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[225cf5] | 112 |
|
---|
| 113 | /** Draws a box around the mesh.
|
---|
| 114 | *
|
---|
| 115 | */
|
---|
| 116 | void GLMoleculeObject::drawSelectionBox(QGLPainter *painter)
|
---|
| 117 | {
|
---|
| 118 | painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial);
|
---|
| 119 | QVector3DArray array;
|
---|
[3927ef] | 120 | qreal radius = 1.0;
|
---|
[225cf5] | 121 | array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
|
---|
| 122 | array.append( radius, -radius, -radius); array.append( radius, radius, -radius);
|
---|
| 123 | array.append( radius, radius, -radius); array.append(-radius, radius, -radius);
|
---|
| 124 | array.append(-radius, radius, -radius); array.append(-radius, -radius, -radius);
|
---|
| 125 |
|
---|
| 126 | array.append(-radius, -radius, radius); array.append( radius, -radius, radius);
|
---|
| 127 | array.append( radius, -radius, radius); array.append( radius, radius, radius);
|
---|
| 128 | array.append( radius, radius, radius); array.append(-radius, radius, radius);
|
---|
| 129 | array.append(-radius, radius, radius); array.append(-radius, -radius, radius);
|
---|
| 130 |
|
---|
| 131 | array.append(-radius, -radius, -radius); array.append(-radius, -radius, radius);
|
---|
| 132 | array.append( radius, -radius, -radius); array.append( radius, -radius, radius);
|
---|
| 133 | array.append(-radius, radius, -radius); array.append(-radius, radius, radius);
|
---|
| 134 | array.append( radius, radius, -radius); array.append( radius, radius, radius);
|
---|
| 135 | painter->clearAttributes();
|
---|
| 136 | painter->setVertexAttribute(QGL::Position, array);
|
---|
| 137 | painter->draw(QGL::Lines, 24);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[72a4c1] | 140 | void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane)
|
---|
[907636] | 141 | {
|
---|
| 142 | // Position the model at its designated position, scale, and orientation.
|
---|
| 143 | painter->modelViewMatrix().push();
|
---|
| 144 | painter->modelViewMatrix().translate(m_position);
|
---|
| 145 | if (m_scale != 1.0f)
|
---|
| 146 | painter->modelViewMatrix().scale(m_scale);
|
---|
| 147 | if (m_rotationAngle != 0.0f)
|
---|
| 148 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
|
---|
[fbb1f1] | 149 | if (m_scaleZ != 1.0f)
|
---|
| 150 | painter->modelViewMatrix().scale(1.0f, 1.0f, m_scaleZ);
|
---|
[907636] | 151 |
|
---|
| 152 | // Apply the material and effect to the painter.
|
---|
| 153 | QGLMaterial *material;
|
---|
| 154 | if (m_hovering)
|
---|
| 155 | material = m_hoverMaterial;
|
---|
[8a77ac] | 156 | else if (m_selected)
|
---|
| 157 | material = m_selectionMaterial;
|
---|
[907636] | 158 | else
|
---|
| 159 | material = m_material;
|
---|
[3b229e] | 160 |
|
---|
| 161 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
|
---|
| 162 |
|
---|
[907636] | 163 | painter->setColor(material->diffuseColor());
|
---|
| 164 | painter->setFaceMaterial(QGL::AllFaces, material);
|
---|
| 165 | if (m_effect)
|
---|
| 166 | painter->setUserEffect(m_effect);
|
---|
| 167 | else
|
---|
| 168 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
| 169 |
|
---|
| 170 | // Mark the object for object picking purposes.
|
---|
| 171 | int prevObjectId = painter->objectPickId();
|
---|
| 172 | if (m_objectId != -1)
|
---|
| 173 | painter->setObjectPickId(m_objectId);
|
---|
| 174 |
|
---|
| 175 | // Draw the geometry mesh.
|
---|
[72a4c1] | 176 | QVector4D pos4d(m_position, -1);
|
---|
| 177 | qreal distance = QVector4D::dotProduct(cameraPlane, pos4d);
|
---|
| 178 |
|
---|
| 179 | if (distance > detailMinDistance[DETAIL_LOW])
|
---|
| 180 | m_mesh[DETAIL_LOW]->draw(painter);
|
---|
| 181 | else if (distance > detailMinDistance[DETAIL_MEDIUM])
|
---|
| 182 | m_mesh[DETAIL_MEDIUM]->draw(painter);
|
---|
| 183 | else if (distance > detailMinDistance[DETAIL_HIGH])
|
---|
| 184 | m_mesh[DETAIL_HIGH]->draw(painter);
|
---|
| 185 | else if (distance > detailMinDistance[DETAIL_HIGHEST])
|
---|
| 186 | m_mesh[DETAIL_HIGHEST]->draw(painter);
|
---|
[907636] | 187 |
|
---|
[225cf5] | 188 | // Draw a box around the mesh, if selected.
|
---|
| 189 | if (m_selected)
|
---|
| 190 | drawSelectionBox(painter);
|
---|
[8a77ac] | 191 |
|
---|
[907636] | 192 | // Turn off the user effect, if present.
|
---|
| 193 | if (m_effect)
|
---|
| 194 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
| 195 |
|
---|
| 196 | // Revert to the previous object identifier.
|
---|
| 197 | painter->setObjectPickId(prevObjectId);
|
---|
| 198 |
|
---|
| 199 | // Restore the modelview matrix.
|
---|
| 200 | painter->modelViewMatrix().pop();
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | bool GLMoleculeObject::event(QEvent *e)
|
---|
| 204 | {
|
---|
| 205 | // Convert the raw event into a signal representing the user's action.
|
---|
| 206 | if (e->type() == QEvent::MouseButtonPress) {
|
---|
| 207 | QMouseEvent *me = (QMouseEvent *)e;
|
---|
| 208 | if (me->button() == Qt::LeftButton)
|
---|
| 209 | emit pressed();
|
---|
| 210 | } else if (e->type() == QEvent::MouseButtonRelease) {
|
---|
| 211 | QMouseEvent *me = (QMouseEvent *)e;
|
---|
| 212 | if (me->button() == Qt::LeftButton) {
|
---|
| 213 | emit released();
|
---|
| 214 | if (me->x() >= 0) // Positive: inside object, Negative: outside.
|
---|
| 215 | emit clicked();
|
---|
| 216 | }
|
---|
| 217 | } else if (e->type() == QEvent::MouseButtonDblClick) {
|
---|
| 218 | emit doubleClicked();
|
---|
| 219 | } else if (e->type() == QEvent::Enter) {
|
---|
| 220 | m_hovering = true;
|
---|
[407638e] | 221 | emit hoverChanged(this);
|
---|
[907636] | 222 | } else if (e->type() == QEvent::Leave) {
|
---|
| 223 | m_hovering = false;
|
---|
[407638e] | 224 | emit hoverChanged(NULL);
|
---|
[907636] | 225 | }
|
---|
| 226 | return QObject::event(e);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /** Returns the ref to the Material for element No \a from the map.
|
---|
| 230 | *
|
---|
| 231 | * \note We create a new one if the element is missing.
|
---|
| 232 | *
|
---|
| 233 | * @param no element no
|
---|
| 234 | * @return ref to QGLMaterial
|
---|
| 235 | */
|
---|
| 236 | QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
|
---|
| 237 | {
|
---|
[3b229e] | 238 | ASSERT( (no > 0) && (no < MAX_ELEMENTS),
|
---|
[907636] | 239 | "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
|
---|
| 240 | if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
|
---|
| 241 | // get present one
|
---|
| 242 | return ElementNoMaterialMap[no];
|
---|
| 243 | } else {
|
---|
| 244 | // create new one
|
---|
| 245 | LOG(1, "Creating new material for element "+toString(no)+".");
|
---|
| 246 | QGLMaterial *newmaterial = new QGLMaterial(NULL);
|
---|
| 247 |
|
---|
[3b229e] | 248 | // create material for element
|
---|
| 249 | periodentafel *periode = World::getInstance().getPeriode();
|
---|
| 250 | const element *desiredelement = periode->FindElement(no);
|
---|
| 251 | ASSERT(desiredelement != NULL,
|
---|
| 252 | "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
|
---|
| 253 | const unsigned char* color = desiredelement->getColor();
|
---|
| 254 | LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
|
---|
| 255 | newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
|
---|
[907636] | 256 | newmaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
| 257 | newmaterial->setShininess( 128 );
|
---|
| 258 | ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
|
---|
| 259 |
|
---|
| 260 | return newmaterial;
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[3b229e] | 264 | /** Create the 3 materials shared by all objects.
|
---|
| 265 | *
|
---|
| 266 | */
|
---|
| 267 | void GLMoleculeObject::initStaticMaterials()
|
---|
| 268 | {
|
---|
| 269 | if (!m_hoverMaterial){
|
---|
| 270 | m_hoverMaterial = new QGLMaterial(NULL);
|
---|
| 271 | m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) );
|
---|
| 272 | m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
| 273 | m_hoverMaterial->setShininess( 128 );
|
---|
| 274 | }
|
---|
| 275 | if (!m_selectionMaterial){
|
---|
| 276 | m_selectionMaterial = new QGLMaterial(NULL);
|
---|
| 277 | m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) );
|
---|
| 278 | m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) );
|
---|
| 279 | m_selectionMaterial->setShininess( 128 );
|
---|
| 280 | }
|
---|
| 281 | if (!m_selectionBoxMaterial){
|
---|
| 282 | m_selectionBoxMaterial = new QGLMaterial(NULL);
|
---|
| 283 | m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) );
|
---|
| 284 | m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) );
|
---|
| 285 | m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) );
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[907636] | 289 | /** Static function to be called when Materials have to be removed.
|
---|
| 290 | *
|
---|
| 291 | */
|
---|
| 292 | void GLMoleculeObject::cleanMaterialMap()
|
---|
| 293 | {
|
---|
| 294 | for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
|
---|
| 295 | !ElementNoMaterialMap.empty();
|
---|
| 296 | iter = ElementNoMaterialMap.begin()) {
|
---|
| 297 | delete iter->second;
|
---|
| 298 | ElementNoMaterialMap.erase(iter);
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
[c67518] | 301 |
|
---|
| 302 |
|
---|
| 303 | void GLMoleculeObject::setSelected(bool value)
|
---|
| 304 | {
|
---|
| 305 | if (value != m_selected){
|
---|
| 306 | m_selected = value;
|
---|
| 307 | emit selectionChanged();
|
---|
| 308 | }
|
---|
| 309 | }
|
---|