Browse Source

rad/dregree tools; added rotation and scaling

develop
Robin Thoni 7 years ago
parent
commit
38255d20f3

+ 20
- 7
TheGame/renderwidget.cpp View File

10
 RenderWidget::RenderWidget(QWidget *parent) :
10
 RenderWidget::RenderWidget(QWidget *parent) :
11
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
11
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
12
   , _radius(5.0)
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
     _device = new OpenGLRenderDevice(this);
17
     _device = new OpenGLRenderDevice(this);
17
     _engine = new UGameEngine(_device);
18
     _engine = new UGameEngine(_device);
18
 
19
 
19
     UGEEntityCube* cube = new UGEEntityCube(_engine);
20
     UGEEntityCube* cube = new UGEEntityCube(_engine);
20
     cube->setTextureId("test");
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
 //    cube->hide();
25
 //    cube->hide();
23
     _engine->addEntity(cube);
26
     _engine->addEntity(cube);
24
     _engine->addEntity(new UGEEntityAxes(_engine));
27
     _engine->addEntity(new UGEEntityAxes(_engine));
32
     _engine->addEntity(obj);
35
     _engine->addEntity(obj);
33
     obj->hide();
36
     obj->hide();
34
 
37
 
38
+    _entity = cube;
39
+//    animate();
35
 }
40
 }
36
 
41
 
37
 void RenderWidget::initializeGL()
42
 void RenderWidget::initializeGL()
87
         rotate(-diff.x(), -diff.y());
92
         rotate(-diff.x(), -diff.y());
88
 
93
 
89
         _lastPoint = event->pos();
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
 //    qDebug() << event->pos() << dd;
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
 void RenderWidget::wheelEvent(QWheelEvent *event)
105
 void RenderWidget::wheelEvent(QWheelEvent *event)
133
     }
138
     }
134
     update();
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 View File

31
 signals:
31
 signals:
32
 
32
 
33
 public slots:
33
 public slots:
34
+    void animate();
34
 
35
 
35
 private:
36
 private:
36
     AbstractRenderDevice* _device;
37
     AbstractRenderDevice* _device;
39
 
40
 
40
     QPoint _lastPoint;
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
     Vector3D pos;
49
     Vector3D pos;
50
+
51
+    double _angle;
52
+
53
+    UGEEntity* _entity;
49
 };
54
 };
50
 
55
 
51
 #endif // RENDERWIDGET_H
56
 #endif // RENDERWIDGET_H

+ 5
- 2
UGameEngine/UGameEngine.pro View File

24
     utils/texturevector3d.cpp \
24
     utils/texturevector3d.cpp \
25
     utils/vector2d.cpp \
25
     utils/vector2d.cpp \
26
     utils/vector3d.cpp \
26
     utils/vector3d.cpp \
27
-    utils/matrix3x3.cpp
27
+    utils/matrix3x3.cpp \
28
+    utils/tools.cpp
28
 
29
 
29
 HEADERS += engine/ugameengine.h\
30
 HEADERS += engine/ugameengine.h\
30
     engine/abstractrenderdevice.h \
31
     engine/abstractrenderdevice.h \
41
     utils/vector2d.h \
42
     utils/vector2d.h \
42
     utils/matrixmxn.h \
43
     utils/matrixmxn.h \
43
     utils/matrixmxn.hxx \
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
 # FLEX && BISON
50
 # FLEX && BISON

+ 39
- 4
UGameEngine/entities/ugeentity.cpp View File

1
 #include "ugeentity.h"
1
 #include "ugeentity.h"
2
+#include "utils/tools.h"
2
 
3
 
3
 UGEEntity::UGEEntity(QObject *parent)
4
 UGEEntity::UGEEntity(QObject *parent)
4
     : QObject(parent)
5
     : QObject(parent)
6
+    , _scale(1.0, 1.0, 1.0)
5
     , _visible(true)
7
     , _visible(true)
6
 {
8
 {
7
 }
9
 }
51
 
53
 
52
 void UGEEntity::setRotation(const Vector3D &rotation)
