Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject.cpp

    r0aa122 r3927ef  
    4242#include "CodePatterns/MemDebug.hpp"
    4343
     44QGLMaterial *GLMoleculeObject::m_hoverMaterial = NULL;
     45QGLMaterial *GLMoleculeObject::m_selectionMaterial = NULL;
     46QGLMaterial *GLMoleculeObject::m_selectionBoxMaterial = NULL;
     47
    4448
    4549GLMoleculeObject::GLMoleculeObject(QGLSceneNode *GLMoleculeObject, QObject *parent)
    4650   : QObject(parent)
    4751{
    48    m_mesh = 0;
    49    m_GLMoleculeObject = GLMoleculeObject;
     52   m_mesh = GLMoleculeObject;
    5053   m_scale = 1.0f;
     54   m_scaleZ = 1.0f;
    5155   m_rotationAngle = 0.0f;
    5256   m_effect = 0;
    5357   m_objectId = -1;
    5458   m_hovering = false;
     59   m_selected = false;
    5560   m_material = 0;
    56    m_hoverMaterial = 0;
     61   initStaticMaterials();
    5762}
    5863
     
    6166{
    6267   scene->setParent(this);
    63    m_mesh = 0;
    64    m_GLMoleculeObject = scene->mainNode();
     68   m_mesh = scene->mainNode();
    6569   m_scale = 1.0f;
     70   m_scaleZ = 1.0f;
    6671   m_rotationAngle = 0.0f;
    6772   m_effect = 0;
    6873   m_objectId = -1;
    6974   m_hovering = false;
     75   m_selected = false;
    7076   m_material = 0;
    71    m_hoverMaterial = 0;
     77   initStaticMaterials();
    7278}
    7379
    7480GLMoleculeObject::~GLMoleculeObject()
    7581{
    76    delete m_mesh;
    7782}
    7883
     
    8287   if (m_objectId != -1)
    8388       view->registerObject(m_objectId, this);
     89}
     90
     91
     92/** Draws a box around the mesh.
     93 *
     94 */
     95void GLMoleculeObject::drawSelectionBox(QGLPainter *painter)
     96{
     97  painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial);
     98  QVector3DArray array;
     99  qreal radius = 1.0;
     100  array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
     101  array.append( radius, -radius, -radius); array.append( radius,  radius, -radius);
     102  array.append( radius,  radius, -radius); array.append(-radius,  radius, -radius);
     103  array.append(-radius,  radius, -radius); array.append(-radius, -radius, -radius);
     104
     105  array.append(-radius, -radius,  radius); array.append( radius, -radius,  radius);
     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
     110  array.append(-radius, -radius, -radius); array.append(-radius, -radius,  radius);
     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  painter->clearAttributes();
     115  painter->setVertexAttribute(QGL::Position, array);
     116  painter->draw(QGL::Lines, 24);
    84117}
    85118
     
    93126   if (m_rotationAngle != 0.0f)
    94127       painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
     128   if (m_scaleZ != 1.0f)
     129       painter->modelViewMatrix().scale(1.0f, 1.0f, m_scaleZ);
    95130
    96131   // Apply the material and effect to the painter.
     
    98133   if (m_hovering)
    99134       material = m_hoverMaterial;
     135   else if (m_selected)
     136       material = m_selectionMaterial;
    100137   else
    101138       material = m_material;
     139
     140   ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
     141
    102142   painter->setColor(material->diffuseColor());
    103143   painter->setFaceMaterial(QGL::AllFaces, material);
     
    113153
    114154   // Draw the geometry mesh.
    115    if (m_GLMoleculeObject)
    116        m_GLMoleculeObject->draw(painter);
    117    else
    118        m_mesh->draw(painter);
     155   m_mesh->draw(painter);
     156
     157   // Draw a box around the mesh, if selected.
     158   if (m_selected)
     159     drawSelectionBox(painter);
    119160
    120161   // Turn off the user effect, if present.
     
    164205QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
    165206{
    166   ASSERT( (no >= 0) && (no < MAX_ELEMENTS),
     207  ASSERT( (no > 0) && (no < MAX_ELEMENTS),
    167208      "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
    168209  if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
     
    174215    QGLMaterial *newmaterial = new QGLMaterial(NULL);
    175216
    176     if (no == 0) { // create hover material
    177       newmaterial->setAmbientColor( QColor(0, 128, 128) );
    178     } else { // create material for element
    179       periodentafel *periode = World::getInstance().getPeriode();
    180       const element *desiredelement = periode->FindElement(no);
    181       ASSERT(desiredelement != NULL,
    182           "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
    183       const unsigned char* color = desiredelement->getColor();
    184       LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
    185       newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
    186     }
     217    // create material for element
     218    periodentafel *periode = World::getInstance().getPeriode();
     219    const element *desiredelement = periode->FindElement(no);
     220    ASSERT(desiredelement != NULL,
     221        "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
     222    const unsigned char* color = desiredelement->getColor();
     223    LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
     224    newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
    187225    newmaterial->setSpecularColor( QColor(60, 60, 60) );
    188226    newmaterial->setShininess( 128 );
     
    190228
    191229    return newmaterial;
     230  }
     231}
     232
     233/** Create the 3 materials shared by all objects.
     234 *
     235 */
     236void GLMoleculeObject::initStaticMaterials()
     237{
     238  if (!m_hoverMaterial){
     239    m_hoverMaterial = new QGLMaterial(NULL);
     240    m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) );
     241    m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) );
     242    m_hoverMaterial->setShininess( 128 );
     243  }
     244  if (!m_selectionMaterial){
     245    m_selectionMaterial = new QGLMaterial(NULL);
     246    m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) );
     247    m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) );
     248    m_selectionMaterial->setShininess( 128 );
     249  }
     250  if (!m_selectionBoxMaterial){
     251    m_selectionBoxMaterial = new QGLMaterial(NULL);
     252    m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) );
     253    m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) );
     254    m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) );
    192255  }
    193256}
     
    205268  }
    206269}
     270
     271
     272void GLMoleculeObject::setSelected(bool value)
     273{
     274  if (value != m_selected){
     275    m_selected = value;
     276    emit selectionChanged();
     277  }
     278}
Note: See TracChangeset for help on using the changeset viewer.