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

1313 строки
43 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2013 Cédric Lecacheur <jordx@free.fr>
  6. // (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the Do What The Fuck You Want To
  9. // Public License, Version 2, as published by Sam Hocevar. See
  10. // http://www.wtfpl.net/ for more details.
  11. //
  12. //
  13. // The EasyMesh class
  14. // ------------------
  15. //
  16. #if defined HAVE_CONFIG_H
  17. # include "config.h"
  18. #endif
  19. #if defined _XBOX
  20. # define _USE_MATH_DEFINES /* for M_PI */
  21. # include <xtl.h>
  22. # undef near /* Fuck Microsoft */
  23. # undef far /* Fuck Microsoft again */
  24. #elif defined _WIN32
  25. # define _USE_MATH_DEFINES /* for M_PI */
  26. # define WIN32_LEAN_AND_MEAN
  27. # include <windows.h>
  28. # undef near /* Fuck Microsoft */
  29. # undef far /* Fuck Microsoft again */
  30. #endif
  31. #include "core.h"
  32. #include "easymesh/easymesh-compiler.h"
  33. extern char const *lolfx_shiny;
  34. namespace lol
  35. {
  36. EasyMesh::EasyMesh()
  37. : m_color(0), m_color2(0), m_ignore_winding_on_scale(0)
  38. {
  39. m_cursors.Push(0, 0);
  40. }
  41. bool EasyMesh::Compile(char const *command)
  42. {
  43. EasyMeshCompiler mc(*this);
  44. return mc.ParseString(command);
  45. }
  46. void EasyMesh::OpenBrace()
  47. {
  48. m_cursors.Push(m_vert.Count(), m_indices.Count());
  49. }
  50. void EasyMesh::CloseBrace()
  51. {
  52. m_cursors.Pop();
  53. }
  54. void EasyMesh::MeshConvert(Shader* provided_shader)
  55. {
  56. if(provided_shader == NULL)
  57. {
  58. m_gpu.shader = Shader::Create(lolfx_shiny);
  59. }
  60. else
  61. {
  62. m_gpu.shader = provided_shader;
  63. }
  64. m_gpu.modelview = m_gpu.shader->GetUniformLocation("in_ModelView");
  65. m_gpu.view = m_gpu.shader->GetUniformLocation("in_View");
  66. m_gpu.invview = m_gpu.shader->GetUniformLocation("in_Inv_View");
  67. m_gpu.proj = m_gpu.shader->GetUniformLocation("in_Proj");
  68. m_gpu.normalmat = m_gpu.shader->GetUniformLocation("in_NormalMat");
  69. m_gpu.damage = m_gpu.shader->GetUniformLocation("in_Damage");
  70. m_gpu.coord = m_gpu.shader->GetAttribLocation("in_Vertex",
  71. VertexUsage::Position, 0);
  72. m_gpu.norm = m_gpu.shader->GetAttribLocation("in_Normal",
  73. VertexUsage::Normal, 0);
  74. m_gpu.color = m_gpu.shader->GetAttribLocation("in_Color",
  75. VertexUsage::Color, 0);
  76. m_gpu.vdecl = new VertexDeclaration(
  77. VertexStream<vec3,vec3,u8vec4>(VertexUsage::Position,
  78. VertexUsage::Normal,
  79. VertexUsage::Color));
  80. Array<vec3,vec3,u8vec4> vertexlist;
  81. for (int i = 0; i < m_vert.Count(); i++)
  82. vertexlist.Push(m_vert[i].m1,
  83. m_vert[i].m2,
  84. (u8vec4)(m_vert[i].m3 * 255.f));
  85. Array<uint16_t> indexlist;
  86. for (int i = 0; i < m_indices.Count(); i += 3)
  87. {
  88. indexlist << m_indices[i + 0];
  89. indexlist << m_indices[i + 1];
  90. indexlist << m_indices[i + 2];
  91. }
  92. m_gpu.vbo = new VertexBuffer(vertexlist.Bytes());
  93. void *mesh = m_gpu.vbo->Lock(0, 0);
  94. memcpy(mesh, &vertexlist[0], vertexlist.Bytes());
  95. m_gpu.vbo->Unlock();
  96. m_gpu.ibo = new IndexBuffer(indexlist.Bytes());
  97. void *indices = m_gpu.ibo->Lock(0, 0);
  98. memcpy(indices, &indexlist[0], indexlist.Bytes());
  99. m_gpu.ibo->Unlock();
  100. m_gpu.vertexcount = vertexlist.Count();
  101. m_gpu.indexcount = indexlist.Count();
  102. }
  103. void EasyMesh::Render(mat4 const &model, float damage)
  104. {
  105. mat4 modelview = Scene::GetDefault()->GetViewMatrix() * model;
  106. mat3 normalmat = transpose(inverse(mat3(modelview)));
  107. m_gpu.shader->Bind();
  108. m_gpu.shader->SetUniform(m_gpu.modelview, modelview);
  109. m_gpu.shader->SetUniform(m_gpu.view, Scene::GetDefault()->GetViewMatrix());
  110. m_gpu.shader->SetUniform(m_gpu.invview, inverse(Scene::GetDefault()->GetViewMatrix()));
  111. m_gpu.shader->SetUniform(m_gpu.proj, Scene::GetDefault()->GetProjMatrix());
  112. m_gpu.shader->SetUniform(m_gpu.normalmat, normalmat);
  113. m_gpu.shader->SetUniform(m_gpu.damage, damage);
  114. m_gpu.vdecl->Bind();
  115. m_gpu.vdecl->SetStream(m_gpu.vbo, m_gpu.coord, m_gpu.norm, m_gpu.color);
  116. m_gpu.ibo->Bind();
  117. m_gpu.vdecl->DrawIndexedElements(MeshPrimitive::Triangles,
  118. 0, 0, m_gpu.vertexcount,
  119. 0, m_gpu.indexcount);
  120. m_gpu.ibo->Unbind();
  121. m_gpu.vdecl->Unbind();
  122. }
  123. //-------------------
  124. // "Collisions" functions
  125. //-------------------
  126. #define VX_ALONE -2
  127. #define VX_MASTER -1
  128. //helpers func to retrieve a vertex.
  129. int FindVertexInDict(int search_idx, Array< int, int > const &vertex_dict)
  130. {
  131. //Resolve current vertex idx in the dictionnary (if exist)
  132. for (int j = 0; j < vertex_dict.Count(); j++)
  133. if (vertex_dict[j].m1 == search_idx)
  134. return j;
  135. return -1;
  136. }
  137. //helpers func to retrieve a triangle.
  138. int FindTriangleInDict(int search_idx, Array< int, Array< vec3, vec3, vec3 > > const &triangle_isec)
  139. {
  140. //Resolve current vertex idx in the dictionnary (if exist)
  141. for (int j = 0; j < triangle_isec.Count(); j++)
  142. if (triangle_isec[j].m1 == search_idx)
  143. return j;
  144. return -1;
  145. }
  146. //Will update the given list with all the vertices on the same spot.
  147. void EasyMesh::UpdateVertexDict(Array< int, int > &vertex_dict)
  148. {
  149. //First, build the vertex Dictionnary
  150. for (int i = 0; i < m_vert.Count(); i++)
  151. {
  152. int CurIdx = FindVertexInDict(i, vertex_dict);
  153. //go through all vertices and do the match-up.
  154. if (CurIdx == -1)
  155. {
  156. for (int j = i + 1; j < m_vert.Count(); j++)
  157. {
  158. if (sqlength(m_vert[i].m1 - m_vert[j].m1) < CSG_EPSILON)
  159. {
  160. if (CurIdx == -1)
  161. {
  162. CurIdx = vertex_dict.Count();
  163. vertex_dict.Push(i, VX_MASTER);
  164. }
  165. vertex_dict.Push(j, CurIdx);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. void EasyMesh::MeshCsg(int csg_operation)
  172. {
  173. //A vertex dictionnary for vertices on the same spot.
  174. Array< int, int > vertex_dict;
  175. //This list keeps track of the triangle that will need deletion at the end.
  176. Array< int > triangle_to_kill;
  177. //Listing for each triangle of the vectors intersecting it. <tri_Id, <Point0, Point1, tri_isec_Normal>>
  178. Array< int, Array< vec3, vec3, vec3 > > triangle_isec;
  179. //keep a track of the intersection point on the triangle. <pos, side_id>
  180. Array< vec3, int > triangle_vertex;
  181. for (int k = 0; k < 10; k++)
  182. triangle_vertex.Push(vec3(.0f), 0);
  183. //bsp infos
  184. CsgBsp mesh_bsp_0;
  185. CsgBsp mesh_bsp_1;
  186. if (m_cursors.Count() == 0)
  187. return;
  188. //BSP BUILD : We use the brace logic, csg should be used as : "[ exp .... [exp .... csg]]"
  189. int cursor_start = (m_cursors.Count() < 2)?(0):(m_cursors[(m_cursors.Count() - 2)].m2);
  190. for (int mesh_id = 0; mesh_id < 2; mesh_id++)
  191. {
  192. int start_point = (mesh_id == 0)?(cursor_start):(m_cursors.Last().m2);
  193. int end_point = (mesh_id == 0)?(m_cursors.Last().m2):(m_indices.Count());
  194. CsgBsp &mesh_bsp = (mesh_id == 0)?(mesh_bsp_0):(mesh_bsp_1);
  195. for (int i = start_point; i < end_point; i += 3)
  196. mesh_bsp.AddTriangleToTree(i, m_vert[m_indices[i]].m1, m_vert[m_indices[i + 1]].m1, m_vert[m_indices[i + 2]].m1);
  197. }
  198. //BSP Useage : let's crunch all triangles on the correct BSP
  199. int indices_count = m_indices.Count();
  200. for (int mesh_id = 0; mesh_id < 2; mesh_id++)
  201. {
  202. int start_point = (mesh_id == 0)?(cursor_start):(m_cursors.Last().m2);
  203. int end_point = (mesh_id == 0)?(m_cursors.Last().m2):(indices_count);
  204. CsgBsp &mesh_bsp = (mesh_id == 0)?(mesh_bsp_1):(mesh_bsp_0);
  205. Array< vec3, int, int, float > vert_list;
  206. Array< int, int, int, int > tri_list;
  207. vec3 n0(.0f); vec3 n1(.0f);
  208. vec4 c0(.0f); vec4 c1(.0f);
  209. //Reserve some memory
  210. vert_list.Reserve(3);
  211. tri_list.Reserve(3);
  212. for (int i = start_point; i < end_point; i += 3)
  213. {
  214. int Result = mesh_bsp.TestTriangleToTree(m_vert[m_indices[i]].m1, m_vert[m_indices[i + 1]].m1, m_vert[m_indices[i + 2]].m1, vert_list, tri_list);
  215. int tri_base_idx = m_indices.Count();
  216. //one split has been done, we need to had the new vertices & the new triangles.
  217. if (Result == 1)
  218. {
  219. triangle_to_kill.Push(i);
  220. #if 1
  221. int base_idx = m_vert.Count();
  222. for (int k = 3; k < vert_list.Count(); k++)
  223. {
  224. int P0 = (vert_list[k].m2 < 3)?(m_indices[i + vert_list[k].m2]):(base_idx + vert_list[k].m2 - 3);
  225. int P1 = (vert_list[k].m3 < 3)?(m_indices[i + vert_list[k].m3]):(base_idx + vert_list[k].m3 - 3);
  226. AddVertex(vert_list[k].m1);
  227. //Normal : bad calculations there.
  228. n0 = m_vert[P0].m2;
  229. n1 = m_vert[P1].m2;
  230. SetCurVertNormal(normalize(n0 + (n1 - n0) * vert_list[k].m4));
  231. #if 1
  232. //Color
  233. c0 = m_vert[P0].m3;
  234. c1 = m_vert[P1].m3;
  235. vec4 res = c0 + ((c1 - c0) * vert_list[k].m4);
  236. SetCurVertColor(res);
  237. #else
  238. if (mesh_id == 0)
  239. SetCurVertColor(vec4(1.0f, .0f, .0f, 1.0f));
  240. else
  241. SetCurVertColor(vec4(.0f, 1.0f, 1.0f, 1.0f));
  242. #endif
  243. }
  244. for (int k = 0; k < tri_list.Count(); k++)
  245. {
  246. int P0 = (tri_list[k].m2 < 3)?(m_indices[i + tri_list[k].m2]):(base_idx + (tri_list[k].m2 - 3));
  247. int P1 = (tri_list[k].m3 < 3)?(m_indices[i + tri_list[k].m3]):(base_idx + (tri_list[k].m3 - 3));
  248. int P2 = (tri_list[k].m4 < 3)?(m_indices[i + tri_list[k].m4]):(base_idx + (tri_list[k].m4 - 3));
  249. AppendTriangle(P0, P1, P2, 0);
  250. }
  251. #endif
  252. }
  253. #if 1
  254. //Main case
  255. if (Result >= 0)
  256. {
  257. for (int k = 0; k < tri_list.Count(); k++)
  258. {
  259. int tri_idx = ((tri_list.Count() == 1)?(i):(tri_base_idx + k * 3));
  260. //Triangle Kill Test
  261. if (//csgu : CSGUnion() -> m0_Outside + m1_Outside
  262. (csg_operation == CSG_UNION && tri_list[k].m1 == LEAF_BACK) ||
  263. //csgs : CSGSubstract() -> m0_Outside + m1_Inside-inverted
  264. (csg_operation == CSG_SUBSTRACT &&
  265. ((mesh_id == 0 && tri_list[k].m1 == LEAF_BACK) ||
  266. (mesh_id == 1 && tri_list[k].m1 == LEAF_FRONT))) ||
  267. //csga : CSGAnd() -> Inside + Inside
  268. (csg_operation == CSG_AND && tri_list[k].m1 == LEAF_FRONT))
  269. {
  270. triangle_to_kill.Push(tri_idx);
  271. }
  272. //Triangle Invert Test
  273. if (//csgs : CSGSubstract() -> m0_Outside + m1_Inside-inverted
  274. (csg_operation == CSG_SUBSTRACT && mesh_id == 1 && tri_list[k].m1 == LEAF_BACK) ||
  275. //csgx : CSGXor() -> Outside/Inside-inverted + Outside/Inside-inverted
  276. (csg_operation == CSG_XOR && tri_list[k].m1 == LEAF_BACK))
  277. {
  278. //a Xor means we will share vertices with the outside, so duplicate the vertices.
  279. //TODO : This operation disconnect all triangle, in some cases, not a good thing.
  280. if (csg_operation == CSG_XOR)
  281. {
  282. for (int l = 0; l < 3; l++)
  283. {
  284. AddDuplicateVertex(m_indices[tri_idx + l]);
  285. m_indices[tri_idx + l] = m_vert.Count() - 1;
  286. }
  287. }
  288. m_indices[tri_idx + 1] += m_indices[tri_idx + 2];
  289. m_indices[tri_idx + 2] = m_indices[tri_idx + 1] - m_indices[tri_idx + 2];
  290. m_indices[tri_idx + 1] = m_indices[tri_idx + 1] - m_indices[tri_idx + 2];
  291. ComputeNormals(tri_idx, 3);
  292. }
  293. }
  294. }
  295. #endif
  296. vert_list.Empty();
  297. tri_list.Empty();
  298. }
  299. }
  300. for (int i = 0; i < m_vert.Count(); i++)
  301. if (length(m_vert[i].m2) < 1.0f)
  302. i = i;
  303. int dir = 1;
  304. for (int i = 0; i >= 0 && i < triangle_to_kill.Count() - 1; i += dir)
  305. {
  306. if (triangle_to_kill[i] < triangle_to_kill[i + 1] && dir < 0)
  307. dir = 1;
  308. if (triangle_to_kill[i] == triangle_to_kill[i + 1])
  309. {
  310. triangle_to_kill.Remove(i);
  311. dir = -1;
  312. }
  313. if (triangle_to_kill[i] > triangle_to_kill[i + 1])
  314. {
  315. triangle_to_kill[i] += triangle_to_kill[i + 1];
  316. triangle_to_kill[i + 1] = triangle_to_kill[i] - triangle_to_kill[i + 1];
  317. triangle_to_kill[i] = triangle_to_kill[i] - triangle_to_kill[i + 1];
  318. dir = -1;
  319. }
  320. if (i == 0 && dir == -1)
  321. dir = 1;
  322. }
  323. for (int i = triangle_to_kill.Count() - 1; i >= 0; i--)
  324. m_indices.Remove(triangle_to_kill[i], 3);
  325. m_cursors.Last().m1 = m_vert.Count();
  326. m_cursors.Last().m2 = m_indices.Count();
  327. #if 0
  328. UpdateVertexDict(vertex_dict);
  329. for (int t0 = 0; t0 < m_indices.Count(); t0 += 3)
  330. {
  331. for (int t1 = t0 + 3; t1 < m_indices.Count(); t1 += 3)
  332. {
  333. int CommonVertices = 0;
  334. //Search for common vertices, if > 1 the two triangle share a side, so no split is required.
  335. for (int k = 0; k < 3; k++)
  336. {
  337. int ref_master = FindVertexInDict(m_indices[t0 + k], vertex_dict);
  338. if (ref_master != -1)
  339. {
  340. if (vertex_dict[ref_master].m2 != VX_MASTER)
  341. ref_master = vertex_dict[ref_master].m2;
  342. for (int l = 0; l < 3; l++)
  343. {
  344. int test_master = FindVertexInDict(m_indices[t1 + l], vertex_dict);
  345. if (test_master != -1)
  346. {
  347. if (vertex_dict[test_master].m2 != VX_MASTER)
  348. test_master = vertex_dict[test_master].m2;
  349. if (test_master == ref_master)
  350. {
  351. CommonVertices++;
  352. break;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. if (CommonVertices < 2)
  359. {
  360. vec3 iP0, iP1;
  361. //Build the triangle intersection list
  362. if (TriangleIsectTriangle(m_vert[m_indices[t0]].m1, m_vert[m_indices[t0 + 1]].m1, m_vert[m_indices[t0 + 2]].m1,
  363. m_vert[m_indices[t1]].m1, m_vert[m_indices[t1 + 1]].m1, m_vert[m_indices[t1 + 2]].m1,
  364. iP0, iP1))
  365. {
  366. int CurIdx = FindTriangleInDict(t0, triangle_isec);
  367. if (CurIdx == -1)
  368. {
  369. CurIdx = triangle_isec.Count();
  370. triangle_isec.Push(t0, Array<vec3, vec3, vec3>());
  371. }
  372. triangle_isec[CurIdx].m2.Push(iP0, iP1, vec3(.0f));
  373. CurIdx = FindTriangleInDict(t1, triangle_isec);
  374. if (CurIdx == -1)
  375. {
  376. CurIdx = triangle_isec.Count();
  377. triangle_isec.Push(t1, Array<vec3, vec3, vec3>());
  378. }
  379. triangle_isec[CurIdx].m2.Push(iP0, iP1, vec3(.0f));
  380. }
  381. }
  382. }
  383. }
  384. /* seems to be counter-productive in some rare cases. */
  385. /*
  386. //Every intersection has been found, let's remove those that exist twice.
  387. for(int i = 0; i < triangle_isec.Count(); i++)
  388. {
  389. for(int j = 0; j < triangle_isec[i].m2.Count(); j++)
  390. {
  391. for(int k = j + 1; k < triangle_isec[i].m2.Count(); k++)
  392. {
  393. //if the two Dir-vector are parallel & the fist Dir-vector is parallel to the (P0, P1)-vector, this is the same intersection, so kill it.
  394. if (abs(dot(normalize(triangle_isec[i].m2[j].m2 - triangle_isec[i].m2[j].m1),
  395. normalize(triangle_isec[i].m2[k].m2 - triangle_isec[i].m2[k].m1)))
  396. >= 1.0 &&
  397. abs(dot(normalize(triangle_isec[i].m2[j].m2 - triangle_isec[i].m2[j].m1),
  398. normalize(triangle_isec[i].m2[k].m1 - triangle_isec[i].m2[j].m1)))
  399. >= 1.0 )
  400. triangle_isec[i].m2.Remove(k--);
  401. }
  402. }
  403. }
  404. */
  405. //Now, the triangle intersection tab should be nice and cosy, so we can start actually cutting some triangles.
  406. vec3 isecV[2] = { vec3(.0f), vec3(.0f) };
  407. int isecI[2] = { -1, -1 };
  408. int v_idx0 = 0; int v_idx1 = 0;
  409. int new_v_idx[2] = { 0, 0 };
  410. vec3 n0(.0f); vec3 n1(.0f);
  411. vec4 c0(.0f); vec4 c1(.0f);
  412. for(int i = 0; i < triangle_isec.Count(); i++)
  413. {
  414. int tri_idx = triangle_isec[i].m1;
  415. for(int j = 0; j < triangle_isec[i].m2.Count(); j++)
  416. {
  417. //Get intersection on actual triangle sides.
  418. if (RayIsectTriangleSide(m_vert[m_indices[tri_idx]].m1, m_vert[m_indices[tri_idx + 1]].m1, m_vert[m_indices[tri_idx + 2]].m1,
  419. triangle_isec[i].m2[j].m1, triangle_isec[i].m2[j].m2,
  420. isecV[0], isecI[0], isecV[1], isecI[1]))
  421. {
  422. //Check if the found intersections point are in the triangle. If not, ignore.
  423. //Cases are :
  424. // 1) at least one dot is negative (one point in the triangle).
  425. // 2) the two dot are positive but the intersection point are on all parts of the triangle, and therefore negative.
  426. //If one of the point is on one side, some calculations tweak are needed.
  427. //If the two points are on the triangle sides, just go with it.
  428. bool should_proceed_with_cutting = true;
  429. //find out if points are on one of the side
  430. int p0_tri_idx = ((sqlength(triangle_isec[i].m2[j].m1 - isecV[0]) < CSG_EPSILON)?(0):(
  431. (sqlength(triangle_isec[i].m2[j].m1 - isecV[1]) < CSG_EPSILON)?(1):(-1)));
  432. int p1_tri_idx = ((sqlength(triangle_isec[i].m2[j].m2 - isecV[0]) < CSG_EPSILON)?(0):(
  433. (sqlength(triangle_isec[i].m2[j].m2 - isecV[1]) < CSG_EPSILON)?(1):(-1)));
  434. if (p0_tri_idx < 0 || p1_tri_idx < 0)
  435. {
  436. float dot0 = (p0_tri_idx >= 0)?(1.0f):(dot(triangle_isec[i].m2[j].m1 - isecV[0],
  437. triangle_isec[i].m2[j].m1 - isecV[1]));
  438. float dot1 = (p1_tri_idx >= 0)?(1.0f):(dot(triangle_isec[i].m2[j].m2 - isecV[0],
  439. triangle_isec[i].m2[j].m2 - isecV[1]));
  440. float dot2 = dot(triangle_isec[i].m2[j].m1 - isecV[(p0_tri_idx == -1)?(0):(1 - p0_tri_idx)],
  441. triangle_isec[i].m2[j].m2 - isecV[(p1_tri_idx == -1)?(0):(1 - p1_tri_idx)]);
  442. should_proceed_with_cutting = (((dot0 < .0f) || dot1 < .0f) || (dot0 > .0f && dot1 > .0f && dot2 < .0f));
  443. }
  444. if (should_proceed_with_cutting)
  445. {
  446. //Add the new vertices
  447. int b_idx = 0;
  448. for(int k = 0; k < 2; k++)
  449. {
  450. if (b_idx == isecI[k])
  451. b_idx++;
  452. new_v_idx[k] = m_vert.Count();
  453. AddVertex(isecV[k]);
  454. //bad calculations of normal there.
  455. n0 = m_vert[m_indices[tri_idx + isecI[k]]].m2;
  456. n1 = m_vert[m_indices[tri_idx + (isecI[k] + 1) % 3]].m2;
  457. SetCurVertNormal(normalize((n0 + n1) * .5f));
  458. //color
  459. #if 0
  460. c0 = m_vert[m_indices[tri_idx + isecI[k]]].m3;
  461. c1 = m_vert[m_indices[tri_idx + (isecI[k] + 1) % 3]].m3;
  462. SetCurVertColor((c0 + c1) * .5f);
  463. #else
  464. SetCurVertColor(vec4(1.0f, 0.0f, 0.0f, 1.0f));
  465. #endif
  466. }
  467. //small trick, b_idx is the side index that has no intersection.
  468. v_idx0 = (b_idx == 1)?(1):(0);
  469. v_idx1 = (b_idx == 1)?(0):(1);
  470. //Add the new triangles
  471. AppendTriangle(m_indices[tri_idx + b_idx], new_v_idx[v_idx0], new_v_idx[v_idx1], 0);
  472. AppendTriangle(m_indices[tri_idx + ((b_idx + 2) % 3)], new_v_idx[v_idx1], new_v_idx[v_idx0], 0);
  473. //Replace the current triangle by on of the new one, instead of erasing it.
  474. m_indices[tri_idx + ((b_idx + 2) % 3)] = new_v_idx[v_idx0];
  475. if (j + 1 < triangle_isec[i].m2.Count())
  476. {
  477. triangle_isec[i].m2.Remove(j--);
  478. //add the two new triangle to the checklist.
  479. triangle_isec.Push(m_indices.Count() - 6, triangle_isec[i].m2);
  480. triangle_isec.Push(m_indices.Count() - 3, triangle_isec[i].m2);
  481. }
  482. }
  483. }
  484. }
  485. }
  486. #endif
  487. //DONE for the splitting !
  488. }
  489. //-------------------
  490. void EasyMesh::ToggleScaleWinding()
  491. {
  492. m_ignore_winding_on_scale = !m_ignore_winding_on_scale;
  493. }
  494. void EasyMesh::SetCurColor(vec4 const &color)
  495. {
  496. m_color = color;
  497. }
  498. void EasyMesh::SetCurColor2(vec4 const &color)
  499. {
  500. m_color2 = color;
  501. }
  502. void EasyMesh::AddVertex(vec3 const &coord)
  503. {
  504. m_vert.Push(coord, vec3(0.f, 1.f, 0.f), m_color);
  505. }
  506. void EasyMesh::AddDuplicateVertex(int i)
  507. {
  508. m_vert.Push(m_vert[i].m1, vec3(0.f, 1.f, 0.f), m_vert[i].m3);
  509. }
  510. void EasyMesh::AppendQuad(int i1, int i2, int i3, int i4, int base)
  511. {
  512. m_indices << base + i1;
  513. m_indices << base + i2;
  514. m_indices << base + i3;
  515. m_indices << base + i4;
  516. m_indices << base + i1;
  517. m_indices << base + i3;
  518. }
  519. void EasyMesh::AppendQuadDuplicateVerts(int i1, int i2, int i3, int i4, int base)
  520. {
  521. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  522. m_indices << m_vert.Count(); AddDuplicateVertex(base + i2);
  523. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  524. m_indices << m_vert.Count(); AddDuplicateVertex(base + i4);
  525. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  526. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  527. }
  528. void EasyMesh::AppendTriangle(int i1, int i2, int i3, int base)
  529. {
  530. m_indices << base + i1;
  531. m_indices << base + i2;
  532. m_indices << base + i3;
  533. }
  534. void EasyMesh::AppendTriangleDuplicateVerts(int i1, int i2, int i3, int base)
  535. {
  536. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  537. m_indices << m_vert.Count(); AddDuplicateVertex(base + i2);
  538. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  539. }
  540. void EasyMesh::ComputeNormals(int start, int vcount)
  541. {
  542. for (int i = 0; i < vcount; i += 3)
  543. {
  544. vec3 v0 = m_vert[m_indices[start + i + 2]].m1
  545. - m_vert[m_indices[start + i + 0]].m1;
  546. vec3 v1 = m_vert[m_indices[start + i + 1]].m1
  547. - m_vert[m_indices[start + i + 0]].m1;
  548. vec3 n = normalize(cross(v1, v0));
  549. for (int j = 0; j < 3; j++)
  550. m_vert[m_indices[start + i + j]].m2 = n;
  551. }
  552. }
  553. void EasyMesh::SetVertColor(vec4 const &color)
  554. {
  555. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  556. m_vert[i].m3 = color;
  557. }
  558. void EasyMesh::SetCurVertNormal(vec3 const &normal)
  559. {
  560. m_vert[m_vert.Count() - 1].m2 = normal;
  561. }
  562. void EasyMesh::SetCurVertColor(vec4 const &color)
  563. {
  564. m_vert[m_vert.Count() - 1].m3 = color;
  565. }
  566. void EasyMesh::Translate(vec3 const &v)
  567. {
  568. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  569. m_vert[i].m1 += v;
  570. }
  571. void EasyMesh::RotateX(float t) { Rotate(t, vec3(1, 0, 0)); }
  572. void EasyMesh::RotateY(float t) { Rotate(t, vec3(0, 1, 0)); }
  573. void EasyMesh::RotateZ(float t) { Rotate(t, vec3(0, 0, 1)); }
  574. void EasyMesh::Rotate(float t, vec3 const &axis)
  575. {
  576. mat3 m = mat3::rotate(t, axis);
  577. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  578. {
  579. m_vert[i].m1 = m * m_vert[i].m1;
  580. m_vert[i].m2 = m * m_vert[i].m2;
  581. }
  582. }
  583. void EasyMesh::RadialJitter(float r)
  584. {
  585. Array<int> Welded;
  586. Welded.Push(-1);
  587. for (int i = m_cursors.Last().m1 + 1; i < m_vert.Count(); i++)
  588. {
  589. int j, k;
  590. for (j = m_cursors.Last().m1, k = 0; j < i; j++, k++)
  591. {
  592. if(Welded[k] < 0)
  593. {
  594. vec3 diff = m_vert[i].m1 - m_vert[j].m1;
  595. if(diff.x > 0.1f || diff.x < -0.1f)
  596. continue;
  597. if(diff.y > 0.1f || diff.y < -0.1f)
  598. continue;
  599. if(diff.z > 0.1f || diff.z < -0.1f)
  600. continue;
  601. break;
  602. }
  603. }
  604. if(j == i)
  605. Welded.Push(-1);
  606. else
  607. Welded.Push(j);
  608. }
  609. int i, j;
  610. for (i = m_cursors.Last().m1, j = 0; i < m_vert.Count(); i++, j++)
  611. {
  612. if(Welded[j] == -1)
  613. m_vert[i].m1 *= 1.0f + RandF(r);
  614. else
  615. m_vert[i].m1 = m_vert[Welded[j]].m1;
  616. }
  617. ComputeNormals(m_cursors.Last().m2, m_indices.Count() - m_cursors.Last().m2);
  618. }
  619. void EasyMesh::TaperX(float y, float z, float xoff)
  620. {
  621. /* FIXME: this code breaks normals! */
  622. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  623. {
  624. m_vert[i].m1.y *= 1.f + (y * m_vert[i].m1.x + xoff);
  625. m_vert[i].m1.z *= 1.f + (z * m_vert[i].m1.x + xoff);
  626. }
  627. }
  628. void EasyMesh::TaperY(float x, float z, float yoff)
  629. {
  630. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  631. {
  632. m_vert[i].m1.x *= 1.f + (x * m_vert[i].m1.y + yoff);
  633. m_vert[i].m1.z *= 1.f + (z * m_vert[i].m1.y + yoff);
  634. }
  635. }
  636. void EasyMesh::TaperZ(float x, float y, float zoff)
  637. {
  638. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  639. {
  640. m_vert[i].m1.x *= 1.f + (x * m_vert[i].m1.z + zoff);
  641. m_vert[i].m1.y *= 1.f + (y * m_vert[i].m1.z + zoff);
  642. }
  643. }
  644. void EasyMesh::Scale(vec3 const &s)
  645. {
  646. vec3 const invs = vec3(1) / s;
  647. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  648. {
  649. m_vert[i].m1 *= s;
  650. m_vert[i].m2 = normalize(m_vert[i].m2 * invs);
  651. }
  652. /* Flip winding if the scaling involves mirroring */
  653. if (!m_ignore_winding_on_scale && s.x * s.y * s.z < 0)
  654. {
  655. for (int i = m_cursors.Last().m2; i < m_indices.Count(); i += 3)
  656. {
  657. uint16_t tmp = m_indices[i + 0];
  658. m_indices[i + 0] = m_indices[i + 1];
  659. m_indices[i + 1] = tmp;
  660. }
  661. }
  662. }
  663. void EasyMesh::MirrorX() { DupAndScale(vec3(-1, 1, 1)); }
  664. void EasyMesh::MirrorY() { DupAndScale(vec3(1, -1, 1)); }
  665. void EasyMesh::MirrorZ() { DupAndScale(vec3(1, 1, -1)); }
  666. void EasyMesh::DupAndScale(vec3 const &s)
  667. {
  668. int vlen = m_vert.Count() - m_cursors.Last().m1;
  669. int tlen = m_indices.Count() - m_cursors.Last().m2;
  670. for (int i = 0; i < vlen; i++)
  671. m_vert << m_vert[m_cursors.Last().m1++];
  672. for (int i = 0; i < tlen; i++)
  673. m_indices << m_indices[m_cursors.Last().m2++] + vlen;
  674. Scale(s);
  675. m_cursors.Last().m1 -= vlen;
  676. m_cursors.Last().m2 -= tlen;
  677. }
  678. void EasyMesh::AppendCylinder(int nsides, float h, float r1, float r2,
  679. int dualside, int smooth)
  680. {
  681. int vbase = m_vert.Count();
  682. mat3 rotmat = mat3::rotate(360.0f / nsides, 0.f, 1.f, 0.f);
  683. vec3 p1(r1, -h * .5f, 0.f), p2(r2, h * .5f, 0.f), n;
  684. /* Construct normal */
  685. if (r2 != .0f)
  686. n = vec3(r2, h * .5f, 0.f);
  687. else
  688. n = vec3(r1, h * .5f, 0.f);
  689. n.y = r1 * (r1 - r2) / h;
  690. if (!smooth)
  691. n = mat3::rotate(180.0f / nsides, 0.f, 1.f, 0.f) * n;
  692. n = normalize(n);
  693. /* FIXME: normals should be flipped in two-sided mode, but that
  694. * means duplicating the vertices again... */
  695. for (int i = 0; i < nsides; i++)
  696. {
  697. AddVertex(p1); SetCurVertNormal(n);
  698. AddVertex(p2); SetCurVertNormal(n);
  699. SetCurVertColor(m_color2);
  700. if (smooth)
  701. {
  702. int j = (i + 1) % nsides;
  703. AppendQuad(j * 2, j * 2 + 1, i * 2 + 1, i * 2, vbase);
  704. if (dualside)
  705. AppendQuad(i * 2, i * 2 + 1, j * 2 + 1, j * 2, vbase);
  706. }
  707. p1 = rotmat * p1;
  708. p2 = rotmat * p2;
  709. if (!smooth)
  710. {
  711. AddVertex(p1); SetCurVertNormal(n);
  712. AddVertex(p2); SetCurVertNormal(n);
  713. SetCurVertColor(m_color2);
  714. AppendQuad(i * 4 + 2, i * 4 + 3, i * 4 + 1, i * 4, vbase);
  715. if (dualside)
  716. AppendQuad(i * 4, i * 4 + 1, i * 4 + 3, i * 4 + 2, vbase);
  717. }
  718. n = rotmat * n;
  719. }
  720. }
  721. void EasyMesh::AppendCapsule(int ndivisions, float h, float r)
  722. {
  723. int ibase = m_indices.Count();
  724. Array<vec3> vertices;
  725. /* FIXME: we don't know how to handle even-divided capsules, so we
  726. * force the count to be odd. */
  727. if (h)
  728. ndivisions |= 1;
  729. /* Fill in the icosahedron vertices, rotating them so that there
  730. * is a vertex at [0 1 0] and [0 -1 0] after normalisation. */
  731. float phi = 0.5f + 0.5f * sqrt(5.f);
  732. mat3 mat = mat3::rotate(asin(1.f / sqrt(2.f + phi)) * (180.f / (float)M_PI),
  733. vec3(0.f, 0.f, 1.f));
  734. for (int i = 0; i < 4; i++)
  735. {
  736. float x = (i & 1) ? 0.5f : -0.5f;
  737. float y = (i & 2) ? phi * 0.5f : phi * -0.5f;
  738. vertices << mat * vec3(x, y, 0.f);
  739. vertices << mat * vec3(0.f, x, y);
  740. vertices << mat * vec3(y, 0.f, x);
  741. }
  742. static int const trilist[] =
  743. {
  744. 0, 1, 2, 2, 4, 6, 3, 8, 1, 9, 4, 8,
  745. 7, 0, 5, 7, 11, 3, 10, 5, 6, 10, 9, 11,
  746. 0, 3, 1, 7, 3, 0, 1, 4, 2, 8, 4, 1,
  747. 2, 5, 0, 6, 5, 2, 6, 9, 10, 4, 9, 6,
  748. 7, 10, 11, 5, 10, 7, 8, 11, 9, 3, 11, 8
  749. };
  750. for (unsigned i = 0; i < sizeof(trilist) / sizeof(*trilist); i += 3)
  751. {
  752. vec3 const &a = vertices[trilist[i]];
  753. vec3 const &b = vertices[trilist[i + 1]];
  754. vec3 const &c = vertices[trilist[i + 2]];
  755. vec3 const vb = 1.f / ndivisions * (b - a);
  756. vec3 const vc = 1.f / ndivisions * (c - a);
  757. int line = ndivisions + 1;
  758. for (int v = 0, x = 0, y = 0; x < ndivisions + 1; v++)
  759. {
  760. vec3 p[] = { a + x * vb + y * vc,
  761. p[0] + vb,
  762. p[0] + vc,
  763. p[0] + vb + vc };
  764. /* FIXME: when we normalise here, we get a volume that is slightly
  765. * smaller than the sphere of radius 1, since we are not using
  766. * the midradius. */
  767. for (int k = 0; k < 4; k++)
  768. p[k] = normalize(p[k]) * r;
  769. /* If this is a capsule, grow in the Z direction */
  770. if (h > 0.f)
  771. {
  772. for (int k = 0; k < 4; k++)
  773. p[k].y += (p[k].y > 0.f) ? 0.5f * h : -0.5f * h;
  774. }
  775. /* Add zero, one or two triangles */
  776. if (y < line - 1)
  777. {
  778. AddVertex(p[0]);
  779. AddVertex(p[1]);
  780. AddVertex(p[2]);
  781. AppendTriangle(0, 2, 1, m_vert.Count() - 3);
  782. }
  783. if (y < line - 2)
  784. {
  785. AddVertex(p[1]);
  786. AddVertex(p[3]);
  787. AddVertex(p[2]);
  788. AppendTriangle(0, 2, 1, m_vert.Count() - 3);
  789. }
  790. y++;
  791. if (y == line)
  792. {
  793. x++;
  794. y = 0;
  795. line--;
  796. }
  797. }
  798. }
  799. ComputeNormals(ibase, m_indices.Count() - ibase);
  800. }
  801. void EasyMesh::AppendSphere(int ndivisions, vec3 const &size)
  802. {
  803. OpenBrace();
  804. AppendCapsule(ndivisions, 0.f, 1.f);
  805. Scale(size);
  806. CloseBrace();
  807. }
  808. void EasyMesh::AppendTorus(int ndivisions, float r1, float r2)
  809. {
  810. int ibase = m_indices.Count();
  811. int nidiv = ndivisions; /* Cross-section */
  812. int njdiv = ndivisions; /* Full circumference */
  813. for (int j = 0; j < njdiv; j++)
  814. for (int i = 0; i < 2 * nidiv; i++)
  815. {
  816. for (int di = 0; di < 2; di++)
  817. for (int dj = 0; dj < 2; dj++)
  818. {
  819. int i2 = (i + di) % nidiv;
  820. int j2 = (j + dj) % njdiv;
  821. float x = 0.5f * (r1 + r2) + 0.5f * (r2 - r1) * (float)lol::cos(2.0 * M_PI * i2 / nidiv);
  822. float y = 0.5f * (r2 - r1) * (float)lol::sin(2.0 * M_PI * i2 / nidiv);
  823. float z = 0.0f;
  824. float ca = (float)lol::cos(2.0 * M_PI * j2 / njdiv);
  825. float sa = (float)lol::sin(2.0 * M_PI * j2 / njdiv);
  826. float x2 = x * ca - z * sa;
  827. float z2 = z * ca + x * sa;
  828. AddVertex(vec3(x2, y, z2));
  829. }
  830. AppendTriangle(0, 2, 3, m_vert.Count() - 4);
  831. AppendTriangle(0, 3, 1, m_vert.Count() - 4);
  832. }
  833. ComputeNormals(ibase, m_indices.Count() - ibase);
  834. }
  835. void EasyMesh::AppendBox(vec3 const &size, float chamf)
  836. {
  837. AppendBox(size, chamf, false);
  838. }
  839. void EasyMesh::AppendSmoothChamfBox(vec3 const &size, float chamf)
  840. {
  841. AppendBox(size, chamf, true);
  842. }
  843. void EasyMesh::AppendFlatChamfBox(vec3 const &size, float chamf)
  844. {
  845. AppendBox(size, chamf, false);
  846. }
  847. void EasyMesh::AppendBox(vec3 const &size, float chamf, bool smooth)
  848. {
  849. if (chamf < 0.0f)
  850. {
  851. AppendBox(size + vec3(chamf * 2.0f), -chamf, smooth);
  852. return;
  853. }
  854. int vbase = m_vert.Count();
  855. int ibase = m_indices.Count();
  856. vec3 d = size * 0.5f;
  857. AddVertex(vec3(-d.x, -d.y, -d.z - chamf));
  858. AddVertex(vec3(-d.x, +d.y, -d.z - chamf));
  859. AddVertex(vec3(+d.x, +d.y, -d.z - chamf));
  860. AddVertex(vec3(+d.x, -d.y, -d.z - chamf));
  861. AddVertex(vec3(-d.x - chamf, -d.y, +d.z));
  862. AddVertex(vec3(-d.x - chamf, +d.y, +d.z));
  863. AddVertex(vec3(-d.x - chamf, +d.y, -d.z));
  864. AddVertex(vec3(-d.x - chamf, -d.y, -d.z));
  865. AddVertex(vec3(+d.x, -d.y, +d.z + chamf));
  866. AddVertex(vec3(+d.x, +d.y, +d.z + chamf));
  867. AddVertex(vec3(-d.x, +d.y, +d.z + chamf));
  868. AddVertex(vec3(-d.x, -d.y, +d.z + chamf));
  869. AddVertex(vec3(+d.x + chamf, -d.y, -d.z));
  870. AddVertex(vec3(+d.x + chamf, +d.y, -d.z));
  871. AddVertex(vec3(+d.x + chamf, +d.y, +d.z));
  872. AddVertex(vec3(+d.x + chamf, -d.y, +d.z));
  873. AddVertex(vec3(-d.x, -d.y - chamf, +d.z));
  874. AddVertex(vec3(-d.x, -d.y - chamf, -d.z));
  875. AddVertex(vec3(+d.x, -d.y - chamf, -d.z));
  876. AddVertex(vec3(+d.x, -d.y - chamf, +d.z));
  877. AddVertex(vec3(-d.x, +d.y + chamf, -d.z));
  878. AddVertex(vec3(-d.x, +d.y + chamf, +d.z));
  879. AddVertex(vec3(+d.x, +d.y + chamf, +d.z));
  880. AddVertex(vec3(+d.x, +d.y + chamf, -d.z));
  881. /* The 6 quads on each side of the box */
  882. for (int i = 0; i < 24; i += 4)
  883. AppendQuad(i, i + 1, i + 2, i + 3, vbase);
  884. ComputeNormals(ibase, m_indices.Count() - ibase);
  885. ibase = m_indices.Count();
  886. /* The 8 quads at each edge of the box */
  887. if (chamf)
  888. {
  889. static int const quadlist[48] =
  890. {
  891. 0, 3, 18, 17, 4, 7, 17, 16, 8, 11, 16, 19, 12, 15, 19, 18,
  892. 2, 1, 20, 23, 6, 5, 21, 20, 10, 9, 22, 21, 14, 13, 23, 22,
  893. 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2,
  894. };
  895. for (int i = 0; i < 48; i += 4)
  896. {
  897. if (smooth)
  898. AppendQuad(quadlist[i], quadlist[i + 1],
  899. quadlist[i + 2], quadlist[i + 3], vbase);
  900. else
  901. AppendQuadDuplicateVerts(quadlist[i], quadlist[i + 1],
  902. quadlist[i + 2], quadlist[i + 3], vbase);
  903. }
  904. }
  905. /* The 8 triangles at each corner of the box */
  906. if (chamf)
  907. {
  908. static int const trilist[24] =
  909. {
  910. 3, 12, 18, 15, 8, 19, 11, 4, 16, 7, 0, 17,
  911. 2, 23, 13, 14, 22, 9, 10, 21, 5, 6, 20, 1,
  912. };
  913. for (int i = 0; i < 24; i += 3)
  914. {
  915. if (smooth)
  916. AppendTriangle(trilist[i], trilist[i + 1],
  917. trilist[i + 2], vbase);
  918. else
  919. AppendTriangleDuplicateVerts(trilist[i], trilist[i + 1],
  920. trilist[i + 2], vbase);
  921. }
  922. }
  923. if (!smooth)
  924. ComputeNormals(ibase, m_indices.Count() - ibase);
  925. }
  926. void EasyMesh::AppendStar(int nbranches, float r1, float r2,
  927. int fade, int fade2)
  928. {
  929. int vbase = m_vert.Count();
  930. AddVertex(vec3(0.f, 0.f, 0.f));
  931. mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f);
  932. vec3 p1(r1, 0.f, 0.f), p2(r2, 0.f, 0.f);
  933. p2 = rotmat * p2;
  934. rotmat = rotmat * rotmat;
  935. for (int i = 0; i < nbranches; i++)
  936. {
  937. AddVertex(p1);
  938. if (fade2)
  939. SetCurVertColor(m_color2);
  940. AddVertex(p2);
  941. if (fade)
  942. SetCurVertColor(m_color2);
  943. AppendQuad(0, 2 * i + 1, 2 * i + 2, (2 * i + 3) % (2 * nbranches),
  944. vbase);
  945. p1 = rotmat * p1;
  946. p2 = rotmat * p2;
  947. }
  948. }
  949. void EasyMesh::AppendExpandedStar(int nbranches, float r1,
  950. float r2, float extrar)
  951. {
  952. int vbase = m_vert.Count();
  953. AddVertex(vec3(0.f, 0.f, 0.f));
  954. mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f);
  955. vec3 p1(r1, 0.f, 0.f), p2(r2, 0.f, 0.f),
  956. p3(r1 + extrar, 0.f, 0.f), p4(r2 + extrar, 0.f, 0.f);;
  957. p2 = rotmat * p2;
  958. p4 = rotmat * p4;
  959. rotmat = rotmat * rotmat;
  960. for (int i = 0; i < nbranches; i++)
  961. {
  962. AddVertex(p1);
  963. AddVertex(p2);
  964. AddVertex(p3); SetCurVertColor(m_color2);
  965. AddVertex(p4); SetCurVertColor(m_color2);
  966. int j = (i + 1) % nbranches;
  967. AppendQuad(0, 4 * i + 1, 4 * i + 2, 4 * j + 1, vbase);
  968. AppendQuad(4 * i + 1, 4 * i + 3, 4 * i + 4, 4 * i + 2, vbase);
  969. AppendQuad(4 * j + 1, 4 * i + 2, 4 * i + 4, 4 * j + 3, vbase);
  970. p1 = rotmat * p1;
  971. p2 = rotmat * p2;
  972. p3 = rotmat * p3;
  973. p4 = rotmat * p4;
  974. }
  975. }
  976. void EasyMesh::AppendDisc(int nsides, float r, int fade)
  977. {
  978. int vbase = m_vert.Count();
  979. AddVertex(vec3(0.f, 0.f, 0.f));
  980. mat3 rotmat = mat3::rotate(360.0f / nsides, 0.f, 1.f, 0.f);
  981. vec3 p1(r, 0.f, 0.f);
  982. for (int i = 0; i < nsides; i++)
  983. {
  984. AddVertex(p1);
  985. if (fade)
  986. SetCurVertColor(m_color2);
  987. AppendTriangle(0, i + 1, ((i + 1) % nsides) + 1, vbase);
  988. p1 = rotmat * p1;
  989. }
  990. }
  991. void EasyMesh::AppendSimpleTriangle(float size, int fade)
  992. {
  993. mat3 m = mat3::rotate(120.f, 0.f, 1.f, 0.f);
  994. vec3 p(0.f, 0.f, size);
  995. AddVertex(p);
  996. p = m * p;
  997. AddVertex(p);
  998. if (fade)
  999. SetCurVertColor(m_color2);
  1000. p = m * p;
  1001. AddVertex(p);
  1002. if (fade)
  1003. SetCurVertColor(m_color2);
  1004. AppendTriangle(0, 1, 2, m_vert.Count() - 3);
  1005. }
  1006. void EasyMesh::AppendSimpleQuad(float size, int fade)
  1007. {
  1008. AppendSimpleQuad(vec2(size * .5f), vec2(size * -.5f), 0.f, fade);
  1009. }
  1010. void EasyMesh::AppendSimpleQuad(vec2 p1, vec2 p2, float z, int fade)
  1011. {
  1012. AddVertex(vec3(p2.x, z, -p1.y));
  1013. AddVertex(vec3(p2.x, z, -p2.y));
  1014. AddVertex(vec3(p1.x, z, -p2.y));
  1015. if (fade)
  1016. SetCurVertColor(m_color2);
  1017. AddVertex(vec3(p1.x, z, -p1.y));
  1018. if (fade)
  1019. SetCurVertColor(m_color2);
  1020. AppendQuad(3, 2, 1, 0, m_vert.Count() - 4);
  1021. ComputeNormals(m_indices.Count() - 6, 6);
  1022. }
  1023. void EasyMesh::AppendCog(int nbsides, float h, float r10, float r20,
  1024. float r1, float r2, float r12, float r22,
  1025. float sidemul, int offset)
  1026. {
  1027. int ibase = m_indices.Count();
  1028. int vbase = m_vert.Count();
  1029. /* FIXME: enforce this some other way */
  1030. if (r12 < 0)
  1031. h = -h;
  1032. mat3 rotmat = mat3::rotate(180.0f / nbsides, 0.f, 1.f, 0.f);
  1033. mat3 smat1 = mat3::rotate(sidemul * 180.0f / nbsides, 0.f, 1.f, 0.f);
  1034. mat3 smat2 = mat3::rotate(sidemul * -360.0f / nbsides, 0.f, 1.f, 0.f);
  1035. vec3 p[12];
  1036. p[0] = vec3(r10, h * .5f, 0.f);
  1037. p[1] = rotmat * p[0];
  1038. p[2] = vec3(r1, h * .5f, 0.f);
  1039. p[3] = rotmat * p[2];
  1040. p[4] = smat1 * (rotmat * vec3(r1 + r12, h * .5f, 0.f));
  1041. p[5] = smat2 * (rotmat * p[4]);
  1042. p[6] = vec3(r20, h * -.5f, 0.f);
  1043. p[7] = rotmat * p[6];
  1044. p[8] = vec3(r2, h * -.5f, 0.f);
  1045. p[9] = rotmat * p[8];
  1046. p[10] = smat1 * (rotmat * vec3(r2 + r22, h * -.5f, 0.f));
  1047. p[11] = smat2 * (rotmat * p[10]);
  1048. if (offset & 1)
  1049. for (int n = 0; n < 12; n++)
  1050. p[n] = rotmat * p[n];
  1051. rotmat = rotmat * rotmat;
  1052. for (int i = 0; i < nbsides; i++)
  1053. {
  1054. /* Each vertex will share three faces, so three different
  1055. * normals, therefore we add each vertex three times. */
  1056. for (int n = 0; n < 3 * 12; n++)
  1057. {
  1058. AddVertex(p[n / 3]);
  1059. if (n / 3 >= 6)
  1060. SetCurVertColor(m_color2);
  1061. }
  1062. int j = 3 * 12 * i, k = 3 * 12 * ((i + 1) % nbsides);
  1063. /* The top and bottom faces */
  1064. AppendQuad(j, j + 6, j + 9, j + 3, vbase);
  1065. AppendQuad(j + 21, j + 27, j + 24, j + 18, vbase);
  1066. AppendQuad(j + 3, j + 9, k + 6, k, vbase);
  1067. AppendQuad(k + 18, k + 24, j + 27, j + 21, vbase);
  1068. AppendQuad(j + 9, j + 12, j + 15, k + 6, vbase);
  1069. AppendQuad(k + 24, j + 33, j + 30, j + 27, vbase);
  1070. /* The inner side quads */
  1071. AppendQuad(j + 1, j + 4, j + 22, j + 19, vbase);
  1072. AppendQuad(j + 5, k + 2, k + 20, j + 23, vbase);
  1073. /* The outer side quads */
  1074. AppendQuad(j + 10, j + 7, j + 25, j + 28, vbase);
  1075. AppendQuad(j + 13, j + 11, j + 29, j + 31, vbase);
  1076. AppendQuad(j + 16, j + 14, j + 32, j + 34, vbase);
  1077. AppendQuad(k + 8, j + 17, j + 35, k + 26, vbase);
  1078. for (int n = 0; n < 12; n++)
  1079. p[n] = rotmat * p[n];
  1080. }
  1081. ComputeNormals(ibase, m_indices.Count() - ibase);
  1082. }
  1083. void EasyMesh::Chamfer(float f)
  1084. {
  1085. int vlen = m_vert.Count() - m_cursors.Last().m1;
  1086. int ilen = m_indices.Count() - m_cursors.Last().m2;
  1087. /* Step 1: enumerate all faces. This is done by merging triangles
  1088. * that are coplanar and share an edge. */
  1089. int *triangle_classes = new int[ilen / 3];
  1090. for (int i = 0; i < ilen / 3; i++)
  1091. triangle_classes[i] = -1;
  1092. for (int i = 0; i < ilen / 3; i++)
  1093. {
  1094. }
  1095. /* Fun shit: reduce all triangles */
  1096. int *vertices = new int[vlen];
  1097. memset(vertices, 0, vlen * sizeof(int));
  1098. for (int i = 0; i < ilen; i++)
  1099. vertices[m_indices[i]]++;
  1100. for (int i = 0; i < ilen / 3; i++)
  1101. {
  1102. #if 0
  1103. if (vertices[m_indices[i * 3]] > 1)
  1104. continue;
  1105. if (vertices[m_indices[i * 3 + 1]] > 1)
  1106. continue;
  1107. if (vertices[m_indices[i * 3 + 2]] > 1)
  1108. continue;
  1109. #endif
  1110. vec3 bary = 1.f / 3.f * (m_vert[m_indices[i * 3]].m1 +
  1111. m_vert[m_indices[i * 3 + 1]].m1 +
  1112. m_vert[m_indices[i * 3 + 2]].m1);
  1113. for (int k = 0; k < 3; k++)
  1114. {
  1115. vec3 &p = m_vert[m_indices[i * 3 + k]].m1;
  1116. p -= normalize(p - bary) * f;
  1117. }
  1118. }
  1119. }
  1120. } /* namespace lol */