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