summaryrefslogtreecommitdiff
path: root/src/wx/gl_util.h
blob: 3c8ba6dc5625d93b1ce2143cf9fb324fbdad5f90 (plain)
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
/*
    Copyright (C) 2025 Carl Hetherington <cth@carlh.net>

    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 <http://www.gnu.org/licenses/>.

*/


#ifndef DCPOMATIC_GL_UTIL_H
#define DCPOMATIC_GL_UTIL_H


#include <dcp/types.h>
#include <wx/wx.h>


/* This will only build on an new-enough wxWidgets: see the comment in gl_video_view.h */
#if wxCHECK_VERSION(3,1,0)

#ifdef DCPOMATIC_OSX
#define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl3.h>
#endif

#ifdef DCPOMATIC_LINUX
#include <GL/glu.h>
#include <GL/glext.h>
#endif

#ifdef DCPOMATIC_WINDOWS
#include <GL/glu.h>
#include <GL/wglext.h>
#endif


class Image;


extern GLuint compile_shader(GLenum type, char const* source);
extern void check_gl_error(char const* last);


namespace dcpomatic {
namespace gl {

class Rectangle
{
public:
	Rectangle(wxSize canvas_size, float x, float y, dcp::Size size)
		: _canvas_size(canvas_size)
	{
		auto const x1 = x_pixels_to_gl(x);
		auto const y1 = y_pixels_to_gl(y);
		auto const x2 = x_pixels_to_gl(x + size.width);
		auto const y2 = y_pixels_to_gl(y + size.height);

		/* The texture coordinates here have to account for the fact that when we put images into the texture OpenGL
		 * expected us to start at the lower left but we actually started at the top left.  So although the
		 * top of the texture is at 1.0 we pretend it's the other way round.
		 */

		// bottom right
		_vertices[0] = x2;
		_vertices[1] = y2;
		_vertices[2] = 0.0f;
		_vertices[3] = 1.0f;
		_vertices[4] = 1.0f;

		// top right
		_vertices[5] = x2;
		_vertices[6] = y1;
		_vertices[7] = 0.0f;
		_vertices[8] = 1.0f;
		_vertices[9] = 0.0f;

		// top left
		_vertices[10] = x1;
		_vertices[11] = y1;
		_vertices[12] = 0.0f;
		_vertices[13] = 0.0f;
		_vertices[14] = 0.0f;

		// bottom left
		_vertices[15] = x1;
		_vertices[16] = y2;
		_vertices[17] = 0.0f;
		_vertices[18] = 0.0f;
		_vertices[19] = 1.0f;
	}

	float const * vertices() const {
		return _vertices;
	}

	/** @return size of data returned from vertices() in bytes */
	int const size() const {
		return sizeof(_vertices);
	}

private:
	/* @param x x position in pixels where 0 is left and canvas_width is right on screen */
	float x_pixels_to_gl(int x) const {
		return (x * 2.0f / _canvas_size.GetWidth()) - 1.0f;
	}

	/* @param y y position in pixels where 0 is top and canvas_height is bottom on screen */
	float y_pixels_to_gl(int y) const {
		return 1.0f - (y * 2.0f / _canvas_size.GetHeight());
	}

	wxSize _canvas_size;
	float _vertices[20];
};


class Texture
{
public:
	/** @param unit Unit, or index, of this texture (from 0 to GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1) */
	Texture(GLint unpack_alignment, int unit = 0);
	~Texture();

	Texture(Texture const&) = delete;
	Texture& operator=(Texture const&) = delete;

	void bind();
	void set(std::shared_ptr<const Image> image, int component = 0);

private:
	GLuint _name;
	GLint _unpack_alignment;
	int _unit;
	boost::optional<dcp::Size> _size;
};


}
}


#endif
#endif