2 Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "gl_video_view.h"
22 #include "lib/image.h"
23 #include "lib/dcpomatic_assert.h"
24 #include "lib/exceptions.h"
25 #include <boost/bind.hpp>
29 #include <OpenGL/glu.h>
30 #include <OpenGL/glext.h>
37 using boost::shared_ptr;
39 GLVideoView::GLVideoView (wxWindow *parent)
41 _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
42 _context = new wxGLContext (_canvas);
43 _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this, _1));
45 glGenTextures (1, &_id);
46 glBindTexture (GL_TEXTURE_2D, _id);
47 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
50 GLVideoView::~GLVideoView ()
52 glDeleteTextures (1, &_id);
54 /* XXX: should we delete this? */
59 check_gl_error (char const * last)
61 GLenum const e = glGetError ();
62 if (e != GL_NO_ERROR) {
63 throw GLError (last, e);
68 GLVideoView::paint (wxPaintEvent &)
70 _canvas->SetCurrent (*_context);
71 wxPaintDC dc (_canvas);
72 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
73 check_gl_error ("glClear");
75 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
76 check_gl_error ("glClearColor");
77 glEnable (GL_TEXTURE_2D);
78 check_gl_error ("glEnable GL_TEXTURE_2D");
79 glEnable (GL_COLOR_MATERIAL);
80 check_gl_error ("glEnable GL_COLOR_MATERIAL");
82 check_gl_error ("glEnable GL_BLEND");
83 glDisable (GL_DEPTH_TEST);
84 check_gl_error ("glDisable GL_DEPTH_TEST");
85 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
87 glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
88 glMatrixMode (GL_PROJECTION);
91 gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
92 glMatrixMode (GL_MODELVIEW);
95 glTranslatef (0, 0, 0);
101 glVertex2f (0, _size->height);
103 glVertex2f (_size->width, _size->height);
105 glVertex2f (_size->width, 0);
113 _canvas->SwapBuffers();
117 GLVideoView::set_image (shared_ptr<const Image> image)
119 /* XXX: put this somewhere sensible */
120 cout << glGetString (GL_VERSION) << "\n";
122 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
123 DCPOMATIC_ASSERT (!image->aligned());
125 _size = image->size ();
126 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
127 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
128 check_gl_error ("glTexImage2D");
130 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
131 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
132 check_gl_error ("glTexParameteri");
134 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
135 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
136 check_gl_error ("glTexParameterf");