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
   , _radius(5.0)
9
   , _radius(5.0)
10
   , _phi(45)
10
   , _phi(45)
11
   , _theta(45)
11
   , _theta(45)
12
+  , _reverse(false)
12
 {
13
 {
13
     _device = new OpenGLRenderDevice(this);
14
     _device = new OpenGLRenderDevice(this);
14
     _engine = new UGameEngine(_device);
15
     _engine = new UGameEngine(_device);
15
     setMouseTracking(false);
16
     setMouseTracking(false);
17
+    setFocusPolicy(Qt::StrongFocus);
16
 }
18
 }
17
 
19
 
18
 void RenderWidget::initializeGL()
20
 void RenderWidget::initializeGL()
25
 
27
 
26
 void RenderWidget::paintGL()
28
 void RenderWidget::paintGL()
27
 {
29
 {
28
-    float theta = _theta / 180.0 * M_PI;
30
+    float theta = fabs(_theta / 180.0 * M_PI);
29
     float phi = _phi / 180.0 * M_PI;
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
     _engine->draw();
38
     _engine->draw();
37
 
39
 
38
     drawAxes();
40
     drawAxes();
43
     _device->resize(width, height);
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
 void RenderWidget::mousePressEvent(QMouseEvent *event)
59
 void RenderWidget::mousePressEvent(QMouseEvent *event)
47
 {
60
 {
48
     _lastPoint = event->pos();
61
     _lastPoint = event->pos();
52
 {
65
 {
53
     QPoint diff = event->pos() - _lastPoint;
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
     _lastPoint = event->pos();
70
     _lastPoint = event->pos();
61
-    update();
62
 }
71
 }
63
 
72
 
64
 void RenderWidget::wheelEvent(QWheelEvent *event)
73
 void RenderWidget::wheelEvent(QWheelEvent *event)
67
     update();
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
 void RenderWidget::drawAxes()
110
 void RenderWidget::drawAxes()
71
 {
111
 {
72
     _device->drawLine(ColorVector3D(Qt::red, 0.0, 0.0, 0.0), ColorVector3D(Qt::red, 1.0, 0.0, 0.0), 2.5);
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
 #include <QGLWidget>
4
 #include <QGLWidget>
5
 #include <QMouseEvent>
5
 #include <QMouseEvent>
6
 #include <QWheelEvent>
6
 #include <QWheelEvent>
7
+#include <QKeyEvent>
7
 #include "engine/ugameengine.h"
8
 #include "engine/ugameengine.h"
8
 #include "openglrenderdevice.h"
9
 #include "openglrenderdevice.h"
9
 
10
 
18
     void paintGL();
19
     void paintGL();
19
     void resizeGL(int width, int height);
20
     void resizeGL(int width, int height);
20
 
21
 
22
+    float normalizeAngle(float angle);
23
+
21
     void mousePressEvent(QMouseEvent* event);
24
     void mousePressEvent(QMouseEvent* event);
22
     void mouseMoveEvent(QMouseEvent* event);
25
     void mouseMoveEvent(QMouseEvent* event);
23
     void wheelEvent(QWheelEvent* event);
26
     void wheelEvent(QWheelEvent* event);
27
+    void keyPressEvent(QKeyEvent* event);
28
+
29
+    void rotate(float phi, float theta);
24
 
30
 
25
 signals:
31
 signals:
26
 
32
 
42
 
48
 
43
     float _theta;
49
     float _theta;
44
 
50
 
51
+    bool _reverse;
52
+
45
 };
53
 };
46
 
54
 
47
 #endif // RENDERWIDGET_H
55
 #endif // RENDERWIDGET_H

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

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

Loading…
Cancel
Save