Browse Source

getVectorNearestIntesection

develop
Robin Thoni 7 years ago
parent
commit
fa07b378d1

+ 4
- 2
TheGame/TheGame.pro View File

@@ -16,11 +16,13 @@ CONFIG += c++11
16 16
 SOURCES += main.cpp\
17 17
         mainwindow.cpp \
18 18
     renderwidget.cpp \
19
-    openglrenderdevice.cpp
19
+    openglrenderdevice.cpp \
20
+    gamecube.cpp
20 21
 
21 22
 HEADERS  += mainwindow.h \
22 23
     renderwidget.h \
23
-    openglrenderdevice.h
24
+    openglrenderdevice.h \
25
+    gamecube.h
24 26
 
25 27
 FORMS    += mainwindow.ui
26 28
 

+ 7
- 0
TheGame/gamecube.cpp View File

@@ -0,0 +1,7 @@
1
+#include "gamecube.h"
2
+
3
+GameCube::GameCube(QObject *parent)
4
+    : UGEEntityCube(parent)
5
+{
6
+    _size = 1;
7
+}

+ 14
- 0
TheGame/gamecube.h View File

@@ -0,0 +1,14 @@
1
+#ifndef GAMECUBE_H
2
+#define GAMECUBE_H
3
+
4
+#include "entities/ugeentitycube.h"
5
+
6
+class GameCube : public UGEEntityCube
7
+{
8
+public:
9
+    GameCube(QObject *parent = 0);
10
+
11
+public slots:
12
+};
13
+
14
+#endif // GAMECUBE_H

+ 4
- 4
TheGame/openglrenderdevice.cpp View File

@@ -30,7 +30,7 @@ Vector3D OpenGLRenderDevice::get2DFrom3D(const Vector3D &pos)
30 30
      return Vector3D(gx, gy, gz);
31 31
 }
32 32
 
33
-Vector3D OpenGLRenderDevice::get3DFrom2D(int x, int y)
33
+Vector3D OpenGLRenderDevice::get3DFrom2D(const Vector2D &pos)
34 34
 {
35 35
     double gx, gy, gz;
36 36
     double modelMatrix[16];
@@ -40,7 +40,7 @@ Vector3D OpenGLRenderDevice::get3DFrom2D(int x, int y)
40 40
     glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
41 41
     glGetIntegerv(GL_VIEWPORT, viewport);
42 42
 
43
-    gluUnProject(x, y, 1, modelMatrix, projMatrix, viewport, &gx, &gy, &gz);
43
+    gluUnProject(pos.getX(), pos.getY(), 1, modelMatrix, projMatrix, viewport, &gx, &gy, &gz);
44 44
     return Vector3D(gx, gy, gz);
45 45
 }
46 46
 
@@ -105,7 +105,6 @@ void OpenGLRenderDevice::initialize(int fov, int width, int height)
105 105
 //    glEnable(GL_LIGHT0);
106 106
 //    glEnable(GL_LIGHTING);
107 107
 
108
-    glEnable(GL_DEPTH_TEST);
109 108
 //    glDisable(GL_BLEND);
110 109
 //    glDisable(GL_POLYGON_SMOOTH);
111 110
 //    glEnable(GL_TEXTURE_2D);
