Browse Source

getVectorNearestIntesection

develop
Robin Thoni 7 years ago
parent
commit
fa07b378d1

+ 4
- 2
TheGame/TheGame.pro View File

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

+ 7
- 0
TheGame/gamecube.cpp View File

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

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
      return Vector3D(gx, gy, gz);
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
     double gx, gy, gz;
35
     double gx, gy, gz;
36
     double modelMatrix[16];
36
     double modelMatrix[16];
40
     glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
40
     glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
41
     glGetIntegerv(GL_VIEWPORT, viewport);
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
     return Vector3D(gx, gy, gz);
44
     return Vector3D(gx, gy, gz);
45
 }
45
 }
46
 
46
 
105
 //    glEnable(GL_LIGHT0);
105
 //    glEnable(GL_LIGHT0);
106
 //    glEnable(GL_LIGHTING);
106
 //    glEnable(GL_LIGHTING);
107
 
107
 
108
-    glEnable(GL_DEPTH_TEST);
109
 //    glDisable(GL_BLEND);
108
 //    glDisable(GL_BLEND);
110
 //    glDisable(GL_POLYGON_SMOOTH);
109
 //    glDisable(GL_POLYGON_SMOOTH);
111
 //    glEnable(GL_TEXTURE_2D);
110
 //    glEnable(GL_TEXTURE_2D);
