-
Notifications
You must be signed in to change notification settings - Fork 3
/
glscene.cpp
184 lines (144 loc) · 5.14 KB
/
glscene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <QtOpenGL>
#include "glscene.h"
GLScene::GLScene(QWidget *parent) : QGLWidget(parent)
{
setFocusPolicy(Qt::StrongFocus);
setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
_alpha = 0;
_beta = 0;
_distance = -20;
_scale = 20.0 / WORLD_SCALE_FACTOR;
camera().setUpDirection(QVector3D(0, -1, 0));
camera().setPosition(QVector3D(0, 0, _distance));
//cameraTransformation.rotate(_alpha, 0, 1, 0);
//cameraTransformation.rotate(_beta, 1, 0, 0);
}
GLScene::~GLScene()
{
}
void GLScene::resizeGL(int width, int height)
{
if (height == 0) {
height = 1;
}
camera().projMatrix().setToIdentity();
camera().projMatrix().ortho(-width, width, -height, height, 0.1, 10000 );
//camera().projMatrix().perspective(60.0, (qreal)width/(qreal)height, 0.5f, 1000 );
//pMatrix.rotate(90, QVector3D(0,0,1));
//pMatrix.translate(QVector3D(0,-25,0));
glViewport(0, 0, width, height);
}
void GLScene::initializeGL(){
qglClearColor(Qt::black);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
_shader.addShaderFromSourceFile(QGLShader::Vertex, _shader_dir + "texture.vsh" );
_shader.addShaderFromSourceFile(QGLShader::Fragment, _shader_dir + "texture.fsh" );
_shader.link();
emit initialized();
qDebug() << "GL Scene initialized";
}
void GLScene::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera().viewMatrix().setToIdentity();
camera().lookAt(QVector3D(0, 0, 0));
camera().viewMatrix().scale(_scale);
QListIterator<QBox2DItem*> i(_glitems);
while(i.hasNext()){
QBox2DItem *item = i.next();
item->draw();
}
}
void GLScene::updateGL() {
QGLWidget::updateGL();
}
QVector4D GLScene::unproject(const QVector3D & screen){
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
const qreal xNorm = (2.0f * ((screen.x() - viewport[0]) / (viewport[2] - viewport[0]))) - 1.0f;
const qreal yNorm = 1.0f - (2.0f * ((screen.y() - viewport[1]) / (viewport[3] - viewport[1])));
QMatrix4x4 pvMatrixInv = camera().projViewProduct().inverted();
QVector4D worldPoint = pvMatrixInv * QVector4D(xNorm, yNorm, screen.z(), 1);
if (worldPoint.w() == 0){
return QVector4D(0,0,0,0);
}
worldPoint.setW(1 / worldPoint.w());
worldPoint.setX(worldPoint.x() * worldPoint.w());
worldPoint.setY(worldPoint.y() * worldPoint.w());
return worldPoint;
}
QPointF GLScene::mapToScene(const QPointF &p){
// Code below will work OK while projection is ortogonal
// For some reason in perspective projection everything is flipped and not accurate
QVector4D rNear = unproject(QVector3D(p.x(), p.y(), -1));
QVector4D rFar = unproject(QVector3D(p.x(), p.y(), 1));
QVector<QVector3D> triangle;
triangle << QVector3D(0, 0, 0) << QVector3D(0, 1, 0) << QVector3D(1, 0, 0);
QVector3D normal = QVector3D::normal(triangle.at(0), triangle.at(1), triangle.at(2));
qreal d1 = QVector3D::dotProduct(rNear.toVector3D() - triangle.at(0), normal);
qreal d2 = QVector3D::dotProduct(rFar.toVector3D() - triangle.at(0), normal);
QVector3D point = rNear.toVector3D() + (rFar.toVector3D() - rNear.toVector3D()) * (-d2 / (d2 - d1));
return point.toPointF();
}
void GLScene::mousePressEvent(QMouseEvent *event) {
QPointF pos = mapToScene(event->pos());
if (event->button() == Qt::LeftButton) {
emit mouseLeftButtonPressed(pos);
} else
if (event->button() == Qt::RightButton) {
emit mouseRightButtonPressed(pos);
}
}
void GLScene::mouseReleaseEvent(QMouseEvent *event) {
if (event->button() == Qt::RightButton) {
emit mouseRightButtonReleased();
} else
if (event->button() == Qt::LeftButton) {
emit mouseLeftButtonReleased();
}
}
void GLScene::mouseMoveEvent(QMouseEvent *event) {
QPointF pos = mapToScene(event->pos());
emit mouseMoved(pos);
}
void GLScene::keyPressEvent(QKeyEvent *event) {
emit keyPressed(event->key());
}
void GLScene::keyReleaseEvent(QKeyEvent *event) {
emit keyReleased(event->key());
}
QSize GLScene::sizeHint() const {
return QSize(640,480);
}
void GLScene::addItem(QBox2DItem *item) {
//qDebug() << "Add item: " << item->name();
_glitems << item;
item->_glscene = this;
if (!item->textureName().isNull()) {
if (!_textures.contains(item->textureName())) {
qDebug() << "Loading texture: " << item->textureName();
GLuint textureID =
bindTexture(QPixmap( _texture_dir + item->textureName()),
GL_TEXTURE_2D);
_textures.insert(item->textureName(), textureID);
}
}
}
void GLScene::removeItem(QBox2DItem *item) {
qDebug() << "Remove item: " << item->name();
_glitems.removeOne(item);
}
void GLScene::zoomIn() { _scale *= 1.1; }
void GLScene::zoomOut() { _scale *= 0.9; }
void GLScene::clear() { _glitems.clear(); }
GLCamera& GLScene::camera() {
return _camera;
}
QHash<QString,GLuint>& GLScene::textures(){
return _textures;
}
QGLShaderProgram* GLScene::shader(){
return &_shader;
}