-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ApplyPaletteShader.cpp
84 lines (67 loc) 路 2.21 KB
/
ApplyPaletteShader.cpp
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
/*****************************************************************************
* Copyright (c) 2014-2018 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#ifndef DISABLE_OPENGL
# include "ApplyPaletteShader.h"
namespace
{
struct VDStruct
{
GLfloat position[2];
GLfloat texturecoordinate[2];
};
} // namespace
constexpr VDStruct VertexData[4] = {
{ -1.0f, -1.0f, 0.0f, 0.0f },
{ 1.0f, -1.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f },
};
ApplyPaletteShader::ApplyPaletteShader()
: OpenGLShaderProgram("applypalette")
{
GetLocations();
glGenBuffers(1, &_vbo);
glGenVertexArrays(1, &_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData), VertexData, GL_STATIC_DRAW);
glBindVertexArray(_vao);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, sizeof(VDStruct), (void*)offsetof(VDStruct, position));
glVertexAttribPointer(
vTextureCoordinate, 2, GL_FLOAT, GL_FALSE, sizeof(VDStruct), (void*)offsetof(VDStruct, texturecoordinate));
glEnableVertexAttribArray(vPosition);
glEnableVertexAttribArray(vTextureCoordinate);
Use();
glUniform1i(uTexture, 0);
}
ApplyPaletteShader::~ApplyPaletteShader()
{
glDeleteBuffers(1, &_vbo);
glDeleteVertexArrays(1, &_vao);
}
void ApplyPaletteShader::GetLocations()
{
uTexture = GetUniformLocation("uTexture");
uPalette = GetUniformLocation("uPalette");
vPosition = GetAttributeLocation("vPosition");
vTextureCoordinate = GetAttributeLocation("vTextureCoordinate");
}
void ApplyPaletteShader::SetTexture(GLuint texture)
{
OpenGLAPI::SetTexture(0, GL_TEXTURE_2D, texture);
}
void ApplyPaletteShader::SetPalette(const vec4* glPalette)
{
glUniform4fv(uPalette, 256, (const GLfloat*)glPalette);
}
void ApplyPaletteShader::Draw()
{
glBindVertexArray(_vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
#endif /* DISABLE_OPENGL */