Преглед на файлове

rad/dregree tools; added rotation and scaling

develop
Robin Thoni преди 8 години
родител
ревизия
38255d20f3

+ 20
- 7
TheGame/renderwidget.cpp Целия файл

@@ -10,15 +10,18 @@
10 10
 RenderWidget::RenderWidget(QWidget *parent) :
11 11
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
12 12
   , _radius(5.0)
13
-  , _phi(45)
14
-  , _theta(45)
13
+  , _phi(45.0)
14
+  , _theta(45.0)
15
+  , _angle(0.0)
15 16
 {
16 17
     _device = new OpenGLRenderDevice(this);
17 18
     _engine = new UGameEngine(_device);
18 19
 
19 20
     UGEEntityCube* cube = new UGEEntityCube(_engine);
20 21
     cube->setTextureId("test");
21
-//    cube->move(Vector3D(0, 1, 0));
22
+    cube->rotate(Vector3D(0.0, 45.0, 45.0));
23
+    cube->move(Vector3D(0, 1, 0));
24
+    cube->setScale(Vector3D(1.0, 2.0, 1.0));
22 25
 //    cube->hide();
23 26
     _engine->addEntity(cube);
24 27
     _engine->addEntity(new UGEEntityAxes(_engine));
@@ -32,6 +35,8 @@ RenderWidget::RenderWidget(QWidget *parent) :
32 35
     _engine->addEntity(obj);
33 36
     obj->hide();
34 37
 
38
+    _entity = cube;
39
+//    animate();
35 40
 }
36 41
 
37 42
 void RenderWidget::initializeGL()
@@ -87,14 +92,14 @@ void RenderWidget::mouseMoveEvent(QMouseEvent *event)
87 92
         rotate(-diff.x(), -diff.y());
88 93
 
89 94
         _lastPoint = event->pos();
95
+        update();
90 96
     }
91 97
 
92
-    Vector3D dd = _device->get2DFrom3D(Vector3D(0.5, 0.5, 0.5));
93
-    dd.setY(height() - dd.getY());
98
+//    Vector3D dd = _device->get2DFrom3D(Vector3D(0.5, 0.5, 0.5));
99
+//    dd.setY(height() - dd.getY());
94 100
 //    qDebug() << event->pos() << dd;
95
-    pos = _device->get3DFrom2D(event->x(), height() - event->y());
101
+//    pos = _device->get3DFrom2D(event->x(), height() - event->y());
96 102
 
97
-    update();
98 103
 }
99 104
 
100 105
 void RenderWidget::wheelEvent(QWheelEvent *event)
@@ -133,3 +138,11 @@ void RenderWidget::rotate(float phi, float theta)
133 138
     }
134 139
     update();
135 140
 }
141
+
142
+void RenderWidget::animate()
143
+{
144
+//    _angle += 0.1;
145
+    _entity->rotate(Vector3D(0.0, 2.0, 2.0));
146
+    QTimer::singleShot(20, this, SLOT(animate()));
147
+    update();
148
+}

+ 8
- 3
TheGame/renderwidget.h Целия файл

@@ -31,6 +31,7 @@ protected:
31 31
 signals:
32 32
 
33 33
 public slots:
34
+    void animate();
34 35
 
35 36
 private:
36 37
     AbstractRenderDevice* _device;
@@ -39,13 +40,17 @@ private:
39 40
 
40 41
     QPoint _lastPoint;
41 42
 
42
-    float _radius;
43
+    double _radius;
43 44
 
44
-    float _phi;
45
+    double _phi;
45 46
 
46
-    float _theta;
47
+    double _theta;
47 48
 
48 49
     Vector3D pos;
50
+
51
+    double _angle;
52
+
53
+    UGEEntity* _entity;
49 54
 };
50 55
 
51 56
 #endif // RENDERWIDGET_H

+ 5
- 2
UGameEngine/UGameEngine.pro Целия файл

@@ -24,7 +24,8 @@ SOURCES += engine/ugameengine.cpp \
24 24
     utils/texturevector3d.cpp \
25 25
     utils/vector2d.cpp \
26 26
     utils/vector3d.cpp \
27
-    utils/matrix3x3.cpp
27
+    utils/matrix3x3.cpp \
28
+    utils/tools.cpp
28 29
 
