Browse Source

added gluunproject/gluproject in render device

develop
Robin Thoni 7 years ago
parent
commit
00d36979f4

+ 41
- 5
TheGame/openglrenderdevice.cpp View File

2
 #include "GL/gl.h"
2
 #include "GL/gl.h"
3
 #include "GL/glu.h"
3
 #include "GL/glu.h"
4
 
4
 
5
-OpenGLRenderDevice::OpenGLRenderDevice(QObject *parent) :
6
-    AbstractRenderDevice(parent)
5
+OpenGLRenderDevice::OpenGLRenderDevice(QObject *parent)
6
+    : AbstractRenderDevice(parent)
7
+    , _width(0)
8
+    , _height(0)
9
+    , _fov(0)
7
 {
10
 {
8
 }
11
 }
9
 
12
 
13
+Vector3D OpenGLRenderDevice::get2DFrom3D(const Vector3D &pos)
14
+{
15
+    double gx, gy, gz;
16
+    double modelMatrix[16];
17
+    double projMatrix[16];
18
+    GLint viewport[4];
19
+    glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
20
+    glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
21
+    glGetIntegerv(GL_VIEWPORT, viewport);
22
+
23
+     gluProject(pos.getX(), pos.getY(), pos.getZ(), modelMatrix, projMatrix, viewport, &gx, &gy, &gz);
24
+     return Vector3D(gx, gy, gz);
25
+}
26
+
27
+Vector3D OpenGLRenderDevice::get3DFrom2D(int x, int y)
28
+{
29
+    double gx, gy, gz;
30
+    double modelMatrix[16];
31
+    double projMatrix[16];
32
+    GLint viewport[4];
33
+    glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
34
+    glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
35
+    glGetIntegerv(GL_VIEWPORT, viewport);
36
+
37
+    gluUnProject(x, y, 1, modelMatrix, projMatrix, viewport, &gx, &gy, &gz);
38
+    return Vector3D(gx, gy, gz);
39
+}
40
+
10
 void OpenGLRenderDevice::initialize(int fov, int width, int height)
41
 void OpenGLRenderDevice::initialize(int fov, int width, int height)
11
 {
42
 {
43
+    _fov = fov;
44
+    _width = width;
45
+    _height = height;
12
     glClearColor(_clearColor.redF(), _clearColor.greenF(),
46
     glClearColor(_clearColor.redF(), _clearColor.greenF(),
13
                  _clearColor.blue(), _clearColor.alpha());
47
                  _clearColor.blue(), _clearColor.alpha());
14
 
48
 
18
     glEnable(GL_MULTISAMPLE);
52
     glEnable(GL_MULTISAMPLE);
19
 
53
 
20
     glMatrixMode(GL_PROJECTION);
54
     glMatrixMode(GL_PROJECTION);
21
-    gluPerspective(fov, width / height, 0.1, 100.0);
55
+    gluPerspective(_fov, _width / _height, 0.1, 100.0);
22
     glMatrixMode(GL_MODELVIEW);
56
     glMatrixMode(GL_MODELVIEW);
23
 }
57
 }
24
 
58
 
25
 void OpenGLRenderDevice::resize(int width, int height)
59
 void OpenGLRenderDevice::resize(int width, int height)
26
 {
60
 {
27
-    int side = qMin(width, height);
28
-    glViewport((width - side) / 2, (height - side) / 2, side, side);
61
+    _width = width;
62
+    _height = height;
63
+    int side = qMin(_width, _height);
64
+    glViewport((_width - side) / 2, (_height - side) / 2, side, side);
29
 }
65
 }
30
 
66
 
31
 void OpenGLRenderDevice::preDraw()
67
 void OpenGLRenderDevice::preDraw()

+ 11
- 0
TheGame/openglrenderdevice.h View File

9
 public:
9
 public:
10
     explicit OpenGLRenderDevice(QObject *parent = 0);
10
     explicit OpenGLRenderDevice(QObject *parent = 0);
11
 
11
 
12
+    virtual Vector3D get2DFrom3D(const Vector3D& pos);
13
+
14
+    virtual Vector3D get3DFrom2D(int x, int y);
15
+
12
 signals:
16
 signals:
13
 
17
 
14
 public slots:
18
 public slots:
29
 
33
 
30
     virtual void drawPolygon(const QList<ColorVector3D>& points);
34
     virtual void drawPolygon(const QList<ColorVector3D>& points);
31
 
35
 
36
+private:
37
+    int _width;
38
+
39
+    int _height;
40
+
41
+    int _fov;
42
+
32
 };
