Browse Source

Engine; opengl render

develop
Robin Thoni 7 years ago
parent
commit
28faed7639

+ 4
- 2
TheGame/TheGame.pro View File

14
 
14
 
15
 SOURCES += main.cpp\
15
 SOURCES += main.cpp\
16
         mainwindow.cpp \
16
         mainwindow.cpp \
17
-    renderwidget.cpp
17
+    renderwidget.cpp \
18
+    openglrenderdevice.cpp
18
 
19
 
19
 HEADERS  += mainwindow.h \
20
 HEADERS  += mainwindow.h \
20
-    renderwidget.h
21
+    renderwidget.h \
22
+    openglrenderdevice.h
21
 
23
 
22
 FORMS    += mainwindow.ui
24
 FORMS    += mainwindow.ui
23
 
25
 

+ 44
- 0
TheGame/openglrenderdevice.cpp View File

1
+#include "openglrenderdevice.h"
2
+#include "GL/gl.h"
3
+#include "GL/glu.h"
4
+
5
+OpenGLRenderDevice::OpenGLRenderDevice(QObject *parent) :
6
+    AbstractRenderDevice(parent)
7
+{
8
+}
9
+
10
+void OpenGLRenderDevice::initialize(int fov, int width, int height)
11
+{
12
+    glClearColor((GLfloat)_clearColor.red() / 255.0, (GLfloat)_clearColor.green() / 255.0,
13
+                      (GLfloat)_clearColor.blue() / 255.0, (GLfloat) _clearColor.alpha() / 255.0);
14
+
15
+    glEnable(GL_DEPTH_TEST);
16
+    glEnable(GL_CULL_FACE);
17
+    glShadeModel(GL_SMOOTH);
18
+    glEnable(GL_MULTISAMPLE);
19
+
20
+    glMatrixMode(GL_PROJECTION);
21
+    gluPerspective(fov, width/height, 0.1, 100.0);
22
+    glMatrixMode(GL_MODELVIEW);
23
+}
24
+
25
+void OpenGLRenderDevice::resize(int width, int height)
26
+{
27
+    int side = qMin(width, height);
28
+    glViewport((width - side) / 2, (height - side) / 2, side, side);
29
+}
30
+
31
+void OpenGLRenderDevice::preDraw()
32
+{
33
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
34
+    glLoadIdentity();
35
+
36
+    gluLookAt(_lookEye.getX(), _lookEye.getY(), _lookEye.getZ(),
37
+               _lookCenter.getX(), _lookCenter.getY(), _lookCenter.getZ(),
38
+               _lookUp.getX(), _lookUp.getY(), _lookUp.getZ());
39
+}
40
+
41
+void OpenGLRenderDevice::postDraw()
42
+{
43
+
44
+}

+ 26
- 0
TheGame/openglrenderdevice.h View File

1
+#ifndef OPENGLRENDERDEVICE_H
2
+#define OPENGLRENDERDEVICE_H
3
+
4
+#include "abstractrenderdevice.h"
5
+
6
+class OpenGLRenderDevice : public AbstractRenderDevice
7
+{
8
+    Q_OBJECT
9
+public:
10
+    explicit OpenGLRenderDevice(QObject *parent = 0);
11
+
12
+signals:
13
+
14
+public slots:
15
+
16
+    virtual void initialize(int fov, int width, int height);
17
+
18
+    virtual void resize(int width, int height);
19
+
20
+    virtual void preDraw();
21
+
22
+    virtual void postDraw();
23
+
24
+};
25
+
26
+#endif // OPENGLRENDERDEVICE_H

+ 14
- 19
TheGame/renderwidget.cpp View File

1
 #include "renderwidget.h"
1
 #include "renderwidget.h"
2
-#include <GL/glu.h>
3
 #include <QTimer>
2
 #include <QTimer>
4
 #include <math.h>
3
 #include <math.h>
5
 
4
 
6
 RenderWidget::RenderWidget(QWidget *parent) :
5
 RenderWidget::RenderWidget(QWidget *parent) :
7
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
6
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
8
   , angle(0)
7
   , angle(0)
9
-  , _fov(70)
10
 {
8
 {
9
+    _device = new OpenGLRenderDevice(this);
10
+    _engine = new UGameEngine(_device);
11
 }
11
 }
12
 
12
 
13
 void RenderWidget::initializeGL()
13
 void RenderWidget::initializeGL()
