-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.c++
424 lines (376 loc) · 12.7 KB
/
window.c++
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "shared.h++"
#ifdef DEBUG
#include <cerrno>
#endif
#include "shaders.h++"
#include "window.h++"
#include "texture.h++"
#ifdef DEBUG
static void printArray(const GLfloat data[],int length){
int i;
for(i=0;i<length;i++){
printf("%f ",data[i]);
}
printf("\n");
}
static void printGlError(){
while(true){
GLenum err=glGetError();
printf("gl error code: %i\n",err);
if(err==GL_NO_ERROR){
break;
}
switch(err){
case GL_NO_ERROR:
printf("GL_NO_ERROR\n");
break;
case GL_INVALID_ENUM:
printf("GL_INVALID_ENUM\n");
break;
case GL_INVALID_VALUE:
printf("GL_INVALID_VALUE\n");
break;
case GL_INVALID_OPERATION:
printf("GL_INVALID_OPERATION\n");
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
printf("GL_INVALID_FRAMEBUFFER_OPERATION\n");
break;
case GL_OUT_OF_MEMORY:
printf("GL_OUT_OF_MEMORY");
break;
case GL_STACK_UNDERFLOW:
printf("GL_STACK_UNDERFLOW");
break;
case GL_STACK_OVERFLOW:
printf("GL_STACK_OVERFLOW");
break;
default:
printf("GL error");
break;
}
}
}
static bool checkFramebuffer(){
// check if the frame buffer is initialized properly
// and if not, exit
switch(glCheckFramebufferStatus(GL_FRAMEBUFFER)){
case GL_FRAMEBUFFER_COMPLETE:
printf("No error\n");
return false;
case GL_FRAMEBUFFER_UNDEFINED:
printf("GL_FRAMEBUFFER_UNDEFINED\n");
return true;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
printf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT\n");
return true;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
printf("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT\n");
return true;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
printf("GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER\n");
return true;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
printf("GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER\n");
return true;
case GL_FRAMEBUFFER_UNSUPPORTED:
printf("GL_FRAMEBUFFER_UNSUPPORTED\n");
return true;
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
printf("GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE\n");
return true;
case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
printf("GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS\n");
return true;
default:
printf("Framebuffer not initialized properly\n");
return true;
}
}
#endif
#define CALL(f,...) if(f!=NULL){(*f)(__VA_ARGS__);}
void defaultdraw(Window *w,void*){
glDrawArrays(GL_TRIANGLES, 0, w->draw_vertices);
}
window_exception::window_exception(const char* message): msg(message) {}
window_exception::~window_exception() noexcept {}
const char* window_exception::what() const noexcept {
return msg;
}
Window::Window(int width, int height, const char* name):
width(width),height(height),program(0),
handles_array(NULL),numhandles(0),
#if HAS_OPENCV
saveframes(false),
#endif
draw(&defaultdraw),
closed(false){
window = SDL_CreateWindow(name, 0, 0,
width, height, SDL_WINDOW_OPENGL);
gl_context = SDL_GL_CreateContext(window);
glewInit();
// Global draw state
glViewport(0, 0, width, height); // use 0,0,width,height as the rectangle to draw in (x,y,w,h)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // set the background color
draw_vertices=12;
}
#if HAS_OPENCV
void Window::enableSaveFrames(int frames){
saveframes=true;
frameTexture=0;
framestosave=frames;
framesdone=0;
Window::setupSaveFrames();
}
#else
void Window::enableSaveFrames(int){
throw new window_exception("Can't save frames without opencv!");
}
#endif
void Window::makeShader(std::filesystem::path vertex_shader_path, std::filesystem::path fragment_shader_path){
if(program!=0){
glDeleteProgram(program); // delete the program
program=0;
}
// Shader setup.
try{
program = compile_shader(vertex_shader_path, fragment_shader_path);
}catch(shader_compile_exception& e){
printf("exception in shader compilation: %s\n", e.what());
printf("log: \n%s\n\n", e.log);
exit(EXIT_FAILURE);
}
glUseProgram(program); // use the compiled program
}
void Window::makeShaderFromSource(char *vertex_shader_source, char *fragment_shader_source){
if(program!=0){
glDeleteProgram(program); // delete the program
program=0;
}
// Shader setup.
try{
program = compile_shader_from_source(vertex_shader_source, fragment_shader_source);
}catch(shader_compile_exception& e){
printf("exception in shader compilation: %s\n", e.what());
printf("log: \n%s\n\n", e.log);
exit(EXIT_FAILURE);
}
glUseProgram(program); // use the compiled program
}
void Window::mainLoop(){
// for(auto attr:handles){
// DEBUGP(WINDOW_GL_DEBUG,"%s %i\n",attr.first,attr.second);
// DEBUGR(WINDOW_GL_DEBUG,errno=0);
// glEnableVertexAttribArray(glGetAttribLocation(program,attr.first));
// DEBUGP(WINDOW_GL_DEBUG,"enable %i\n",errno);
// }
closed=false;
while (!closed) {
#if HAS_OPENCV
if(framestosave>0&&framesdone==framestosave){
break;
}
#endif
CALL(onframe,this,data);
DEBUGP(WINDOW_DEBUG,"frame\n");
glClear(GL_COLOR_BUFFER_BIT // clear the background
| GL_DEPTH_BUFFER_BIT); // and the depth buffer
CALL(draw,this,data);
#if HAS_OPENCV
if(saveframes){
DEBUGP(WINDOW_DEBUG,"frame written\n");
Window::writeFrame();
}
#endif
SDL_GL_SwapWindow(window); // draw the graphics buffer to the screen
SDL_Event event;
// Events management
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
DEBUGP(WINDOW_KEY_DEBUG,"key down %c %i\n",event.key.keysym.sym,event.key.keysym.sym);
CALL(onkeydown,this,event.key.keysym,data);
break;
case SDL_KEYUP:
DEBUGP(WINDOW_KEY_DEBUG,"key up %c %i\n",event.key.keysym.sym,event.key.keysym.sym);
CALL(onkeyup,this,event.key.keysym,data);
break;
case SDL_QUIT:
// handling of close button
closed = true;
break;
default:
break;
}
}
#if HAS_OPENCV
framesdone++;
#endif
}
// for(auto attr:handles){
// DEBUGP(WINDOW_DEBUG,"%s %i\n",attr.first,attr.second);
// glDisableVertexAttribArray(glGetAttribLocation(program,attr.first));
// }
}
Window::vertexdata::vertexdata(int handle):handle(handle){}
Window::vertexdata Window::addVertexData(GLfloat data[],GLint size,GLint stride){
GLuint handle;
glGenBuffers(1, &handle);
int handlei=numhandles++;
handles_array=(GLuint*)realloc(handles_array,numhandles*sizeof(GLuint));
strides_array=(GLuint*)realloc(strides_array,numhandles*sizeof(GLuint));
handles_array[handlei]=handle;
strides_array[handlei]=stride;
glBindBuffer(GL_ARRAY_BUFFER, handle);
DEBUGP(WINDOW_GL_DEBUG, "%i %i %i\n",handlei,stride,handle);
DEBUGP(WINDOW_GL_DEBUG, "%li\n",size*stride*sizeof(float));
glBufferData(GL_ARRAY_BUFFER, size*stride*sizeof(float), data, GL_STATIC_DRAW);
return Window::vertexdata(handlei);
}
void Window::applyVertexData(vertexdata v,const char* name,GLint floatspervertex,GLint offset){
GLint attribute = glGetAttribLocation(program, name);
GLuint handle=handles_array[v.handle],stride=strides_array[v.handle];
glBindBuffer(GL_ARRAY_BUFFER, handle);
DEBUGP(WINDOW_GL_DEBUG, "%i %i %i %li %li\n",v.handle,stride,handle,stride*sizeof(float), offset*sizeof(float));
glVertexAttribPointer(attribute, floatspervertex, GL_FLOAT, GL_FALSE, stride*sizeof(float), (const GLvoid*)(offset*sizeof(float)));
glEnableVertexAttribArray(attribute);
}
void Window::setUniformMat4x4(const char* name,const matrix4x4 &matrix){
GLint uniform = glGetUniformLocation(program, name);
DEBUGP(WINDOW_ATTR_DEBUG,"%s",name);
DEBUGR(WINDOW_ATTR_DEBUG,
printArray(matrix.contents,4);
printArray(matrix.contents+4,4);
printArray(matrix.contents+8,4);
printArray(matrix.contents+12,4);
);
glUniformMatrix4fv(uniform, 1, GL_FALSE, matrix.contents);
}
void Window::setUniformVec3(const char* name,float *vec){
GLint uniform = glGetUniformLocation(program, name);
DEBUGP(WINDOW_ATTR_DEBUG,"%s",name);
DEBUGR(WINDOW_ATTR_DEBUG,
printArray(vec,3);
);
glUniform3fv(uniform, 1, vec);
}
void Window::setUniformFloat(const char* name,float f){
GLint uniform = glGetUniformLocation(program, name);
DEBUGP(WINDOW_ATTR_DEBUG,"%s",name);
DEBUGR(WINDOW_ATTR_DEBUG,
printf("%f\n",f);
);
glUniform1f(uniform, f);
}
void Window::setUniformInt(const char* name,GLuint i){
GLint uniform = glGetUniformLocation(program, name);
DEBUGP(WINDOW_ATTR_DEBUG,"%s",name);
DEBUGR(WINDOW_ATTR_DEBUG,
printf("%i\n",i);
);
glUniform1i(uniform, i);
}
#if HAS_OPENCV
void Window::setupSaveFrames(){
// create a framebuffer
frameBuffer=0;
glGenFramebuffers(1,&frameBuffer);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
// create a texture to render the framebuffer to
frameTexture=0;
glGenTextures(1,&frameTexture);
glActiveTexture(GL_TEXTURE0);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
glBindTexture(GL_TEXTURE_2D,frameTexture);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
// actually initialize the texture
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,NULL);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
DEBUGR(WINDOW_GL_DEBUG,
int w;
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&w);
int h;
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&h);
printf("%i %i %i\n",frameTexture,w,h);
);
// set interpolation
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
// create a depth buffer
GLuint depthbuffer=0;
glGenRenderbuffers(1,&depthbuffer);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
glBindRenderbuffer(GL_RENDERBUFFER,depthbuffer);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
// allocate memory for the depth buffer
glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT32F,width,height);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
// attach the depth buffer to the frame buffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depthbuffer);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
// attach the texture to the frame buffer
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,frameTexture,0);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
// set the framebuffer to be drawn
GLenum drawBuffers[1]={GL_COLOR_ATTACHMENT0};
glDrawBuffers(1,drawBuffers);
DEBUGR(WINDOW_GL_DEBUG,printGlError());
DEBUGR(WINDOW_GL_DEBUG,checkFramebuffer());
// set the size of the screen
glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer);
glViewport(0,0,width,height);
// initialize an opencv video writer
const char* fourcc="MP42";
int codec = CV_FOURCC(fourcc[0],fourcc[1],fourcc[2],fourcc[3]);
const char* filename = "output/file.avi";
cv::Size frameSize(width,height);
writer=new cv::VideoWriter(filename, codec, 60, frameSize, true);
}
void Window::writeFrame(){
DEBUGP(WINDOW_WRITEFRAME_DEBUG,"write frame\n");
GLchar *pixels=readTexture(frameTexture);
DEBUGP(WINDOW_WRITEFRAME_DEBUG,"got pixels array\n");
cv::Mat tmp(height,width,CV_8UC3,pixels,getRowSize(frameTexture)),image;
cv::flip(tmp,image,0);
DEBUGP(WINDOW_WRITEFRAME_DEBUG,"converted to cv::Mat\n");
writer->write(image);
DEBUGP(WINDOW_WRITEFRAME_DEBUG,"write frame\n");
free(pixels);
DEBUGP(WINDOW_WRITEFRAME_DEBUG,"free pixels\n");
glBindFramebuffer(GL_READ_FRAMEBUFFER, frameBuffer);
//glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
//glBindFramebuffer(GL_READ_FRAMEBUFFER, frameBuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer);
//glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}
#endif
Window::~Window(){
glDeleteBuffers(numhandles, handles_array); // delete all the buffers
glDeleteProgram(program); // delete the program
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
free(handles_array);
#if HAS_OPENCV
if(saveframes){
printf("Write complete !\n");
writer->release();
}
#endif
}