29 30
 HEADERS += engine/ugameengine.h\
30 31
     engine/abstractrenderdevice.h \
@@ -41,7 +42,9 @@ HEADERS += engine/ugameengine.h\
41 42
     utils/vector2d.h \
42 43
     utils/matrixmxn.h \
43 44
     utils/matrixmxn.hxx \
44
-    utils/matrix3x3.h
45
+    utils/matrix3x3.h \
46
+    utils/tools.h \
47
+    utils/tools.hxx
45 48
 
46 49
 
47 50
 # FLEX && BISON

+ 39
- 4
UGameEngine/entities/ugeentity.cpp Целия файл

@@ -1,7 +1,9 @@
1 1
 #include "ugeentity.h"
2
+#include "utils/tools.h"
2 3
 
3 4
 UGEEntity::UGEEntity(QObject *parent)
4 5
     : QObject(parent)
6
+    , _scale(1.0, 1.0, 1.0)
5 7
     , _visible(true)
6 8
 {
7 9
 }
@@ -51,14 +53,14 @@ Vector3D UGEEntity::getRotation() const
51 53
 
52 54
 void UGEEntity::setRotation(const Vector3D &rotation)
53 55
 {
54
-    _rotation = rotation;
56
+    _rotation = Tools::normalizeAngle(rotation);
55 57
     emit rotationChanged();
56 58
     emit rotationChanged(rotation);
57 59
 }
58 60
 
59 61
 void UGEEntity::rotate(const Vector3D &rotation)
60 62
 {
61
-    _rotation += rotation;
63
+    _rotation = Tools::normalizeAngle(rotation + _rotation);
62 64
     emit rotationChanged();
63 65
     emit rotationChanged(rotation);
64 66
 }
@@ -121,8 +123,13 @@ void UGEEntity::setColor(const QColor &color)
121 123
 
122 124
 Vector3D UGEEntity::getRealPoint(const Vector3D &pos)
123 125
 {
124
-    Vector3D p = pos + _position;
125
-    return p;
126
+    Matrix3x3 scale;
127
+    scale.setScalar(0, 0, _scale.getX());
128
+    scale.setScalar(1, 1, _scale.getY());
129
+    scale.setScalar(2, 2, _scale.getZ());
130
+    Vector3D p = scale.multMatrix(pos);
131
+    Matrix3x3 rot = getRotationMatrix(_rotation);
132
+    return (rot.multMatrix(p) + _position);
126 133
 }
127 134
 
128 135
 ColorVector3D UGEEntity::getRealPoint(const ColorVector3D &pos)
@@ -135,6 +142,34 @@ TextureVector3D UGEEntity::getRealPoint(const TextureVector3D &pos)
135 142
     return TextureVector3D(pos.getTextureCoord(), getRealPoint((ColorVector3D)pos));
136 143
 }
137 144
 
145
+Matrix3x3 UGEEntity::getRotationMatrix(const Vector3D &rotation)
146
+{
147
+    Vector3D r = Tools::degreeToRad(rotation);
148
+
149
+    Matrix3x3 mx;
150
+    mx.setToIdentity();
151
+    mx.setScalar(1, 1, cos(r.getX()));
152
+    mx.setScalar(1, 2, -sin(r.getX()));
153
+    mx.setScalar(2, 1, sin(r.getX()));
154
+    mx.setScalar(2, 2, cos(r.getX()));
155
+
156
+    Matrix3x3 my;
157
+    my.setToIdentity();
158
+    my.setScalar(0, 0, cos(r.getY()));
159
+    my.setScalar(0, 2, sin(r.getY()));
160
+    my.setScalar(2, 0, -sin(r.getY()));
161
+    my.setScalar(2, 2, cos(r.getY()));
162
+
163
+    Matrix3x3 mz;
164
+    mz.setToIdentity();
165
+    mz.setScalar(0, 0, cos(r.getZ()));
166
+    mz.setScalar(0, 1, -sin(r.getZ()));
167
+    mz.setScalar(1, 0, sin(r.getZ()));
168
+    mz.setScalar(1, 1, cos(r.getZ()));
169
+
170
+    return mx.multMatrix(my).multMatrix(mz);
171
+}
172
+
138 173
 void UGEEntity::drawPoint(AbstractRenderDevice *device, const ColorVector3D &point)
