1#include "shaders.h"
2
3#include "../util/transform.h"
4#include "../util/shader.h"
5
6#include <stdlib.h>
7#include <stdio.h>
8
9static const char *sliBasicVertexShaderCode =
10"#version 150 \n"
11"uniform mat4 u_Projection;"
12"uniform mat4 u_Modelview;"
13"in vec2 a_Vertex;"
14"void main()"
15"{"
16"gl_Position = u_Projection * u_Modelview * vec4(a_Vertex, 0.0, 1.0);"
17"}";
18
19static const char *sliBasicFragmentShaderCode =
20"#version 150 \n"
21"uniform vec4 u_Color;"
22"out vec4 f_Color;"
23"void main()"
24"{"
25"f_Color = u_Color;"
26"}";
27
28static const char *sliPointVertexShaderCode =
29"#version 150 \n"
30"uniform mat4 u_Projection;"
31"in vec2 a_Vertex;"
32"in vec4 a_Color;"
33"out vec4 v_Color;"
34"void main()"
35"{"
36"v_Color = a_Color;"
37"gl_Position = u_Projection * vec4(a_Vertex, 0.0, 1.0);"
38"}";
39
40static const char *sliPointFragmentShaderCode =
41"#version 150 \n"
42"in vec4 v_Color;"
43"out vec4 f_Color;"
44"void main()"
45"{"
46"f_Color = v_Color;"
47"}";
48
49static const char *sliTextureVertexShaderCode =
50"#version 150 \n"
51"uniform mat4 u_Projection;"
52"uniform mat4 u_Modelview;"
53"in vec2 a_Vertex;"
54"in vec2 a_TexCoord;"
55"out vec2 v_TexCoord;"
56"void main()"
57"{"
58"v_TexCoord = a_TexCoord;"
59"gl_Position = u_Projection * u_Modelview * vec4(a_Vertex, 0.0, 1.0);"
60"}";
61
62static const char *sliTextureFragmentShaderCode =
63"#version 150 \n"
64"uniform sampler2D u_Texture;"
65"uniform vec4 u_Color;"
66"uniform vec2 u_Tiling;"
67"uniform vec2 u_Scroll;"
68"in vec2 v_TexCoord;"
69"out vec4 f_Color;"
70"void main()"
71"{"
72"f_Color = texture(u_Texture, (v_TexCoord * u_Tiling) + u_Scroll) * u_Color;"
73"}";
74
75static const char *sliTextVertexShaderCode =
76"#version 150 \n"
77"uniform mat4 u_Projection;"
78"uniform mat4 u_Modelview;"
79"in vec2 a_Vertex;"
80"in vec2 a_TexCoord;"
81"out vec2 v_TexCoord;"
82"void main()"
83"{"
84"v_TexCoord = a_TexCoord;"
85"gl_Position = u_Projection * u_Modelview * vec4(a_Vertex, 0.0, 1.0);"
86"}";
87
88static const char *sliTextFragmentShaderCode =
89"#version 150 \n"
90"uniform sampler2D u_Texture;"
91"uniform vec4 u_Color;"
92"in vec2 v_TexCoord;"
93"out vec4 f_Color;"
94"void main()"
95"{"
96"float luminance = texture(u_Texture, v_TexCoord).r;"
97"f_Color = u_Color * vec4(luminance);"
98"}";
99
100Shader *sliBasicShader;
101Shader *sliPointShader;
102Shader *sliTextureShader;
103Shader *sliTextShader;
104
105void sliShadersInit(Mat4 *projection)
106{
107 sliBasicShader = shaderLoad(sliBasicVertexShaderCode, sliBasicFragmentShaderCode);
108 shaderBindAttrib(sliBasicShader, "a_Vertex", 0);
109 shaderLink(sliBasicShader);
110 shaderBind(sliBasicShader);
111 shaderUniformMatrix4fv(sliBasicShader, "u_Projection", 1, (float*)projection);
112 shaderFinalize(sliBasicShader);
113
114 sliPointShader = shaderLoad(sliPointVertexShaderCode, sliPointFragmentShaderCode);
115 shaderBindAttrib(sliPointShader, "a_Vertex", 0);
116 shaderBindAttrib(sliPointShader, "a_Color", 1);
117 shaderLink(sliPointShader);
118 shaderBind(sliPointShader);
119 shaderUniformMatrix4fv(sliPointShader, "u_Projection", 1, (float*)projection);
120 shaderFinalize(sliPointShader);
121
122 sliTextureShader = shaderLoad(sliTextureVertexShaderCode, sliTextureFragmentShaderCode);
123 shaderBindAttrib(sliTextureShader, "a_Vertex", 0);
124 shaderBindAttrib(sliTextureShader, "a_TexCoord", 1);
125 shaderLink(sliTextureShader);
126 shaderBind(sliTextureShader);
127 shaderUniformMatrix4fv(sliTextureShader, "u_Projection", 1, (float*)projection);
128 shaderUniform1i(sliTextureShader, "u_Texture", 0);
129 shaderFinalize(sliTextureShader);
130
131 sliTextShader = shaderLoad(sliTextVertexShaderCode, sliTextFragmentShaderCode);
132 shaderBindAttrib(sliTextShader, "a_Vertex", 0);
133 shaderBindAttrib(sliTextShader, "a_TexCoord", 1);
134 shaderLink(sliTextShader);
135 shaderBind(sliTextShader);
136 shaderUniformMatrix4fv(sliTextShader, "u_Projection", 1, (float*)projection);
137 shaderUniform1i(sliTextShader, "u_Texture", 0);
138 shaderFinalize(sliTextShader);
139}
140
141void sliShadersDestroy()
142{
143 shaderDelete(sliBasicShader);
144 shaderDelete(sliPointShader);
145 shaderDelete(sliTextureShader);
146 shaderDelete(sliTextShader);
147}
148