Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

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