source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject.cpp@ 3b229e

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 3b229e was 3b229e, checked in by Frederik Heber <heber@…>, 13 years ago

GL: made hover/selection materials static

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