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

+ 2
- 0
TheGame/renderwidget.cpp Целия файл

@@ -2,6 +2,7 @@
2 2
 #include <QTimer>
3 3
 #include <math.h>
4 4
 #include <QDebug>
5
+#include "entities/ugeentitycube.h"
5 6
 
6 7
 RenderWidget::RenderWidget(QWidget *parent) :
7 8
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
@@ -13,6 +14,7 @@ RenderWidget::RenderWidget(QWidget *parent) :
13 14
 {
14 15
     _device = new OpenGLRenderDevice(this);
15 16
     _engine = new UGameEngine(_device);
17
+    _engine->addEntity(new UGEEntityCube(_engine));
16 18
     setMouseTracking(false);
17 19
     setFocusPolicy(Qt::StrongFocus);
18 20
 }

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

@@ -15,13 +15,15 @@ SOURCES += engine/ugameengine.cpp \
15 15
     engine/abstractrenderdevice.cpp \
16 16
     entities/ugeentity.cpp \
17 17
     utils/vector3d.cpp \
18
-    utils/colorvector3d.cpp
18
+    utils/colorvector3d.cpp \
19
+    entities/ugeentitycube.cpp
19 20
 
20 21
 HEADERS += engine/ugameengine.h\
21 22
     engine/abstractrenderdevice.h \
22 23
     entities/ugeentity.h \
23 24
     utils/vector3d.h \
24
-    utils/colorvector3d.h
25
+    utils/colorvector3d.h \
26
+    entities/ugeentitycube.h
25 27
 
26 28
 unix {
27 29
     target.path = /usr/lib

+ 8
- 1
UGameEngine/engine/ugameengine.cpp Целия файл

@@ -16,8 +16,15 @@ void UGameEngine::draw()
16 16
 
17 17
     for(int i = 0; i < _entitites.size(); ++i) {
18 18
         UGEEntity* entity = _entitites[i];
19
-        entity->draw(_device);
19
+        if (entity->isVisible()) {
20
+            entity->draw(_device);
21
+        }
20 22
     }
21 23
 
22 24
     _device->postDraw();
23 25
 }
26
+
27
+void UGameEngine::addEntity(UGEEntity *entity)
28
+{
29
+    _entitites.append(entity);
30
+}

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

@@ -13,6 +13,9 @@ public:
13 13
 
14 14
     void draw();
15 15
 
16
+public slots:
17
+    void addEntity(UGEEntity* entity);
18
+
16 19
 protected:
17 20
     QList<UGEEntity*> _entitites;
18 21
 

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

@@ -89,3 +89,30 @@ void UGEEntity::hide()
89 89
     _visible = false;
90 90
 }
91 91
 
92
+Vector3D UGEEntity::getRealPoint(const Vector3D &pos)
93
+{
94
+    return pos;
95
+}
96
+
97
+ColorVector3D UGEEntity::getRealPoint(const ColorVector3D &pos)
98
+{
99
+    return ColorVector3D(pos.getColor(), getRealPoint((Vector3D)pos));
100
+}
101
+
102
+void UGEEntity::drawPoint(AbstractRenderDevice *device, const ColorVector3D &point)
103
+{
104
+    device->drawPoint(getRealPoint(point));
105
+}
106
+
107
+void UGEEntity::drawLine(AbstractRenderDevice *device, const ColorVector3D &begin, const ColorVector3D &end, double width)
108
+{
109
+    device->drawLine(getRealPoint(begin), getRealPoint(end), width);
110
+}
111
+
112
+void UGEEntity::drawPolygon(AbstractRenderDevice *device, QList<ColorVector3D> points)
113
+{
114
+    for (int i = 0; i < points.size(); ++i) {
115
+        points[i] = getRealPoint(points[i]);
116
+    }
117
+    device->drawPolygon(points);
118
+}

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

@@ -35,6 +35,15 @@ public:
35 35
     void show();
36 36
     void hide();
37 37
 
38
+    Vector3D getRealPoint(const Vector3D& pos);
39
+    ColorVector3D getRealPoint(const ColorVector3D& pos);
40
+
41
+    virtual void drawPoint(AbstractRenderDevice* device, const ColorVector3D& point);
42
+
43
+    virtual void drawLine(AbstractRenderDevice* device, const ColorVector3D& begin, const ColorVector3D& end, double width = 1.0);
44
+
45
+    virtual void drawPolygon(AbstractRenderDevice* device, QList<ColorVector3D> points);
46
+
38 47
     virtual void draw(AbstractRenderDevice* device) = 0;
39 48
 
40 49
 private:

+ 33
- 0
UGameEngine/entities/ugeentitycube.cpp Целия файл

@@ -0,0 +1,33 @@
1
+#include "ugeentitycube.h"
2
+
3
+UGEEntityCube::UGEEntityCube(QObject *parent)
4
+    : UGEEntity(parent)
5
+    , _size(1.0)
6
+{
7
+}
8
+
9
+float UGEEntityCube::getSize() const
10
+{
11
+    return _size;
12
+}
13
+
14
+void UGEEntityCube::draw(AbstractRenderDevice *device)
15
+{
16
+    QColor color;
17
+    float r = _size / 2;
18
+    QList<ColorVector3D> points;
19
+    points << ColorVector3D(color, -r, -r, r) << ColorVector3D(color, -r, r, r) << ColorVector3D(color, r, r, r) << ColorVector3D(color, r, -r, r);
20
+    points << ColorVector3D(color, -r, -r, -r) << ColorVector3D(color, -r, r, -r) << ColorVector3D(color, r, r, -r) << ColorVector3D(color, r, -r, -r);
21
+    drawPolygon(device, QList<ColorVector3D>() << points[3] << points[2] << points[1] << points[0]);
22
+    drawPolygon(device, QList<ColorVector3D>() << points[2] << points[3] << points[7] << points[6]);
23
+    drawPolygon(device, QList<ColorVector3D>() << points[6] << points[7] << points[4] << points[5]);
24
+    drawPolygon(device, QList<ColorVector3D>() << points[5] << points[4] << points[0] << points[1]);
25
+    drawPolygon(device, QList<ColorVector3D>() << points[1] << points[2] << points[6] << points[5]);
26
+    drawPolygon(device, QList<ColorVector3D>() << points[4] << points[7] << points[3] << points[0]);
27
+}
28
+
29
+void UGEEntityCube::setSize(float size)
30
+{
31
+    _size = size;
32
+}
33
+

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

@@ -0,0 +1,26 @@
1
+#ifndef UGEENTITYCUBE_H
2
+#define UGEENTITYCUBE_H
3
+
4
+#include "ugeentity.h"
5
+
6
+class UGEEntityCube : public UGEEntity
7
+{
8
+    Q_OBJECT
9
+public:
10
+    explicit UGEEntityCube(QObject *parent = 0);
11
+
12
+    float getSize() const;
13
+
14
+    virtual void draw(AbstractRenderDevice* device);
15
+
16
+signals:
17
+
18
+public slots:
19
+    void setSize(float size);
20
+
21
+private:
22
+    float _size;
23
+
24
+};
25
+
26
+#endif // UGEENTITYCUBE_H

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