source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject.cpp@ 613f8c

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 613f8c was 613f8c, checked in by Frederik Heber <heber@…>, 13 years ago

added a selection material to GLMoleculeObject (still unused)

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
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"
35#include "Element/element.hpp"
36#include "Element/periodentafel.hpp"
37#include "World.hpp"
38
39GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap;
40
41
42#include "CodePatterns/MemDebug.hpp"
43
44
45GLMoleculeObject::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;
57 m_selectionMaterial = 0;
58}
59
60GLMoleculeObject::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;
73 m_selectionMaterial = 0;
74}
75
76GLMoleculeObject::~GLMoleculeObject()
77{
78 delete m_mesh;
79}
80
81void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
82{
83 Q_UNUSED(painter);
84 if (m_objectId != -1)
85 view->registerObject(m_objectId, this);
86}
87
88void 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
133bool 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 */
166QGLMaterial* 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 */
200void 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}
Note: See TracBrowser for help on using the repository browser.