Browse Source

vector qdebug; camera block angle

develop
Robin Thoni 8 years ago
parent
commit
577ea07099
4 changed files with 69 additions and 12 deletions
  1. 52
    12
      TheGame/renderwidget.cpp
  2. 8
    0
      TheGame/renderwidget.h
  3. 6
    0
      UGameEngine/utils/vector3d.cpp
  4. 3
    0
      UGameEngine/utils/vector3d.h

+ 52
- 12
TheGame/renderwidget.cpp View File

@@ -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);

+ 8
- 0
TheGame/renderwidget.h View File

@@ -4,6 +4,7 @@
4 4
 #include <QGLWidget>
5 5
 #include <QMouseEvent>
6 6
 #include <QWheelEvent>
7
+#include <QKeyEvent>
7 8
 #include "engine/ugameengine.h"
8 9
 #include "openglrenderdevice.h"
9 10
 
@@ -18,9 +19,14 @@ protected:
18 19
     void paintGL();
19 20
     void resizeGL(int width, int height);
20 21
 
22
+    float normalizeAngle(float angle);
23
+
21 24
     void mousePressEvent(QMouseEvent* event);
22 25
     void mouseMoveEvent(QMouseEvent* event);
23 26
     void wheelEvent(QWheelEvent* event);
27
+    void keyPressEvent(QKeyEvent* event);
28
+
29
+    void rotate(float phi, float theta);
24 30
 
25 31
 signals:
26 32
 
@@ -42,6 +48,8 @@ private:
42 48
 
43 49
     float _theta;
44 50
 
51
+    bool _reverse;
52
+
45 53
 };
46 54
 
47 55
 #endif // RENDERWIDGET_H

+ 6
- 0
UGameEngine/utils/vector3d.cpp View File

@@ -227,3 +227,9 @@ Vector3D::operator bool()
227 227
 {
228 228
     return !isNull();
229 229
 }
230
+
231
+
232
+QDebug operator<<(QDebug dbg, const Vector3D &v)
233
+{
234
+    return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
235
+}

+ 3
- 0
UGameEngine/utils/vector3d.h View File

@@ -1,6 +1,8 @@
1 1
 #ifndef VECTOR3D_H
2 2
 #define VECTOR3D_H
3 3
 
4
+#include <QDebug>
5
+
4 6
 class Vector3D
5 7
 {
6 8
 public:
@@ -70,5 +72,6 @@ private:
70 72
     double _z;
71 73
 };
72 74
 
75
+QDebug operator<<(QDebug dbg, const Vector3D& v);
73 76
 
74 77
 #endif // VECTOR3D_H

Loading…
Cancel
Save