14
 {
14
 {
15
     makeCurrent();
15
     makeCurrent();
16
-    qglClearColor(Qt::gray);
16
+    _device->setClearColor(Qt::gray);
17
+    _device->initialize(70, width(), height());
17
 
18
 
18
-    glEnable(GL_DEPTH_TEST);
19
-    glEnable(GL_CULL_FACE);
20
-    glShadeModel(GL_SMOOTH);
21
-    glEnable(GL_MULTISAMPLE);
22
-
23
-    glMatrixMode(GL_PROJECTION);
24
-    gluPerspective(_fov, width()/height(), 0.1, 100.0);
25
-    glMatrixMode(GL_MODELVIEW);
26
     animate();
19
     animate();
27
 }
20
 }
28
 
21
 
29
 void RenderWidget::paintGL()
22
 void RenderWidget::paintGL()
30
 {
23
 {
31
-    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
32
-    glLoadIdentity();
33
-
34
     float rad = (float)angle / 180.0 * M_PI;
24
     float rad = (float)angle / 180.0 * M_PI;
35
     float radius = 5.0f;
25
     float radius = 5.0f;
26
+    _device->lookAt(Vector3D(radius * cos(rad), 5.0f, radius * sin(rad)),
27
+                   Vector3D(0.0f, 0.0f, 0.0f));
28
+    _engine->draw();
29
+//    device->preDraw();
30
+
31
+
32
+
36
 
33
 
37
-    gluLookAt(radius * cos(rad), 5.0f, radius * sin(rad),
38
-               0.0f, 0.0f, 0.0f,
39
-               0.0f, 1.0f, 0.0f);
40
 
34
 
41
     drawAxes();
35
     drawAxes();
36
+
37
+//    device->postDraw();
42
 }
38
 }
43
 
39
 
44
 void RenderWidget::resizeGL(int width, int height)
40
 void RenderWidget::resizeGL(int width, int height)
45
 {
41
 {
46
-    int side = qMin(width, height);
47
-    glViewport((width - side) / 2, (height - side) / 2, side, side);
42
+    _device->resize(width, height);
48
 }
43
 }
49
 
44
 
50
 void RenderWidget::animate()
45
 void RenderWidget::animate()

+ 4
- 3
TheGame/renderwidget.h View File

3
 
3
 
4
 #include <QGLWidget>
4
 #include <QGLWidget>
5
 #include "ugameengine.h"
5
 #include "ugameengine.h"
6
+#include "openglrenderdevice.h"
6
 
7
 
7
 class RenderWidget : public QGLWidget
8
 class RenderWidget : public QGLWidget
8
 {
9
 {
23
     void drawAxes();
24
     void drawAxes();
24
 
25
 
25
 private:
26
 private:
26
-    int angle;
27
+    OpenGLRenderDevice* _device;
27
 
28
 
28
-    float _fov;
29
+    int angle;
29
 
30
 
30
-    QSharedPointer<UGameEngine> _engine;
31
+    UGameEngine* _engine;
31
 
32
 
32
 };
33
 };
33
 
34
 

+ 5
- 4
UGameEngine/UGameEngine.pro View File

4
 #
4
 #
5
 #-------------------------------------------------
5
 #-------------------------------------------------
6
 
6
 
7
-QT       -= gui
7
+QT       += gui core
8
 
8
 
9
 TARGET = UGameEngine
9
 TARGET = UGameEngine
10
 TEMPLATE = lib
10
 TEMPLATE = lib
13
 
13
 
14
 SOURCES += ugameengine.cpp \
14
 SOURCES += ugameengine.cpp \
15
     ugeentity.cpp \
15
     ugeentity.cpp \
16
-    vector3d.cpp
16
+    vector3d.cpp \
17
+    abstractrenderdevice.cpp
17
 
18
 
18
 HEADERS += ugameengine.h\
19
 HEADERS += ugameengine.h\
19
-        ugameengine_global.h \
20
     ugeentity.h \
20
     ugeentity.h \
21
-    vector3d.h
21
+    vector3d.h \
22
+    abstractrenderdevice.h
22
 
23
 
