-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenglcontext.cpp
More file actions
178 lines (155 loc) · 6.03 KB
/
openglcontext.cpp
File metadata and controls
178 lines (155 loc) · 6.03 KB
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
#include "openglcontext.h"
#include <iostream>
#include <QApplication>
#include <QProcessEnvironment>
#include <QOpenGLContext>
#include <QDebug>
OpenGLContext::OpenGLContext(QWidget *parent)
: QOpenGLWidget(parent)
{
// Check whether automatic testing is enabled
autotesting = qgetenv("CIS277_AUTOTESTING") != nullptr;
if (autotesting) {
/*** AUTOMATIC TESTING: DO NOT MODIFY ***/
/*** Save one frame and exit */
/***/ connect(&timer, SIGNAL(timeout()), this, SLOT(saveImageAndQuit()));
/***/ timer.setSingleShot(true);
/***/ timer.start(0);
} else {
// Allow the timer to redraw the window
connect(&timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
// Tell the timer to redraw 60 times per second
timer.start(16);
}
}
OpenGLContext::~OpenGLContext()
{}
inline const char *glGS(GLenum e)
{
return reinterpret_cast<const char *>(glGetString(e));
}
void OpenGLContext::debugContextVersion()
{
QOpenGLContext *ctx = context();
QSurfaceFormat form = format();
QSurfaceFormat ctxform = ctx->format();
QSurfaceFormat::OpenGLContextProfile prof = ctxform.profile();
const char *profile =
prof == QSurfaceFormat::CoreProfile ? "Core" :
prof == QSurfaceFormat::CompatibilityProfile ? "Compatibility" :
"None";
int ctxmajor = ctxform.majorVersion();
int ctxminor = ctxform.minorVersion();
bool valid = ctx->isValid();
int formmajor = form.majorVersion();
int formminor = form.minorVersion();
const char *vendor = glGS(GL_VENDOR);
const char *renderer = glGS(GL_RENDERER);
const char *version = glGS(GL_VERSION);
const char* s_glsl = glGS(GL_SHADING_LANGUAGE_VERSION);
printf("Widget version: %d.%d\n", ctxmajor, ctxminor);
printf("Context valid: %s\n", valid ? "yes" : "NO");
printf("Format version: %d.%d\n", formmajor, formminor);
printf("Profile: %s\n", profile);
printf(" Vendor: %s\n", vendor);
printf(" Renderer: %s\n", renderer);
printf(" Version: %s\n", version);
printf(" GLSL: %s\n", s_glsl);
QString glsl = s_glsl;
if (ctxmajor < 3 || glsl.startsWith("1.10") || glsl.startsWith("1.20")) {
printf("ERROR: "
"Unable to get an OpenGL 3.x context with GLSL 1.30 or newer. "
"If your hardware should support it, update your drivers. "
"If you have switchable graphics, make sure that you are using the discrete GPU.\n");
QApplication::exit();
} else if ((ctxmajor == 3 && ctxminor < 2) || glsl.startsWith("1.30") || glsl.startsWith("1.40")) {
printf("WARNING: "
"Enable to get an OpenGL 3.2 context with GLSL 1.50. "
"If your hardware should support it, update your drivers. "
"If you have switchable graphics, make sure that you are using the discrete GPU. "
"If you cannot get 3.2 support, it is possible to port this project....");
// Note: doing this requires at least the following actions:
// * Change the header and base class in openglcontext.h to 3.0/3.1 instead of 3.2 Core.
// * Change the shaders to require GLSL 1.30 or 1.40.
}
}
void OpenGLContext::printGLErrorLog()
{
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
std::cerr << "OpenGL error " << error << ": ";
const char *e =
error == GL_INVALID_OPERATION ? "GL_INVALID_OPERATION" :
error == GL_INVALID_ENUM ? "GL_INVALID_ENUM" :
error == GL_INVALID_VALUE ? "GL_INVALID_VALUE" :
error == GL_INVALID_INDEX ? "GL_INVALID_INDEX" :
error == GL_INVALID_OPERATION ? "GL_INVALID_OPERATION" :
QString::number(error).toUtf8().constData();
std::cerr << e << std::endl;
// Throwing here allows us to use the debugger to track down the error.
#ifndef __APPLE__
// Don't do this on OS X.
// http://lists.apple.com/archives/mac-opengl/2012/Jul/msg00038.html
throw;
#endif
}
}
void OpenGLContext::printLinkInfoLog(int prog)
{
GLint linked;
glGetProgramiv(prog, GL_LINK_STATUS, &linked);
if (linked == GL_TRUE) {
return;
}
std::cerr << "GLSL LINK ERROR" << std::endl;
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &infoLogLen);
if (infoLogLen > 0) {
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetProgramInfoLog(prog, infoLogLen, &charsWritten, infoLog);
std::cerr << "InfoLog:" << std::endl << infoLog << std::endl;
delete[] infoLog;
}
// Throwing here allows us to use the debugger to track down the error.
throw;
}
void OpenGLContext::printShaderInfoLog(int shader)
{
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (compiled == GL_TRUE) {
return;
}
std::cerr << "GLSL COMPILE ERROR" << std::endl;
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLen);
if (infoLogLen > 0) {
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetShaderInfoLog(shader, infoLogLen, &charsWritten, infoLog);
std::cerr << "InfoLog:" << std::endl << infoLog << std::endl;
delete[] infoLog;
}
// Throwing here allows us to use the debugger to track down the error.
throw;
}
/*** AUTOMATIC TESTING: DO NOT MODIFY ***/
/***/ void OpenGLContext::saveImageAndQuit() {
/***/ glFlush();
/***/ QImage image = grabFramebuffer();
/***/ image.save("image.png");
/***/ QApplication::quit();
/***/ }
void OpenGLContext::timerUpdate()
{
// This function is called roughly 60 times per second.
// Use it to update your scene and then tell it to redraw.
// (Don't update your scene in paintGL, because it
// sometimes gets called automatically by Qt.)
update();
}