@@ -131,6 +130,7 @@ void OpenGLRenderDevice::preDraw()
131 130
 {
132 131
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133 132
     glLoadIdentity();
133
+    glEnable(GL_DEPTH_TEST);
134 134
 
135 135
     gluLookAt(_lookEye.getX(), _lookEye.getY(), _lookEye.getZ(),
136 136
                _lookCenter.getX(), _lookCenter.getY(), _lookCenter.getZ(),
@@ -139,7 +139,7 @@ void OpenGLRenderDevice::preDraw()
139 139
 
140 140
 void OpenGLRenderDevice::postDraw()
141 141
 {
142
-
142
+    glDisable(GL_DEPTH_TEST);
143 143
 }
144 144
 
145 145
 void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)

+ 1
- 1
TheGame/openglrenderdevice.h View File

@@ -17,7 +17,7 @@ public:
17 17
 
18 18
     virtual Vector3D get2DFrom3D(const Vector3D& pos);
19 19
 
20
-    virtual Vector3D get3DFrom2D(int x, int y);
20
+    virtual Vector3D get3DFrom2D(const Vector2D& pos);
21 21
 
22 22
 signals:
23 23
 

+ 25
- 7
TheGame/renderwidget.cpp View File

@@ -8,12 +8,15 @@
8 8
 #include "entities/ugeentitywavefrontobj.h"
9 9
 #include "cameras/rotationcamera.h"
10 10
 #include "cameras/freeflycamera.h"
11
+#include "gamecube.h"
11 12
 
12 13
 RenderWidget::RenderWidget(QWidget *parent) :
13 14
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
14 15
 {
15 16
     setMouseTracking(true);
16 17
     setFocusPolicy(Qt::StrongFocus);
18
+    setAutoFillBackground(false);
19
+    setAutoBufferSwap(false);
17 20
 
18 21
     _engine = new UGameEngine(new OpenGLRenderDevice(this));
19 22
     _camera = new FreeFlyCamera(_engine, this);
@@ -21,9 +24,9 @@ RenderWidget::RenderWidget(QWidget *parent) :
21 24
     _engine->addEntity(new UGEEntityAxes(_engine));
22 25
 
23 26
     for (int i = 0; i < 1000; ++i) {
24
-        UGEEntityCube* cube = new UGEEntityCube(_engine);
27
+        UGEEntityCube* cube = new GameCube(_engine);
25 28
         cube->setTextureId("test");
26
-        cube->rotate(Vector3D(0.0, 45.0, 45.0));
29
+//        cube->rotate(Vector3D(0.0, 45.0, 45.0));
27 30
         cube->move(Vector3D(0, i, i));
28 31
 //        cube->setScale(Vector3D(1.0, 2.0, 1.0));
29 32
         _engine->addEntity(cube);
@@ -51,8 +54,6 @@ void RenderWidget::paintGL()
51 54
 {
52 55
     _camera->updateLookAt();
53 56
     _engine->draw();
54
-//    _device->drawLine(ColorVector3D(Qt::black, 0, 0, 0), ColorVector3D(Qt::black, pos));
55
-//    _device->drawPoint(ColorVector3D(Qt::magenta, 0.5, 0.5, 0.5));
56 57
 }
57 58
 
58 59
 void RenderWidget::resizeGL(int width, int height)
@@ -63,6 +64,10 @@ void RenderWidget::resizeGL(int width, int height)
63 64
 void RenderWidget::mousePressEvent(QMouseEvent *event)
64 65
 {
65 66
     _camera->mousePressEvent(event);
67
+    if (event->buttons() & Qt::LeftButton) {
68
+        Vector3D pos = _engine->get3DFrom2D(Vector2D(event->x(), height() - event->y()));
69
+        qDebug() << pos.getX() << pos.getY() << pos.getZ();
70
+    }
66 71
 }
67 72
 
68 73
 void RenderWidget::mouseReleaseEvent(QMouseEvent *event)
@@ -101,11 +106,24 @@ void RenderWidget::keyReleaseEvent(QKeyEvent *event)
101 106
     _camera->keyReleaseEvent(event);
102 107
 }
103 108
 
109
+void RenderWidget::paintEvent(QPaintEvent *event)
110
+{
111
+    QGLWidget::paintEvent(event);
112
+    int r = 20;
113
+    QPainter p;
114
+    p.begin(this);
115
+    QPoint c(width() / 2, height() / 2);
116
+    p.drawLine(c.x() - r, c.y(), c.x() + r, c.y());
117
+    p.drawLine(c.x(), c.y() - r, c.x(), c.y() + r);
118
+    p.end();
119
+    swapBuffers();
120
+}
121
+
104 122
 void RenderWidget::animate()
105 123
 {
106
-    for (int i = 0; i < _entities.size(); ++i) {
107
-        _entities[i]->rotate(Vector3D(0.0, 2.0, 2.0));
108
-    }
124
+//    for (int i = 0; i < _entities.size(); ++i) {
125
+//        _entities[i]->rotate(Vector3D(0.0, 2.0, 2.0));
126
+//    }
109 127
     QTimer::singleShot(20, this, SLOT(animate()));
110 128
     update();
111 129
 }

+ 2
- 0
TheGame/renderwidget.h View File

@@ -28,6 +28,8 @@ protected:
28 28
     void keyPressEvent(QKeyEvent* event);
29 29
     void keyReleaseEvent(QKeyEvent* event);
30 30
 
31
+    void paintEvent(QPaintEvent* event);
32
+
31 33
 signals:
32 34
 
33 35
 public slots:

+ 1
- 1
UGameEngine/engine/abstractrenderdevice.h View File

@@ -27,7 +27,7 @@ public:
27 27
 
28 28
     virtual Vector3D get2DFrom3D(const Vector3D& pos) = 0;
29 29
 
30
-    virtual Vector3D get3DFrom2D(int x, int y) = 0;
30
+    virtual Vector3D get3DFrom2D(const Vector2D& pos) = 0;
31 31
 
32 32
 public slots:
33 33
     virtual void setClearColor(const QColor &clearColor);

+ 17
- 1
UGameEngine/engine/ugameengine.cpp View File

@@ -59,7 +59,7 @@ void UGameEngine::draw()
59 59
         }
60 60
     }
