You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

428 lines
13 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 <cstdlib>
  14. #include <cmath>
  15. #ifdef WIN32
  16. # define WIN32_LEAN_AND_MEAN
  17. # include <windows.h>
  18. #endif
  19. #include "core.h"
  20. #include "lolgl.h"
  21. namespace lol
  22. {
  23. struct Tile
  24. {
  25. TileSet *tileset;
  26. uint32_t prio;
  27. vec3 pos;
  28. int id, o;
  29. };
  30. static Shader *stdshader = NULL;
  31. /*
  32. * Scene implementation class
  33. */
  34. class SceneData
  35. {
  36. friend class Scene;
  37. private:
  38. static int Compare(void const *p1, void const *p2)
  39. {
  40. Tile const *t1 = (Tile const *)p1;
  41. Tile const *t2 = (Tile const *)p2;
  42. return t2->prio - t1->prio;
  43. }
  44. mat4 model_matrix;
  45. Tile *tiles;
  46. int ntiles;
  47. float angle;
  48. #if defined HAVE_GL_2X && !defined __APPLE__
  49. GLuint vao;
  50. #endif
  51. GLuint *bufs;
  52. int nbufs;
  53. static Scene *scene;
  54. };
  55. Scene *SceneData::scene = NULL;
  56. /*
  57. * Public Scene class
  58. */
  59. Scene::Scene(float angle)
  60. : data(new SceneData())
  61. {
  62. data->tiles = 0;
  63. data->ntiles = 0;
  64. data->angle = angle;
  65. data->bufs = 0;
  66. data->nbufs = 0;
  67. #if defined HAVE_GL_2X && !defined __APPLE__
  68. glGenVertexArrays(1, &data->vao);
  69. #endif
  70. }
  71. Scene::~Scene()
  72. {
  73. /* FIXME: this must be done while the GL context is still active.
  74. * Change the code architecture to make sure of that. */
  75. /* XXX: The test is necessary because of a crash with PSGL. */
  76. if (data->nbufs > 0)
  77. glDeleteBuffers(data->nbufs, data->bufs);
  78. #if defined HAVE_GL_2X && !defined __APPLE__
  79. glDeleteVertexArrays(1, &data->vao);
  80. #endif
  81. free(data->bufs);
  82. delete data;
  83. }
  84. Scene *Scene::GetDefault()
  85. {
  86. if (!SceneData::scene)
  87. SceneData::scene = new Scene(0.0f);
  88. return SceneData::scene;
  89. }
  90. void Scene::Reset()
  91. {
  92. if (SceneData::scene)
  93. delete SceneData::scene;
  94. SceneData::scene = NULL;
  95. }
  96. void Scene::AddTile(TileSet *tileset, int id, vec3 pos, int o)
  97. {
  98. if ((data->ntiles % 1024) == 0)
  99. data->tiles = (Tile *)realloc(data->tiles,
  100. (data->ntiles + 1024) * sizeof(Tile));
  101. /* FIXME: this sorting only works for a 45-degree camera */
  102. data->tiles[data->ntiles].prio = -pos.y - 2 * 32 * pos.z + (o ? 0 : 32);
  103. data->tiles[data->ntiles].tileset = tileset;
  104. data->tiles[data->ntiles].id = id;
  105. data->tiles[data->ntiles].pos = pos;
  106. data->tiles[data->ntiles].o = o;
  107. data->ntiles++;
  108. }
  109. void Scene::Render() // XXX: rename to Blit()
  110. {
  111. if (!stdshader)
  112. {
  113. #if !defined __CELLOS_LV2__
  114. stdshader = Shader::Create(
  115. #if !defined HAVE_GLES_2X
  116. "#version 130\n"
  117. #endif
  118. "\n"
  119. #if defined HAVE_GLES_2X
  120. "attribute vec3 in_Position;\n"
  121. "attribute vec2 in_TexCoord;\n"
  122. "varying vec2 pass_TexCoord;\n"
  123. #else
  124. "in vec3 in_Position;\n"
  125. "in vec2 in_TexCoord;\n"
  126. #endif
  127. "uniform mat4 proj_matrix;\n"
  128. "uniform mat4 view_matrix;\n"
  129. "uniform mat4 model_matrix;\n"
  130. "\n"
  131. "void main()\n"
  132. "{\n"
  133. " gl_Position = proj_matrix * view_matrix * model_matrix"
  134. " * vec4(in_Position, 1.0);\n"
  135. #if defined HAVE_GLES_2X
  136. " pass_TexCoord = in_TexCoord;\n"
  137. #else
  138. " gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
  139. #endif
  140. "}\n",
  141. #if !defined HAVE_GLES_2X
  142. "#version 130\n"
  143. #else
  144. "precision mediump float;\n"
  145. #endif
  146. "\n"
  147. "uniform sampler2D in_Texture;\n"
  148. #if defined HAVE_GLES_2X
  149. "varying vec2 pass_TexCoord;\n"
  150. #endif
  151. "\n"
  152. "void main()\n"
  153. "{\n"
  154. #if defined HAVE_GLES_2X
  155. " vec4 col = texture2D(in_Texture, pass_TexCoord);\n"
  156. //" vec4 col = vec4(0.5, 1.0, 0.0, 0.5);\n"
  157. //" vec4 col = vec4(pass_TexCoord * 4.0, 0.0, 0.25);\n"
  158. #else
  159. " vec4 col = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
  160. #endif
  161. #if 0
  162. " float mul = 2.0;\n"
  163. #if 1
  164. " vec2 d1 = mod(vec2(gl_FragCoord), vec2(2.0, 2.0));\n"
  165. " float t1 = mod(3.0 * d1.x + 2.0 * d1.y, 4.0);\n"
  166. " float dx2 = mod(floor(gl_FragCoord.x * 0.5), 2.0);\n"
  167. " float dy2 = mod(floor(gl_FragCoord.y * 0.5), 2.0);\n"
  168. " float t2 = mod(3.0 * dx2 + 2.0 * dy2, 4.0);\n"
  169. " float dx3 = mod(floor(gl_FragCoord.x * 0.25), 2.0);\n"
  170. " float dy3 = mod(floor(gl_FragCoord.y * 0.25), 2.0);\n"
  171. " float t3 = mod(3.0 * dx3 + 2.0 * dy3, 4.0);\n"
  172. " t1 = (1.0 + 16.0 * t1 + 4.0 * t2 + t3) / 65.0;\n"
  173. " t2 = t1;\n"
  174. " t3 = t1;\n"
  175. #else
  176. " float rand = sin(gl_FragCoord.x * 1.23456) * 123.456\n"
  177. " + cos(gl_FragCoord.y * 2.34567) * 789.012;\n"
  178. " float t1 = mod(sin(rand) * 17.13043, 1.0);\n"
  179. " float t2 = mod(sin(rand) * 27.13043, 1.0);\n"
  180. " float t3 = mod(sin(rand) * 37.13043, 1.0);\n"
  181. #endif
  182. " float fracx = fract(col.x * mul);\n"
  183. " float fracy = fract(col.y * mul);\n"
  184. " float fracz = fract(col.z * mul);\n"
  185. " fracx = fracx > t1 ? 1.0 : 0.0;\n"
  186. " fracy = fracy > t2 ? 1.0 : 0.0;\n"
  187. " fracz = fracz > t3 ? 1.0 : 0.0;\n"
  188. " col.x = (floor(col.x * mul) + fracx) / mul;\n"
  189. " col.y = (floor(col.y * mul) + fracy) / mul;\n"
  190. " col.z = (floor(col.z * mul) + fracz) / mul;\n"
  191. #endif
  192. " gl_FragColor = col;\n"
  193. "}\n");
  194. #else
  195. stdshader = Shader::Create(
  196. "void main(float4 in_Position : POSITION,"
  197. " float2 in_TexCoord : TEXCOORD0,"
  198. " uniform float4x4 proj_matrix,"
  199. " uniform float4x4 view_matrix,"
  200. " uniform float4x4 model_matrix,"
  201. " out float2 out_TexCoord : TEXCOORD0,"
  202. " out float4 out_Position : POSITION)"
  203. "{"
  204. " out_Position = mul(proj_matrix, mul(view_matrix, mul(model_matrix, in_Position)));"
  205. " out_TexCoord = in_TexCoord;"
  206. "}",
  207. "void main(float2 in_TexCoord : TEXCOORD0,"
  208. " float4 in_FragCoord : WPOS,"
  209. " uniform sampler2D tex,"
  210. " out float4 out_FragColor : COLOR)"
  211. "{"
  212. " float4 col = tex2D(tex, in_TexCoord);"
  213. #if 0
  214. " float mul = 2.0;\n"
  215. " float t1, t2, t3;\n"
  216. #if 1
  217. " float dx1 = frac(in_FragCoord.x * 0.5) * 2.0;\n"
  218. " float dy1 = frac(in_FragCoord.y * 0.5) * 2.0;\n"
  219. " t1 = frac((3.0 * dx1 + 2.0 * dy1) / 4.0) * 4.0;\n"
  220. " float dx2 = frac(floor(in_FragCoord.x * 0.5) * 0.5) * 2.0;\n"
  221. " float dy2 = frac(floor(in_FragCoord.y * 0.5) * 0.5) * 2.0;\n"
  222. " t2 = frac((3.0 * dx2 + 2.0 * dy2) / 4.0) * 4.0;\n"
  223. " float dx3 = frac(floor(in_FragCoord.x * 0.25) * 0.5) * 2.0;\n"
  224. " float dy3 = frac(floor(in_FragCoord.y * 0.25) * 0.5) * 2.0;\n"
  225. " t3 = frac((3.0 * dx3 + 2.0 * dy3) / 4.0) * 4.0;\n"
  226. " t1 = (1.0 + 4.0 * t1 + t2) / 17.0;\n"
  227. " t2 = t1;\n"
  228. " t3 = t1;\n"
  229. #else
  230. " float rand = sin(in_FragCoord.x * 1.23456) * 123.456\n"
  231. " + cos(in_FragCoord.y * 2.34567) * 789.012;\n"
  232. " t1 = frac(sin(rand) * 17.13043);\n"
  233. " t2 = frac(sin(rand) * 27.13043);\n"
  234. " t3 = frac(sin(rand) * 37.13043);\n"
  235. #endif
  236. " float fracx = frac(col.x * mul);\n"
  237. " float fracy = frac(col.y * mul);\n"
  238. " float fracz = frac(col.z * mul);\n"
  239. " fracx = fracx > t1 ? 1.0 : 0.0;\n"
  240. " fracy = fracy > t2 ? 1.0 : 0.0;\n"
  241. " fracz = fracz > t3 ? 1.0 : 0.0;\n"
  242. " col.x = (floor(col.x * mul) + fracx) / mul;\n"
  243. " col.y = (floor(col.y * mul) + fracy) / mul;\n"
  244. " col.z = (floor(col.z * mul) + fracz) / mul;\n"
  245. #endif
  246. " out_FragColor = col;"
  247. "}");
  248. #endif
  249. }
  250. #if 0
  251. // Randomise, then sort.
  252. for (int i = 0; i < data->ntiles; i++)
  253. {
  254. Tile tmp = data->tiles[i];
  255. int j = rand() % data->ntiles;
  256. data->tiles[i] = data->tiles[j];
  257. data->tiles[j] = tmp;
  258. }
  259. #endif
  260. qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
  261. // XXX: debug stuff
  262. data->model_matrix = mat4::translate(320.0f, 240.0f, 0.0f);
  263. data->model_matrix *= mat4::rotate(-data->angle, 1.0f, 0.0f, 0.0f);
  264. #if 0
  265. static float f = 0.0f;
  266. f += 0.01f;
  267. data->model_matrix *= mat4::rotate(6.0f * sinf(f), 1.0f, 0.0f, 0.0f);
  268. data->model_matrix *= mat4::rotate(17.0f * cosf(f), 0.0f, 0.0f, 1.0f);
  269. #endif
  270. data->model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
  271. // XXX: end of debug stuff
  272. GLuint uni_mat, uni_tex, attr_pos, attr_tex;
  273. #if !defined __CELLOS_LV2__
  274. attr_pos = stdshader->GetAttribLocation("in_Position");
  275. attr_tex = stdshader->GetAttribLocation("in_TexCoord");
  276. #endif
  277. stdshader->Bind();
  278. uni_mat = stdshader->GetUniformLocation("proj_matrix");
  279. stdshader->SetUniform(uni_mat, Video::GetProjMatrix());
  280. uni_mat = stdshader->GetUniformLocation("view_matrix");
  281. stdshader->SetUniform(uni_mat, Video::GetViewMatrix());
  282. uni_mat = stdshader->GetUniformLocation("model_matrix");
  283. stdshader->SetUniform(uni_mat, data->model_matrix);
  284. #if !defined __CELLOS_LV2__
  285. uni_tex = stdshader->GetUniformLocation("in_Texture");
  286. glUniform1i(uni_tex, 0);
  287. #else
  288. // WTF? this doesn't exist
  289. //uni_tex = stdshader->GetUniformLocation("in_Texture");
  290. //cgGLSetParameter1i((CGparameter)(intptr_t)uni_tex, 0);
  291. #endif
  292. #if !defined HAVE_GLES_2X
  293. glEnable(GL_TEXTURE_2D);
  294. #endif
  295. glEnable(GL_DEPTH_TEST);
  296. glDepthFunc(GL_LEQUAL);
  297. #if defined HAVE_GL_2X && !defined __APPLE__
  298. glEnable(GL_ALPHA_TEST);
  299. glAlphaFunc(GL_GEQUAL, 0.01f);
  300. #endif
  301. glEnable(GL_BLEND);
  302. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  303. for (int buf = 0, i = 0, n; i < data->ntiles; i = n, buf += 2)
  304. {
  305. /* Generate new vertex / texture coord buffers if necessary */
  306. if (buf + 2 > data->nbufs)
  307. {
  308. data->bufs = (GLuint *)realloc(data->bufs, (buf + 2) * sizeof(GLuint));
  309. glGenBuffers(buf + 2 - data->nbufs, data->bufs + data->nbufs);
  310. data->nbufs = buf + 2;
  311. }
  312. /* Count how many quads will be needed */
  313. for (n = i + 1; n < data->ntiles; n++)
  314. if (data->tiles[i].tileset != data->tiles[n].tileset)
  315. break;
  316. /* Create a vertex array object */
  317. float *vertex = (float *)malloc(6 * 3 * (n - i) * sizeof(float));
  318. float *texture = (float *)malloc(6 * 2 * (n - i) * sizeof(float));
  319. for (int j = i; j < n; j++)
  320. {
  321. data->tiles[i].tileset->BlitTile(data->tiles[j].id,
  322. data->tiles[j].pos, data->tiles[j].o,
  323. vertex + 18 * (j - i), texture + 12 * (j - i));
  324. }
  325. stdshader->Bind();
  326. /* Bind texture */
  327. data->tiles[i].tileset->Bind();
  328. /* Bind vertex, color and texture coordinate buffers */
  329. #if defined HAVE_GL_2X && !defined __APPLE__
  330. glBindVertexArray(data->vao);
  331. #endif
  332. #if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
  333. glEnableVertexAttribArray(attr_pos);
  334. glEnableVertexAttribArray(attr_tex);
  335. glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
  336. glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat),
  337. vertex, GL_STATIC_DRAW);
  338. glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
  339. glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
  340. glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat),
  341. texture, GL_STATIC_DRAW);
  342. glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
  343. #else
  344. glEnableClientState(GL_VERTEX_ARRAY);
  345. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  346. glVertexPointer(3, GL_FLOAT, 0, vertex);
  347. glTexCoordPointer(2, GL_FLOAT, 0, texture);
  348. #endif
  349. /* Draw arrays */
  350. glDrawArrays(GL_TRIANGLES, 0, (n - i) * 6);
  351. #if defined HAVE_GL_2X && !defined __APPLE__
  352. glBindVertexArray(0);
  353. #endif
  354. #if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
  355. glDisableVertexAttribArray(attr_pos);
  356. glDisableVertexAttribArray(attr_tex);
  357. #else
  358. glDisableClientState(GL_VERTEX_ARRAY);
  359. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  360. #endif
  361. free(vertex);
  362. free(texture);
  363. }
  364. free(data->tiles);
  365. data->tiles = 0;
  366. data->ntiles = 0;
  367. #if !defined HAVE_GLES_2X
  368. glDisable(GL_TEXTURE_2D);
  369. #endif
  370. glDisable(GL_DEPTH_TEST);
  371. #if defined HAVE_GL_2X && !defined __APPLE__
  372. glDisable(GL_ALPHA_TEST);
  373. #endif
  374. glDisable(GL_BLEND);
  375. }
  376. } /* namespace lol */