#include "meshobject.h"
#include "qglview.h"
MeshObject::MeshObject(QGLSceneNode *meshObject, QObject *parent)
: QObject(parent)
{
m_mesh = 0;
m_meshObject = meshObject;
m_scale = 1.0f;
m_rotationAngle = 0.0f;
m_effect = 0;
m_objectId = -1;
m_hovering = false;
m_material = 0;
m_hoverMaterial = 0;
}
MeshObject::MeshObject(QGLAbstractScene *scene, QObject *parent)
: QObject(parent)
{
scene->setParent(this);
m_mesh = 0;
m_meshObject = scene->mainNode();
m_scale = 1.0f;
m_rotationAngle = 0.0f;
m_effect = 0;
m_objectId = -1;
m_hovering = false;
m_material = 0;
m_hoverMaterial = 0;
}
MeshObject::~MeshObject()
{
delete m_mesh;
}
void MeshObject::initialize(QGLView *view, QGLPainter *painter)
{
Q_UNUSED(painter);
if (m_objectId != -1)
view->registerObject(m_objectId, this);
}
void MeshObject::draw(QGLPainter *painter)
{
painter->modelViewMatrix().push();
painter->modelViewMatrix().translate(m_position);
if (m_scale != 1.0f)
painter->modelViewMatrix().scale(m_scale);
if (m_rotationAngle != 0.0f)
painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
QGLMaterial *material;
if (m_hovering)
material = m_hoverMaterial;
else
material = m_material;
painter->setColor(material->diffuseColor());
painter->setFaceMaterial(QGL::AllFaces, material);
if (m_effect)
painter->setUserEffect(m_effect);
else
painter->setStandardEffect(QGL::LitMaterial);
int prevObjectId = painter->objectPickId();
if (m_objectId != -1)
painter->setObjectPickId(m_objectId);
if (m_meshObject)
m_meshObject->draw(painter);
else
m_mesh->draw(painter);
if (m_effect)
painter->setStandardEffect(QGL::LitMaterial);
painter->setObjectPickId(prevObjectId);
painter->modelViewMatrix().pop();
}
bool MeshObject::event(QEvent *e)
{
if (e->type() == QEvent::MouseButtonPress) {
QMouseEvent *me = (QMouseEvent *)e;
if (me->button() == Qt::LeftButton)
emit pressed();
} else if (e->type() == QEvent::MouseButtonRelease) {
QMouseEvent *me = (QMouseEvent *)e;
if (me->button() == Qt::LeftButton) {
emit released();
if (me->x() >= 0)
emit clicked();
}
} else if (e->type() == QEvent::MouseButtonDblClick) {
emit doubleClicked();
} else if (e->type() == QEvent::Enter) {
m_hovering = true;
emit hoverChanged();
} else if (e->type() == QEvent::Leave) {
m_hovering = false;
emit hoverChanged();
}
return QObject::event(e);
}