aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/Shaders/base.shader.h
blob: 3790a47ad787ee5a93aee748731bdd86eec92400 (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
/*
 * https://stackoverflow.com/questions/10868958/what-does-sampler2d-store
 * The sampler2D is bound to a texture unit. The glUniform call binds it to texture
 * unit zero. The glActiveTexture call is only needed if you are going to use multiple
 * texture units (because GL_TEXTURE0 is the default anyway).
*/
/*
#VERTEX_SHADER

vertex vert(vertex v)
{
    return v;
}

#END_VERTEX_SHADER

#FRAGMENT_SHADER

vec4 frag(vec4 color, Texture tex, vertex v)
{
    return Texel(tex, v.uv);
}

#END_FRAGMENT_SHADER

*/

static const char* base_shared = R"(
#define Number	float
#define Texture sampler2D 
#define Canvas  sampler2D 
#define Color   vec4
#define Vec2	vec2
#define Vec3	vec3
#define Vec4	vec4

#define texel   texture2D

struct Vertex
{
    vec2 xy; 
    vec2 uv;
};

)";

static const int BASE_SHARED_SIZE = strlen(base_shared);

static const char* base_vertex = R"(
#version 130 core

%s

uniform mat4 _projectionMatrix_;
uniform mat4 _modelMatrix_;

in vec2 _vert_coord_;
in vec2 _tex_coord_;

out vec4 _color;
out vec2 _xy;
out vec2 _uv;

%s

void main()
{
	vec4 v = _modelMatrix_ * vec4(_vert_coord_, 0, 1.0);
    Vertex _v = vert(Vertex(v.xy, _tex_coord_));
	gl_Position = _projectionMatrix_ * vec4(_v.xy, 0, 1.0f);
	_color = gl_Color;
    _xy = _v.xy;
    _uv = _v.uv;
}
)";

static const int BASE_VERTEX_SHADER_SIZE = strlen(base_vertex) + BASE_SHARED_SIZE;

#define formatVertexShader(buf, program) sprintf(buf,base_vertex, base_shared, program)

static const char* base_fragment = R"(
#version 130 core

%s

uniform Texture _main_texture_;

in vec4 _color;
in vec2 _xy; 
in vec2 _uv;

out vec4 _outColor_;

%s

void main()
{
    _outColor_ = frag(_color, _main_texture_, Vertex(_xy, _uv));
}
)";

static const int BASE_FRAGMENT_SHADER_SIZE = strlen(base_fragment) + BASE_SHARED_SIZE;

#define formatFragmentShader(buf, program) sprintf(buf, base_fragment, base_shared, program)