-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmainwindow.cpp
116 lines (87 loc) · 2.8 KB
/
mainwindow.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
#include "mainwindow.h"
#include <glview.h>
#include <drawables.hpp>
#include <scene.h>
#include <QBoxLayout>
#include <QFile>
#include <QPushButton>
#include <QTextStream>
#include <iostream>
#include <glm/gtx/rotate_vector.hpp>
static const int textureWidth = 640;
static const int textureHeight = 480;
static const glm::vec3 ORIGINAL_EYE(0,0,-40);
static const float ROTATION_SPEED = 0.1f;
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
// Initialize OpenGL View
_glView = new GLView(textureWidth, textureHeight, [=] (GLView * newGlView) mutable
{
// Setup Scene
dwg::Scene defaultScene;
defaultScene.spheres = getDefaultSceneSpheres();
defaultScene.planes = getDefaultScenePlanes();
defaultScene.lights = getDefaultSceneLights();
// Initialize Raytracer
_raytracer = std::make_shared<RayTracing>(defaultScene, newGlView->getBaseTexture(), textureWidth, textureWidth);
_raytracer->setEye(ORIGINAL_EYE);
});
_glView->setFixedSize(textureWidth, textureHeight);
// Timer for rotation update
_qtimer = new QTimer(this);
_qtimer->setInterval(0); // max 60 FPS
QObject::connect(_qtimer, &QTimer::timeout, [&]
{
float deltaTime = _updateTimer.elapsedSec();
_updateTimer.restart();
_raytracer->setEye(glm::rotateY(_raytracer->getEye(), glm::pi<float>()*ROTATION_SPEED*deltaTime));
_updateScene();
});
// Buttons
QPushButton * drawButton = new QPushButton("Draw");
QObject::connect(drawButton, &QPushButton::clicked,[=]
{
_raytracer->setEye(ORIGINAL_EYE);
_updateScene();
});
QPushButton *rotateButton = new QPushButton("Rotate");
QObject::connect(rotateButton, &QPushButton::clicked,[=]
{
if(!_qtimer->isActive())
{
_updateTimer.restart();
_qtimer->start();
}
else
{
_qtimer->stop();
}
});
// Populate view
QVBoxLayout * vlayout = new QVBoxLayout();
vlayout->addWidget(_glView);
QHBoxLayout * hLayout = new QHBoxLayout();
hLayout->addWidget(drawButton);
hLayout->addWidget(rotateButton);
vlayout->addLayout(hLayout);
setLayout(vlayout);
}
void MainWindow::_updateScene()
{
util::Timer t;
// Active openGL Context
_glView->makeCurrent();
// Raytracing
_raytracer->update();
// Finish OpenGL Context
_glView->doneCurrent();
// Redraw
_glView->repaint();
float elapsedTime = t.elapsedMilliSec();
setWindowTitle(QString::fromStdString("Rendered: ") + QString::fromStdString(std::to_string(elapsedTime)) + QString(" ms") +
QString(" (") + QString::fromStdString(std::to_string((1.0f/elapsedTime)*1e3f)) + QString(" FPS)"));
}
MainWindow::~MainWindow()
{
}