43
 };
33
 
44
 
34
 #endif // OPENGLRENDERDEVICE_H
45
 #endif // OPENGLRENDERDEVICE_H

+ 9
- 13
TheGame/renderwidget.cpp View File

8
 
8
 
9
 RenderWidget::RenderWidget(QWidget *parent) :
9
 RenderWidget::RenderWidget(QWidget *parent) :
10
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
10
     QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
11
-  , angle(0)
12
   , _radius(5.0)
11
   , _radius(5.0)
13
   , _phi(45)
12
   , _phi(45)
14
   , _theta(45)
13
   , _theta(45)
15
-  , _reverse(false)
16
 {
14
 {
17
     _device = new OpenGLRenderDevice(this);
15
     _device = new OpenGLRenderDevice(this);
18
     _engine = new UGameEngine(_device);
16
     _engine = new UGameEngine(_device);
43
                 );
41
                 );
44
     _device->lookAt(center, Vector3D(0.0f, 0.0f, 0.0f));
42
     _device->lookAt(center, Vector3D(0.0f, 0.0f, 0.0f));
45
     _engine->draw();
43
     _engine->draw();
46
-//    _device->drawLine(ColorVector3D(Qt::black, 0, 0, 0), ColorVector3D(Qt::black, wx, wy, wz));
44
+    _device->drawLine(ColorVector3D(Qt::black, 0, 0, 0), ColorVector3D(Qt::black, pos));
45
+    _device->drawPoint(ColorVector3D(Qt::magenta, 0.5, 0.5, 0.5));
47
 }
46
 }
48
 
47
 
49
 void RenderWidget::resizeGL(int width, int height)
48
 void RenderWidget::resizeGL(int width, int height)
76
 
75
 
77
         _lastPoint = event->pos();
76
         _lastPoint = event->pos();
78
     }
77
     }
79
-//    double modelMatrix[16];
80
-//    double projMatrix[16];
81
-//    GLint viewport[4];
82
-//    glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
83
-//    glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
84
-//    glGetIntegerv(GL_VIEWPORT, viewport);
85
 
78
 
86
-//    gluUnProject(event->x(), height() - event->y(), 0, modelMatrix, projMatrix, viewport, &wx, &wy, &wz);
87
-//    qDebug() << event->pos() << wx << wy << wz;
88
-//    update();
79
+    Vector3D dd = _device->get2DFrom3D(Vector3D(0.5, 0.5, 0.5));
80
+    dd.setY(height() - dd.getY());
81
+    qDebug() << event->pos() << dd;
82
+    pos = _device->get3DFrom2D(event->x(), height() - event->y());
83
+
84
+    update();
89
 }
85
 }
90
 
86
 
91
 void RenderWidget::wheelEvent(QWheelEvent *event)
87
 void RenderWidget::wheelEvent(QWheelEvent *event)
92
 {
88
 {
93
-    _radius = qMax(2.0, _radius - event->delta() / 20.0);
89
+    _radius = qMax(2.0, _radius - event->delta() / 30.0);
94
     update();
90
     update();
95
 }
91
 }
96
 
92
 

+ 2
- 6
TheGame/renderwidget.h View File

33
 public slots:
33
 public slots:
34
 
34
 
35
 private:
35
 private:
36
-    OpenGLRenderDevice* _device;
37
-
38
-    int angle;
36
+    AbstractRenderDevice* _device;
39
 
37
 
40
     UGameEngine* _engine;
38
     UGameEngine* _engine;
41
 
39
 
47
 
45
 
48
     float _theta;
46
     float _theta;
49
 
47
 
50
-    bool _reverse;
51
-
52
-//    double wx,wy,wz;
48
+    Vector3D pos;
53
 };
49
 };
54
 
50
 
55
 #endif // RENDERWIDGET_H
51
 #endif // RENDERWIDGET_H

+ 4
- 0
UGameEngine/engine/abstractrenderdevice.h View File

23
 
23
 
24
     QColor getClearColor() const;
24
     QColor getClearColor() const;
25
 
25
 
26
+    virtual Vector3D get2DFrom3D(const Vector3D& pos) = 0;
27
+
28
+    virtual Vector3D get3DFrom2D(int x, int y) = 0;
29
+
26
 public slots:
30
 public slots:
27
     virtual void setClearColor(const QColor &getClearColor);
31
     virtual void setClearColor(const QColor &getClearColor);
28
 
32
 

Loading…
Cancel
Save