sources.go 22 KB

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