summaryrefslogtreecommitdiff
path: root/src/effects_gl.cpp
blob: 64a0758d39ac6b40795387ef172b3ce4e2108758 (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
#if SPECIAL
#include "main.h"
#endif

void GL_UpdateTexture(gl_effect_layer *Test, void *Data, uint16 Width, uint16 Height, uint16 BytesPerPixel, bool32 Multisample);
static uint16 Effect_GL_InitShader(const char *Effect);
static void GL_BindDefaultVertexArrays();

void Effect_GL_Start(gl_effect_layer *Test, int Width, int Height, int BytesPerPixel, void *EffectBitmapAddress, uint16 ShaderProgram)
{
    GL_UpdateTexture(Test, EffectBitmapAddress, Width, Height, BytesPerPixel, 0);

    GL_BindDefaultVertexArrays();

    glUseProgram(ShaderProgram);

    glBindFramebuffer(GL_FRAMEBUFFER, Test->FramebufferObject);
    glBindRenderbuffer(GL_RENDERBUFFER, Test->Color_Renderbuffer);

    glBindTexture(GL_TEXTURE_2D, Test->Texture);
    int ByteFlag = (BytesPerPixel == 4) ? GL_RGBA : GL_RGBA16;
    int ByteFlag2 = (BytesPerPixel == 4) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
    glTexImage2D(GL_TEXTURE_2D, 0, ByteFlag, Width, Height, 0, GL_RGBA,
                 ByteFlag2, EffectBitmapAddress);
}

void Effect_GL_DrawColor(int Width, int Height, int BytesPerPixel, void *EffectBitmapAddress,
                         uint16 ShaderProgram, v4 Color)
{
    gl_effect_layer Test = {};

    int ByteFlag = (BytesPerPixel == 4) ? GL_RGBA : GL_RGBA16;
    int ByteFlag2 = (BytesPerPixel == 4) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
    Effect_GL_Start(&Test, Width, Height, BytesPerPixel, EffectBitmapAddress, ShaderProgram);

    int Uniform = glGetUniformLocation(ShaderProgram, "Color");
    glUniform4f(Uniform, Color.r, Color.g, Color.b, Color.a);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glReadPixels(0, 0, Width, Height, GL_RGBA, ByteFlag2, EffectBitmapAddress);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    GL_DeleteHWBuffer(&Test);
}

void Effect_GL_GaussianBlur(int Width, int Height, int BytesPerPixel, void *EffectBitmapAddress,
                            uint16 ShaderProgram, real32 Radius)
{
    gl_effect_layer Test = {};

    int ByteFlag = (BytesPerPixel == 4) ? GL_RGBA : GL_RGBA16;
    int ByteFlag2 = (BytesPerPixel == 4) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
    Effect_GL_Start(&Test, Width, Height, BytesPerPixel, EffectBitmapAddress, ShaderProgram);

    // horizontal pass
    int Uniform = glGetUniformLocation(ShaderProgram, "Radius");
    glUniform1f(Uniform, Radius + 1.60f);
    Uniform = glGetUniformLocation(ShaderProgram, "Direction");
    glUniform2f(Uniform, 1.0f, 0.0f);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glReadPixels(0, 0, Width, Height, GL_RGBA, ByteFlag2, EffectBitmapAddress);
    //

    // vertical pass
    glTexImage2D(GL_TEXTURE_2D, 0, ByteFlag, Width, Height, 0, GL_RGBA,
                 ByteFlag2, EffectBitmapAddress);

    Radius = glGetUniformLocation(ShaderProgram, "Direction");
    glUniform2f(Uniform, 0.0f, 1.0f);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glReadPixels(0, 0, Width, Height, GL_RGBA, ByteFlag2, EffectBitmapAddress);
    //

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    GL_DeleteHWBuffer(&Test);
}

void Effect_GL_Levels(int Width, int Height, int BytesPerPixel, void *EffectBitmapAddress,
                         uint16 ShaderProgram, real32 Min, real32 Mid, real32 Max, v4 ColMin, v4 ColMid, v4 ColMax)
{
    gl_effect_layer Test = {};

    int ByteFlag = (BytesPerPixel == 4) ? GL_RGBA : GL_RGBA16;
    int ByteFlag2 = (BytesPerPixel == 4) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
    Effect_GL_Start(&Test, Width, Height, BytesPerPixel, EffectBitmapAddress, ShaderProgram);

    int Uniform = glGetUniformLocation(ShaderProgram, "Start");
    glUniform1f(Uniform, Min);
    Uniform = glGetUniformLocation(ShaderProgram, "Mid");
    glUniform1f(Uniform, Mid);
    Uniform = glGetUniformLocation(ShaderProgram, "End");
    glUniform1f(Uniform, Max);
    Uniform = glGetUniformLocation(ShaderProgram, "StartCol");
    glUniform4f(Uniform, ColMin.r, ColMin.g, ColMin.b,  ColMin.a);
    Uniform = glGetUniformLocation(ShaderProgram, "MidCol");
    glUniform4f(Uniform, ColMid.r, ColMid.g, ColMid.b,  ColMid.a);
    Uniform = glGetUniformLocation(ShaderProgram, "EndCol");
    glUniform4f(Uniform, ColMax.r, ColMax.g, ColMax.b,  ColMax.a);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glReadPixels(0, 0, Width, Height, GL_RGBA, ByteFlag2, EffectBitmapAddress);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    GL_DeleteHWBuffer(&Test);
}