139 174
 {
140 175
     device->drawPoint(getRealPoint(point));

+ 3
- 0
UGameEngine/entities/ugeentity.h Целия файл

@@ -5,6 +5,7 @@
5 5
 #include <QPoint>
6 6
 #include <QSharedPointer>
7 7
 #include "utils/vector3d.h"
8
+#include "utils/matrix3x3.h"
8 9
 #include "engine/abstractrenderdevice.h"
9 10
 
10 11
 class UGEEntity : public QObject
@@ -42,6 +43,8 @@ public:
42 43
     ColorVector3D getRealPoint(const ColorVector3D& pos);
43 44
     TextureVector3D getRealPoint(const TextureVector3D& pos);
44 45
 
46
+    Matrix3x3 getRotationMatrix(const Vector3D& rotation);
47
+
45 48
     virtual void drawPoint(AbstractRenderDevice* device, const ColorVector3D& point);
46 49
 
47 50
     virtual void drawLine(AbstractRenderDevice* device, const ColorVector3D& begin, const ColorVector3D& end, double width = 1.0);

+ 4
- 0
UGameEngine/utils/matrixmxn.h Целия файл

@@ -13,6 +13,8 @@ uge_gm_tmpl_mnt class GenericMatrix
13 13
 public:
14 14
     virtual ~GenericMatrix();
15 15
 
16
+    static T getIdentityMatrix();
17
+
16 18
     T& setScalar(unsigned i, unsigned j, double value);
17 19
     double getScalar(unsigned i, unsigned j) const;
18 20
 
@@ -23,6 +25,8 @@ public:
23 25
     T& fill(double k);
24 26
     T& fill(double scalars[M][N]);
25 27
 
28
+    T& setToIdentity();
29
+
26 30
     T& add(double k);
27 31
     T& add(double scalars[M][N]);
28 32
     T& add(const T& other);

+ 19
- 2
UGameEngine/utils/matrixmxn.hxx Целия файл

@@ -24,6 +24,13 @@ uge_gm_tmpl_mnt GenericMatrix<M, N, T>::~GenericMatrix()
24 24
 {
25 25
 }
26 26
 
27
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::getIdentityMatrix()
28
+{
29
+    T m;
30
+    m.setToIdentity();
31
+    return m;
32
+}
33
+
27 34
 uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::setScalar(unsigned i, unsigned j, double value)
28 35
 {
29 36
     _scalars[i][j] = value;
@@ -72,6 +79,16 @@ uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::fill(double scalars[M][N])
72 79
     return *getThis();
73 80
 }
74 81
 
82
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::setToIdentity()
83
+{
84
+    for (int i = 0; i < M; ++i) {
85
+        for (int j = 0; j < N; ++j) {
86
+            _scalars[i][j] = (i == j);
87
+        }
88
+    }
89
+    return *getThis();
90
+}
91
+
75 92
 uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::add(double k)
76 93
 {
77 94
     return add(T(k));
@@ -151,9 +168,9 @@ MatrixMxN<M, P> GenericMatrix<M, N, T>::multMatrix(const MatrixMxN<N, P>& other)
151 168
         for (int j = 0; j < P; ++j) {
152 169
             double t = 0;
153 170
             for (int k = 0; k < N; ++k) {
154
-                t += _scalars[i][k] * other._scalars[k][j];
171
+                t += _scalars[i][k] * other.getScalar(k, j);
155 172
             }
156
-            m._scalars[i][j] = t;
173
+            m.setScalar(i, j, t);
157 174
         }
158 175
     }
159 176
     return m;

+ 23
- 0
UGameEngine/utils/tools.cpp Целия файл

@@ -0,0 +1,23 @@
1
+#include "tools.h"
2
+#include <math.h>
3
+
4
+double Tools::normalizeAngle(double angle)
5
+{
6
+    while (angle >= 360.0) {
7
+        angle -= 360.0;
8
+    }
9
+    while (angle < 0.0) {
10
+        angle += 360.0;
11
+    }
12
+    return angle;
13
+}
14
+
15
+double Tools::radToDegree(double angle)
16
+{
17
+    return angle * 180.0 / M_PI;
18
+}
19
+
20
+double Tools::degreeToRad(double angle)
21
+{
22
+    return angle * M_PI / 180.0;
23
+}

+ 21
- 0
UGameEngine/utils/tools.h Целия файл

@@ -0,0 +1,21 @@
1
+#ifndef TOOLS_H
2
+#define TOOLS_H
3
+
4
+#include "utils/matrixmxn.h"
5
+
6
+class Tools
7
+{
8
+public:
9
+    static double normalizeAngle(double angle);
10
+    uge_gm_tmpl_mn static MatrixMxN<M, N> normalizeAngle(const MatrixMxN<M, N>& rotation);
11
+
12
+    static double radToDegree(double angle);
13
+    uge_gm_tmpl_mn static MatrixMxN<M, N> radToDegree(const MatrixMxN<M, N>& rotation);
14
+
15
+    static double degreeToRad(double angle);
16
+    uge_gm_tmpl_mn static MatrixMxN<M, N> degreeToRad(const MatrixMxN<M, N>& rotation);
17
+};
18
+
19
+#include "utils/tools.hxx"
20
+
21
+#endif // TOOLS_H

+ 28
- 0
UGameEngine/utils/tools.hxx Целия файл

@@ -0,0 +1,28 @@
1
+#ifndef TOOLS_HXX
2
+#define TOOLS_HXX
3
+
4
+#include <math.h>
5
+#define uge_gm_tmpl_mn template<unsigned M, unsigned N>
6
+
7
+uge_gm_tmpl_mn MatrixMxN<M, N> Tools::normalizeAngle(const MatrixMxN<M, N> &rotation)
8
+{
9
+    MatrixMxN<M, N> other;
10
+    for (int i = 0; i < M; ++i) {
11
+        for (int j = 0; j < N; ++j) {
12
+            other.setScalar(i, j, normalizeAngle(rotation.getScalar(i)));
13
+        }
14
+    }
15
+    return other;
16
+}
17
+
18
+uge_gm_tmpl_mn MatrixMxN<M, N> Tools::radToDegree(const MatrixMxN<M, N> &rotation)
19
+{
20
+    return rotation * MatrixMxN<M, N>(180.0 / M_PI);
21
+}
22
+
23
+uge_gm_tmpl_mn MatrixMxN<M, N> Tools::degreeToRad(const MatrixMxN<M, N> &rotation)
24
+{
25
+    return rotation * MatrixMxN<M, N>(M_PI / 180.0);
26
+}
27
+
28
+#endif // TOOLS_HXX

+ 5
- 5
UGameEngine/utils/vectorxd.h Целия файл

@@ -4,10 +4,10 @@
4 4
 #include <QDebug>
5 5
 #include "matrixmxn.h"
6 6
 
7
-#define tmplx template<unsigned X>
8
-#define tmpl template<unsigned X, class T>
7
+#define uge_gv_tmpl_x template<unsigned X>
8
+#define uge_gv_tmpl_xt template<unsigned X, class T>
9 9
 
10
-tmpl class GenericVector: public GenericMatrix<X, 1, T >
10
+uge_gv_tmpl_xt class GenericVector: public GenericMatrix<X, 1, T >
11 11
 {
12 12
 public:
13 13
 
@@ -32,9 +32,9 @@ private:
32 32
     T* getThis() const;
33 33
 };
34 34
 
35
-tmplx using VectorXD = MatrixMxN<X, 1>;
35
+uge_gv_tmpl_x using VectorXD = MatrixMxN<X, 1>;
36 36
 
37
-tmplx class MatrixMxN<X, 1>: public GenericVector<X, VectorXD<X>>
37
+uge_gv_tmpl_x class MatrixMxN<X, 1>: public GenericVector<X, VectorXD<X>>
38 38
 {
39 39
 public:
40 40
     MatrixMxN();

+ 15
- 15
UGameEngine/utils/vectorxd.hxx Целия файл

@@ -1,46 +1,46 @@
1 1
 #include <math.h>
2 2
 
3
-#define tmplx template<unsigned X>
4
-#define tmpl template<unsigned X, class T>
3
+#define uge_gv_tmpl_x template<unsigned X>
4
+#define uge_gv_tmpl_xt template<unsigned X, class T>
5 5
 
6
-tmplx VectorXD<X>::MatrixMxN()
6
+uge_gv_tmpl_x VectorXD<X>::MatrixMxN()
7 7
 {
8 8
     this->fill(0);
9 9
 }
10 10
 
11
-tmplx VectorXD<X>::MatrixMxN(double k)
11
+uge_gv_tmpl_x VectorXD<X>::MatrixMxN(double k)
12 12
 {
13 13
     this->fill(k);
14 14
 }
15 15
 
16
-tmplx VectorXD<X>::MatrixMxN(const double scalars[X])
16
+uge_gv_tmpl_x VectorXD<X>::MatrixMxN(const double scalars[X])
17 17
 {
18 18
     this->fill(scalars);
19 19
 }
20 20
 
21
-tmplx VectorXD<X>::MatrixMxN(const VectorXD<X>& other)
21
+uge_gv_tmpl_x VectorXD<X>::MatrixMxN(const VectorXD<X>& other)
22 22
 {
23 23
     for (unsigned i = 0; i < X; ++i) {
24 24
         _scalars[i] = other._scalars[i];
25 25
     }
26 26
 }
27 27
 
28
-tmpl T& GenericVector<X, T>::setScalar(unsigned i, double value)
28
+uge_gv_tmpl_xt T& GenericVector<X, T>::setScalar(unsigned i, double value)
29 29
 {
30 30
     return setScalar(i, 0,value);
31 31
 }
32 32
 
33
-tmpl double GenericVector<X, T>::getScalar(unsigned i) const
33
+uge_gv_tmpl_xt double GenericVector<X, T>::getScalar(unsigned i) const
34 34
 {
35 35
     return getScalar(i, 0);
36 36
 }
37 37
 
38
-tmpl double GenericVector<X, T>::operator[](unsigned i) const
38
+uge_gv_tmpl_xt double GenericVector<X, T>::operator[](unsigned i) const
39 39
 {
40 40
     return getScalar(i);
41 41
 }
42 42
 
43
-tmpl double GenericVector<X, T>::dotProduct(const T &other) const
43
+uge_gv_tmpl_xt double GenericVector<X, T>::dotProduct(const T &other) const
44 44
 {
45 45
     double total = 0;
46 46
     for (unsigned i = 0; i < X; ++i) {
@@ -49,7 +49,7 @@ tmpl double GenericVector<X, T>::dotProduct(const T &other) const
49 49
     return total;
50 50
 }
51 51
 
52
-tmpl T& GenericVector<X, T>::crossProduct(const T& other)
52
+uge_gv_tmpl_xt T& GenericVector<X, T>::crossProduct(const T& other)
53 53
 {
54 54
     T t = *getThis();
55 55
     for (unsigned i = 0; i < X; ++i) {
@@ -60,12 +60,12 @@ tmpl T& GenericVector<X, T>::crossProduct(const T& other)
60 60
     return *getThis();
61 61
 }
62 62
 
63
-tmpl T GenericVector<X, T>::crossProduct(const T &v1, const T &v2)
63
+uge_gv_tmpl_xt T GenericVector<X, T>::crossProduct(const T &v1, const T &v2)
64 64
 {
65 65
     return T(v1).crossProduct(v2);
66 66
 }
67 67
 
68
-tmpl double GenericVector<X, T>::norm() const
68
+uge_gv_tmpl_xt double GenericVector<X, T>::norm() const
69 69
 {
70 70
     double total = 0;
71 71
     for (unsigned i = 0; i < X; ++i) {
@@ -74,12 +74,12 @@ tmpl double GenericVector<X, T>::norm() const
74 74
     return sqrt(total);
75 75
 }
76 76
 
77
-tmpl T* GenericVector<X, T>::getThis() const
77
+uge_gv_tmpl_xt T* GenericVector<X, T>::getThis() const
78 78
 {
79 79
     return (T*)this;
80 80
 }
81 81
 
82
-//tmpl QDebug operator<<(QDebug dbg, const T &v)
82
+//uge_gv_tmpl_xt QDebug operator<<(QDebug dbg, const T &v)
83 83
 //{
84 84
 //    return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
85 85
 //}

Loading…
Отказ
Запис