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

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 b1b413 was 72a4c1, checked in by Michael Ankele <ankele@…>, 13 years ago

GL: using multiple levels of detail for the meshes

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