25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

350 lines
9.8 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdlib>
  14. #ifdef WIN32
  15. # define WIN32_LEAN_AND_MEAN
  16. # include <windows.h>
  17. #endif
  18. #include "core.h"
  19. #include "lolgl.h"
  20. LOLFX_RESOURCE_DECLARE(tile);
  21. LOLFX_RESOURCE_DECLARE(line);
  22. namespace lol
  23. {
  24. /*
  25. * The global g_scene object, initialised by Video::Init
  26. */
  27. Scene *g_scene = nullptr;
  28. /*
  29. * A quick and dirty Tile structure for 2D blits
  30. */
  31. struct Tile
  32. {
  33. TileSet *tileset;
  34. uint32_t prio;
  35. vec3 pos;
  36. vec2 scale;
  37. int id, o;
  38. };
  39. /*
  40. * Scene implementation class
  41. */
  42. class SceneData
  43. {
  44. friend class Scene;
  45. private:
  46. static int Compare(void const *p1, void const *p2)
  47. {
  48. Tile const *t1 = (Tile const *)p1;
  49. Tile const *t2 = (Tile const *)p2;
  50. if (t1->pos.z > t2->pos.z)
  51. return 1;
  52. if (t1->pos.z < t2->pos.z)
  53. return -1;
  54. return 0;
  55. }
  56. Array<vec3, vec3, vec4> m_lines;
  57. Shader *m_line_shader;
  58. VertexDeclaration *m_line_vdecl;
  59. Array<Tile> m_tiles;
  60. Array<Light *> m_lights;
  61. Shader *m_tile_shader;
  62. VertexDeclaration *m_tile_vdecl;
  63. Array<VertexBuffer *> m_tile_bufs;
  64. Camera *m_default_cam;
  65. Array<Camera *> m_camera_stack;
  66. };
  67. /*
  68. * Public Scene class
  69. */
  70. Scene::Scene(ivec2 size)
  71. : data(new SceneData())
  72. {
  73. /* Create a default orthographic camera, in case the user doesn’t. */
  74. data->m_default_cam = new Camera();
  75. mat4 proj = mat4::ortho(0, size.x, 0, size.y, -1000.f, 1000.f);
  76. data->m_default_cam->SetProjection(proj);
  77. PushCamera(data->m_default_cam);
  78. data->m_tile_shader = 0;
  79. data->m_tile_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
  80. VertexStream<vec2>(VertexUsage::TexCoord));
  81. data->m_line_shader = 0;
  82. data->m_line_vdecl = new VertexDeclaration(VertexStream<vec3,vec4>(VertexUsage::Position, VertexUsage::Color));
  83. }
  84. Scene::~Scene()
  85. {
  86. PopCamera(data->m_default_cam);
  87. /* FIXME: this must be done while the GL context is still active.
  88. * Change the code architecture to make sure of that. */
  89. /* FIXME: also, make sure we do not add code to Reset() that will
  90. * reallocate stuff */
  91. Reset();
  92. delete data->m_line_vdecl;
  93. delete data->m_tile_vdecl;
  94. delete data;
  95. }
  96. void Scene::PushCamera(Camera *cam)
  97. {
  98. Ticker::Ref(cam);
  99. data->m_camera_stack.Push(cam);
  100. }
  101. void Scene::PopCamera(Camera *cam)
  102. {
  103. /* Parse from the end because that’s probably where we’ll find
  104. * our camera first. */
  105. for (int i = data->m_camera_stack.Count(); i--; )
  106. {
  107. if (data->m_camera_stack[i] == cam)
  108. {
  109. Ticker::Unref(cam);
  110. data->m_camera_stack.Remove(i);
  111. return;
  112. }
  113. }
  114. ASSERT(false, "trying to pop a nonexistent camera from the scene");
  115. }
  116. Camera *Scene::GetCamera()
  117. {
  118. return data->m_camera_stack.Last();
  119. }
  120. void Scene::Reset()
  121. {
  122. for (int i = 0; i < data->m_tile_bufs.Count(); i++)
  123. delete data->m_tile_bufs[i];
  124. data->m_tile_bufs.Empty();
  125. data->m_lights.Empty();
  126. }
  127. void Scene::AddTile(TileSet *tileset, int id, vec3 pos, int o, vec2 scale)
  128. {
  129. ASSERT(id < tileset->GetTileCount());
  130. Tile t;
  131. /* FIXME: this sorting only works for a 45-degree camera */
  132. t.prio = -pos.y - 2 * 32 * pos.z + (o ? 0 : 32);
  133. t.tileset = tileset;
  134. t.id = id;
  135. t.pos = pos;
  136. t.o = o;
  137. t.scale = scale;
  138. data->m_tiles.Push(t);
  139. }
  140. void Scene::AddLine(vec3 a, vec3 b, vec4 color)
  141. {
  142. data->m_lines.Push(a, b, color);
  143. }
  144. void Scene::AddLight(Light *l)
  145. {
  146. data->m_lights.Push(l);
  147. }
  148. Array<Light *> const &Scene::GetLights() const
  149. {
  150. return data->m_lights;
  151. }
  152. void Scene::Render() // XXX: rename to Blit()
  153. {
  154. RenderContext rc;
  155. /* Early test if nothing needs to be rendered */
  156. if (data->m_tiles.Count())
  157. {
  158. rc.SetDepthFunc(DepthFunc::LessOrEqual);
  159. rc.SetBlendFunc(BlendFunc::SrcAlpha, BlendFunc::OneMinusSrcAlpha);
  160. rc.SetAlphaFunc(AlphaFunc::GreaterOrEqual, 0.01f);
  161. PushCamera(data->m_default_cam);
  162. #if defined USE_D3D9 || defined _XBOX
  163. #elif !defined HAVE_GLES_2X
  164. glEnable(GL_TEXTURE_2D);
  165. #endif
  166. if (!data->m_tile_shader)
  167. data->m_tile_shader = Shader::Create(LOLFX_RESOURCE_NAME(tile));
  168. #if 0
  169. // Randomise, then sort.
  170. for (int i = 0; i < data->m_tiles.Count(); i++)
  171. {
  172. Tile tmp = data->m_tiles[i];
  173. int j = rand<int>() % data->m_tiles.Count();
  174. data->m_tiles[i] = data->m_tiles[j];
  175. data->m_tiles[j] = tmp;
  176. }
  177. #endif
  178. qsort(&data->m_tiles[0], data->m_tiles.Count(),
  179. sizeof(Tile), SceneData::Compare);
  180. ShaderUniform uni_mat, uni_tex, uni_texsize;
  181. ShaderAttrib attr_pos, attr_tex;
  182. attr_pos = data->m_tile_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
  183. attr_tex = data->m_tile_shader->GetAttribLocation("in_TexCoord", VertexUsage::TexCoord, 0);
  184. data->m_tile_shader->Bind();
  185. uni_mat = data->m_tile_shader->GetUniformLocation("proj_matrix");
  186. data->m_tile_shader->SetUniform(uni_mat, GetCamera()->GetProjection());
  187. uni_mat = data->m_tile_shader->GetUniformLocation("view_matrix");
  188. data->m_tile_shader->SetUniform(uni_mat, GetCamera()->GetView());
  189. uni_mat = data->m_tile_shader->GetUniformLocation("model_matrix");
  190. data->m_tile_shader->SetUniform(uni_mat, mat4(1.f));
  191. uni_tex = data->m_tile_shader->GetUniformLocation("in_Texture");
  192. data->m_tile_shader->SetUniform(uni_tex, 0);
  193. uni_texsize = data->m_tile_shader->GetUniformLocation("in_TexSize");
  194. for (int buf = 0, i = 0, n; i < data->m_tiles.Count(); i = n, buf += 2)
  195. {
  196. /* Count how many quads will be needed */
  197. for (n = i + 1; n < data->m_tiles.Count(); n++)
  198. if (data->m_tiles[i].tileset != data->m_tiles[n].tileset)
  199. break;
  200. /* Create a vertex array object */
  201. VertexBuffer *vb1 = new VertexBuffer(6 * 3 * (n - i) * sizeof(float));
  202. float *vertex = (float *)vb1->Lock(0, 0);
  203. VertexBuffer *vb2 = new VertexBuffer(6 * 2 * (n - i) * sizeof(float));
  204. float *texture = (float *)vb2->Lock(0, 0);
  205. data->m_tile_bufs.Push(vb1);
  206. data->m_tile_bufs.Push(vb2);
  207. for (int j = i; j < n; j++)
  208. {
  209. data->m_tiles[i].tileset->BlitTile(data->m_tiles[j].id,
  210. data->m_tiles[j].pos, data->m_tiles[j].o,
  211. data->m_tiles[j].scale,
  212. vertex + 18 * (j - i), texture + 12 * (j - i));
  213. }
  214. vb1->Unlock();
  215. vb2->Unlock();
  216. /* Bind texture */
  217. data->m_tiles[i].tileset->Bind();
  218. data->m_tile_shader->SetUniform(uni_texsize,
  219. (vec2)data->m_tiles[i].tileset->GetTextureSize());
  220. /* Bind vertex and texture coordinate buffers */
  221. data->m_tile_vdecl->Bind();
  222. data->m_tile_vdecl->SetStream(vb1, attr_pos);
  223. data->m_tile_vdecl->SetStream(vb2, attr_tex);
  224. /* Draw arrays */
  225. data->m_tile_vdecl->DrawElements(MeshPrimitive::Triangles, 0, (n - i) * 6);
  226. data->m_tile_vdecl->Unbind();
  227. data->m_tiles[i].tileset->Unbind();
  228. }
  229. data->m_tiles.Empty();
  230. data->m_tile_shader->Unbind();
  231. #if defined USE_D3D9 || defined _XBOX
  232. /* TODO */
  233. #elif !defined HAVE_GLES_2X
  234. glDisable(GL_TEXTURE_2D);
  235. #endif
  236. PopCamera(data->m_default_cam);
  237. }
  238. if (data->m_lines.Count())
  239. {
  240. rc.SetDepthFunc(DepthFunc::LessOrEqual);
  241. rc.SetBlendFunc(BlendFunc::SrcAlpha, BlendFunc::OneMinusSrcAlpha);
  242. rc.SetAlphaFunc(AlphaFunc::GreaterOrEqual, 0.01f);
  243. int linecount = data->m_lines.Count();
  244. if (!data->m_line_shader)
  245. data->m_line_shader = Shader::Create(LOLFX_RESOURCE_NAME(line));
  246. VertexBuffer *vb = new VertexBuffer((sizeof(vec3) + sizeof(vec4)) * 2 * linecount);
  247. float *vertex = (float *)vb->Lock(0, 0);
  248. for (int i = 0; i < linecount; i++)
  249. {
  250. memcpy(vertex, &data->m_lines[i].m1, sizeof(vec3));
  251. vertex += 3;
  252. memcpy(vertex, &data->m_lines[i].m3, sizeof(vec4));
  253. vertex += 4;
  254. memcpy(vertex, &data->m_lines[i].m2, sizeof(vec3));
  255. vertex += 3;
  256. memcpy(vertex, &data->m_lines[i].m3, sizeof(vec4));
  257. vertex += 4;
  258. }
  259. vb->Unlock();
  260. data->m_line_shader->Bind();
  261. ShaderUniform uni_mat, uni_tex;
  262. ShaderAttrib attr_pos, attr_col;
  263. attr_pos = data->m_line_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
  264. attr_col = data->m_line_shader->GetAttribLocation("in_Color", VertexUsage::Color, 0);
  265. data->m_line_shader->Bind();
  266. uni_mat = data->m_line_shader->GetUniformLocation("proj_matrix");
  267. data->m_line_shader->SetUniform(uni_mat, GetCamera()->GetProjection());
  268. uni_mat = data->m_line_shader->GetUniformLocation("view_matrix");
  269. data->m_line_shader->SetUniform(uni_mat, GetCamera()->GetView());
  270. data->m_line_vdecl->Bind();
  271. data->m_line_vdecl->SetStream(vb, attr_pos, attr_col);
  272. data->m_line_vdecl->DrawElements(MeshPrimitive::Lines, 0, 2 * linecount);
  273. data->m_line_vdecl->Unbind();
  274. data->m_line_shader->Unbind();
  275. data->m_lines.Empty();
  276. delete vb;
  277. }
  278. }
  279. } /* namespace lol */