Переглянути джерело

opengl draw functions; 3d drag camera

develop
Robin Thoni 8 роки тому
джерело
коміт
11c9e57bd4

+ 35
- 3
TheGame/openglrenderdevice.cpp Переглянути файл

@@ -9,8 +9,8 @@ OpenGLRenderDevice::OpenGLRenderDevice(QObject *parent) :
9 9
 
10 10
 void OpenGLRenderDevice::initialize(int fov, int width, int height)
11 11
 {
12
-    glClearColor((GLfloat)_clearColor.red() / 255.0, (GLfloat)_clearColor.green() / 255.0,
13
-                      (GLfloat)_clearColor.blue() / 255.0, (GLfloat) _clearColor.alpha() / 255.0);
12
+    glClearColor(_clearColor.redF(), _clearColor.greenF(),
13
+                 _clearColor.blue(), _clearColor.alpha());
14 14
 
15 15
     glEnable(GL_DEPTH_TEST);
16 16
     glEnable(GL_CULL_FACE);
@@ -18,7 +18,7 @@ void OpenGLRenderDevice::initialize(int fov, int width, int height)
18 18
     glEnable(GL_MULTISAMPLE);
19 19
 
20 20
     glMatrixMode(GL_PROJECTION);
21
-    gluPerspective(fov, width/height, 0.1, 100.0);
21
+    gluPerspective(fov, width / height, 0.1, 100.0);
22 22
     glMatrixMode(GL_MODELVIEW);
23 23
 }
24 24
 
@@ -42,3 +42,35 @@ void OpenGLRenderDevice::postDraw()
42 42
 {
43 43
 
44 44
 }
45
+
46
+void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)
47
+{
48
+    glColor4f(point.getColor().redF(), point.getColor().greenF(),
49
+                  point.getColor().blue(), point.getColor().alpha());
50
+    glVertex3d(point.getX(), point.getY(), point.getZ());
51
+}
52
+
53
+void OpenGLRenderDevice::drawPoint(const ColorVector3D &point)
54
+{
55
+    glBegin(GL_POINTS);
56
+    drawVertex(point);
57
+    glEnd();
58
+}
59
+
60
+void OpenGLRenderDevice::drawLine(const ColorVector3D &begin, const ColorVector3D &end, double width)
61
+{
62
+    glLineWidth(width);
63
+    glBegin(GL_LINES);
64
+    drawVertex(begin);
65
+    drawVertex(end);
66
+    glEnd();
67
+}
68
+
69
+void OpenGLRenderDevice::drawPolygon(const QList<ColorVector3D> &points)
70
+{
71
+    glBegin(GL_POLYGON);
72
+    for (int i = 0; i < points.size(); ++i) {
73
+        drawVertex(points[i]);
74
+    }
75
+    glEnd();
76
+}

+ 10
- 0
TheGame/openglrenderdevice.h Переглянути файл

@@ -9,6 +9,8 @@ class OpenGLRenderDevice : public AbstractRenderDevice
9 9
 public:
10 10
     explicit OpenGLRenderDevice(QObject *parent = 0);
11 11
 
12
+//    static float
13
+
12 14
 signals:
13 15
 
14 16
 public slots:
@@ -21,6 +23,14 @@ public slots:
21 23
 
22 24
     virtual void postDraw();
23 25
 
26
+    virtual void drawVertex(const ColorVector3D& point);
27
+
28
+    virtual void drawPoint(const ColorVector3D& point);
29
+
30
+    virtual void drawLine(const ColorVector3D& begin, const ColorVector3D& end, double width = 1.0);
31
+
32
+    virtual void drawPolygon(const QList<ColorVector3D>& points);
33
+
24 34
 };
25 35
 
26 36
 #endif // OPENGLRENDERDEVICE_H

+ 36
- 24
TheGame/renderwidget.cpp Переглянути файл

@@ -1,13 +1,18 @@
1 1
 #include "renderwidget.h"
2 2
 #include <QTimer>
3 3
 #include <math.h>
4
+#include <QDebug>
4 5
 
5 6
 RenderWidget::RenderWidget(QWidget *parent) :
6 7
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
7 8
   , angle(0)
9
+  , _radius(5.0)
10
+  , _phi(45)
11
+  , _theta(45)
8 12
 {
9 13
     _device = new OpenGLRenderDevice(this);
10 14
     _engine = new UGameEngine(_device);
15
+    setMouseTracking(false);
11 16
 }
12 17
 
13 18
 void RenderWidget::initializeGL()
@@ -16,14 +21,17 @@ void RenderWidget::initializeGL()
16 21
     _device->setClearColor(Qt::gray);
