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