From 38ebc160d90fba626c47e9e27994c61a7e05686f Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Fri, 4 Jan 2019 00:32:28 +0000 Subject: [PATCH] GLView integration (not working). --- hacks/.gitignore | 1 + src/wx/film_viewer.cc | 2 + src/wx/gl_view.cc | 160 ++++++++++++++++++++++++++++++++++++++++++ src/wx/gl_view.h | 44 ++++++++++++ 4 files changed, 207 insertions(+) create mode 100644 src/wx/gl_view.cc create mode 100644 src/wx/gl_view.h diff --git a/hacks/.gitignore b/hacks/.gitignore index fbadc130f..97bb8a57b 100644 --- a/hacks/.gitignore +++ b/hacks/.gitignore @@ -21,3 +21,4 @@ text.png text text.exe LOCAL_APPDATA_FONTCONFIG_CACHE +gl diff --git a/src/wx/film_viewer.cc b/src/wx/film_viewer.cc index 5777b9771..acbbcdd25 100644 --- a/src/wx/film_viewer.cc +++ b/src/wx/film_viewer.cc @@ -270,7 +270,9 @@ FilmViewer::display_player_video () */ _state_timer.set ("get image"); + /* XXX: do we need to store _frame? */ _frame = _player_video.first->image (bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true); + _gl_view->set_image (_frame); _state_timer.set ("ImageChanged"); ImageChanged (_player_video.first); diff --git a/src/wx/gl_view.cc b/src/wx/gl_view.cc new file mode 100644 index 000000000..25df1496a --- /dev/null +++ b/src/wx/gl_view.cc @@ -0,0 +1,160 @@ +/* + Copyright (C) 2018 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + +#include "gl_view.h" +#include "lib/image.h" +#include +#include + +using std::cout; +using boost::shared_ptr; + +GLView::GLView (wxWindow *parent) + : wxGLCanvas (parent, wxID_ANY, 0) +{ + _context = new wxGLContext (this); + Bind (wxEVT_PAINT, boost::bind(&GLView::paint, this, _1)); + Bind (wxEVT_SIZE, boost::bind(&GLView::sized, this)); +} + +GLView::~GLView () +{ + delete _context; +} + +static void +check_gl_error (char const * last) +{ + GLenum const e = glGetError (); + if (e != GL_NO_ERROR) { + cout << last << " failed " << e << " ("; + if (e == GL_INVALID_VALUE) { + cout << "invalid value"; + } else if (e == GL_INVALID_ENUM) { + cout << "invalid enum"; + } else { + cout << "unknown"; + } + cout << ")\n"; + } +} + +void +GLView::sized () +{ + glViewport (0, 0, GetSize().x, GetSize().y); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glOrtho (0, GetSize().x, GetSize().y, 0, -1, 1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +void +GLView::paint (wxPaintEvent &) +{ + SetCurrent (*_context); + wxPaintDC (this); + glClear (GL_COLOR_BUFFER_BIT); + + glClearColor (0.0f, 0.0f, 0.0f, 1.0f); + glEnable (GL_TEXTURE_2D); + glDisable (GL_DEPTH_TEST); + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + int const width = GetSize().x; + int const height = GetSize().y; + + glPixelStorei (GL_UNPACK_ALIGNMENT, 1); + + GLuint tex; + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + check_gl_error ("glTexParameteri"); + + glClear(GL_COLOR_BUFFER_BIT); + glColor3f(1, 1, 1); + glBindTexture(GL_TEXTURE_2D, tex); + glEnable(GL_TEXTURE_2D); + glBegin(GL_QUADS); + glTexCoord2i(0, 0); + glVertex2i(0, 0); + glTexCoord2i(0, 1); + glVertex2i(0, height); + glTexCoord2i(1, 1); + glVertex2i(width, height); + glTexCoord2i(1, 0); + glVertex2i(width, 0); + glEnd(); + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); + + glFlush(); + SwapBuffers(); +} + +void +GLView::set_image (shared_ptr image) +{ + if (image->size() != _texture_size) { + glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, image->size().width, image->size().height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); + check_gl_error ("glTexImage2D"); + glBindTexture(GL_TEXTURE_2D, 0); + check_gl_error ("glBindTexture #1"); + _texture_size = image->size(); + } + + int const width = image->size().width; + int const height = image->size().height; + unsigned char foo[width * height * 3]; + unsigned char* p = foo; + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + if (((y / 16) % 3) == 0) { + *p++ = char(x * 256 / width); + } else { + *p++ = 0; + } + if (((y / 16) % 3) == 1) { + *p++ = char(x * 256 / width); + } else { + *p++ = 0; + } + if (((y / 16) % 3) == 2) { + *p++ = char(x * 256 / width); + } else { + *p++ = 0; + } + } + } + + glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _texture_size.width, _texture_size.height, GL_RGB, GL_UNSIGNED_BYTE, foo);//image->data()[0]); + check_gl_error ("glTexSubImage2D"); + glBindTexture(GL_TEXTURE_2D, 0); + check_gl_error ("glBindTexture2D #2"); + + glFlush(); + if (GetXWindow()) { + SwapBuffers(); + } +} diff --git a/src/wx/gl_view.h b/src/wx/gl_view.h new file mode 100644 index 000000000..1e6482316 --- /dev/null +++ b/src/wx/gl_view.h @@ -0,0 +1,44 @@ +/* + Copyright (C) 2018 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + +#include +#include +#include +#include + +#undef None + +class Image; + +class GLView : public wxGLCanvas +{ +public: + GLView (wxWindow* parent); + ~GLView (); + + void set_image (boost::shared_ptr image); + +private: + void paint (wxPaintEvent& event); + void sized (); + + wxGLContext* _context; + dcp::Size _texture_size; +}; -- 2.30.2