|  | @@ -9,10 +9,12 @@ RenderWidget::RenderWidget(QWidget *parent) :
 | 
		
	
		
			
			| 9 | 9 |    , _radius(5.0)
 | 
		
	
		
			
			| 10 | 10 |    , _phi(45)
 | 
		
	
		
			
			| 11 | 11 |    , _theta(45)
 | 
		
	
		
			
			|  | 12 | +  , _reverse(false)
 | 
		
	
		
			
			| 12 | 13 |  {
 | 
		
	
		
			
			| 13 | 14 |      _device = new OpenGLRenderDevice(this);
 | 
		
	
		
			
			| 14 | 15 |      _engine = new UGameEngine(_device);
 | 
		
	
		
			
			| 15 | 16 |      setMouseTracking(false);
 | 
		
	
		
			
			|  | 17 | +    setFocusPolicy(Qt::StrongFocus);
 | 
		
	
		
			
			| 16 | 18 |  }
 | 
		
	
		
			
			| 17 | 19 |  
 | 
		
	
		
			
			| 18 | 20 |  void RenderWidget::initializeGL()
 | 
		
	
	
		
			
			|  | @@ -25,14 +27,14 @@ void RenderWidget::initializeGL()
 | 
		
	
		
			
			| 25 | 27 |  
 | 
		
	
		
			
			| 26 | 28 |  void RenderWidget::paintGL()
 | 
		
	
		
			
			| 27 | 29 |  {
 | 
		
	
		
			
			| 28 |  | -    float theta = _theta / 180.0 * M_PI;
 | 
		
	
		
			
			|  | 30 | +    float theta = fabs(_theta / 180.0 * M_PI);
 | 
		
	
		
			
			| 29 | 31 |      float phi = _phi / 180.0 * M_PI;
 | 
		
	
		
			
			| 30 |  | -    _device->lookAt(Vector3D(
 | 
		
	
		
			
			| 31 |  | -                        _radius * sin(theta) * sin(phi),
 | 
		
	
		
			
			| 32 |  | -                        _radius * cos(theta),
 | 
		
	
		
			
			| 33 |  | -                        _radius * sin(theta) * cos(phi)
 | 
		
	
		
			
			| 34 |  | -                        ),
 | 
		
	
		
			
			| 35 |  | -                   Vector3D(0.0f, 0.0f, 0.0f));
 | 
		
	
		
			
			|  | 32 | +    Vector3D center = Vector3D(
 | 
		
	
		
			
			|  | 33 | +                _radius * sin(theta) * sin(phi),
 | 
		
	
		
			
			|  | 34 | +                _radius * cos(theta),
 | 
		
	
		
			
			|  | 35 | +                _radius * sin(theta) * cos(phi)
 | 
		
	
		
			
			|  | 36 | +                );
 | 
		
	
		
			
			|  | 37 | +    _device->lookAt(center, Vector3D(0.0f, 0.0f, 0.0f));
 | 
		
	
		
			
			| 36 | 38 |      _engine->draw();
 | 
		
	
		
			
			| 37 | 39 |  
 | 
		
	
		
			
			| 38 | 40 |      drawAxes();
 | 
		
	
	
		
			
			|  | @@ -43,6 +45,17 @@ void RenderWidget::resizeGL(int width, int height)
 | 
		
	
		
			
			| 43 | 45 |      _device->resize(width, height);
 | 
		
	
		
			
			| 44 | 46 |  }
 | 
		
	
		
			
			| 45 | 47 |  
 | 
		
	
		
			
			|  | 48 | +float RenderWidget::normalizeAngle(float angle)
 | 
		
	
		
			
			|  | 49 | +{
 | 
		
	
		
			
			|  | 50 | +    while (angle >= 360.0) {
 | 
		
	
		
			
			|  | 51 | +        angle -= 360.0;
 | 
		
	
		
			
			|  | 52 | +    }
 | 
		
	
		
			
			|  | 53 | +    while (angle < 0.0) {
 | 
		
	
		
			
			|  | 54 | +        angle += 360.0;
 | 
		
	
		
			
			|  | 55 | +    }
 | 
		
	
		
			
			|  | 56 | +    return angle;
 | 
		
	
		
			
			|  | 57 | +}
 | 
		
	
		
			
			|  | 58 | +
 | 
		
	
		
			
			| 46 | 59 |  void RenderWidget::mousePressEvent(QMouseEvent *event)
 | 
		
	
		
			
			| 47 | 60 |  {
 | 
		
	
		
			
			| 48 | 61 |      _lastPoint = event->pos();
 | 
		
	
	
		
			
			|  | @@ -52,13 +65,9 @@ void RenderWidget::mouseMoveEvent(QMouseEvent *event)
 | 
		
	
		
			
			| 52 | 65 |  {
 | 
		
	
		
			
			| 53 | 66 |      QPoint diff = event->pos() - _lastPoint;
 | 
		
	
		
			
			| 54 | 67 |  
 | 
		
	
		
			
			| 55 |  | -    _phi -= diff.x();
 | 
		
	
		
			
			| 56 |  | -    _theta -= diff.y();
 | 
		
	
		
			
			| 57 |  | -
 | 
		
	
		
			
			| 58 |  | -    qDebug() << _phi << _theta;
 | 
		
	
		
			
			|  | 68 | +    rotate(-diff.x(), -diff.y());
 | 
		
	
		
			
			| 59 | 69 |  
 | 
		
	
		
			
			| 60 | 70 |      _lastPoint = event->pos();
 | 
		
	
		
			
			| 61 |  | -    update();
 | 
		
	
		
			
			| 62 | 71 |  }
 | 
		
	
		
			
			| 63 | 72 |  
 | 
		
	
		
			
			| 64 | 73 |  void RenderWidget::wheelEvent(QWheelEvent *event)
 | 
		
	
	
		
			
			|  | @@ -67,6 +76,37 @@ void RenderWidget::wheelEvent(QWheelEvent *event)
 | 
		
	
		
			
			| 67 | 76 |      update();
 | 
		
	
		
			
			| 68 | 77 |  }
 | 
		
	
		
			
			| 69 | 78 |  
 | 
		
	
		
			
			|  | 79 | +void RenderWidget::keyPressEvent(QKeyEvent *event)
 | 
		
	
		
			
			|  | 80 | +{
 | 
		
	
		
			
			|  | 81 | +    if (event->key() == Qt::Key_Up) {
 | 
		
	
		
			
			|  | 82 | +        rotate(0.0, 1.0);
 | 
		
	
		
			
			|  | 83 | +    }
 | 
		
	
		
			
			|  | 84 | +    else if (event->key() == Qt::Key_Down) {
 | 
		
	
		
			
			|  | 85 | +        rotate(0.0, -1.0);
 | 
		
	
		
			
			|  | 86 | +    }
 | 
		
	
		
			
			|  | 87 | +    else if (event->key() == Qt::Key_Left) {
 | 
		
	
		
			
			|  | 88 | +        rotate(1.0, 0.0);
 | 
		
	
		
			
			|  | 89 | +    }
 | 
		
	
		
			
			|  | 90 | +    else if (event->key() == Qt::Key_Right) {
 | 
		
	
		
			
			|  | 91 | +        rotate(-1.0, 0.0);
 | 
		
	
		
			
			|  | 92 | +    }
 | 
		
	
		
			
			|  | 93 | +}
 | 
		
	
		
			
			|  | 94 | +
 | 
		
	
		
			
			|  | 95 | +void RenderWidget::rotate(float phi, float theta)
 | 
		
	
		
			
			|  | 96 | +{
 | 
		
	
		
			
			|  | 97 | +    _phi = normalizeAngle(_phi + phi);
 | 
		
	
		
			
			|  | 98 | +    if (_theta + theta >= 180.0) {
 | 
		
	
		
			
			|  | 99 | +        theta = 179.0;
 | 
		
	
		
			
			|  | 100 | +    }
 | 
		
	
		
			
			|  | 101 | +    else if (_theta + theta < 1.0) {
 | 
		
	
		
			
			|  | 102 | +        theta = 1.0;
 | 
		
	
		
			
			|  | 103 | +    }
 | 
		
	
		
			
			|  | 104 | +    else {
 | 
		
	
		
			
			|  | 105 | +        _theta = normalizeAngle(_theta + theta);
 | 
		
	
		
			
			|  | 106 | +    }
 | 
		
	
		
			
			|  | 107 | +    update();
 | 
		
	
		
			
			|  | 108 | +}
 | 
		
	
		
			
			|  | 109 | +
 | 
		
	
		
			
			| 70 | 110 |  void RenderWidget::drawAxes()
 | 
		
	
		
			
			| 71 | 111 |  {
 | 
		
	
		
			
			| 72 | 112 |      _device->drawLine(ColorVector3D(Qt::red, 0.0, 0.0, 0.0), ColorVector3D(Qt::red, 1.0, 0.0, 0.0), 2.5);
 |