17 22
     _device->initialize(70, width(), height());
18 23
 
19
-    animate();
20 24
 }
21 25
 
22 26
 void RenderWidget::paintGL()
23 27
 {
24
-    float rad = (float)angle / 180.0 * M_PI;
25
-    float radius = 5.0f;
26
-    _device->lookAt(Vector3D(radius * cos(rad), 5.0f, radius * sin(rad)),
28
+    float theta = _theta / 180.0 * M_PI;
29
+    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
+                        ),
27 35
                    Vector3D(0.0f, 0.0f, 0.0f));
28 36
     _engine->draw();
29 37
 
@@ -35,32 +43,36 @@ void RenderWidget::resizeGL(int width, int height)
35 43
     _device->resize(width, height);
36 44
 }
37 45
 
38
-void RenderWidget::animate()
46
+void RenderWidget::mousePressEvent(QMouseEvent *event)
39 47
 {
40
-    angle = (angle + 3) % 360;
48
+    _lastPoint = event->pos();
49
+}
50
+
51
+void RenderWidget::mouseMoveEvent(QMouseEvent *event)
52
+{
53
+    QPoint diff = event->pos() - _lastPoint;
54
+
55
+    _phi -= diff.x();
56
+    _theta -= diff.y();
57
+
58
+    qDebug() << _phi << _theta;
59
+
60
+    _lastPoint = event->pos();
41 61
     update();
42
-    QTimer::singleShot(50, this, SLOT(animate()));
43 62
 }
44 63
 
45
-void RenderWidget::drawAxes()
64
+void RenderWidget::wheelEvent(QWheelEvent *event)
46 65
 {
47
-    glLineWidth(2.5);
66
+    _radius = qMax(2.0, _radius - event->delta() / 20.0);
67
+    update();
68
+}
48 69
 
49
-    glColor3f(1.0, 0.0, 0.0);
50
-    glBegin(GL_LINES);
51
-    glVertex3f(0.0, 0.0, 0.0);
52
-    glVertex3f(1.0, 0.0, 0.0);
53
-    glEnd();
70
+void RenderWidget::drawAxes()
71
+{
72
+    _device->drawLine(ColorVector3D(Qt::red, 0.0, 0.0, 0.0), ColorVector3D(Qt::red, 1.0, 0.0, 0.0), 2.5);
73
+    _device->drawLine(ColorVector3D(Qt::green, 0.0, 0.0, 0.0), ColorVector3D(Qt::green, 0.0, 1.0, 0.0), 2.5);
74
+    _device->drawLine(ColorVector3D(Qt::blue, 0.0, 0.0, 0.0), ColorVector3D(Qt::blue, 0.0, 0.0, 1.0), 2.5);
54 75
 
55
-    glColor3f(0.0, 1.0, 0.0);
56
-    glBegin(GL_LINES);
57
-    glVertex3f(0.0, 0.0, 0.0);
58
-    glVertex3f(0.0, 1.0, 0.0);
59
-    glEnd();
76
+    _device->drawPolygon(QList<ColorVector3D>() << ColorVector3D(Qt::red, 0, 0, 0) << ColorVector3D(Qt::green, 1, 0, 0) << ColorVector3D(Qt::transparent, 1, 1, 0) << ColorVector3D(Qt::blue, 0, 1, 0));
60 77
 
61
-    glColor3f(0.0, 0.0, 1.0);
62
-    glBegin(GL_LINES);
63
-    glVertex3f(0.0, 0.0, 0.0);
64
-    glVertex3f(0.0, 0.0, 1.0);
65
-    glEnd();
66 78
 }

+ 14
- 2
TheGame/renderwidget.h Переглянути файл

@@ -2,6 +2,8 @@
2 2
 #define RENDERWIDGET_H
3 3
 
4 4
 #include <QGLWidget>
5
+#include <QMouseEvent>
6
+#include <QWheelEvent>
5 7
 #include "ugameengine.h"
6 8
 #include "openglrenderdevice.h"
7 9
 
@@ -16,11 +18,13 @@ protected:
16 18
     void paintGL();
17 19
     void resizeGL(int width, int height);
18 20
 
21
+    void mousePressEvent(QMouseEvent* event);
22
+    void mouseMoveEvent(QMouseEvent* event);
23
+    void wheelEvent(QWheelEvent* event);
24
+
19 25
 signals:
20 26
 
21 27
 public slots:
22
-    void animate();
23
-
24 28
     void drawAxes();
25 29
 
26 30
 private:
@@ -30,6 +34,14 @@ private:
30 34
 
31 35
     UGameEngine* _engine;
32 36
 
