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