Hackz.
[dcpomatic.git] / src / wx / gl_view.cc
1 /*
2     Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "gl_view.h"
22 #include "lib/image.h"
23 #include "lib/dcpomatic_assert.h"
24 #include <boost/bind.hpp>
25 #include <iostream>
26
27 #ifdef DCPOMATIC_OSX
28 #include <OpenGL/glu.h>
29 #include <OpenGL/glext.h>
30 #else
31 #include <GL/glu.h>
32 #include <GL/glext.h>
33 #endif
34
35 using std::cout;
36 using boost::shared_ptr;
37
38 GLView::GLView (wxWindow *parent)
39         : wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
40 {
41         _context = new wxGLContext (this);
42         Bind (wxEVT_PAINT, boost::bind(&GLView::paint, this, _1));
43
44         glGenTextures (1, &_id);
45         glBindTexture (GL_TEXTURE_2D, _id);
46         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
47 }
48
49 GLView::~GLView ()
50 {
51         glDeleteTextures (1, &_id);
52         delete _context;
53 }
54
55 static void
56 check_gl_error (char const * last)
57 {
58         GLenum const e = glGetError ();
59         if (e != GL_NO_ERROR) {
60                 cout << last << " failed " << e << " (";
61                 if (e == GL_INVALID_VALUE) {
62                         cout << "invalid value";
63                 } else if (e == GL_INVALID_ENUM) {
64                         cout << "invalid enum";
65                 } else {
66                         cout << "unknown";
67                 }
68                 cout << ")\n";
69         }
70 }
71
72 void
73 GLView::paint (wxPaintEvent &)
74 {
75         SetCurrent (*_context);
76         wxPaintDC (this);
77         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
78         check_gl_error ("glClear");
79
80         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
81         check_gl_error ("glClearColor");
82         glEnable (GL_TEXTURE_2D);
83         check_gl_error ("glEnable GL_TEXTURE_2D");
84         glEnable (GL_COLOR_MATERIAL);
85         check_gl_error ("glEnable GL_COLOR_MATERIAL");
86         glEnable (GL_BLEND);
87         check_gl_error ("glEnable GL_BLEND");
88         glDisable (GL_DEPTH_TEST);
89         check_gl_error ("glDisable GL_DEPTH_TEST");
90         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
91
92         glViewport (0, 0, GetSize().x, GetSize().y);
93         glMatrixMode (GL_PROJECTION);
94         glLoadIdentity ();
95
96         gluOrtho2D (0, GetSize().x, GetSize().y, 0);
97         glMatrixMode (GL_MODELVIEW);
98         glLoadIdentity ();
99
100         glTranslatef (0, 0, 0);
101
102         if (_size) {
103                 glBegin (GL_QUADS);
104
105                 glTexCoord2f (0, 1);
106                 glVertex2f (0, _size->height);
107                 glTexCoord2f (1, 1);
108                 glVertex2f (_size->width, _size->height);
109                 glTexCoord2f (1, 0);
110                 glVertex2f (_size->width, 0);
111                 glTexCoord2f (0, 0);
112                 glVertex2f (0, 0);
113
114                 glEnd ();
115         }
116
117         glFlush();
118         SwapBuffers();
119 }
120
121 void
122 GLView::set_image (shared_ptr<const Image> image)
123 {
124         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
125         DCPOMATIC_ASSERT (!image->aligned());
126
127         _size = image->size ();
128         cout << "Here we go with " << _size.width << "x" << _size.height << "\n";
129         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
130         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size.width, _size.height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
131         check_gl_error ("glTexImage2D");
132
133         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
134         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
135         check_gl_error ("glTexParameteri");
136
137         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
138         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
139         check_gl_error ("glTexParameterf");
140
141         Refresh ();
142 }