sources.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. // File generated by G3NSHADERS. Do not edit.
  2. // To regenerate this file install 'g3nshaders' and execute:
  3. // 'go generate' in this folder.
  4. package shaders
  5. const include_phong_model_source = `/***
  6. phong lighting model
  7. Parameters:
  8. position: input vertex position in camera coordinates
  9. normal: input vertex normal in camera coordinates
  10. camDir: input camera directions
  11. matAmbient: input material ambient color
  12. matDiffuse: input material diffuse color
  13. ambdiff: output ambient+diffuse color
  14. spec: output specular color
  15. Uniforms:
  16. AmbientLightColor[]
  17. DiffuseLightColor[]
  18. DiffuseLightPosition[]
  19. PointLightColor[]
  20. PointLightPosition[]
  21. PointLightLinearDecay[]
  22. PointLightQuadraticDecay[]
  23. MatSpecularColor
  24. MatShininess
  25. *****/
  26. void phongModel(vec4 position, vec3 normal, vec3 camDir, vec3 matAmbient, vec3 matDiffuse, out vec3 ambdiff, out vec3 spec) {
  27. vec3 ambientTotal = vec3(0.0);
  28. vec3 diffuseTotal = vec3(0.0);
  29. vec3 specularTotal = vec3(0.0);
  30. #if AMB_LIGHTS>0
  31. // Ambient lights
  32. for (int i = 0; i < AMB_LIGHTS; i++) {
  33. ambientTotal += AmbientLightColor[i] * matAmbient;
  34. }
  35. #endif
  36. #if DIR_LIGHTS>0
  37. // Directional lights
  38. for (int i = 0; i < DIR_LIGHTS; i++) {
  39. // Diffuse reflection
  40. // DirLightPosition is the direction of the current light
  41. vec3 lightDirection = normalize(DirLightPosition(i));
  42. // Calculates the dot product between the light direction and this vertex normal.
  43. float dotNormal = max(dot(lightDirection, normal), 0.0);
  44. diffuseTotal += DirLightColor(i) * matDiffuse * dotNormal;
  45. // Specular reflection
  46. // Calculates the light reflection vector
  47. vec3 ref = reflect(-lightDirection, normal);
  48. if (dotNormal > 0.0) {
  49. specularTotal += DirLightColor(i) * MatSpecularColor * pow(max(dot(ref, camDir), 0.0), MatShininess);
  50. }
  51. }
  52. #endif
  53. #if POINT_LIGHTS>0
  54. // Point lights
  55. for (int i = 0; i < POINT_LIGHTS; i++) {
  56. // Common calculations
  57. // Calculates the direction and distance from the current vertex to this point light.
  58. vec3 lightDirection = PointLightPosition(i) - vec3(position);
  59. float lightDistance = length(lightDirection);
  60. // Normalizes the lightDirection
  61. lightDirection = lightDirection / lightDistance;
  62. // Calculates the attenuation due to the distance of the light
  63. float attenuation = 1.0 / (1.0 + PointLightLinearDecay(i) * lightDistance +
  64. PointLightQuadraticDecay(i) * lightDistance * lightDistance);
  65. // Diffuse reflection
  66. float dotNormal = max(dot(lightDirection, normal), 0.0);
  67. diffuseTotal += PointLightColor(i) * matDiffuse * dotNormal * attenuation;
  68. // Specular reflection
  69. // Calculates the light reflection vector
  70. vec3 ref = reflect(-lightDirection, normal);
  71. if (dotNormal > 0.0) {
  72. specularTotal += PointLightColor(i) * MatSpecularColor *
  73. pow(max(dot(ref, camDir), 0.0), MatShininess) * attenuation;
  74. }
  75. }
  76. #endif
  77. #if SPOT_LIGHTS>0
  78. for (int i = 0; i < SPOT_LIGHTS; i++) {
  79. // Calculates the direction and distance from the current vertex to this spot light.
  80. vec3 lightDirection = SpotLightPosition(i) - vec3(position);
  81. float lightDistance = length(lightDirection);
  82. lightDirection = lightDirection / lightDistance;
  83. // Calculates the attenuation due to the distance of the light
  84. float attenuation = 1.0 / (1.0 + SpotLightLinearDecay(i) * lightDistance +
  85. SpotLightQuadraticDecay(i) * lightDistance * lightDistance);
  86. // Calculates the angle between the vertex direction and spot direction
  87. // If this angle is greater than the cutoff the spotlight will not contribute
  88. // to the final color.
  89. float angle = acos(dot(-lightDirection, SpotLightDirection(i)));
  90. float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
  91. if (angle < cutoff) {
  92. float spotFactor = pow(dot(-lightDirection, SpotLightDirection(i)), SpotLightAngularDecay(i));
  93. // Diffuse reflection
  94. float dotNormal = max(dot(lightDirection, normal), 0.0);
  95. diffuseTotal += SpotLightColor(i) * matDiffuse * dotNormal * attenuation * spotFactor;
  96. // Specular reflection
  97. vec3 ref = reflect(-lightDirection, normal);
  98. if (dotNormal > 0.0) {
  99. specularTotal += SpotLightColor(i) * MatSpecularColor * pow(max(dot(ref, camDir), 0.0), MatShininess) * attenuation * spotFactor;
  100. }
  101. }
  102. }
  103. #endif
  104. // Sets output colors
  105. ambdiff = ambientTotal + MatEmissiveColor + diffuseTotal;
  106. spec = specularTotal;
  107. }
  108. `
  109. const include_material_source = `//
  110. // Material properties uniform
  111. //
  112. // Material parameters uniform array
  113. uniform vec3 Material[6];
  114. // Macros to access elements inside the Material array
  115. #define MatAmbientColor Material[0]
  116. #define MatDiffuseColor Material[1]
  117. #define MatSpecularColor Material[2]
  118. #define MatEmissiveColor Material[3]
  119. #define MatShininess Material[4].x
  120. #define MatOpacity Material[4].y
  121. #define MatPointSize Material[4].z
  122. #define MatPointRotationZ Material[5].x
  123. #if MAT_TEXTURES > 0
  124. // Texture unit sampler array
  125. uniform sampler2D MatTexture[MAT_TEXTURES];
  126. // Texture parameters (3*vec2 per texture)
  127. uniform vec2 MatTexinfo[3*MAT_TEXTURES];
  128. // Macros to access elements inside the MatTexinfo array
  129. #define MatTexOffset(a) MatTexinfo[(3*a)]
  130. #define MatTexRepeat(a) MatTexinfo[(3*a)+1]
  131. #define MatTexFlipY(a) bool(MatTexinfo[(3*a)+2].x)
  132. #define MatTexVisible(a) bool(MatTexinfo[(3*a)+2].y)
  133. #endif
  134. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  135. // This macro is used to mix the texture with the specified index with the material color.
  136. // It should be called for each texture index. It uses two externally defined variables:
  137. // vec4 texColor
  138. // vec4 texMixed
  139. #define MIX_TEXTURE(i) \
  140. if (MatTexVisible(i)) { \
  141. texColor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i)); \
  142. if (i == 0) { \
  143. texMixed = texColor; \
  144. } else { \
  145. texMixed = mix(texMixed, texColor, texColor.a); \
  146. } \
  147. }
  148. `
  149. const include_lights_source = `//
  150. // Lights uniforms
  151. //
  152. // Ambient lights uniforms
  153. #if AMB_LIGHTS>0
  154. uniform vec3 AmbientLightColor[AMB_LIGHTS];
  155. #endif
  156. // Directional lights uniform array. Each directional light uses 2 elements
  157. #if DIR_LIGHTS>0
  158. uniform vec3 DirLight[2*DIR_LIGHTS];
  159. // Macros to access elements inside the DirectionalLight uniform array
  160. #define DirLightColor(a) DirLight[2*a]
  161. #define DirLightPosition(a) DirLight[2*a+1]
  162. #endif
  163. // Point lights uniform array. Each point light uses 3 elements
  164. #if POINT_LIGHTS>0
  165. uniform vec3 PointLight[3*POINT_LIGHTS];
  166. // Macros to access elements inside the PointLight uniform array
  167. #define PointLightColor(a) PointLight[3*a]
  168. #define PointLightPosition(a) PointLight[3*a+1]
  169. #define PointLightLinearDecay(a) PointLight[3*a+2].x
  170. #define PointLightQuadraticDecay(a) PointLight[3*a+2].y
  171. #endif
  172. #if SPOT_LIGHTS>0
  173. // Spot lights uniforms. Each spot light uses 5 elements
  174. uniform vec3 SpotLight[5*SPOT_LIGHTS];
  175. // Macros to access elements inside the PointLight uniform array
  176. #define SpotLightColor(a) SpotLight[5*a]
  177. #define SpotLightPosition(a) SpotLight[5*a+1]
  178. #define SpotLightDirection(a) SpotLight[5*a+2]
  179. #define SpotLightAngularDecay(a) SpotLight[5*a+3].x
  180. #define SpotLightCutoffAngle(a) SpotLight[5*a+3].y
  181. #define SpotLightLinearDecay(a) SpotLight[5*a+3].z
  182. #define SpotLightQuadraticDecay(a) SpotLight[5*a+4].x
  183. #endif
  184. `
  185. const include_attributes_source = `//
  186. // Vertex attributes
  187. //
  188. layout(location = 0) in vec3 VertexPosition;
  189. layout(location = 1) in vec3 VertexNormal;
  190. layout(location = 2) in vec3 VertexColor;
  191. layout(location = 3) in vec2 VertexTexcoord;
  192. layout(location = 4) in float VertexDistance;
  193. layout(location = 5) in vec4 VertexTexoffsets;
  194. `
  195. const basic_fragment_source = `//
  196. // Fragment Shader template
  197. //
  198. in vec3 Color;
  199. out vec4 FragColor;
  200. void main() {
  201. FragColor = vec4(Color, 1.0);
  202. }
  203. `
  204. const standard_fragment_source = `//
  205. // Fragment Shader template
  206. //
  207. #include <material>
  208. // Inputs from Vertex shader
  209. in vec3 ColorFrontAmbdiff;
  210. in vec3 ColorFrontSpec;
  211. in vec3 ColorBackAmbdiff;
  212. in vec3 ColorBackSpec;
  213. in vec2 FragTexcoord;
  214. // Output
  215. out vec4 FragColor;
  216. void main() {
  217. // Mix material color with textures colors
  218. vec4 texMixed = vec4(1);
  219. vec4 texColor;
  220. #if MAT_TEXTURES==1
  221. MIX_TEXTURE(0)
  222. #elif MAT_TEXTURES==2
  223. MIX_TEXTURE(0)
  224. MIX_TEXTURE(1)
  225. #elif MAT_TEXTURES==3
  226. MIX_TEXTURE(0)
  227. MIX_TEXTURE(1)
  228. MIX_TEXTURE(2)
  229. #endif
  230. vec4 colorAmbDiff;
  231. vec4 colorSpec;
  232. if (gl_FrontFacing) {
  233. colorAmbDiff = vec4(ColorFrontAmbdiff, MatOpacity);
  234. colorSpec = vec4(ColorFrontSpec, 0);
  235. } else {
  236. colorAmbDiff = vec4(ColorBackAmbdiff, MatOpacity);
  237. colorSpec = vec4(ColorBackSpec, 0);
  238. }
  239. FragColor = min(colorAmbDiff * texMixed + colorSpec, vec4(1));
  240. }
  241. `
  242. const panel_vertex_source = `//
  243. // Vertex shader panel
  244. //
  245. #include <attributes>
  246. // Model uniforms
  247. uniform mat4 ModelMatrix;
  248. // Outputs for fragment shader
  249. out vec2 FragTexcoord;
  250. void main() {
  251. // Always flip texture coordinates
  252. vec2 texcoord = VertexTexcoord;
  253. texcoord.y = 1 - texcoord.y;
  254. FragTexcoord = texcoord;
  255. // Set position
  256. vec4 pos = vec4(VertexPosition.xyz, 1);
  257. gl_Position = ModelMatrix * pos;
  258. }
  259. `
  260. const sprite_fragment_source = `//
  261. // Fragment shader for sprite
  262. //
  263. #include <material>
  264. // Inputs from vertex shader
  265. in vec3 Color;
  266. in vec2 FragTexcoord;
  267. // Output
  268. out vec4 FragColor;
  269. void main() {
  270. // Combine all texture colors and opacity
  271. vec4 texCombined = vec4(1);
  272. #if MAT_TEXTURES>0
  273. for (int i = 0; i < {{.MatTexturesMax}}; i++) {
  274. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i));
  275. if (i == 0) {
  276. texCombined = texcolor;
  277. } else {
  278. texCombined = mix(texCombined, texcolor, texcolor.a);
  279. }
  280. }
  281. #endif
  282. // Combine material color with texture
  283. FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
  284. }
  285. `
  286. const point_fragment_source = `#include <material>
  287. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  288. // This macro is used to mix the texture with the specified index with the material color.
  289. // It should be called for each texture index.
  290. #define MIX_POINT_TEXTURE(i) \
  291. if (MatTexVisible(i)) { \
  292. vec2 pt = gl_PointCoord - vec2(0.5); \
  293. vec4 texColor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
  294. if (i == 0) { \
  295. texMixed = texColor; \
  296. } else { \
  297. texMixed = mix(texMixed, texColor, texColor.a); \
  298. } \
  299. }
  300. // Inputs from vertex shader
  301. in vec3 Color;
  302. flat in mat2 Rotation;
  303. // Output
  304. out vec4 FragColor;
  305. void main() {
  306. // Mix material color with textures colors
  307. vec4 texMixed = vec4(1);
  308. #if MAT_TEXTURES==1
  309. MIX_POINT_TEXTURE(0)
  310. #elif MAT_TEXTURES==2
  311. MIX_POINT_TEXTURE(0)
  312. MIX_POINT_TEXTURE(1)
  313. #elif MAT_TEXTURES==3
  314. MIX_POINT_TEXTURE(0)
  315. MIX_POINT_TEXTURE(1)
  316. MIX_POINT_TEXTURE(2)
  317. #endif
  318. // Generates final color
  319. FragColor = min(vec4(Color, MatOpacity) * texMixed, vec4(1));
  320. }
  321. `
  322. const phong_vertex_source = `//
  323. // Vertex Shader
  324. //
  325. #include <attributes>
  326. // Model uniforms
  327. uniform mat4 ModelViewMatrix;
  328. uniform mat3 NormalMatrix;
  329. uniform mat4 MVP;
  330. #include <material>
  331. // Output variables for Fragment shader
  332. out vec4 Position;
  333. out vec3 Normal;
  334. out vec3 CamDir;
  335. out vec2 FragTexcoord;
  336. void main() {
  337. // Transform this vertex position to camera coordinates.
  338. Position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  339. // Transform this vertex normal to camera coordinates.
  340. Normal = normalize(NormalMatrix * VertexNormal);
  341. // Calculate the direction vector from the vertex to the camera
  342. // The camera is at 0,0,0
  343. CamDir = normalize(-Position.xyz);
  344. // Flips texture coordinate Y if requested.
  345. vec2 texcoord = VertexTexcoord;
  346. #if MAT_TEXTURES>0
  347. if (MatTexFlipY(0)) {
  348. texcoord.y = 1 - texcoord.y;
  349. }
  350. #endif
  351. FragTexcoord = texcoord;
  352. gl_Position = MVP * vec4(VertexPosition, 1.0);
  353. }
  354. `
  355. const panel_fragment_source = `//
  356. // Fragment Shader template
  357. //
  358. // Texture uniforms
  359. uniform sampler2D MatTexture;
  360. uniform vec2 MatTexinfo[3];
  361. // Macros to access elements inside the MatTexinfo array
  362. #define MatTexOffset MatTexinfo[0]
  363. #define MatTexRepeat MatTexinfo[1]
  364. #define MatTexFlipY bool(MatTexinfo[2].x) // not used
  365. #define MatTexVisible bool(MatTexinfo[2].y) // not used
  366. // Inputs from vertex shader
  367. in vec2 FragTexcoord;
  368. // Input uniform
  369. uniform vec4 Panel[8];
  370. #define Bounds Panel[0] // panel bounds in texture coordinates
  371. #define Border Panel[1] // panel border in texture coordinates
  372. #define Padding Panel[2] // panel padding in texture coordinates
  373. #define Content Panel[3] // panel content area in texture coordinates
  374. #define BorderColor Panel[4] // panel border color
  375. #define PaddingColor Panel[5] // panel padding color
  376. #define ContentColor Panel[6] // panel content color
  377. #define TextureValid bool(Panel[7].x) // texture valid flag
  378. // Output
  379. out vec4 FragColor;
  380. /***
  381. * Checks if current fragment texture coordinate is inside the
  382. * supplied rectangle in texture coordinates:
  383. * rect[0] - position x [0,1]
  384. * rect[1] - position y [0,1]
  385. * rect[2] - width [0,1]
  386. * rect[3] - height [0,1]
  387. */
  388. bool checkRect(vec4 rect) {
  389. if (FragTexcoord.x < rect[0]) {
  390. return false;
  391. }
  392. if (FragTexcoord.x > rect[0] + rect[2]) {
  393. return false;
  394. }
  395. if (FragTexcoord.y < rect[1]) {
  396. return false;
  397. }
  398. if (FragTexcoord.y > rect[1] + rect[3]) {
  399. return false;
  400. }
  401. return true;
  402. }
  403. void main() {
  404. // Discard fragment outside of received bounds
  405. // Bounds[0] - xmin
  406. // Bounds[1] - ymin
  407. // Bounds[2] - xmax
  408. // Bounds[3] - ymax
  409. if (FragTexcoord.x <= Bounds[0] || FragTexcoord.x >= Bounds[2]) {
  410. discard;
  411. }
  412. if (FragTexcoord.y <= Bounds[1] || FragTexcoord.y >= Bounds[3]) {
  413. discard;
  414. }
  415. // Check if fragment is inside content area
  416. if (checkRect(Content)) {
  417. // If no texture, the color will be the material color.
  418. vec4 color = ContentColor;
  419. if (TextureValid) {
  420. // Adjust texture coordinates to fit texture inside the content area
  421. vec2 offset = vec2(-Content[0], -Content[1]);
  422. vec2 factor = vec2(1/Content[2], 1/Content[3]);
  423. vec2 texcoord = (FragTexcoord + offset) * factor;
  424. vec4 texColor = texture(MatTexture, texcoord * MatTexRepeat + MatTexOffset);
  425. // Mix content color with texture color ???
  426. //color = mix(color, texColor, texColor.a);
  427. color = texColor;
  428. }
  429. FragColor = color;
  430. return;
  431. }
  432. // Checks if fragment is inside paddings area
  433. if (checkRect(Padding)) {
  434. FragColor = PaddingColor;
  435. return;
  436. }
  437. // Checks if fragment is inside borders area
  438. if (checkRect(Border)) {
  439. FragColor = BorderColor;
  440. return;
  441. }
  442. // Fragment is in margins area (always transparent)
  443. FragColor = vec4(1,1,1,0);
  444. }
  445. `
  446. const phong_fragment_source = `//
  447. // Fragment Shader template
  448. //
  449. // Inputs from vertex shader
  450. in vec4 Position; // Vertex position in camera coordinates.
  451. in vec3 Normal; // Vertex normal in camera coordinates.
  452. in vec3 CamDir; // Direction from vertex to camera
  453. in vec2 FragTexcoord;
  454. #include <lights>
  455. #include <material>
  456. #include <phong_model>
  457. // Final fragment color
  458. out vec4 FragColor;
  459. void main() {
  460. // Mix material color with textures colors
  461. vec4 texMixed = vec4(1);
  462. vec4 texColor;
  463. #if MAT_TEXTURES==1
  464. MIX_TEXTURE(0)
  465. #elif MAT_TEXTURES==2
  466. MIX_TEXTURE(0)
  467. MIX_TEXTURE(1)
  468. #elif MAT_TEXTURES==3
  469. MIX_TEXTURE(0)
  470. MIX_TEXTURE(1)
  471. MIX_TEXTURE(2)
  472. #endif
  473. // Combine material with texture colors
  474. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  475. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  476. // Inverts the fragment normal if not FrontFacing
  477. vec3 fragNormal = Normal;
  478. if (!gl_FrontFacing) {
  479. fragNormal = -fragNormal;
  480. }
  481. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  482. vec3 Ambdiff, Spec;
  483. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  484. // Final fragment color
  485. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  486. }
  487. `
  488. const sprite_vertex_source = `//
  489. // Vertex shader for sprites
  490. //
  491. #include <attributes>
  492. // Input uniforms
  493. uniform mat4 MVP;
  494. #include <material>
  495. // Outputs for fragment shader
  496. out vec3 Color;
  497. out vec2 FragTexcoord;
  498. void main() {
  499. // Applies transformation to vertex position
  500. gl_Position = MVP * vec4(VertexPosition, 1.0);
  501. // Outputs color
  502. Color = MatDiffuseColor;
  503. // Flips texture coordinate Y if requested.
  504. vec2 texcoord = VertexTexcoord;
  505. #if MAT_TEXTURES>0
  506. if (MatTexFlipY[0]) {
  507. texcoord.y = 1 - texcoord.y;
  508. }
  509. #endif
  510. FragTexcoord = texcoord;
  511. }
  512. `
  513. const standard_vertex_source = `//
  514. // Vertex shader standard
  515. //
  516. #include <attributes>
  517. // Model uniforms
  518. uniform mat4 ModelViewMatrix;
  519. uniform mat3 NormalMatrix;
  520. uniform mat4 MVP;
  521. #include <lights>
  522. #include <material>
  523. #include <phong_model>
  524. // Outputs for the fragment shader.
  525. out vec3 ColorFrontAmbdiff;
  526. out vec3 ColorFrontSpec;
  527. out vec3 ColorBackAmbdiff;
  528. out vec3 ColorBackSpec;
  529. out vec2 FragTexcoord;
  530. void main() {
  531. // Transform this vertex normal to camera coordinates.
  532. vec3 normal = normalize(NormalMatrix * VertexNormal);
  533. // Calculate this vertex position in camera coordinates
  534. vec4 position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  535. // Calculate the direction vector from the vertex to the camera
  536. // The camera is at 0,0,0
  537. vec3 camDir = normalize(-position.xyz);
  538. // Calculates the vertex Ambient+Diffuse and Specular colors using the Phong model
  539. // for the front and back
  540. phongModel(position, normal, camDir, MatAmbientColor, MatDiffuseColor, ColorFrontAmbdiff, ColorFrontSpec);
  541. phongModel(position, -normal, camDir, MatAmbientColor, MatDiffuseColor, ColorBackAmbdiff, ColorBackSpec);
  542. vec2 texcoord = VertexTexcoord;
  543. #if MAT_TEXTURES > 0
  544. // Flips texture coordinate Y if requested.
  545. if (MatTexFlipY(0)) {
  546. texcoord.y = 1 - texcoord.y;
  547. }
  548. #endif
  549. FragTexcoord = texcoord;
  550. gl_Position = MVP * vec4(VertexPosition, 1.0);
  551. }
  552. `
  553. const point_vertex_source = `#include <attributes>
  554. // Model uniforms
  555. uniform mat4 MVP;
  556. // Material uniforms
  557. #include <material>
  558. // Outputs for fragment shader
  559. out vec3 Color;
  560. flat out mat2 Rotation;
  561. void main() {
  562. // Rotation matrix for fragment shader
  563. float rotSin = sin(MatPointRotationZ);
  564. float rotCos = cos(MatPointRotationZ);
  565. Rotation = mat2(rotCos, rotSin, - rotSin, rotCos);
  566. // Sets the vertex position
  567. vec4 pos = MVP * vec4(VertexPosition, 1.0);
  568. gl_Position = pos;
  569. // Sets the size of the rasterized point decreasing with distance
  570. gl_PointSize = (1.0 - pos.z / pos.w) * MatPointSize;
  571. // Outputs color
  572. Color = MatEmissiveColor;
  573. }
  574. `
  575. const basic_vertex_source = `//
  576. // Vertex shader basic
  577. //
  578. #include <attributes>
  579. // Model uniforms
  580. uniform mat4 MVP;
  581. // Final output color for fragment shader
  582. out vec3 Color;
  583. void main() {
  584. Color = VertexColor;
  585. gl_Position = MVP * vec4(VertexPosition, 1.0);
  586. }
  587. `
  588. // Maps include name with its source code
  589. var includeMap = map[string]string{
  590. "phong_model": include_phong_model_source,
  591. "material": include_material_source,
  592. "lights": include_lights_source,
  593. "attributes": include_attributes_source,
  594. }
  595. // Maps shader name with its source code
  596. var shaderMap = map[string]string{
  597. "basic_fragment": basic_fragment_source,
  598. "standard_fragment": standard_fragment_source,
  599. "panel_vertex": panel_vertex_source,
  600. "sprite_fragment": sprite_fragment_source,
  601. "point_fragment": point_fragment_source,
  602. "phong_vertex": phong_vertex_source,
  603. "panel_fragment": panel_fragment_source,
  604. "phong_fragment": phong_fragment_source,
  605. "sprite_vertex": sprite_vertex_source,
  606. "standard_vertex": standard_vertex_source,
  607. "point_vertex": point_vertex_source,
  608. "basic_vertex": basic_vertex_source,
  609. }
  610. // Maps program name with Proginfo struct with shaders names
  611. var programMap = map[string]ProgramInfo{
  612. "basic": {"basic_vertex", "basic_fragment", ""},
  613. "panel": {"panel_vertex", "panel_fragment", ""},
  614. "phong": {"phong_vertex", "phong_fragment", ""},
  615. "point": {"point_vertex", "point_fragment", ""},
  616. "sprite": {"sprite_vertex", "sprite_fragment", ""},
  617. "standard": {"standard_vertex", "standard_fragment", ""},
  618. }