131
 {
130
 {
132
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
131
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133
     glLoadIdentity();
132
     glLoadIdentity();
133
+    glEnable(GL_DEPTH_TEST);
134
 
134
 
135
     gluLookAt(_lookEye.getX(), _lookEye.getY(), _lookEye.getZ(),
135
     gluLookAt(_lookEye.getX(), _lookEye.getY(), _lookEye.getZ(),
136
                _lookCenter.getX(), _lookCenter.getY(), _lookCenter.getZ(),
136
                _lookCenter.getX(), _lookCenter.getY(), _lookCenter.getZ(),
139
 
139
 
140
 void OpenGLRenderDevice::postDraw()
140
 void OpenGLRenderDevice::postDraw()
141
 {
141
 {
142
-
142
+    glDisable(GL_DEPTH_TEST);
143
 }
143
 }
144
 
144
 
145
 void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)
145
 void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)

+ 1
- 1
TheGame/openglrenderdevice.h View File

17
 
17
 
18
     virtual Vector3D get2DFrom3D(const Vector3D& pos);
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
 signals:
22
 signals:
23
 
23
 

+ 25
- 7
TheGame/renderwidget.cpp View File

8
 #include "entities/ugeentitywavefrontobj.h"
8
 #include "entities/ugeentitywavefrontobj.h"
9
 #include "cameras/rotationcamera.h"
9
 #include "cameras/rotationcamera.h"
10
 #include "cameras/freeflycamera.h"
10
 #include "cameras/freeflycamera.h"
11
+#include "gamecube.h"
11
 
12
 
12
 RenderWidget::RenderWidget(QWidget *parent) :
13
 RenderWidget::RenderWidget(QWidget *parent) :
13
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
14
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
14
 {
15
 {
15
     setMouseTracking(true);
16
     setMouseTracking(true);
16
     setFocusPolicy(Qt::StrongFocus);
17
     setFocusPolicy(Qt::StrongFocus);
18
+    setAutoFillBackground(false);
19
+    setAutoBufferSwap(false);
17
 
20
 
18
     _engine = new UGameEngine(new OpenGLRenderDevice(this));
21
     _engine = new UGameEngine(new OpenGLRenderDevice(this));
19
     _camera = new FreeFlyCamera(_engine, this);
22
     _camera = new FreeFlyCamera(_engine, this);
21
     _engine->addEntity(new UGEEntityAxes(_engine));
24
     _engine->addEntity(new UGEEntityAxes(_engine));
22
 
25
 
23
     for (int i = 0; i < 1000; ++i) {
26
     for (int i = 0; i < 1000; ++i) {
24
-        UGEEntityCube* cube = new UGEEntityCube(_engine);
27
+        UGEEntityCube* cube = new GameCube(_engine);
25
         cube->setTextureId("test");
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
         cube->move(Vector3D(0, i, i));
30
         cube->move(Vector3D(0, i, i));
28
 //        cube->setScale(Vector3D(1.0, 2.0, 1.0));
31
 //        cube->setScale(Vector3D(1.0, 2.0, 1.0));
29
         _engine->addEntity(cube);
32
         _engine->addEntity(cube);
51
 {
54
 {
52
     _camera->updateLookAt();
55
     _camera->updateLookAt();
53
     _engine->draw();
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
 void RenderWidget::resizeGL(int width, int height)
59
 void RenderWidget::resizeGL(int width, int height)
63
 void RenderWidget::mousePressEvent(QMouseEvent *event)
64
 void RenderWidget::mousePressEvent(QMouseEvent *event)
64
 {
65
 {
65
     _camera->mousePressEvent(event);
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
 void RenderWidget::mouseReleaseEvent(QMouseEvent *event)
73
 void RenderWidget::mouseReleaseEvent(QMouseEvent *event)
101
     _camera->keyReleaseEvent(event);
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
 void RenderWidget::animate()
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
     QTimer::singleShot(20, this, SLOT(animate()));
127
     QTimer::singleShot(20, this, SLOT(animate()));
110
     update();
128
     update();
111
 }
129
 }

+ 2
- 0
TheGame/renderwidget.h View File

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

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

27
 
27
 
28
     virtual Vector3D get2DFrom3D(const Vector3D& pos) = 0;
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
 public slots:
32
 public slots:
33
     virtual void setClearColor(const QColor &clearColor);
33
     virtual void setClearColor(const QColor &clearColor);

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

59
         }
59
         }
60
     }
60
     }
61
     int draw = time.elapsed();
61
     int draw = time.elapsed();
62
-    qDebug() << update << draw << (update + draw);
62
+//    qDebug() << update << draw << (update + draw);
63
 
63
 
64
     _device->postDraw();
64
     _device->postDraw();
65
 }
65
 }
74
     return _entitites[i];
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
 void UGameEngine::addEntity(UGEEntity *entity)
93
 void UGameEngine::addEntity(UGEEntity *entity)
78
 {
94
 {
79
     _entitites.append(entity);
95
     _entitites.append(entity);

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

14
     UGameEngine(AbstractRenderDevice* device);
14
     UGameEngine(AbstractRenderDevice* device);
15
     virtual ~UGameEngine();
15
     virtual ~UGameEngine();
16
 
16
 
17
-    void update();
18
-
19
     void draw();
17
     void draw();
20
 
18
 
21
     const QList<UGEEntity*>& getEntities() const;
19
     const QList<UGEEntity*>& getEntities() const;
22
 
20
 
23
     UGEEntity *getEntity(int i) const;
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
 public slots:
29
 public slots:
26
     void addEntity(UGEEntity* entity);
30
     void addEntity(UGEEntity* entity);
27
 
31
 
36
     void loadTextureFromFile(const QVariant& id, const QString& filename);
40
     void loadTextureFromFile(const QVariant& id, const QString& filename);
37
 
41
 
38
 protected:
42
 protected:
43
+    void update();
44
+
39
     QList<UGEEntity*> _entitites;
45
     QList<UGEEntity*> _entitites;
40
 
46
 
41
     AbstractRenderDevice* _device;
47
     AbstractRenderDevice* _device;

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

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

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

11
     drawLine(device, ColorVector3D(Qt::green, 0.0, 0.0, 0.0), ColorVector3D(Qt::green, 0.0, 1.0, 0.0), 2.5);
11
     drawLine(device, ColorVector3D(Qt::green, 0.0, 0.0, 0.0), ColorVector3D(Qt::green, 0.0, 1.0, 0.0), 2.5);
12
     drawLine(device, ColorVector3D(Qt::blue, 0.0, 0.0, 0.0), ColorVector3D(Qt::blue, 0.0, 0.0, 1.0), 2.5);
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
 
11
 
12
     virtual void onDraw(AbstractRenderDevice* device);
12
     virtual void onDraw(AbstractRenderDevice* device);
13
 
13
 
14
+    virtual Vector3D getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos, bool* ok);
15
+
14
 signals:
16
 signals:
15
 
17
 
16
 public slots:
18
 public slots:

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

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

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

19
         drawPolygon(device, poly);
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
 
12
 
13
     virtual void onDraw(AbstractRenderDevice* device);
13
     virtual void onDraw(AbstractRenderDevice* device);
14
 
14
 
15
+    virtual Vector3D getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos, bool *ok);
16
+
15
 signals:
17
 signals:
16
 
18
 
17
 public slots:
19
 public slots:

Loading…
Cancel
Save