/
gui.cpp
521 lines (468 loc) · 17 KB
/
gui.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include <GL/glew.h>
#include <gst/gst.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gtk/gtkgl.h>
/*** Use OpenGL extensions. ***/
/* #include <gdk/gdkglglext.h> */
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <gdk/gdkkeysyms.h>
#include "gltools.h"
#include "pipeline.h"
#include "draw.h"
#include "gui.h"
#include "application.h"
#define TIMEOUT_INTERVAL 10
static const GLenum PIXEL_TYPE = GL_UNSIGNED_SHORT_5_6_5;
/**************************************************************************
* Global variable declarations.
**************************************************************************/
// TODO: remove them
static gboolean animate = FALSE;
static guint timeout_id = 0;
/**************************************************************************
* The following section contains the function prototype declarations.
**************************************************************************/
// TODO: move to the header file
static void timeout_add(GtkWidget* widget);
static void timeout_remove(GtkWidget* widget);
static void toggle_animation(GtkWidget* widget);
static gboolean timeout (GtkWidget* widget);
/*
* The timeout function. Often in animations,
* timeout functions are suitable for continous
* frame updates.
*/
static gboolean timeout(GtkWidget *widget)
{
//g_print (".");
/*** Fill in the details here ***/
/* Invalidate the whole window. */
gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
/* Update synchronously. */
gdk_window_process_updates (widget->window, FALSE);
return TRUE;
}
/**************************************************************************
* The following section contains the timeout function management routines.
**************************************************************************/
/**
* Helper functions to add or remove the timeout function.
*/
static void timeout_add (GtkWidget *widget)
{
if (timeout_id == 0) {
timeout_id = g_timeout_add(TIMEOUT_INTERVAL, (GSourceFunc) timeout, widget);
}
}
static void timeout_remove (GtkWidget *widget)
{
if (timeout_id != 0) {
g_source_remove(timeout_id);
timeout_id = 0;
}
}
/**
* The "map_event" signal handler. Any processing required when the
* OpenGL-capable drawing area is mapped should be done here.
*/
static gboolean map_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
g_print("%s: \"map_event\":\n", gtk_widget_get_name (widget));
if (animate)
timeout_add (widget);
return TRUE;
}
/**
* The "unmap_event" signal handler. Any processing required when the
* OpenGL-capable drawing area is unmapped should be done here.
*/
static gboolean unmap_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
g_print ("%s: \"unmap_event\":\n", gtk_widget_get_name (widget));
timeout_remove (widget);
return TRUE;
}
/**
* Toggle animation.
*/
static void toggle_animation(GtkWidget *widget)
{
animate = !animate;
if (animate) {
timeout_add (widget);
} else {
timeout_remove (widget);
gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
}
}
/**
* Called when the window has become fullscreen or not.
*/
gboolean Gui::onWindowStateEvent(GtkWidget* widget, GdkEventWindowState *event, gpointer data)
{
Gui *gui = static_cast<Gui*>(data);
gui->isFullscreen_ = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN);
if (gui->isFullscreen_)
gui->hideCursor();
else
gui->showCursor();
return TRUE;
}
/**
* The "visibility_notify_event" signal handler. Any processing required
* when the OpenGL-capable drawing area is visually obscured should be
* done here.
*/
static gboolean visibility_notify_event(GtkWidget *widget, GdkEventVisibility *event, gpointer data)
{
if (animate)
{
if (event->state == GDK_VISIBILITY_FULLY_OBSCURED)
timeout_remove(widget);
else
timeout_add(widget);
}
return TRUE;
}
void Gui::hideCursor()
{
// FIXME: this is because gtk doesn't support GDK_BLANK_CURSOR before gtk-2.16
char invisible_cursor_bits[] = { 0x0 };
static GdkCursor* cursor = 0;
if (drawing_area_ != NULL) {
if (cursor == 0) {
static GdkBitmap *empty_bitmap;
const static GdkColor color = {0, 0, 0, 0};
empty_bitmap = gdk_bitmap_create_from_data(GDK_WINDOW(drawing_area_->window), invisible_cursor_bits, 1, 1);
cursor = gdk_cursor_new_from_pixmap(empty_bitmap, empty_bitmap, &color, &color, 0, 0);
}
gdk_window_set_cursor(GDK_WINDOW(drawing_area_->window), cursor);
}
}
void Gui::showCursor()
{
/// sets to default
if (drawing_area_ != NULL) {
gdk_window_set_cursor(GDK_WINDOW(drawing_area_->window), NULL);
}
}
gboolean Gui::key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
Gui *gui = static_cast<Gui*>(data);
switch (event->keyval)
{
case GDK_Escape:
gui->toggleFullscreen(widget);
break;
case GDK_q:
// Quit application on ctrl-q, this quits the main loop
// (if there is one)
if (event->state & GDK_CONTROL_MASK)
{
g_print("Ctrl-Q key pressed, quitting.\n");
Application::get_instance().quit();
}
break;
default:
break;
}
return TRUE;
}
/**
* Handles the "realize" signal to take any necessary actions when the widget is instantiated on a particular display. (Create GDK resources in response to this signal.)
*
* The "realize" signal handler. All the OpenGL initialization
* should be performed here, such as default background colour,
* certain states etc.
*/
void Gui::on_realize(GtkWidget *widget, gpointer data)
{
GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
/*** OpenGL BEGIN ***/
if (! gdk_gl_drawable_gl_begin(gldrawable, glcontext))
{
return;
}
//Gui *gui = static_cast<Gui*>(data);
//gui->glx_context_ = glXGetCurrentContext();
glDisable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0); // black background
glViewport(0, 0, widget->allocation.width, widget->allocation.height);
_set_view(widget->allocation.width, widget->allocation.height);
gdk_gl_drawable_gl_end(gldrawable);
/*** OpenGL END ***/
}
/***
*** The "unrealize" signal handler. Any processing required when
*** the OpenGL-capable window is unrealized should be done here.
***/
static void unrealize (GtkWidget *widget,
gpointer data)
{
GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
g_print ("%s: \"unrealize\"\n", gtk_widget_get_name (widget));
/*** OpenGL BEGIN ***/
if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
return;
/*** Fill in the details here. ***/
gdk_gl_drawable_gl_end (gldrawable);
/*** OpenGL END ***/
}
void Gui::on_delete_event(GtkWidget* widget, GdkEvent* event, gpointer data)
{
Gui *context = static_cast<Gui*>(data);
g_print("Window has been deleted.\n");
Application::get_instance().quit();
}
void Gui::toggleFullscreen(GtkWidget *widget)
{
// toggle fullscreen state
isFullscreen_ ? makeUnfullscreen(widget) : makeFullscreen(widget);
}
void Gui::makeFullscreen(GtkWidget *widget)
{
gtk_window_stick(GTK_WINDOW(widget)); // window is visible on all workspaces
gtk_window_fullscreen(GTK_WINDOW(widget));
}
void Gui::makeUnfullscreen(GtkWidget *widget)
{
gtk_window_unstick(GTK_WINDOW(widget)); // window is not visible on all workspaces
gtk_window_unfullscreen(GTK_WINDOW(widget));
}
/**
* Sets up the orthographic projection.
*
* Makes sure height is always 1.0 in GL modelview coordinates.
*
* Coordinates should give a rendering area height of 1
* and a width of 1.33, when in 4:3 ratio.
*/
void _set_view(int width, int height)
{
//glViewport(0, 0, width, height);
// if >= 4/3:
float w = float(width) / float(height);
float h = 1.0; // constant height
// make sure the aspect ratio of the window is not less wide than 4:3
if (w < 4.0f/3.0f)
{
h = (float(height) / float(width)) * 1.333333f;
w = 1.3333333333f; // constant width
}
//std::cout << "Resized window to " << width << "x" << height << ". Ratio is " << w << "x" << h << std::endl;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w, w, -h, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
//glEnable (GL_LINE_SMOOTH);
//glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
//
// enables transparency blending:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
/**
* Handles the "configure_event" signal to take any necessary actions when the widget changes size.
*
* The "configure_event" signal handler. Any processing required when
* the OpenGL-capable drawing area is re-configured should be done here.
* Almost always it will be used to resize the OpenGL viewport when
* the window is resized.
*/
gboolean Gui::on_configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
{
Gui *context = static_cast<Gui*>(data);
GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
std::cout << "on_configure_event" << std::endl;
/*** OpenGL BEGIN ***/
if (! gdk_gl_drawable_gl_begin(gldrawable, glcontext))
{
std::cout << "Could not begin OpenGL rendering" << std::endl;
return FALSE;
}
glViewport(0, 0, widget->allocation.width, widget->allocation.height);
_set_view(widget->allocation.width, widget->allocation.height);
gdk_gl_drawable_gl_end(gldrawable);
/*** OpenGL END ***/
return TRUE;
}
/**
* Reads the data from the pipeline and updates an OpenGL texture.
*/
// TODO:
bool Gui::create_live_input_texture()
{
Pipeline pipeline = Application::get_instance().get_pipeline();
//std::cout << "create_live_input_texture" << std::endl;
if (pipeline.has_new_live_input_data_) {
std::cout << "Live input texture needs to be updated" << std::endl;
pipeline.has_new_live_input_data_ = false;
if (! live_input_texture_created_)
{
live_input_texture_created_ = true;
std::cout << "live_input_texture_created_ = true" << std::endl;
glGenTextures(1, &live_input_texture_);
std::cout << "called glGenTextures" << std::endl;
}
int width = pipeline.get_width();
int height = pipeline.get_height();
//char* buf = pipeline.last_frame_data_;
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, live_input_texture_);
//std::cout << "called glBindTexture" << std::endl;
//std::cout << "Will call glTexImage2D for an image of " << width << "x" << height << std::endl;
// TODO: update texture, don't create a new one each time
// HERE I am working:
// GL_UNSIGNED_BYTE
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, width, height, 0, GL_RGB,
PIXEL_TYPE,
static_cast<GLvoid *>(pipeline.last_frame_data_));
//std::cout << "called glTexImage2D" << std::endl;
// TODO: simplify those parameters
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
return true;
}
/**
* Draws the stuff
*/
void _draw()
{
std::cout << "drawing" << std::endl;
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glColor4f(1.0, 0.8, 0.2, 1.0);
glPushMatrix();
glScalef(0.5, 0.5, 1.0);
draw::draw_square();
glPopMatrix();
glColor4f(1.0, 1.0, 0.0, 0.8);
int num = 64;
float x;
for (int i = 0; i < num; i++)
{
x = (i / float(num)) * 4 - 2;
draw::draw_line(float(x), -2.0, float(x), 2.0);
draw::draw_line(-2.0, float(x), 2.0, float(x));
}
glColor4f(1.0, 1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
Gui gui = Application::get_instance().get_gui();
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, gui.live_input_texture_);
Pipeline pipeline = Application::get_instance().get_pipeline();
glPushMatrix();
glScalef(0.6666, 0.5, 1.0);
draw::draw_vertically_flipped_textured_square(pipeline.get_width(), pipeline.get_height());
glPopMatrix();
}
/**
* Handles the "expose_event" signal to redraw the contents of the widget.
*
* The "expose_event" signal handler. All the OpenGL re-drawing should
* be done here. This is repeatedly called as the painting routine
* every time the 'expose'/'draw' event is signalled.
*/
gboolean Gui::on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
Gui *gui = static_cast<Gui*>(data);
/*** OpenGL BEGIN ***/
if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
{
return FALSE;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gui->create_live_input_texture();
_draw();
if (gdk_gl_drawable_is_double_buffered(gldrawable))
{
gdk_gl_drawable_swap_buffers(gldrawable);
} else {
glFlush();
}
gdk_gl_drawable_gl_end(gldrawable);
/*** OpenGL END ***/
return TRUE;
}
/**
* Exits the application if OpenGL needs are not met.
*/
Gui::Gui() :
isFullscreen_(false)
{
live_input_texture_created_ = false;
//glx_context_ = NULL;
gint major;
gint minor;
gdk_gl_query_version(&major, &minor);
g_print("\nOpenGL extension version - %d.%d\n", major, minor);
/* Try double-buffered visual */
GdkGLConfig* glconfig;
// the line above does not work in C++ if the cast is not there.
glconfig = gdk_gl_config_new_by_mode(static_cast<GdkGLConfigMode>(GDK_GL_MODE_RGB | GDK_GL_MODE_DOUBLE));
if (glconfig == NULL)
{
g_print("*** Cannot find the double-buffered visual.\n");
g_print("*** Trying single-buffered visual.\n");
/* Try single-buffered visual */
glconfig = gdk_gl_config_new_by_mode(static_cast<GdkGLConfigMode>(GDK_GL_MODE_RGB));
if (glconfig == NULL)
{
g_print ("*** No appropriate OpenGL-capable visual found.\n");
exit(1);
}
}
gltools::examine_gl_config_attrib(glconfig);
// Main GTK window
window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window_, 640, 480);
gtk_window_move (GTK_WINDOW (window_), 300, 10);
gtk_window_set_title(GTK_WINDOW (window_), "Toonloop 1.11 experimental");
GdkGeometry geometry;
geometry.min_width = 1;
geometry.min_height = 1;
geometry.max_width = -1;
geometry.max_height = -1;
gtk_window_set_geometry_hints(GTK_WINDOW(window_), window_, &geometry, GDK_HINT_MIN_SIZE);
// connect window signals:
drawing_area_ = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window_), drawing_area_);
/* Set OpenGL-capability to the widget. */
gtk_widget_set_gl_capability(drawing_area_, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE);
g_signal_connect_after(G_OBJECT(drawing_area_), "realize", G_CALLBACK(on_realize), this);
g_signal_connect(G_OBJECT(drawing_area_), "configure_event", G_CALLBACK(on_configure_event), this);
g_signal_connect(G_OBJECT(drawing_area_), "expose_event", G_CALLBACK(on_expose_event), this);
g_signal_connect (G_OBJECT(drawing_area_), "unrealize", G_CALLBACK(unrealize), NULL);
/* For timeout function. */
g_signal_connect (G_OBJECT (drawing_area_), "map_event", G_CALLBACK(map_event), NULL);
g_signal_connect (G_OBJECT (drawing_area_), "unmap_event", G_CALLBACK(unmap_event), NULL);
g_signal_connect (G_OBJECT (drawing_area_), "visibility_notify_event", G_CALLBACK(visibility_notify_event), NULL);
g_signal_connect(G_OBJECT(window_), "delete-event", G_CALLBACK(on_delete_event), this);
g_signal_connect(G_OBJECT(window_), "key-press-event", G_CALLBACK(key_press_event), this);
// add listener for window-state-event to detect fullscreenness
g_signal_connect(G_OBJECT(window_), "window-state-event", G_CALLBACK(onWindowStateEvent), this);
//area where the video is drawn
//avoid flickering when resizing or obscuring the main window
gtk_widget_realize(drawing_area_);
gdk_window_set_back_pixmap(drawing_area_->window, NULL, FALSE);
gtk_widget_set_app_paintable(drawing_area_, TRUE);
gtk_widget_set_double_buffered(drawing_area_, FALSE);
gtk_widget_show_all(window_);
// Let's start the constant rendering.
toggle_animation(drawing_area_);
}