54
 void UGEEntity::setRotation(const Vector3D &rotation)
53
 {
55
 {
54
-    _rotation = rotation;
56
+    _rotation = Tools::normalizeAngle(rotation);
55
     emit rotationChanged();
57
     emit rotationChanged();
56
     emit rotationChanged(rotation);
58
     emit rotationChanged(rotation);
57
 }
59
 }
58
 
60
 
59
 void UGEEntity::rotate(const Vector3D &rotation)
61
 void UGEEntity::rotate(const Vector3D &rotation)
60
 {
62
 {
61
-    _rotation += rotation;
63
+    _rotation = Tools::normalizeAngle(rotation + _rotation);
62
     emit rotationChanged();
64
     emit rotationChanged();
63
     emit rotationChanged(rotation);
65
     emit rotationChanged(rotation);
64
 }
66
 }
121
 
123
 
122
 Vector3D UGEEntity::getRealPoint(const Vector3D &pos)
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
 ColorVector3D UGEEntity::getRealPoint(const ColorVector3D &pos)
135
 ColorVector3D UGEEntity::getRealPoint(const ColorVector3D &pos)
135
     return TextureVector3D(pos.getTextureCoord(), getRealPoint((ColorVector3D)pos));
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
 void UGEEntity::drawPoint(AbstractRenderDevice *device, const ColorVector3D &point)
173
 void UGEEntity::drawPoint(AbstractRenderDevice *device, const ColorVector3D &point)
139
 {
174
 {
140
     device->drawPoint(getRealPoint(point));
175
     device->drawPoint(getRealPoint(point));

+ 3
- 0
UGameEngine/entities/ugeentity.h View File

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

+ 4
- 0
UGameEngine/utils/matrixmxn.h View File

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

+ 19
- 2
UGameEngine/utils/matrixmxn.hxx View File

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
 uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::setScalar(unsigned i, unsigned j, double value)
34
 uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::setScalar(unsigned i, unsigned j, double value)
28
 {
35
 {
29
     _scalars[i][j] = value;
36
     _scalars[i][j] = value;
72
     return *getThis();
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
 uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::add(double k)
92
 uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::add(double k)
76
 {
93
 {
77
     return add(T(k));
94
     return add(T(k));
151
         for (int j = 0; j < P; ++j) {
168
         for (int j = 0; j < P; ++j) {
152
             double t = 0;
169
             double t = 0;
153
             for (int k = 0; k < N; ++k) {
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
     return m;
176
     return m;

+ 23
- 0
UGameEngine/utils/tools.cpp View File

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 View File

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 View File

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 View File

4
 #include <QDebug>
4
 #include <QDebug>
5
 #include "matrixmxn.h"
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
 public:
12
 public:
13
 
13
 
32
     T* getThis() const;
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
 public:
39
 public:
40
     MatrixMxN();
40
     MatrixMxN();

+ 15
- 15
UGameEngine/utils/vectorxd.hxx View File

1
 #include <math.h>
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
     this->fill(0);
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
     this->fill(k);
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
     this->fill(scalars);
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
     for (unsigned i = 0; i < X; ++i) {
23
     for (unsigned i = 0; i < X; ++i) {
24
         _scalars[i] = other._scalars[i];
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
     return setScalar(i, 0,value);
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
     return getScalar(i, 0);
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
     return getScalar(i);
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
     double total = 0;
45
     double total = 0;
46
     for (unsigned i = 0; i < X; ++i) {
46
     for (unsigned i = 0; i < X; ++i) {
49
     return total;
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
     T t = *getThis();
54
     T t = *getThis();
55
     for (unsigned i = 0; i < X; ++i) {
55
     for (unsigned i = 0; i < X; ++i) {
60
     return *getThis();
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
     return T(v1).crossProduct(v2);
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
     double total = 0;
70
     double total = 0;
71
     for (unsigned i = 0; i < X; ++i) {
71
     for (unsigned i = 0; i < X; ++i) {
74
     return sqrt(total);
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
     return (T*)this;
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
 //    return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
84
 //    return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
85
 //}
85
 //}

Loading…
Cancel
Save