23
 unix {
24
 unix {
24
     target.path = /usr/lib
25
     target.path = /usr/lib

+ 55
- 0
UGameEngine/abstractrenderdevice.cpp View File

1
+#include "abstractrenderdevice.h"
2
+
3
+AbstractRenderDevice::AbstractRenderDevice(QObject *parent) :
4
+    QObject(parent)
5
+{
6
+}
7
+
8
+AbstractRenderDevice::~AbstractRenderDevice()
9
+{
10
+
11
+}
12
+
13
+QColor AbstractRenderDevice::getCurrentColor() const
14
+{
15
+    return _currentColor;
16
+}
17
+
18
+Vector3D AbstractRenderDevice::getLookEye() const
19
+{
20
+    return _lookEye;
21
+}
22
+
23
+Vector3D AbstractRenderDevice::getLookCenter() const
24
+{
25
+    return _lookCenter;
26
+}
27
+
28
+Vector3D AbstractRenderDevice::getLookUp() const
29
+{
30
+    return _lookUp;
31
+}
32
+
33
+void AbstractRenderDevice::setCurrentColor(const QColor &color)
34
+{
35
+    _currentColor = color;
36
+}
37
+
38
+void AbstractRenderDevice::lookAt(const Vector3D &eye, const Vector3D &center, const Vector3D &up)
39
+{
40
+    _lookEye = eye;
41
+    _lookCenter = center;
42
+    _lookUp = up;
43
+}
44
+
45
+QColor AbstractRenderDevice::getClearColor() const
46
+{
47
+    return _clearColor;
48
+}
49
+
50
+void AbstractRenderDevice::setClearColor(const QColor &clearColor)
51
+{
52
+    _clearColor = clearColor;
53
+}
54
+
55
+

+ 53
- 0
UGameEngine/abstractrenderdevice.h View File

1
+#ifndef ABSTRACTRENDERDEVICE_H
2
+#define ABSTRACTRENDERDEVICE_H
3
+
4
+#include <QObject>
5
+#include <QColor>
6
+#include "vector3d.h"
7
+
8
+class AbstractRenderDevice : public QObject
9
+{
10
+    Q_OBJECT
11
+public:
12
+    explicit AbstractRenderDevice(QObject *parent = 0);
13
+
14
+    virtual ~AbstractRenderDevice();
15
+
16
+    QColor getCurrentColor() const;
17
+
18
+    Vector3D getLookEye() const;
19
+
20
+    Vector3D getLookCenter() const;
21
+
22
+    Vector3D getLookUp() const;
23
+
24
+    QColor getClearColor() const;
25
+
26
+public slots:
27
+    virtual void setClearColor(const QColor &getClearColor);
28
+
29
+    virtual void setCurrentColor(const QColor &color);
30
+
31
+    virtual void lookAt(const Vector3D& eye, const Vector3D& center, const Vector3D& up = Vector3D(0.0, 1.0, 0.0));
32
+
33
+    virtual void initialize(int fov, int width, int height) = 0;
34
+
35
+    virtual void resize(int width, int height) = 0;
36
+
37
+    virtual void preDraw() = 0;
38
+
39
+    virtual void postDraw() = 0;
40
+
41
+protected:
42
+    QColor _clearColor;
43
+
44
+    QColor _currentColor;
45
+
46
+    Vector3D _lookEye;
47
+
48
+    Vector3D _lookCenter;
49
+
50
+    Vector3D _lookUp;
51
+};
52
+
53
+#endif // ABSTRACTRENDERDEVICE_H

+ 14
- 1
UGameEngine/ugameengine.cpp View File

1
 #include "ugameengine.h"
1
 #include "ugameengine.h"
2
 
2
 
3
 
3
 
4
-UGameEngine::UGameEngine()
4
+UGameEngine::UGameEngine(AbstractRenderDevice* device)
5
+    : _device(device)
5
 {
6
 {
6
 }
7
 }
8
+
9
+void UGameEngine::draw()
10
+{
11
+    _device->preDraw();
12
+
13
+    for(int i = 0; i < _entitites.size(); ++i) {
14
+        UGEEntity* entity = _entitites[i];
15
+        entity->draw(_device);
16
+    }
17
+
18
+    _device->postDraw();
19
+}

+ 12
- 4
UGameEngine/ugameengine.h View File

1
 #ifndef UGAMEENGINE_H
1
 #ifndef UGAMEENGINE_H
2
 #define UGAMEENGINE_H
2
 #define UGAMEENGINE_H
3
 
3
 
4
-#include "ugameengine_global.h"
4
+#include "abstractrenderdevice.h"
5
+#include "ugeentity.h"
5
 
6
 
6
-class UGAMEENGINESHARED_EXPORT UGameEngine
7
+class UGameEngine : public QObject
7
 {
8
 {
8
-
9
+    Q_OBJECT
9
 public:
10
 public:
10
-    UGameEngine();
11
+    UGameEngine(AbstractRenderDevice* device);
12
+
13
+    void draw();
14
+
15
+protected:
16
+    QList<UGEEntity*> _entitites;
17
+
18
+    AbstractRenderDevice* _device;
11
 };
19
 };
12
 
20
 
13
 #endif // UGAMEENGINE_H
21
 #endif // UGAMEENGINE_H

+ 0
- 12
UGameEngine/ugameengine_global.h View File

1
-#ifndef UGAMEENGINE_GLOBAL_H
2
-#define UGAMEENGINE_GLOBAL_H
3
-
4
-#include <QtCore/qglobal.h>
5
-
6
-#if defined(UGAMEENGINE_LIBRARY)
7
-#  define UGAMEENGINESHARED_EXPORT Q_DECL_EXPORT
8
-#else
9
-#  define UGAMEENGINESHARED_EXPORT Q_DECL_IMPORT
10
-#endif
11
-
12
-#endif // UGAMEENGINE_GLOBAL_H

+ 60
- 0
UGameEngine/ugeentity.cpp View File

4
     QObject(parent)
4
     QObject(parent)
5
 {
5
 {
6
 }
6
 }
7
+
8
+Vector3D UGEEntity::getPosition() const
9
+{
10
+    return _position;
11
+}
12
+
13
+void UGEEntity::setPosition(const Vector3D &position)
14
+{
15
+    _position = position;
16
+}
17
+
18
+void UGEEntity::move(const Vector3D &move)
19
+{
20
+    _position += move;
21
+}
22
+
23
+Vector3D UGEEntity::getSpeed() const
24
+{
25
+    return _speed;
26
+}
27
+
28
+void UGEEntity::setSpeed(const Vector3D &speed)
29
+{
30
+    _speed = speed;
31
+}
32
+
33
+void UGEEntity::accelerate(const Vector3D &speed)
34
+{
35
+    _speed += speed;
36
+}
37
+
38
+Vector3D UGEEntity::getRotation() const
39
+{
40
+    return _rotation;
41
+}
42
+
43
+void UGEEntity::setRotation(const Vector3D &rotation)
44
+{
45
+    _rotation = rotation;
46
+}
47
+
48
+void UGEEntity::rotate(const Vector3D &rotation)
49
+{
50
+    _rotation += rotation;
51
+}
52
+
53
+Vector3D UGEEntity::getScale() const
54
+{
55
+    return _scale;
56
+}
57
+
58
+void UGEEntity::setScale(const Vector3D &scale)
59
+{
60
+    _scale = scale;
61
+}
62
+
63
+void UGEEntity::scale(const Vector3D &scale)
64
+{
65
+    _scale += scale;
66
+}

+ 24
- 2
UGameEngine/ugeentity.h View File

3
 
3
 
4
 #include <QObject>
4
 #include <QObject>
5
 #include <QPoint>
5
 #include <QPoint>
6
+#include <QSharedPointer>
6
 #include "vector3d.h"
7
 #include "vector3d.h"
8
+#include "abstractrenderdevice.h"
7
 
9
 
8
 class UGEEntity : public QObject
10
 class UGEEntity : public QObject
9
 {
11
 {
11
 public:
13
 public:
12
     explicit UGEEntity(QObject *parent = 0);
14
     explicit UGEEntity(QObject *parent = 0);
13
 
15
 
14
-signals:
16
+    Vector3D getPosition() const;
17
+    void setPosition(const Vector3D& position);
18
+    void move(const Vector3D& move);
15
 
19
 
16
-public slots:
20
+    Vector3D getSpeed() const;
21
+    void setSpeed(const Vector3D& speed);
22
+    void accelerate(const Vector3D& speed);
23
+
24
+    Vector3D getRotation() const;
25
+    void setRotation(const Vector3D& rotation);
26
+    void rotate(const Vector3D& rotation);
27
+
28
+    Vector3D getScale() const;
29
+    void setScale(const Vector3D& scale);
30
+    void scale(const Vector3D& scale);
31
+
32
+    virtual void draw(AbstractRenderDevice* device) = 0;
17
 
33
 
18
 private:
34
 private:
19
     Vector3D _position;
35
     Vector3D _position;
20
 
36
 
37
+    Vector3D _speed;
38
+
39
+    Vector3D _rotation;
40
+
41
+    Vector3D _scale;
42
+
21
 };
43
 };
22
 
44
 
23
 #endif // UGEENTITY_H
45
 #endif // UGEENTITY_H

+ 42
- 6
UGameEngine/vector3d.cpp View File

133
     return Vector3D(*this).add(k);
133
     return Vector3D(*this).add(k);
134
 }
134
 }
