| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 | #include "renderwidget.h"
#include <QTimer>
#include <math.h>
#include <QDebug>
#include "entities/ugeentitycube.h"
RenderWidget::RenderWidget(QWidget *parent) :
    QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  , angle(0)
  , _radius(5.0)
  , _phi(45)
  , _theta(45)
  , _reverse(false)
{
    _device = new OpenGLRenderDevice(this);
    _engine = new UGameEngine(_device);
    UGEEntityCube* cube = new UGEEntityCube(_engine);
    cube->move(Vector3D(0, 1, 0));
    _engine->addEntity(cube);
    setMouseTracking(false);
    setFocusPolicy(Qt::StrongFocus);
}
void RenderWidget::initializeGL()
{
    makeCurrent();
    _device->setClearColor(Qt::gray);
    _device->initialize(70, width(), height());
}
void RenderWidget::paintGL()
{
    float theta = fabs(_theta / 180.0 * M_PI);
    float phi = _phi / 180.0 * M_PI;
    Vector3D center = Vector3D(
                _radius * sin(theta) * sin(phi),
                _radius * cos(theta),
                _radius * sin(theta) * cos(phi)
                );
    _device->lookAt(center, Vector3D(0.0f, 0.0f, 0.0f));
    _engine->draw();
    drawAxes();
}
void RenderWidget::resizeGL(int width, int height)
{
    _device->resize(width, height);
}
float RenderWidget::normalizeAngle(float angle)
{
    while (angle >= 360.0) {
        angle -= 360.0;
    }
    while (angle < 0.0) {
        angle += 360.0;
    }
    return angle;
}
void RenderWidget::mousePressEvent(QMouseEvent *event)
{
    _lastPoint = event->pos();
}
void RenderWidget::mouseMoveEvent(QMouseEvent *event)
{
    QPoint diff = event->pos() - _lastPoint;
    rotate(-diff.x(), -diff.y());
    _lastPoint = event->pos();
}
void RenderWidget::wheelEvent(QWheelEvent *event)
{
    _radius = qMax(2.0, _radius - event->delta() / 20.0);
    update();
}
void RenderWidget::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Up) {
        rotate(0.0, 1.0);
    }
    else if (event->key() == Qt::Key_Down) {
        rotate(0.0, -1.0);
    }
    else if (event->key() == Qt::Key_Left) {
        rotate(1.0, 0.0);
    }
    else if (event->key() == Qt::Key_Right) {
        rotate(-1.0, 0.0);
    }
}
void RenderWidget::rotate(float phi, float theta)
{
    _phi = normalizeAngle(_phi + phi);
    if (_theta + theta >= 180.0) {
        theta = 179.0;
    }
    else if (_theta + theta < 1.0) {
        theta = 1.0;
    }
    else {
        _theta = normalizeAngle(_theta + theta);
    }
    update();
}
void RenderWidget::drawAxes()
{
    _device->drawLine(ColorVector3D(Qt::red, 0.0, 0.0, 0.0), ColorVector3D(Qt::red, 1.0, 0.0, 0.0), 2.5);
    _device->drawLine(ColorVector3D(Qt::green, 0.0, 0.0, 0.0), ColorVector3D(Qt::green, 0.0, 1.0, 0.0), 2.5);
    _device->drawLine(ColorVector3D(Qt::blue, 0.0, 0.0, 0.0), ColorVector3D(Qt::blue, 0.0, 0.0, 1.0), 2.5);
    _device->drawPolygon(QList<ColorVector3D>() << ColorVector3D(Qt::red, 0, 0, 0) << ColorVector3D(Qt::green, 1, 0, 0) << ColorVector3D(Qt::transparent, 1, 1, 0) << ColorVector3D(Qt::blue, 0, 1, 0));
}
 |