61 61
     int draw = time.elapsed();
62
-    qDebug() << update << draw << (update + draw);
62
+//    qDebug() << update << draw << (update + draw);
63 63
 
64 64
     _device->postDraw();
65 65
 }
@@ -74,6 +74,22 @@ UGEEntity *UGameEngine::getEntity(int i) const
74 74
     return _entitites[i];
75 75
 }
76 76
 
77
+Vector3D UGameEngine::get2DFrom3D(const Vector3D &pos)
78
+{
79
+    return _device->get2DFrom3D(pos);
80
+}
81
+
82
+Vector3D UGameEngine::get3DFrom2D(const Vector2D &pos)
83
+{
84
+    return _device->get3DFrom2D(pos);
85
+}
86
+
87
+UGEEntity *UGameEngine::getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos)
88
+{
89
+    Vector3D nearestPoint;
90
+    UGEEntity* entity = 0;
91
+}
92
+
77 93
 void UGameEngine::addEntity(UGEEntity *entity)
78 94
 {
79 95
     _entitites.append(entity);

+ 8
- 2
UGameEngine/engine/ugameengine.h View File

@@ -14,14 +14,18 @@ public:
14 14
     UGameEngine(AbstractRenderDevice* device);
15 15
     virtual ~UGameEngine();
16 16
 
17
-    void update();
18
-
19 17
     void draw();
20 18
 
21 19
     const QList<UGEEntity*>& getEntities() const;
22 20
 
23 21
     UGEEntity *getEntity(int i) const;
24 22
 
23
+    Vector3D get2DFrom3D(const Vector3D& pos);
24
+
25
+    Vector3D get3DFrom2D(const Vector2D &pos);
26
+
27
+    UGEEntity* getVectorNearestIntesection(const Vector3D& vector, const Vector3D &pos);
28
+
25 29
 public slots:
26 30
     void addEntity(UGEEntity* entity);
27 31
 
@@ -36,6 +40,8 @@ public slots:
36 40
     void loadTextureFromFile(const QVariant& id, const QString& filename);
37 41
 
38 42
 protected:
43
+    void update();
44
+
39 45
     QList<UGEEntity*> _entitites;
40 46
 
41 47
     AbstractRenderDevice* _device;

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

@@ -59,6 +59,8 @@ public:
59 59
 
60 60
     virtual void onDraw(AbstractRenderDevice* device) = 0;
61 61
 
62
+    virtual Vector3D getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos, bool* ok) = 0;
63
+
62 64
     void update();
63 65
 
64 66
     virtual void onUpdate();
@@ -83,7 +85,7 @@ signals:
83 85
     void colorChanged();
84 86
     void colorChanged(QColor color);
85 87
 
86
-private:
88
+protected:
87 89
     Vector3D _position;
88 90
 
89 91
 //    Vector3D _speed;

+ 6
- 0
UGameEngine/entities/ugeentityaxes.cpp View File

@@ -11,3 +11,9 @@ void UGEEntityAxes::onDraw(AbstractRenderDevice *device)
11 11
     drawLine(device, ColorVector3D(Qt::green, 0.0, 0.0, 0.0), ColorVector3D(Qt::green, 0.0, 1.0, 0.0), 2.5);
12 12
     drawLine(device, ColorVector3D(Qt::blue, 0.0, 0.0, 0.0), ColorVector3D(Qt::blue, 0.0, 0.0, 1.0), 2.5);
13 13
 }
