This repository has been archived by the owner on Mar 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
window.cpp
106 lines (85 loc) · 2.39 KB
/
window.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
#include "window.h"
#include "basicusagescene.h"
#include "glassert.h"
#include <iostream>
#include <QOpenGLContext>
#include <QTimer>
static void infoGL()
{
glCheckError();
const GLubyte *str;
std::cout << "OpenGL infos with gl functions" << std::endl;
str = glGetString(GL_RENDERER);
std::cout << "Renderer : " << str << std::endl;
str = glGetString(GL_VENDOR);
std::cout << "Vendor : " << str << std::endl;
str = glGetString(GL_VERSION);
std::cout << "OpenGL Version : " << str << std::endl;
str = glGetString(GL_SHADING_LANGUAGE_VERSION);
std::cout << "GLSL Version : " << str << std::endl;
glCheckError();
}
Window::Window(QScreen *screen) :
QWindow(screen),
mScene(new BasicUsageScene())
{
setSurfaceType(OpenGLSurface);
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setMajorVersion(3);
format.setMinorVersion(0);
format.setSamples(4);
format.setProfile(QSurfaceFormat::CoreProfile);
setFormat(format);
create();
mContext = new QOpenGLContext();
mContext->setFormat(format);
mContext->create();
mScene->setContext(mContext);
printContextInfos();
initializeGl();
resize(QSize(800, 450));
connect(this, SIGNAL(widthChanged(int)), this, SLOT(resizeGl()));
connect(this, SIGNAL(heightChanged(int)), this, SLOT(resizeGl()));
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateScene()));
timer->start(16);
}
Window::~Window()
{
}
void Window::printContextInfos()
{
if(!mContext->isValid())
std::cerr << "The OpenGL context is invalid!" << std::endl;
mContext->makeCurrent(this);
std::cout << "Window format version is: "
<< format().majorVersion() << "."
<< format().minorVersion() << std::endl;
std::cout << "Context format version is: "
<< mContext->format().majorVersion()
<< "." << mContext->format().minorVersion() << std::endl;
infoGL();
}
void Window::initializeGl()
{
mContext->makeCurrent(this);
mScene->initialize();
}
void Window::paintGl()
{
if( !isExposed() ) return;
mContext->makeCurrent(this);
mScene->render();
mContext->swapBuffers(this);
}
void Window::resizeGl()
{
mContext->makeCurrent(this);
mScene->resize(width(), height());
}
void Window::updateScene()
{
mScene->update(0.0f);
paintGl();
}