16f36cee1676afd2d5761cbec55a298ddbac39c8
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / standard.h
1 #ifndef SHADER_standard_H
2 #define SHADER_standard_H
3 static void shader_standard_link(void);
4 static void shader_standard_register(void);
5 static struct vg_shader _shader_standard = {
6 .name = "standard",
7 .link = shader_standard_link,
8 .vs =
9 {
10 .orig_file = "../../shaders/standard.vs",
11 .static_src =
12 "layout (location=0) in vec3 a_co;\n"
13 "layout (location=1) in vec3 a_norm;\n"
14 "layout (location=2) in vec2 a_uv;\n"
15 "layout (location=3) in vec4 a_colour;\n"
16 "layout (location=4) in vec4 a_weights;\n"
17 "layout (location=5) in ivec4 a_groups;\n"
18 "\n"
19 "#line 2 0 \n"
20 "\n"
21 "uniform mat4 uPv;\n"
22 "uniform mat4x3 uMdl;\n"
23 "\n"
24 "out vec4 aColour;\n"
25 "out vec2 aUv;\n"
26 "out vec3 aNorm;\n"
27 "out vec3 aCo;\n"
28 "out vec3 aWorldCo;\n"
29 "\n"
30 "void main()\n"
31 "{\n"
32 " vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
33 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
34 " aColour = a_colour;\n"
35 " aUv = a_uv;\n"
36 " aNorm = mat3(uMdl) * a_norm;\n"
37 " aCo = a_co;\n"
38 " aWorldCo = world_pos;\n"
39 "}\n"
40 ""},
41 .fs =
42 {
43 .orig_file = "../../shaders/standard.fs",
44 .static_src =
45 "out vec4 FragColor;\n"
46 "\n"
47 "uniform sampler2D uTexMain;\n"
48 "uniform vec4 uColour;\n"
49 "\n"
50 "in vec4 aColour;\n"
51 "in vec2 aUv;\n"
52 "in vec3 aNorm;\n"
53 "in vec3 aCo;\n"
54 "\n"
55 "void main()\n"
56 "{\n"
57 " vec3 diffuse = texture( uTexMain, aUv ).rgb;\n"
58 " float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));\n"
59 " float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));\n"
60 " diffuse += vec3(0.2,0.2,0.2) +\n"
61 " vec3(1.0,1.0,0.9)*light1 + \n"
62 " vec3(0.1,0.3,0.4)*light2;\n"
63 " FragColor = vec4(diffuse*uColour.rgb, aColour.a*uColour.a);\n"
64 "}\n"
65 ""},
66 };
67
68 static GLuint _uniform_standard_uPv;
69 static GLuint _uniform_standard_uMdl;
70 static GLuint _uniform_standard_uTexMain;
71 static GLuint _uniform_standard_uColour;
72 static void shader_standard_uPv(m4x4f m){
73 glUniformMatrix4fv( _uniform_standard_uPv, 1, GL_FALSE, (float *)m );
74 }
75 static void shader_standard_uMdl(m4x3f m){
76 glUniformMatrix4x3fv( _uniform_standard_uMdl, 1, GL_FALSE, (float *)m );
77 }
78 static void shader_standard_uTexMain(int i){
79 glUniform1i( _uniform_standard_uTexMain, i );
80 }
81 static void shader_standard_uColour(v4f v){
82 glUniform4fv( _uniform_standard_uColour, 1, v );
83 }
84 static void shader_standard_register(void){
85 vg_shader_register( &_shader_standard );
86 }
87 static void shader_standard_use(void){ glUseProgram(_shader_standard.id); }
88 static void shader_standard_link(void){
89 _uniform_standard_uPv = glGetUniformLocation( _shader_standard.id, "uPv" );
90 _uniform_standard_uMdl = glGetUniformLocation( _shader_standard.id, "uMdl" );
91 _uniform_standard_uTexMain = glGetUniformLocation( _shader_standard.id, "uTexMain" );
92 _uniform_standard_uColour = glGetUniformLocation( _shader_standard.id, "uColour" );
93 }
94 #endif /* SHADER_standard_H */