135
 
135
 
136
-Vector3D Vector3D::operator+(const Vector3D &v2)
136
+Vector3D &Vector3D::operator+=(const double &k)
137
 {
137
 {
138
-    return Vector3D(*this).add(v2);
138
+    return add(k);
139
+}
140
+
141
+Vector3D Vector3D::operator+(const Vector3D &other)
142
+{
143
+    return Vector3D(*this).add(other);
144
+}
145
+
146
+Vector3D &Vector3D::operator+=(const Vector3D &other)
147
+{
148
+    return add(other);
139
 }
149
 }
140
 
150
 
141
 Vector3D Vector3D::operator-()
151
 Vector3D Vector3D::operator-()
148
     return Vector3D(*this).sub(k);
158
     return Vector3D(*this).sub(k);
149
 }
159
 }
150
 
160
 
151
-Vector3D Vector3D::operator-(const Vector3D &v2)
161
+Vector3D &Vector3D::operator-=(const double &k)
162
+{
163
+    return sub(k);
164
+}
165
+
166
+Vector3D Vector3D::operator-(const Vector3D &other)
167
+{
168
+    return Vector3D(*this).sub(other);
169
+}
170
+
171
+Vector3D &Vector3D::operator-=(const Vector3D &other)
152
 {
172
 {
153
-    return Vector3D(*this).sub(v2);
173
+    return sub(other);
154
 }
174
 }
