Euphoria
defaultfiles.cc
Go to the documentation of this file.
1 #include "render/defaultfiles.h"
2 
3 #include "io/vfs.h"
4 #include "io/vfs_path.h"
5 
6 namespace eu::render
7 {
8  void
9  setup_default_files(std::shared_ptr<io::ReadRootCatalog> catalog)
10  {
11  catalog->register_file_string
12  (
13  io::FilePath{"~/texture_types.json"},
14  R"json(
15  {
16  "name":
17  [
18  "Diffuse",
19  "Specular"
20  ]
21  }
22  )json"
23  );
24 
25  catalog->register_file_string
26  (
27  io::FilePath{"~/default_shader.json"},
28  R"json(
29  {
30  "has_light": true,
31  "ambient": "uMaterial.ambient",
32  "diffuse": "uMaterial.diffuse",
33  "specular": "uMaterial.specular",
34  "shininess": "uMaterial.shininess",
35  "textures":
36  [
37  {
38  "texture": "Diffuse",
39  "uniform": "uDiffuseMap"
40  },
41  {
42  "texture": "Specular",
43  "uniform": "uSpecularMap"
44  }
45  ],
46  "default_textures":
47  [
48  {
49  "texture": "Diffuse",
50  "path": "img-plain/white"
51  },
52  {
53  "texture": "Specular",
54  "path": "img-plain/white"
55  }
56  ]
57  }
58  )json"
59  );
60  catalog->register_file_string
61  (
62  io::FilePath{"~/default_shader.vert"},
63  R"glsl(
64  #version 330 core
65  in vec3 aPosition;
66  in vec3 aNormal;
67  in vec2 aTexCoord;
68 
69  out vec2 texCoord;
70  out vec3 normal;
71  out vec3 fragPositionWorld;
72 
73  uniform mat4 uProjection;
74  uniform mat4 uView;
75  uniform mat4 uModel;
76  uniform mat3 uNormalMatrix;
77 
78  void main()
79  {
80  gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
81  fragPositionWorld = vec3(uModel * vec4(aPosition, 1.0));
82  texCoord = aTexCoord;
83  normal = uNormalMatrix * aNormal;
84  }
85  )glsl"
86  );
87  catalog->register_file_string
88  (
89  io::FilePath{"~/default_shader.frag"},
90  R"glsl(
91  #version 330 core
92  struct Material
93  {
94  vec3 ambient;
95  vec3 diffuse;
96  vec3 specular;
97  float shininess;
98  };
99 
100  struct Light
101  {
102  int type;
103  vec3 ambient;
104  vec3 diffuse;
105  vec3 specular;
106 
107  float attConst;
108  float attLin;
109  float attQuad;
110  float cosCutoffAngleInner;
111  float cosCutoffAngleOuter;
112 
113  vec3 position;
114  vec3 direction;
115  };
116 
117  out vec4 FragColor;
118 
119  in vec2 texCoord;
120  in vec3 normal;
121  in vec3 fragPositionWorld;
122 
123  uniform sampler2D uDiffuseMap;
124  uniform sampler2D uSpecularMap;
125 
126  uniform Light uLight;
127  uniform vec3 uViewPosition;
128  uniform Material uMaterial;
129 
130  void main()
131  {
132  vec3 ambient = uMaterial.ambient * uLight.ambient;
133 
134  vec3 norm = normalize(normal);
135  vec3 lightDir = uLight.type==0 ? normalize(-uLight.direction) : normalize(uLight.position - fragPositionWorld);
136  float diffuse_factor = max(0.0, dot(norm, lightDir));
137  vec3 diffuse = diffuse_factor * uMaterial.diffuse * uLight.diffuse;
138 
139  float specularStrength = 0.5;
140  vec3 viewDir = normalize(uViewPosition - fragPositionWorld);
141  vec3 reflectDir = reflect(-lightDir, norm);
142  float spec = pow(max(0, dot(viewDir, reflectDir)), uMaterial.shininess);
143  vec3 specular = specularStrength * uMaterial.specular * spec * uLight.specular;
144 
145  if(uLight.type == 2)
146  {
147  float theta = dot(lightDir, normalize(-uLight.direction));
148  if(theta > uLight.cosCutoffAngleOuter)
149  {
150  float epsilon = uLight.cosCutoffAngleInner - uLight.cosCutoffAngleOuter;
151  float intensity = clamp((theta - uLight.cosCutoffAngleOuter) / epsilon, 0.0, 1.0);
152  diffuse *= intensity;
153  specular *= intensity;
154  }
155  else
156  {
157  // outside of spotlight
158  diffuse = vec3(0,0,0);
159  specular = vec3(0,0,0);
160  }
161  }
162 
163  if(uLight.type != 0)
164  {
165  float dist = length(uLight.position - fragPositionWorld);
166  float att = 1.0 / (uLight.attConst + dist*uLight.attLin + dist*dist*uLight.attQuad);
167  ambient *= att;
168  diffuse *= att;
169  specular *= att;
170  }
171 
172  vec3 object_color = texture(uDiffuseMap, texCoord).rgb;
173  vec3 specular_map = texture(uSpecularMap, texCoord).rgb;
174 
175  vec3 result = (ambient + diffuse) * object_color + specular * specular_map;
176 
177  FragColor = vec4(result, 1.0);
178  }
179  )glsl"
180  );
181 
182 
184  // basic_shader
185  catalog->register_file_string
186  (
187  io::FilePath{"~/basic_shader.json"},
188  R"json(
189  {
190  "diffuse": "uDiffuse",
191  "has_light": false,
192  "textures": [],
193  "default_textures": []
194  }
195  )json"
196  );
197  catalog->register_file_string
198  (
199  io::FilePath{"~/basic_shader.vert"},
200  R"glsl(
201  #version 330 core
202  in vec3 aPosition;
203 
204  uniform mat4 uProjection;
205  uniform mat4 uView;
206  uniform mat4 uModel;
207 
208  void main()
209  {
210  gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
211  }
212  )glsl"
213  );
214  catalog->register_file_string
215  (
216  io::FilePath{"~/basic_shader.frag"},
217  R"glsl(
218  #version 330 core
219 
220  uniform vec3 uDiffuse;
221 
222  out vec4 FragColor;
223 
224  void main()
225  {
226  FragColor = vec4(uDiffuse, 1.0);
227  }
228  )glsl"
229  );
230 
232  // default_line_shader
233  catalog->register_file_string
234  (
235  io::FilePath{"~/default_line_shader.json"},
236  R"json(
237  {
238  "has_light": false,
239  "textures": [],
240  "default_textures": []
241  }
242  )json"
243  );
244  catalog->register_file_string
245  (
246  io::FilePath{"~/default_line_shader.vert"},
247  R"glsl(
248  #version 330 core
249  in vec3 aPosition;
250  in vec3 aColor;
251 
252  uniform mat4 uProjection;
253  uniform mat4 uView;
254  uniform mat4 uModel;
255 
256  out vec3 color;
257 
258  void main()
259  {
260  gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
261  color = aColor;
262  }
263  )glsl"
264  );
265  catalog->register_file_string
266  (
267  io::FilePath{"~/default_line_shader.frag"},
268  R"glsl(
269  #version 330 core
270 
271  in vec3 color;
272 
273  out vec4 FragColor;
274 
275  void main()
276  {
277  FragColor = vec4(color, 1.0);
278  }
279  )glsl"
280  );
281  }
282 }
void setup_default_files(std::shared_ptr< io::ReadRootCatalog > catalog)
Definition: defaultfiles.cc:9