14
+
15
+Vector3D UGEEntityAxes::getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos, bool *ok)
16
+{
17
+    *ok = false;
18
+    return Vector3D();
19
+}

+ 2
- 0
UGameEngine/entities/ugeentityaxes.h View File

@@ -11,6 +11,8 @@ public:
11 11
 
12 12
     virtual void onDraw(AbstractRenderDevice* device);
13 13
 
14
+    virtual Vector3D getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos, bool* ok);
15
+
14 16
 signals:
15 17
 
16 18
 public slots:

+ 40
- 0
UGameEngine/entities/ugeentitycube.cpp View File

@@ -110,3 +110,43 @@ void UGEEntityCube::onUpdate()
110 110
         }
111 111
     }
112 112
 }
113
+
114
+Vector3D UGEEntityCube::getVectorNearestIntesection(const Vector3D &vector, const Vector3D &origin, bool *ok)
115
+{
116
+    *ok = false;
117
+    return Vector3D();
118
+//    Vector3D inv(1.0 / vector.getX(), 1.0 / vector.getY(), 1.0 / vector.getZ());
119
+//    float tmin, tmax, tymin, tymax, tzmin, tzmax;
120
+
121
+//    tmin = (parameters[r.sign[0]].x() - origin.getX()) * inv.getX();
122
+//    tmax = (parameters[1-r.sign[0]].x() - origin.getX()) * inv.getX();
123
+//    tymin = (parameters[r.sign[1]].y() - origin.getY()) * inv.getY();
124
+//    tymax = (parameters[1-r.sign[1]].y() - origin.getY()) * inv.getY();
125
+//    if ( (tmin > tymax) || (tymin > tmax) ) {
126
+//        *ok = false;
127
+//        return Vector3D();
128
+//    }
129
+//    if (tymin > tmin)
130
+//    tmin = tymin;
131
+//    if (tymax < tmax)
132
+//    tmax = tymax;
133
+//    tzmin = (parameters[r.sign[2]].z() - r.origin.z()) * r.inv_direction.z();
134
+//    tzmax = (parameters[1-r.sign[2]].z() - r.origin.z()) * r.inv_direction.z();
135
+//    if ( (tmin > tzmax) || (tzmin > tmax) ) {
136
+//        *ok = false;
137
+//        return Vector3D();
138
+//    }
139
+//    *ok = true;
140
+//    return _position;
141
+
142
+//    if (tzmin > tmin)
143
+//    tmin = tzmin;
144
+//    if (tzmax < tmax)
145
+//    tmax = tzmax;
146
+//    if ((tmin < t1) && (tmax > t0)) {
147
+//        *ok = true;
148
+//        return _position;
149
+//    }
150
+//    *ok = false;
151
+//    return Vector3D();
152
+}

+ 3
- 1
UGameEngine/entities/ugeentitycube.h View File

@@ -16,6 +16,8 @@ public:
16 16
     virtual void onDraw(AbstractRenderDevice* device);
17 17
     void onUpdate();
18 18
 
19
+    virtual Vector3D getVectorNearestIntesection(const Vector3D &vector, const Vector3D &origin, bool* ok);
20
+
19 21
 signals:
20 22
     void sizeChanged();
21 23
     void sizeChanged(double);
@@ -29,7 +31,7 @@ public slots:
29 31
 
30 32
     void setTextureId(const QVariant &textureId);
31 33
 
32
-private:
34
+protected:
33 35
     double _size;
34 36
 
35 37
     QVariant _textureId;

+ 6
- 0
UGameEngine/entities/ugeentitywavefrontobj.cpp View File

@@ -19,3 +19,9 @@ void UGEEntityWaveFrontObj::onDraw(AbstractRenderDevice *device)
19 19
         drawPolygon(device, poly);
20 20
     }
21 21
 }
22
+
23
+Vector3D UGEEntityWaveFrontObj::getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos, bool *ok)
24
+{
25
+    *ok = false;
26
+    return Vector3D();
27
+}

+ 2
- 0
UGameEngine/entities/ugeentitywavefrontobj.h View File

@@ -12,6 +12,8 @@ public:
12 12
 
13 13
     virtual void onDraw(AbstractRenderDevice* device);
14 14
 
15
+    virtual Vector3D getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos, bool *ok);
16
+
15 17
 signals:
16 18
 
17 19
 public slots:

Loading…
Cancel
Save