37
+    QPoint _lastPoint;
38
+
39
+    float _radius;
40
+
41
+    float _phi;
42
+
43
+    float _theta;
44
+
33 45
 };
34 46
 
35 47
 #endif // RENDERWIDGET_H

+ 4
- 2
UGameEngine/UGameEngine.pro Переглянути файл

@@ -14,12 +14,14 @@ DEFINES += UGAMEENGINE_LIBRARY
14 14
 SOURCES += ugameengine.cpp \
15 15
     ugeentity.cpp \
16 16
     vector3d.cpp \
17
-    abstractrenderdevice.cpp
17
+    abstractrenderdevice.cpp \
18
+    colorvector3d.cpp
18 19
 
19 20
 HEADERS += ugameengine.h\
20 21
     ugeentity.h \
21 22
     vector3d.h \
22
-    abstractrenderdevice.h
23
+    abstractrenderdevice.h \
24
+    colorvector3d.h
23 25
 
24 26
 unix {
25 27
     target.path = /usr/lib

+ 7
- 1
UGameEngine/abstractrenderdevice.h Переглянути файл

@@ -3,7 +3,7 @@
3 3
 
4 4
 #include <QObject>
5 5
 #include <QColor>
6
-#include "vector3d.h"
6
+#include "colorvector3d.h"
7 7
 
8 8
 class AbstractRenderDevice : public QObject
9 9
 {
@@ -38,6 +38,12 @@ public slots:
38 38
 
39 39
     virtual void postDraw() = 0;
40 40
 
41
+    virtual void drawPoint(const ColorVector3D& point) = 0;
42
+
43
+    virtual void drawLine(const ColorVector3D& begin, const ColorVector3D& end, double width = 1.0) = 0;
44
+
45
+    virtual void drawPolygon(const QList<ColorVector3D>& points) = 0;
46
+
41 47
 protected:
42 48
     QColor _clearColor;
43 49
 

+ 30
- 0
UGameEngine/colorvector3d.cpp Переглянути файл

@@ -0,0 +1,30 @@
1
+#include "colorvector3d.h"
2
+
3
+ColorVector3D::ColorVector3D(QColor color, double x, double y, double z)
4
+    : Vector3D(x, y, z)
5
+    , _color(color)
6
+{
7
+}
8
+
9
+ColorVector3D::ColorVector3D(QColor color, const Vector3D &other)
10
+    : Vector3D(other)
11
+    , _color(color)
12
+{
13
+}
14
+
15
+ColorVector3D::ColorVector3D(const ColorVector3D &other)
16
+    : Vector3D(other)
17
+    , _color(other._color)
18
+{
19
+}
20
+
21
+QColor ColorVector3D::getColor() const
22
+{
23
+    return _color;
24
+}
25
+
26
+void ColorVector3D::setColor(const QColor &color)
27
+{
28
+    _color = color;
29
+}
30
+

+ 21
- 0
UGameEngine/colorvector3d.h Переглянути файл

@@ -0,0 +1,21 @@
1
+#ifndef COLORVECTOR3D_H
2
+#define COLORVECTOR3D_H
3
+
4
+#include <QColor>
5
+#include "vector3d.h"
6
+
7
+class ColorVector3D : public Vector3D
8
+{
9
+public:
10
+    ColorVector3D(QColor color = Qt::black, double x = 0.0, double y = 0.0, double z = 0.0);
11
+    ColorVector3D(QColor color = Qt::black, const Vector3D& other = Vector3D());
12
+    ColorVector3D(const ColorVector3D& other);
13
+
14
+    QColor getColor() const;
15
+    void setColor(const QColor &color);
16
+
17
+private:
18
+    QColor _color;
19
+};
20
+
21
+#endif // COLORVECTOR3D_H

+ 4
- 0
UGameEngine/vector3d.cpp Переглянути файл

@@ -15,6 +15,10 @@ Vector3D::Vector3D(const Vector3D &other)
15 15
 {
16 16
 }
17 17
 
18
+Vector3D::~Vector3D()
19
+{
20
+}
21
+
18 22
 double Vector3D::getX() const
19 23
 {
20 24
     return _x;

+ 1
- 0
UGameEngine/vector3d.h Переглянути файл

@@ -6,6 +6,7 @@ class Vector3D
6 6
 public:
7 7
     explicit Vector3D(double x = 0.0, double y = 0.0, double z = 0.0);
8 8
     Vector3D(const Vector3D& other);
9
+    virtual ~Vector3D();
9 10
 
10 11
     double getX() const;
11 12
     Vector3D& setX(double x);

Завантаження…
Відмінити
Зберегти