]> git.sesse.net Git - nageru/blob - glwidget.cpp
Fix a warning.
[nageru] / glwidget.cpp
1 #include <qmetatype.h>
2 #include <qdatastream.h>
3 #include <qtextstream.h>
4 #include <qcursor.h>
5 #include <qcoreevent.h>
6 #include <epoxy/gl.h>
7 #include <epoxy/egl.h>
8 #include "context.h"
9 #include "glwidget.h"
10 #include "mixer.h"
11 #include <QCoreApplication>
12 #include <QGuiApplication>
13 #include <QThread>
14 #include <math.h>
15 #include <thread>
16 #include <movit/resource_pool.h>
17 #undef Success
18 #include <movit/util.h>
19
20 GLWidget::GLWidget(QWidget *parent)
21     : QOpenGLWidget(parent),
22       resource_pool(new movit::ResourcePool)
23 {
24 }
25
26 GLWidget::~GLWidget()
27 {
28 }
29
30 void GLWidget::initializeGL()
31 {
32         printf("egl context=%p\n", eglGetCurrentContext());
33         //printf("threads: %p %p\n", QThread::currentThread(), qGuiApp->thread());
34
35         GLWidget *t = this;
36         set_frame_ready_fallback([t]{
37                 QMetaObject::invokeMethod(t, "update", Qt::AutoConnection);
38         });
39
40         QSurface *surface = create_surface(format());
41         QSurface *surface2 = create_surface(format());
42         QSurface *surface3 = create_surface(format());
43         QSurface *surface4 = create_surface(format());
44         start_mixer(surface, surface2, surface3, surface4);
45
46         // Prepare the shaders to actually get the texture shown (ick).
47         glDisable(GL_BLEND);
48         glDisable(GL_DEPTH_TEST);
49         glDepthMask(GL_FALSE);
50
51         std::string vert_shader =
52                 "#version 130 \n"
53                 "in vec2 position; \n"
54                 "in vec2 texcoord; \n"
55                 "out vec2 tc; \n"
56                 " \n"
57                 "void main() \n"
58                 "{ \n"
59                 "    gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n"
60                 "    tc = texcoord; \n"
61                 "    tc.y = 1.0 - tc.y; \n"
62                 "} \n";
63         std::string frag_shader =
64                 "#version 130 \n"
65                 "in vec2 tc; \n"
66                 "uniform sampler2D tex; \n"
67                 "void main() { \n"
68                 "    gl_FragColor = texture2D(tex, tc); \n"
69                 "} \n";
70         program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader);
71
72         static const float vertices[] = {
73                 0.0f, 2.0f,
74                 0.0f, 0.0f,
75                 2.0f, 0.0f
76         };
77         glGenVertexArrays(1, &vao);
78         glBindVertexArray(vao);
79         position_vbo = movit::fill_vertex_attribute(program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
80         texcoord_vbo = movit::fill_vertex_attribute(program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same as vertices.
81
82 #if 0
83         // Cleanup.
84         cleanup_vertex_attribute(phases[0]->glsl_program_num, "position", position_vbo);
85         cleanup_vertex_attribute(phases[0]->glsl_program_num, "texcoord", texcoord_vbo);
86
87         glDeleteVertexArrays(1, &vao);
88         nheck_error();
89 #endif
90 }
91
92 void GLWidget::paintGL()
93 {
94         DisplayFrame frame;
95         if (!mixer_get_display_frame(&frame)) {
96                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
97                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
98                 return;
99         }
100
101         glUseProgram(program_num);
102         glActiveTexture(GL_TEXTURE0);
103         glBindTexture(GL_TEXTURE_2D, frame.texnum);
104         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
105         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
106         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
107
108         glBindVertexArray(vao);
109         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
110         glDrawArrays(GL_TRIANGLES, 0, 3);
111 }