tilemap.frag 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2008 Joel Bouchard Lamontagne <logicow@gmail.com>
  4. * Copyright (C) 2009 Alistair John Strachan <alistair@devzero.co.uk>
  5. * Copyright (C) 2017 Dr Lancer-X <drlancer@megazeux.org>
  6. * Copyright (C) 2018 Alice Rowan <petrifiedrowan@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #version 110
  23. // Keep these the same as in render_glsl.c
  24. #define CHARSET_COLS 64.0
  25. #define TEX_DATA_WIDTH 512.0
  26. #define TEX_DATA_HEIGHT 1024.0
  27. #define TEX_DATA_PAL_Y 896.0
  28. #define TEX_DATA_LAYER_X 0.0
  29. #define TEX_DATA_LAYER_Y 901.0
  30. // This has to be slightly less than 14.0 to avoid propagating error
  31. // with some very old driver/video card combos.
  32. #define CHAR_H 13.99999
  33. uniform sampler2D baseMap;
  34. varying vec2 vTexcoord;
  35. float fract_(float v)
  36. {
  37. return clamp(fract(v + 0.001) - 0.001, 0.000, 0.999);
  38. }
  39. float floor_(float v)
  40. {
  41. return floor(v + 0.001);
  42. }
  43. int int_(float v)
  44. {
  45. return int(v + 0.01);
  46. }
  47. // NOTE: Layer data packing scheme
  48. // (highest two bits currently unused but included as part of the char)
  49. // w z y x
  50. // 00000000 00000000 00000000 00000000
  51. // CCCCCCCC CCCCCCBB BBBBBBBF FFFFFFFF
  52. // Some older cards/drivers tend o be slightly off; slight variations
  53. // in values here are intentional.
  54. /**
  55. * Get the char number from packed layer data as (approx.) an int.
  56. */
  57. float layer_get_char(vec4 layer_data)
  58. {
  59. return floor_(layer_data.z * 63.75) + (layer_data.w * 255.0) * 64.0;
  60. }
  61. /**
  62. * Get the foreground color from layer data relative to the texture width.
  63. */
  64. float layer_get_fg_color(vec4 layer_data)
  65. {
  66. return
  67. (layer_data.x * 255.001) / TEX_DATA_WIDTH +
  68. fract_(layer_data.y * 127.501) * 512.0 / TEX_DATA_WIDTH;
  69. }
  70. /**
  71. * Get the background color from layer data relative to the texture width.
  72. */
  73. float layer_get_bg_color(vec4 layer_data)
  74. {
  75. return
  76. floor_(layer_data.y * 127.5) / TEX_DATA_WIDTH +
  77. fract_(layer_data.z * 63.751) * 512.0 / TEX_DATA_WIDTH;
  78. }
  79. void main(void)
  80. {
  81. /**
  82. * Get the packed char/color data for this position from the current layer.
  83. * vTexcoord will be provided in the range of x=[0..layer.w), y=[0..layer.h).
  84. */
  85. float layer_x = (vTexcoord.x + TEX_DATA_LAYER_X) / TEX_DATA_WIDTH;
  86. float layer_y = (vTexcoord.y + TEX_DATA_LAYER_Y) / TEX_DATA_HEIGHT;
  87. vec4 layer_data = texture2D(baseMap, vec2(layer_x, layer_y));
  88. /**
  89. * Get the current char and its base position in the texture charset.
  90. * The x position will be normalized to the width of the texture,
  91. * but for the y position it's easier to get the pixel position and
  92. * normalize afterward.
  93. */
  94. float char_num = layer_get_char(layer_data);
  95. float char_x = fract_(char_num / CHARSET_COLS);
  96. float char_y = floor_(char_num / CHARSET_COLS);
  97. /**
  98. * Get the current pixel value of the current char from the texture.
  99. */
  100. float char_tex_x = char_x + fract_(vTexcoord.x) / CHARSET_COLS;
  101. float char_tex_y = (char_y + fract_(vTexcoord.y)) * CHAR_H / TEX_DATA_HEIGHT;
  102. vec4 char_pix = texture2D(baseMap, vec2(char_tex_x, char_tex_y));
  103. /**
  104. * Determine whether this is the foreground or background color of the char,
  105. * get that color from the texture, and output it.
  106. */
  107. float color;
  108. // We could actually check any component here.
  109. if(char_pix.x > 0.5)
  110. {
  111. color = layer_get_fg_color(layer_data);
  112. }
  113. else
  114. {
  115. color = layer_get_bg_color(layer_data);
  116. }
  117. gl_FragColor = texture2D(baseMap,
  118. vec2(color, TEX_DATA_PAL_Y / TEX_DATA_HEIGHT));
  119. }