155
 
175
 
156
 Vector3D Vector3D::operator*(const double &k)
176
 Vector3D Vector3D::operator*(const double &k)
158
     return Vector3D(*this).mult(k);
178
     return Vector3D(*this).mult(k);
159
 }
179
 }
160
 
180
 
161
-double Vector3D::operator*(const Vector3D &v2)
181
+Vector3D &Vector3D::operator*=(const double &k)
182
+{
183
+    return mult(k);
184
+}
185
+
186
+double Vector3D::operator*(const Vector3D &other)
162
 {
187
 {
163
-    return Vector3D(*this).dotProduct(v2);
188
+    return Vector3D(*this).dotProduct(other);
189
+}
190
+
191
+Vector3D &Vector3D::operator*=(const Vector3D &other)
192
+{
193
+    dotProduct(other);
194
+    return *this;
164
 }
195
 }
165
 
196
 
166
 Vector3D Vector3D::operator/(const double &k)
197
 Vector3D Vector3D::operator/(const double &k)
168
     return Vector3D(*this).div(k);
199
     return Vector3D(*this).div(k);
169
 }
200
 }
170
 
201
 
202
+Vector3D &Vector3D::operator/=(const double &k)
203
+{
204
+    return div(k);
205
+}
206
+
171
 bool Vector3D::operator==(const Vector3D &other)
207
 bool Vector3D::operator==(const Vector3D &other)
172
 {
208
 {
173
     return equal(other);
209
     return equal(other);

+ 10
- 3
UGameEngine/vector3d.h View File

39
 
39
 
40
     Vector3D operator+();
40
     Vector3D operator+();
41
     Vector3D operator+(const double& k);
41
     Vector3D operator+(const double& k);
42
-    Vector3D operator+(const Vector3D& v2);
42
+    Vector3D& operator+=(const double& k);
43
+    Vector3D operator+(const Vector3D& other);
44
+    Vector3D& operator+=(const Vector3D& other);
43
 
45
 
44
     Vector3D operator-();
46
     Vector3D operator-();
45
     Vector3D operator-(const double& k);
47
     Vector3D operator-(const double& k);
46
-    Vector3D operator-(const Vector3D& v2);
48
+    Vector3D& operator-=(const double& k);
49
+    Vector3D operator-(const Vector3D& other);
50
+    Vector3D& operator-=(const Vector3D& other);
47
 
51
 
48
     Vector3D operator*(const double& k);
52
     Vector3D operator*(const double& k);
49
-    double operator*(const Vector3D& v2);
53
+    Vector3D& operator*=(const double& k);
54
+    double operator*(const Vector3D& other);
55
+    Vector3D& operator*=(const Vector3D& other);
50
 
56
 
51
     Vector3D operator/(const double& k);
57
     Vector3D operator/(const double& k);
58
+    Vector3D& operator/=(const double& k);
52
 
59
 
53
     bool operator==(const Vector3D& other);
60
     bool operator==(const Vector3D& other);
54
     bool operator!=(const Vector3D& other);
61
     bool operator!=(const Vector3D& other);

Loading…
Cancel
Save