Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

219 строки
6.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 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 "core.h"
  14. #include "lolgl.h"
  15. using namespace std;
  16. namespace lol
  17. {
  18. /*
  19. * Gradient implementation class
  20. */
  21. class GradientData
  22. {
  23. friend class Gradient;
  24. private:
  25. Shader *shader;
  26. VertexDeclaration *m_vdecl;
  27. VertexBuffer *m_vbo, *m_cbo;
  28. };
  29. /*
  30. * Public Gradient class
  31. */
  32. Gradient::Gradient(vec3 aa, vec3 bb)
  33. : data(new GradientData())
  34. {
  35. /* FIXME: this should not be hardcoded */
  36. m_position = aa;
  37. m_bbox[0] = aa;
  38. m_bbox[1] = bb;
  39. data->shader = NULL;
  40. }
  41. void Gradient::TickGame(float seconds)
  42. {
  43. Entity::TickGame(seconds);
  44. }
  45. void Gradient::TickDraw(float seconds)
  46. {
  47. Entity::TickDraw(seconds);
  48. float const vertex[] = { 0.0f, 0.0f, 0.0f,
  49. 640.0f, 0.0f, 0.0f,
  50. 0.0f, 480.0f, 0.0f,
  51. 640.0f, 480.0f, 0.0f,
  52. 0.0f, 480.0f, 0.0f,
  53. 640.0f, 0.0f, 0.0f, };
  54. float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
  55. 0.73f, 0.85f, 0.85f, 1.0f,
  56. 0.0f, 0.0f, 1.0f, 1.0f,
  57. 0.0f, 0.0f, 1.0f, 1.0f,
  58. 0.0f, 0.0f, 1.0f, 1.0f,
  59. 0.73f, 0.85f, 0.85f, 1.0f, };
  60. if (!data->shader)
  61. {
  62. #if !defined __CELLOS_LV2__ && !defined USE_D3D9 && !defined _XBOX
  63. data->shader = Shader::Create(
  64. "#version 130\n"
  65. "\n"
  66. "in vec3 in_Vertex;\n"
  67. "in vec4 in_Color;\n"
  68. "out vec4 pass_Color;\n"
  69. "\n"
  70. "uniform mat4 proj_matrix;\n"
  71. "uniform mat4 view_matrix;\n"
  72. "uniform mat4 model_matrix;\n"
  73. "\n"
  74. "void main()\n"
  75. "{\n"
  76. " gl_Position = proj_matrix * view_matrix * model_matrix"
  77. " * vec4(in_Vertex, 1.0);\n"
  78. " pass_Color = in_Color;\n"
  79. "}\n",
  80. "#version 130\n"
  81. "\n"
  82. "in vec4 pass_Color;\n"
  83. "\n"
  84. "mat4 bayer = mat4( 0.0, 12.0, 3.0, 15.0,"
  85. " 8.0, 4.0, 11.0, 7.0,"
  86. " 2.0, 14.0, 1.0, 13.0,"
  87. " 10.0, 6.0, 9.0, 5.0);\n"
  88. ""
  89. "mat4 cluster = mat4(12.0, 5.0, 6.0, 13.0,"
  90. " 4.0, 0.0, 1.0, 7.0,"
  91. " 11.0, 3.0, 2.0, 8.0,"
  92. " 15.0, 10.0, 9.0, 14.0);\n"
  93. "\n"
  94. "void main()\n"
  95. "{\n"
  96. " vec4 col = pass_Color;\n"
  97. " float t = cluster[int(mod(gl_FragCoord.x, 4.0))]"
  98. " [int(mod(gl_FragCoord.y, 4.0))];\n"
  99. " t = (t + 0.5) / 17.0;\n"
  100. " col.x += fract(t - col.x) - t;\n"
  101. " col.y += fract(t - col.y) - t;\n"
  102. " col.z += fract(t - col.z) - t;\n"
  103. " gl_FragColor = col;\n"
  104. "}\n");
  105. #else
  106. data->shader = Shader::Create(
  107. "void main(float4 in_Vertex : POSITION,"
  108. " float4 in_Color : COLOR,"
  109. " uniform float4x4 proj_matrix,"
  110. " uniform float4x4 view_matrix,"
  111. " uniform float4x4 model_matrix,"
  112. " out float4 out_Color : COLOR,"
  113. " out float4 out_Position : POSITION)"
  114. "{"
  115. " out_Position = mul(proj_matrix, mul(view_matrix, mul(model_matrix, in_Vertex)));"
  116. " out_Color = in_Color;"
  117. "}",
  118. "float4x4 bayer = float4x4( 0.0, 12.0, 3.0, 15.0,"
  119. " 8.0, 4.0, 11.0, 7.0,"
  120. " 2.0, 14.0, 1.0, 13.0,"
  121. " 10.0, 6.0, 9.0, 5.0);\n"
  122. ""
  123. #if 1
  124. "float4x4 cluster = float4x4(12.0, 5.0, 6.0, 13.0,"
  125. " 4.0, 0.0, 1.0, 7.0,"
  126. " 11.0, 3.0, 2.0, 8.0,"
  127. " 15.0, 10.0, 9.0, 14.0);\n"
  128. #endif
  129. "\n"
  130. "void main(float4 in_Color : COLOR,"
  131. " float4 in_FragCoord : WPOS,"
  132. " out float4 out_FragColor : COLOR)"
  133. "{"
  134. " float4 col = in_Color;"
  135. #if 1
  136. " int x = (int)in_FragCoord.x;"
  137. " int y = (int)in_FragCoord.y;"
  138. #if defined USE_D3D9 || defined _XBOX
  139. " float t = bayer[int(frac(x * 0.25) * 4.0)]"
  140. " [int(frac(y * 0.25) * 4.0)];\n"
  141. #else
  142. // FIXME: we cannot address this matrix directly on the PS3
  143. " float t = bayer[0][0];\n"
  144. #endif
  145. " t = (t + 0.5) / 17.0;\n"
  146. " col.x += frac(t - col.x) - t;\n"
  147. " col.y += frac(t - col.y) - t;\n"
  148. " col.z += frac(t - col.z) - t;\n"
  149. #endif
  150. " out_FragColor = col;"
  151. "}");
  152. #endif
  153. data->m_vbo = new VertexBuffer(sizeof(vertex));
  154. data->m_cbo = new VertexBuffer(sizeof(color));
  155. data->m_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
  156. VertexStream<vec4>(VertexUsage::Color));
  157. }
  158. mat4 model_matrix = mat4(1.0f);
  159. ShaderUniform uni_mat;
  160. ShaderAttrib attr_pos, attr_col;
  161. attr_pos = data->shader->GetAttribLocation("in_Vertex", VertexUsage::Position, 0);
  162. attr_col = data->shader->GetAttribLocation("in_Color", VertexUsage::Color, 0);
  163. data->shader->Bind();
  164. uni_mat = data->shader->GetUniformLocation("proj_matrix");
  165. data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetProjMatrix());
  166. uni_mat = data->shader->GetUniformLocation("view_matrix");
  167. data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetViewMatrix());
  168. uni_mat = data->shader->GetUniformLocation("model_matrix");
  169. data->shader->SetUniform(uni_mat, model_matrix);
  170. data->shader->Bind();
  171. data->m_vdecl->Bind();
  172. void *tmp = data->m_vbo->Lock(0, 0);
  173. memcpy(tmp, vertex, sizeof(vertex));
  174. data->m_vbo->Unlock();
  175. tmp = data->m_cbo->Lock(0, 0);
  176. memcpy(tmp, color, sizeof(color));
  177. data->m_cbo->Unlock();
  178. /* Bind vertex and color buffers */
  179. data->m_vdecl->SetStream(data->m_vbo, attr_pos);
  180. data->m_vdecl->SetStream(data->m_cbo, attr_col);
  181. /* Draw arrays */
  182. data->m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
  183. }
  184. Gradient::~Gradient()
  185. {
  186. /* FIXME: destroy shader */
  187. delete data;
  188. }
  189. } /* namespace lol */