-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.h
113 lines (99 loc) · 3.49 KB
/
data.h
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
#ifndef DATA_H
#define DATA_H
#include <QSize>
#include <QOpenGLWidget>
#include <QMatrix4x4>
#include <QOpenGLFunctions_3_3_Core>
struct Rgba {
union {
GLuint rgba;
GLubyte bytes[4];
struct {
GLubyte b;
GLubyte g;
GLubyte r;
GLubyte a;
};
};
Rgba(const GLuint rgba = 0x00000000)
: rgba(rgba) {}
explicit Rgba(const GLubyte *const bytes) { if (bytes) { memcpy(this->bytes, bytes, 4 * sizeof(GLubyte)); } }
explicit Rgba(const GLubyte r, const GLubyte g, const GLubyte b, const GLubyte a)
: b(b), g(g), r(r), a(a) {}
Rgba(const Rgba &rgba)
: Rgba(rgba.rgba) {}
inline bool operator==(const Rgba &rhs) { return this->rgba == rhs.rgba; }
inline bool operator!=(const Rgba &rhs) { return !this->operator==(rhs); }
};
struct Colour : Rgba {
GLshort index;
Colour(const GLuint rgba = 0x00000000, const GLshort index = -1)
: Rgba(rgba), index(index) {}
explicit Colour(const Rgba rgba, const GLshort index = -1)
: Rgba(rgba), index(index) {}
explicit Colour(const GLubyte *const bytes, const GLshort index = -1)
: Rgba(bytes), index(index) {}
explicit Colour(const GLubyte r, const GLubyte g, const GLubyte b, const GLubyte a, const GLshort index = -1)
: Rgba(r, g, b, a), index(index) {}
Colour(const Colour &colour)
: Colour(colour.rgba, colour.index) {}
inline bool operator==(const Colour &rhs) { return this->Rgba::operator==(rhs) && this->index == rhs.index; }
inline bool operator!=(const Colour &rhs) { return !this->operator==(rhs); }
};
class OpenGLData : public QOpenGLFunctions_3_3_Core
{
public:
QOpenGLContext *const context;
QSurface *const surface;
explicit OpenGLData()
: context(QOpenGLContext::currentContext()), surface(context ? context->surface() : nullptr) { initializeOpenGLFunctions(); }
};
class TextureData : public OpenGLData
{
public:
struct Format {
enum Id {
Indexed,
RGBA,
};
Id id;
char *name;
GLint internalFormat;
GLint format;
GLint glEnum;
GLint size;
GLenum buffers[2];
GLint outputBuffer;
};
static const Format FORMATS[2];
const QSize size;
const Format::Id format;
const GLuint texture;
const GLuint framebuffer;
explicit TextureData(const QSize &size, const Format::Id format, const GLubyte *const data = nullptr);
virtual ~TextureData();
GLubyte *readPixel(const QPoint &position, GLubyte *const buffer = nullptr);
void writePixel(const QPoint &position, const GLubyte *const data);
GLubyte *readData(GLubyte *const buffer = nullptr);
void writeData(const GLubyte *const data);
void clear(const GLubyte *const data);
};
class PaletteData : public TextureData
{
public:
explicit PaletteData(const GLuint length, const GLubyte *const data = nullptr);
Rgba colour(const uint index) { Rgba rgba; readPixel(QPoint(index, 0), rgba.bytes); return rgba; }
void setColour(const uint index, const Rgba &rgba) { writePixel(QPoint(index, 0), rgba.bytes); }
GLuint length() const { return size.width(); }
};
class ImageData : public TextureData
{
public:
const QRect rect;
const QMatrix4x4 projectionMatrix;
const GLuint vertexArray;
const GLuint vertexBuffer;
explicit ImageData(const QSize &size, const Format::Id format, const GLubyte *const data = nullptr);
virtual ~ImageData();
};
#endif // DATA_H