-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.cpp
182 lines (139 loc) · 4.09 KB
/
view.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
#include <GL/glew.h>
#include "view.h"
#include <QApplication>
#include <QKeyEvent>
#include "camera.h"
#include "scene.h"
#include "drawcontext.h"
#include "orbitcameracontroller.h"
#include "fpscameracontroller.h"
#include <QSound>
using namespace std;
View::View(QWidget *parent) : QGLWidget(parent)
{
// View needs all mouse move events, not just mouse drag events
setMouseTracking(true);
// Hide the cursor since this is a fullscreen app
//setCursor(Qt::BlankCursor);
// View needs keyboard focus
setFocusPolicy(Qt::StrongFocus);
// The game loop is implemented using a timer
connect(&timer_, SIGNAL(timeout()), this, SLOT(tick()));
}
View::~View()
{
delete scene_;
delete camera_;
}
void View::initializeGL()
{
time_.start();
timer_.start(1000 / 60);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if( GLEW_OK != err )
{
fprintf(stderr, "Error initializing GLEW: %s\n",
glewGetErrorString(err) );
}
glClearColor(1, 0.7, 0, 1.0);
glClearDepth(1.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
QCursor::setPos(mapToGlobal(QPoint(width() / 2, height() / 2)));
camera_ = new Camera();
//cameraController_ = new OrbitCameraController(camera_);
cameraController_ = new FPSCameraController(camera_);
scene_ = new Scene();
//QSound *sound = new QSound("sounds/background.wav");
//sound->setLoops(100);
//sound->play();
}
void View::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float seconds = time_.restart() * 0.001f;
cameraController_->update(seconds);
DrawContext context;
context.deltaTime = seconds;
context.camera = camera_;
scene_->render(context);
}
void View::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
float aspectRatio = (float)w / h;
camera_->setAspectRatio(aspectRatio);
}
void View::mousePressEvent(QMouseEvent *event)
{
cameraController_->mousePressEvent(event);
if (event->button() == Qt::LeftButton) {
QPoint screenPoint = event->pos();
//cout << screenPoint.x() << " " << screenPoint.y() << endl;
float screenX = screenPoint.x();
float screenY = height() - screenPoint.y();
float x = screenX / width() * 2 - 1;
float y = screenY / height() * 2 - 1;
float z;
glReadPixels(screenX, screenY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
vec4 p = {x, y, 0, 1};
p = camera_->transform() * camera_->projectionMatrix().inverted() * p;
p = p / p.w();
vec3 camPos = camera_->position();
vec3 dir = p.toVector3D() - camPos;
dir.normalize();
float t = (0 - camPos.y()) / dir.y();
vec3 hit = camPos + t * dir;
//cout << hit << endl;
if(m_keyDown){
scene_->placeObject(hit);
}
else{
scene_->pick(hit);
}
}
update();
}
void View::mouseMoveEvent(QMouseEvent *event)
{
cameraController_->mouseMoveEvent(event);
update();
}
void View::mouseReleaseEvent(QMouseEvent *event)
{
cameraController_->mouseReleaseEvent(event);
update();
}
void View::wheelEvent(QWheelEvent *event)
{
cameraController_->wheelEvent(event);
}
void View::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape) QApplication::quit();
if(event->key() == Qt::Key_Space){
m_keyDown = true;
}
cameraController_->keyPressEvent(event);
update();
}
void View::keyReleaseEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Space){
m_keyDown = false;
}
cameraController_->keyReleaseEvent(event);
update();
}
void View::tick()
{
// Flag this view for repainting (Qt will call paintGL() soon after)
update();
}