選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

244 行
7.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cmath>
  14. #include "core.h"
  15. #include "lolgl.h"
  16. using namespace std;
  17. namespace lol
  18. {
  19. /*
  20. * Gradient implementation class
  21. */
  22. class GradientData
  23. {
  24. friend class Gradient;
  25. private:
  26. Shader *shader;
  27. GLuint bufs[2];
  28. #if defined HAVE_GL_2X
  29. GLuint vaos[1];
  30. #endif
  31. };
  32. /*
  33. * Public Gradient class
  34. */
  35. Gradient::Gradient(vec3 aa, vec3 bb)
  36. : data(new GradientData())
  37. {
  38. /* FIXME: this should not be hardcoded */
  39. position = aa;
  40. bbox[0] = aa;
  41. bbox[1] = bb;
  42. data->shader = NULL;
  43. }
  44. void Gradient::TickGame(float deltams)
  45. {
  46. Entity::TickGame(deltams);
  47. }
  48. void Gradient::TickDraw(float deltams)
  49. {
  50. Entity::TickDraw(deltams);
  51. if (!data->shader)
  52. {
  53. #if !defined __CELLOS_LV2__
  54. data->shader = Shader::Create(
  55. "#version 130\n"
  56. "\n"
  57. "in vec3 in_Vertex;\n"
  58. "in vec4 in_Color;\n"
  59. "out vec4 pass_Color;\n"
  60. "\n"
  61. "uniform mat4 proj_matrix;\n"
  62. "uniform mat4 view_matrix;\n"
  63. "uniform mat4 model_matrix;\n"
  64. "\n"
  65. "void main()\n"
  66. "{\n"
  67. " gl_Position = proj_matrix * view_matrix * model_matrix"
  68. " * vec4(in_Vertex, 1.0);\n"
  69. " pass_Color = in_Color;\n"
  70. "}\n",
  71. "#version 130\n"
  72. "\n"
  73. "in vec4 pass_Color;\n"
  74. "\n"
  75. "mat4 bayer = mat4( 0.0, 12.0, 3.0, 15.0,"
  76. " 8.0, 4.0, 11.0, 7.0,"
  77. " 2.0, 14.0, 1.0, 13.0,"
  78. " 10.0, 6.0, 9.0, 5.0);\n"
  79. ""
  80. "mat4 cluster = mat4(12.0, 5.0, 6.0, 13.0,"
  81. " 4.0, 0.0, 1.0, 7.0,"
  82. " 11.0, 3.0, 2.0, 8.0,"
  83. " 15.0, 10.0, 9.0, 14.0);\n"
  84. "\n"
  85. "void main()\n"
  86. "{\n"
  87. " vec4 col = pass_Color;\n"
  88. " float t = cluster[int(mod(gl_FragCoord.x, 4.0))]"
  89. " [int(mod(gl_FragCoord.y, 4.0))];\n"
  90. " t = (t + 0.5) / 17.0;\n"
  91. " col.x += fract(t - col.x) - t;\n"
  92. " col.y += fract(t - col.y) - t;\n"
  93. " col.z += fract(t - col.z) - t;\n"
  94. " gl_FragColor = col;\n"
  95. "}\n");
  96. #else
  97. data->shader = Shader::Create(
  98. "void main(float4 in_Vertex : POSITION,"
  99. " float4 in_Color : COLOR,"
  100. " uniform float4x4 proj_matrix,"
  101. " uniform float4x4 view_matrix,"
  102. " uniform float4x4 model_matrix,"
  103. " out float4 out_Color : COLOR,"
  104. " out float4 out_Position : POSITION)"
  105. "{"
  106. " out_Position = mul(proj_matrix, mul(view_matrix, mul(model_matrix, in_Vertex)));"
  107. " out_Color = in_Color;"
  108. "}",
  109. "float4x4 bayer = float4x4( 0.0, 12.0, 3.0, 15.0,"
  110. " 8.0, 4.0, 11.0, 7.0,"
  111. " 2.0, 14.0, 1.0, 13.0,"
  112. " 10.0, 6.0, 9.0, 5.0);\n"
  113. ""
  114. #if 1
  115. "float4x4 cluster = float4x4(12.0, 5.0, 6.0, 13.0,"
  116. " 4.0, 0.0, 1.0, 7.0,"
  117. " 11.0, 3.0, 2.0, 8.0,"
  118. " 15.0, 10.0, 9.0, 14.0);\n"
  119. #endif
  120. "\n"
  121. "void main(float4 in_Color : COLOR,"
  122. " float4 in_FragCoord : WPOS,"
  123. " out float4 out_FragColor : COLOR)"
  124. "{"
  125. " float4 col = in_Color;"
  126. #if 1
  127. " int x = (int)in_FragCoord.x;"
  128. " int y = (int)in_FragCoord.y;"
  129. // FIXME: we cannot address this matrix directly on the PS3
  130. " float t = bayer[0][0];\n"
  131. " t = (t + 0.5) / 17.0 + z - x;\n"
  132. " col.x += frac(t - col.x) - t;\n"
  133. " col.y += frac(t - col.y) - t;\n"
  134. " col.z += frac(t - col.z) - t;\n"
  135. #endif
  136. " out_FragColor = col;"
  137. "}");
  138. #endif
  139. #if !defined __CELLOS_LV2__
  140. glGenBuffers(2, data->bufs);
  141. # if defined HAVE_GL_2X
  142. glGenVertexArrays(1, data->vaos);
  143. # endif
  144. #endif
  145. }
  146. mat4 model_matrix = mat4(1.0f);
  147. GLuint uni_mat, attr_pos, attr_col;
  148. #if !defined __CELLOS_LV2__
  149. attr_pos = data->shader->GetAttribLocation("in_Vertex");
  150. attr_col = data->shader->GetAttribLocation("in_Color");
  151. #endif
  152. data->shader->Bind();
  153. uni_mat = data->shader->GetUniformLocation("proj_matrix");
  154. data->shader->SetUniform(uni_mat, Video::GetProjMatrix());
  155. uni_mat = data->shader->GetUniformLocation("view_matrix");
  156. data->shader->SetUniform(uni_mat, Video::GetViewMatrix());
  157. uni_mat = data->shader->GetUniformLocation("model_matrix");
  158. data->shader->SetUniform(uni_mat, model_matrix);
  159. float const vertex[] = { 0.0f, 0.0f, 0.0f,
  160. 640.0f, 0.0f, 0.0f,
  161. 0.0f, 480.0f, 0.0f,
  162. 0.0f, 480.0f, 0.0f,
  163. 640.0f, 480.0f, 0.0f,
  164. 640.0f, 0.0f, 0.0f, };
  165. float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
  166. 0.73f, 0.85f, 0.85f, 1.0f,
  167. 0.0f, 0.0f, 1.0f, 1.0f,
  168. 0.0f, 0.0f, 1.0f, 1.0f,
  169. 0.0f, 0.0f, 1.0f, 1.0f,
  170. 0.73f, 0.85f, 0.85f, 1.0f, };
  171. data->shader->Bind();
  172. /* Bind vertex, color and texture coordinate buffers */
  173. #if !defined __CELLOS_LV2__
  174. # if defined HAVE_GL_2X
  175. glBindVertexArray(data->vaos[0]);
  176. # endif
  177. glEnableVertexAttribArray(attr_pos);
  178. glEnableVertexAttribArray(attr_col);
  179. glBindBuffer(GL_ARRAY_BUFFER, data->bufs[0]);
  180. glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat),
  181. vertex, GL_DYNAMIC_DRAW);
  182. glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
  183. glBindBuffer(GL_ARRAY_BUFFER, data->bufs[1]);
  184. glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat),
  185. color, GL_DYNAMIC_DRAW);
  186. glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
  187. #else
  188. glEnableClientState(GL_VERTEX_ARRAY);
  189. glEnableClientState(GL_COLOR_ARRAY);
  190. glVertexPointer(3, GL_FLOAT, 0, vertex);
  191. glColorPointer(4, GL_FLOAT, 0, color);
  192. #endif
  193. /* Draw arrays */
  194. glDrawArrays(GL_TRIANGLES, 0, 6);
  195. #if !defined __CELLOS_LV2__
  196. # if defined HAVE_GL_2X
  197. glBindVertexArray(0);
  198. # endif
  199. glDisableVertexAttribArray(attr_pos);
  200. glDisableVertexAttribArray(attr_col);
  201. #else
  202. glDisableClientState(GL_VERTEX_ARRAY);
  203. glDisableClientState(GL_COLOR_ARRAY);
  204. #endif
  205. }
  206. Gradient::~Gradient()
  207. {
  208. /* FIXME: destroy shader */
  209. delete data;
  210. }
  211. } /* namespace lol */