725 rindas
20 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2012 Cédric Lecacheur <jordx@free.fr>
  6. // (c) 2009-2012 Benjamin 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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  11. //
  12. //
  13. // The EasyMesh class
  14. // ------------------
  15. //
  16. #if defined HAVE_CONFIG_H
  17. # include "config.h"
  18. #endif
  19. #include "core.h"
  20. #include "easymesh/easymesh-compiler.h"
  21. extern char const *lolfx_shiny;
  22. namespace lol
  23. {
  24. EasyMesh::EasyMesh()
  25. : m_color(0), m_color2(0)
  26. {
  27. m_cursors.Push(0, 0);
  28. }
  29. bool EasyMesh::Compile(char const *command)
  30. {
  31. EasyMeshCompiler mc(*this);
  32. return mc.ParseString(command);
  33. }
  34. void EasyMesh::OpenBrace()
  35. {
  36. m_cursors.Push(m_vert.Count(), m_indices.Count());
  37. }
  38. void EasyMesh::CloseBrace()
  39. {
  40. m_cursors.Pop();
  41. }
  42. void EasyMesh::MeshConvert()
  43. {
  44. m_gpu.shader = Shader::Create(lolfx_shiny);
  45. m_gpu.modelview = m_gpu.shader->GetUniformLocation("in_ModelView");
  46. m_gpu.proj = m_gpu.shader->GetUniformLocation("in_Proj");
  47. m_gpu.normalmat = m_gpu.shader->GetUniformLocation("in_NormalMat");
  48. m_gpu.damage = m_gpu.shader->GetUniformLocation("in_Damage");
  49. m_gpu.coord = m_gpu.shader->GetAttribLocation("in_Vertex",
  50. VertexUsage::Position, 0);
  51. m_gpu.norm = m_gpu.shader->GetAttribLocation("in_Normal",
  52. VertexUsage::Normal, 0);
  53. m_gpu.color = m_gpu.shader->GetAttribLocation("in_Color",
  54. VertexUsage::Color, 0);
  55. m_gpu.vdecl = new VertexDeclaration(
  56. VertexStream<vec3,vec3,u8vec4>(VertexUsage::Position,
  57. VertexUsage::Normal,
  58. VertexUsage::Color));
  59. Array<vec3,vec3,u8vec4> vertexlist;
  60. for (int i = 0; i < m_vert.Count(); i++)
  61. vertexlist.Push(m_vert[i].m1,
  62. m_vert[i].m2,
  63. (u8vec4)(m_vert[i].m3 * 255.f));
  64. Array<uint16_t> indexlist;
  65. for (int i = 0; i < m_indices.Count(); i += 3)
  66. {
  67. indexlist << m_indices[i + 0];
  68. indexlist << m_indices[i + 1];
  69. indexlist << m_indices[i + 2];
  70. }
  71. m_gpu.vbo = new VertexBuffer(vertexlist.Bytes());
  72. void *mesh = m_gpu.vbo->Lock(0, 0);
  73. memcpy(mesh, &vertexlist[0], vertexlist.Bytes());
  74. m_gpu.vbo->Unlock();
  75. m_gpu.ibo = new IndexBuffer(indexlist.Bytes());
  76. void *indices = m_gpu.ibo->Lock(0, 0);
  77. memcpy(indices, &indexlist[0], indexlist.Bytes());
  78. m_gpu.ibo->Unlock();
  79. m_gpu.vertexcount = vertexlist.Count();
  80. m_gpu.indexcount = indexlist.Count();
  81. }
  82. void EasyMesh::Render(mat4 const &model, float damage)
  83. {
  84. mat4 modelview = Scene::GetDefault()->GetViewMatrix() * model;
  85. mat3 normalmat = transpose(inverse(mat3(modelview)));
  86. m_gpu.shader->Bind();
  87. m_gpu.shader->SetUniform(m_gpu.modelview, modelview);
  88. m_gpu.shader->SetUniform(m_gpu.proj, Scene::GetDefault()->GetProjMatrix());
  89. m_gpu.shader->SetUniform(m_gpu.normalmat, normalmat);
  90. m_gpu.shader->SetUniform(m_gpu.damage, damage);
  91. m_gpu.vdecl->SetStream(m_gpu.vbo, m_gpu.coord, m_gpu.norm, m_gpu.color);
  92. m_gpu.vdecl->Bind();
  93. m_gpu.ibo->Bind();
  94. m_gpu.vdecl->DrawIndexedElements(MeshPrimitive::Triangles,
  95. 0, 0, m_gpu.vertexcount,
  96. 0, m_gpu.indexcount / 3);
  97. m_gpu.ibo->Unbind();
  98. m_gpu.vdecl->Unbind();
  99. }
  100. void EasyMesh::SetCurColor(vec4 const &color)
  101. {
  102. m_color = color;
  103. }
  104. void EasyMesh::SetCurColor2(vec4 const &color)
  105. {
  106. m_color2 = color;
  107. }
  108. void EasyMesh::AddVertex(vec3 const &coord)
  109. {
  110. m_vert.Push(coord, vec3(0.f, 1.f, 0.f), m_color);
  111. }
  112. void EasyMesh::AddDuplicateVertex(int i)
  113. {
  114. m_vert.Push(m_vert[i].m1, vec3(0.f, 1.f, 0.f), m_vert[i].m3);
  115. }
  116. void EasyMesh::AppendQuad(int i1, int i2, int i3, int i4, int base)
  117. {
  118. m_indices << base + i1;
  119. m_indices << base + i2;
  120. m_indices << base + i3;
  121. m_indices << base + i4;
  122. m_indices << base + i1;
  123. m_indices << base + i3;
  124. }
  125. void EasyMesh::AppendQuadDuplicateVerts(int i1, int i2, int i3, int i4, int base)
  126. {
  127. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  128. m_indices << m_vert.Count(); AddDuplicateVertex(base + i2);
  129. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  130. m_indices << m_vert.Count(); AddDuplicateVertex(base + i4);
  131. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  132. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  133. }
  134. void EasyMesh::AppendTriangle(int i1, int i2, int i3, int base)
  135. {
  136. m_indices << base + i1;
  137. m_indices << base + i2;
  138. m_indices << base + i3;
  139. }
  140. void EasyMesh::AppendTriangleDuplicateVerts(int i1, int i2, int i3, int base)
  141. {
  142. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  143. m_indices << m_vert.Count(); AddDuplicateVertex(base + i2);
  144. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  145. }
  146. void EasyMesh::ComputeNormals(int start, int vcount)
  147. {
  148. for (int i = 0; i < vcount; i += 3)
  149. {
  150. vec3 v0 = m_vert[m_indices[start + i + 2]].m1
  151. - m_vert[m_indices[start + i + 0]].m1;
  152. vec3 v1 = m_vert[m_indices[start + i + 1]].m1
  153. - m_vert[m_indices[start + i + 0]].m1;
  154. vec3 n = normalize(cross(v1, v0));
  155. for (int j = 0; j < 3; j++)
  156. m_vert[m_indices[start + i + j]].m2 = n;
  157. }
  158. }
  159. void EasyMesh::SetVertColor(vec4 const &color)
  160. {
  161. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  162. m_vert[i].m3 = color;
  163. }
  164. void EasyMesh::SetCurVertNormal(vec3 const &normal)
  165. {
  166. m_vert[m_vert.Count() - 1].m2 = normal;
  167. }
  168. void EasyMesh::SetCurVertColor(vec4 const &color)
  169. {
  170. m_vert[m_vert.Count() - 1].m3 = color;
  171. }
  172. void EasyMesh::Translate(vec3 const &v)
  173. {
  174. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  175. m_vert[i].m1 += v;
  176. }
  177. void EasyMesh::RotateX(float t) { Rotate(t, vec3(1, 0, 0)); }
  178. void EasyMesh::RotateY(float t) { Rotate(t, vec3(0, 1, 0)); }
  179. void EasyMesh::RotateZ(float t) { Rotate(t, vec3(0, 0, 1)); }
  180. void EasyMesh::Rotate(float t, vec3 const &axis)
  181. {
  182. mat3 m = mat3::rotate(t, axis);
  183. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  184. {
  185. m_vert[i].m1 = m * m_vert[i].m1;
  186. m_vert[i].m2 = m * m_vert[i].m2;
  187. }
  188. }
  189. void EasyMesh::TaperX(float y, float z, float xoff)
  190. {
  191. /* FIXME: this code breaks normals! */
  192. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  193. {
  194. m_vert[i].m1.y *= 1.f + (y * m_vert[i].m1.x + xoff);
  195. m_vert[i].m1.z *= 1.f + (z * m_vert[i].m1.x + xoff);
  196. }
  197. }
  198. void EasyMesh::TaperY(float x, float z, float yoff)
  199. {
  200. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  201. {
  202. m_vert[i].m1.x *= 1.f + (x * m_vert[i].m1.y + yoff);
  203. m_vert[i].m1.z *= 1.f + (z * m_vert[i].m1.y + yoff);
  204. }
  205. }
  206. void EasyMesh::TaperZ(float x, float y, float zoff)
  207. {
  208. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  209. {
  210. m_vert[i].m1.x *= 1.f + (x * m_vert[i].m1.z + zoff);
  211. m_vert[i].m1.y *= 1.f + (y * m_vert[i].m1.z + zoff);
  212. }
  213. }
  214. void EasyMesh::Scale(vec3 const &s)
  215. {
  216. vec3 const invs = vec3(1) / s;
  217. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  218. {
  219. m_vert[i].m1 *= s;
  220. m_vert[i].m2 = normalize(m_vert[i].m2 * invs);
  221. }
  222. /* Flip winding if the scaling involves mirroring */
  223. if (s.x * s.y * s.z < 0)
  224. {
  225. for (int i = m_cursors.Last().m2; i < m_indices.Count(); i += 3)
  226. {
  227. uint16_t tmp = m_indices[i + 0];
  228. m_indices[i + 0] = m_indices[i + 1];
  229. m_indices[i + 1] = tmp;
  230. }
  231. }
  232. }
  233. void EasyMesh::MirrorX() { DupAndScale(vec3(-1, 1, 1)); }
  234. void EasyMesh::MirrorY() { DupAndScale(vec3(1, -1, 1)); }
  235. void EasyMesh::MirrorZ() { DupAndScale(vec3(1, 1, -1)); }
  236. void EasyMesh::DupAndScale(vec3 const &s)
  237. {
  238. int vlen = m_vert.Count() - m_cursors.Last().m1;
  239. int tlen = m_indices.Count() - m_cursors.Last().m2;
  240. for (int i = 0; i < vlen; i++)
  241. m_vert << m_vert[m_cursors.Last().m1++];
  242. for (int i = 0; i < tlen; i++)
  243. m_indices << m_indices[m_cursors.Last().m2++] + vlen;
  244. Scale(s);
  245. m_cursors.Last().m1 -= vlen;
  246. m_cursors.Last().m2 -= tlen;
  247. }
  248. void EasyMesh::AppendCylinder(int nsides, float h, float r1, float r2,
  249. int dualside, int smooth)
  250. {
  251. int vbase = m_vert.Count();
  252. mat3 rotmat = mat3::rotate(360.0f / nsides, 0.f, 1.f, 0.f);
  253. vec3 p1(r1, -h * .5f, 0.f), p2(r2, h * .5f, 0.f), n;
  254. /* Construct normal */
  255. if (r2 != .0f)
  256. n = vec3(r2, h * .5f, 0.f);
  257. else
  258. n = vec3(r1, h * .5f, 0.f);
  259. n.y = r1 * (r1 - r2) / h;
  260. if (!smooth)
  261. n = mat3::rotate(180.0f / nsides, 0.f, 1.f, 0.f) * n;
  262. n = normalize(n);
  263. /* FIXME: normals should be flipped in two-sided mode, but that
  264. * means duplicating the vertices again... */
  265. for (int i = 0; i < nsides; i++)
  266. {
  267. AddVertex(p1); SetCurVertNormal(n);
  268. AddVertex(p2); SetCurVertNormal(n);
  269. SetCurVertColor(m_color2);
  270. if (smooth)
  271. {
  272. int j = (i + 1) % nsides;
  273. AppendQuad(j * 2, j * 2 + 1, i * 2 + 1, i * 2, vbase);
  274. if (dualside)
  275. AppendQuad(i * 2, i * 2 + 1, j * 2 + 1, j * 2, vbase);
  276. }
  277. p1 = rotmat * p1;
  278. p2 = rotmat * p2;
  279. if (!smooth)
  280. {
  281. AddVertex(p1); SetCurVertNormal(n);
  282. AddVertex(p2); SetCurVertNormal(n);
  283. SetCurVertColor(m_color2);
  284. AppendQuad(i * 4 + 2, i * 4 + 3, i * 4 + 1, i * 4, vbase);
  285. if (dualside)
  286. AppendQuad(i * 4, i * 4 + 1, i * 4 + 3, i * 4 + 2, vbase);
  287. }
  288. n = rotmat * n;
  289. }
  290. }
  291. void EasyMesh::AppendSphere(int ndivisions, vec3 const &size)
  292. {
  293. ndivisions *= 2;
  294. int ibase = m_indices.Count();
  295. int vbase = m_vert.Count();
  296. vec3 d = size * 0.5f;
  297. float const pi = acos(-1.0f);
  298. Array<vec2> table;
  299. for (int i = 0; i <= ndivisions; i++)
  300. table.Push(vec2(sin(pi * 2 / ndivisions * i) + 1e-5f,
  301. cos(pi * 2 / ndivisions * i) + 1e-5f));
  302. for (int j = 0; j <= ndivisions / 2; j++)
  303. for (int i = 0; i < ndivisions; i++)
  304. {
  305. int j2 = j + 1;
  306. int i2 = (i + 1) % ndivisions;
  307. AddVertex(d * vec3(table[i], 1.0f) * table[j].xxy);
  308. AddVertex(d * vec3(table[i2], 1.0f) * table[j].xxy);
  309. AddVertex(d * vec3(table[i2], 1.0f) * table[j2].xxy);
  310. AddVertex(d * vec3(table[i], 1.0f) * table[j2].xxy);
  311. }
  312. for (int i = vbase; i < m_vert.Count(); i += 4)
  313. AppendQuad(0, 1, 2, 3, i);
  314. ComputeNormals(ibase, m_indices.Count() - ibase);
  315. }
  316. void EasyMesh::AppendBox(vec3 const &size, float chamf)
  317. {
  318. AppendBox(size, chamf, false);
  319. }
  320. void EasyMesh::AppendSmoothChamfBox(vec3 const &size, float chamf)
  321. {
  322. AppendBox(size, chamf, true);
  323. }
  324. void EasyMesh::AppendFlatChamfBox(vec3 const &size, float chamf)
  325. {
  326. AppendBox(size, chamf, false);
  327. }
  328. void EasyMesh::AppendBox(vec3 const &size, float chamf, bool smooth)
  329. {
  330. if (chamf < 0.0f)
  331. {
  332. AppendBox(size + vec3(chamf * 2.0f), -chamf, smooth);
  333. return;
  334. }
  335. int vbase = m_vert.Count();
  336. int ibase = m_indices.Count();
  337. vec3 d = size * 0.5f;
  338. AddVertex(vec3(-d.x, -d.y, -d.z - chamf));
  339. AddVertex(vec3(-d.x, +d.y, -d.z - chamf));
  340. AddVertex(vec3(+d.x, +d.y, -d.z - chamf));
  341. AddVertex(vec3(+d.x, -d.y, -d.z - chamf));
  342. AddVertex(vec3(-d.x - chamf, -d.y, +d.z));
  343. AddVertex(vec3(-d.x - chamf, +d.y, +d.z));
  344. AddVertex(vec3(-d.x - chamf, +d.y, -d.z));
  345. AddVertex(vec3(-d.x - chamf, -d.y, -d.z));
  346. AddVertex(vec3(+d.x, -d.y, +d.z + chamf));
  347. AddVertex(vec3(+d.x, +d.y, +d.z + chamf));
  348. AddVertex(vec3(-d.x, +d.y, +d.z + chamf));
  349. AddVertex(vec3(-d.x, -d.y, +d.z + chamf));
  350. AddVertex(vec3(+d.x + chamf, -d.y, -d.z));
  351. AddVertex(vec3(+d.x + chamf, +d.y, -d.z));
  352. AddVertex(vec3(+d.x + chamf, +d.y, +d.z));
  353. AddVertex(vec3(+d.x + chamf, -d.y, +d.z));
  354. AddVertex(vec3(-d.x, -d.y - chamf, +d.z));
  355. AddVertex(vec3(-d.x, -d.y - chamf, -d.z));
  356. AddVertex(vec3(+d.x, -d.y - chamf, -d.z));
  357. AddVertex(vec3(+d.x, -d.y - chamf, +d.z));
  358. AddVertex(vec3(-d.x, +d.y + chamf, -d.z));
  359. AddVertex(vec3(-d.x, +d.y + chamf, +d.z));
  360. AddVertex(vec3(+d.x, +d.y + chamf, +d.z));
  361. AddVertex(vec3(+d.x, +d.y + chamf, -d.z));
  362. /* The 6 quads on each side of the box */
  363. for (int i = 0; i < 24; i += 4)
  364. AppendQuad(i, i + 1, i + 2, i + 3, vbase);
  365. ComputeNormals(ibase, m_indices.Count() - ibase);
  366. ibase = m_indices.Count();
  367. /* The 8 quads at each edge of the box */
  368. if (chamf)
  369. {
  370. static int const quadlist[48] =
  371. {
  372. 0, 3, 18, 17, 4, 7, 17, 16, 8, 11, 16, 19, 12, 15, 19, 18,
  373. 2, 1, 20, 23, 6, 5, 21, 20, 10, 9, 22, 21, 14, 13, 23, 22,
  374. 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2,
  375. };
  376. for (int i = 0; i < 48; i += 4)
  377. {
  378. if (smooth)
  379. AppendQuad(quadlist[i], quadlist[i + 1],
  380. quadlist[i + 2], quadlist[i + 3], vbase);
  381. else
  382. AppendQuadDuplicateVerts(quadlist[i], quadlist[i + 1],
  383. quadlist[i + 2], quadlist[i + 3], vbase);
  384. }
  385. }
  386. /* The 8 triangles at each corner of the box */
  387. if (chamf)
  388. {
  389. static int const trilist[24] =
  390. {
  391. 3, 12, 18, 15, 8, 19, 11, 4, 16, 7, 0, 17,
  392. 2, 23, 13, 14, 22, 9, 10, 21, 5, 6, 20, 1,
  393. };
  394. for (int i = 0; i < 24; i += 3)
  395. {
  396. if (smooth)
  397. AppendTriangle(trilist[i], trilist[i + 1],
  398. trilist[i + 2], vbase);
  399. else
  400. AppendTriangleDuplicateVerts(trilist[i], trilist[i + 1],
  401. trilist[i + 2], vbase);
  402. }
  403. }
  404. if (!smooth)
  405. ComputeNormals(ibase, m_indices.Count() - ibase);
  406. }
  407. void EasyMesh::AppendStar(int nbranches, float r1, float r2,
  408. int fade, int fade2)
  409. {
  410. int vbase = m_vert.Count();
  411. AddVertex(vec3(0.f, 0.f, 0.f));
  412. mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f);
  413. vec3 p1(r1, 0.f, 0.f), p2(r2, 0.f, 0.f);
  414. p2 = rotmat * p2;
  415. rotmat = rotmat * rotmat;
  416. for (int i = 0; i < nbranches; i++)
  417. {
  418. AddVertex(p1);
  419. if (fade2)
  420. SetCurVertColor(m_color2);
  421. AddVertex(p2);
  422. if (fade)
  423. SetCurVertColor(m_color2);
  424. AppendQuad(0, 2 * i + 1, 2 * i + 2, (2 * i + 3) % (2 * nbranches),
  425. vbase);
  426. p1 = rotmat * p1;
  427. p2 = rotmat * p2;
  428. }
  429. }
  430. void EasyMesh::AppendExpandedStar(int nbranches, float r1,
  431. float r2, float extrar)
  432. {
  433. int vbase = m_vert.Count();
  434. AddVertex(vec3(0.f, 0.f, 0.f));
  435. mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f);
  436. vec3 p1(r1, 0.f, 0.f), p2(r2, 0.f, 0.f),
  437. p3(r1 + extrar, 0.f, 0.f), p4(r2 + extrar, 0.f, 0.f);;
  438. p2 = rotmat * p2;
  439. p4 = rotmat * p4;
  440. rotmat = rotmat * rotmat;
  441. for (int i = 0; i < nbranches; i++)
  442. {
  443. AddVertex(p1);
  444. AddVertex(p2);
  445. AddVertex(p3); SetCurVertColor(m_color2);
  446. AddVertex(p4); SetCurVertColor(m_color2);
  447. int j = (i + 1) % nbranches;
  448. AppendQuad(0, 4 * i + 1, 4 * i + 2, 4 * j + 1, vbase);
  449. AppendQuad(4 * i + 1, 4 * i + 3, 4 * i + 4, 4 * i + 2, vbase);
  450. AppendQuad(4 * j + 1, 4 * i + 2, 4 * i + 4, 4 * j + 3, vbase);
  451. p1 = rotmat * p1;
  452. p2 = rotmat * p2;
  453. p3 = rotmat * p3;
  454. p4 = rotmat * p4;
  455. }
  456. }
  457. void EasyMesh::AppendDisc(int nsides, float r, int fade)
  458. {
  459. int vbase = m_vert.Count();
  460. AddVertex(vec3(0.f, 0.f, 0.f));
  461. mat3 rotmat = mat3::rotate(360.0f / nsides, 0.f, 1.f, 0.f);
  462. vec3 p1(r, 0.f, 0.f);
  463. for (int i = 0; i < nsides; i++)
  464. {
  465. AddVertex(p1);
  466. if (fade)
  467. SetCurVertColor(m_color2);
  468. AppendTriangle(0, i + 1, ((i + 1) % nsides) + 1, vbase);
  469. p1 = rotmat * p1;
  470. }
  471. }
  472. void EasyMesh::AppendSimpleTriangle(float size, int fade)
  473. {
  474. mat3 m = mat3::rotate(120.f, 0.f, 1.f, 0.f);
  475. vec3 p(0.f, 0.f, size);
  476. AddVertex(p);
  477. p = m * p;
  478. AddVertex(p);
  479. if (fade)
  480. SetCurVertColor(m_color2);
  481. p = m * p;
  482. AddVertex(p);
  483. if (fade)
  484. SetCurVertColor(m_color2);
  485. AppendTriangle(0, 1, 2, m_vert.Count() - 3);
  486. }
  487. void EasyMesh::AppendSimpleQuad(float size, int fade)
  488. {
  489. AppendSimpleQuad(vec2(size * .5f), vec2(size * -.5f), 0.f, fade);
  490. }
  491. void EasyMesh::AppendSimpleQuad(vec2 p1, vec2 p2, float z, int fade)
  492. {
  493. AddVertex(vec3(p2.x, z, -p1.y));
  494. AddVertex(vec3(p2.x, z, -p2.y));
  495. AddVertex(vec3(p1.x, z, -p2.y));
  496. if (fade)
  497. SetCurVertColor(m_color2);
  498. AddVertex(vec3(p1.x, z, -p1.y));
  499. if (fade)
  500. SetCurVertColor(m_color2);
  501. AppendQuad(3, 2, 1, 0, m_vert.Count() - 4);
  502. ComputeNormals(m_indices.Count() - 6, 6);
  503. }
  504. void EasyMesh::AppendCog(int nbsides, float h, float r1, float r2, float r12,
  505. float r22, float sidemul, int offset)
  506. {
  507. int ibase = m_indices.Count();
  508. int vbase = m_vert.Count();
  509. AddVertex(vec3(0.f, h * .5f, 0.f));
  510. AddVertex(vec3(0.f, h * -.5f, 0.f));
  511. SetCurVertColor(m_color2);
  512. mat3 rotmat = mat3::rotate(180.0f / nbsides, 0.f, 1.f, 0.f);
  513. mat3 smat1 = mat3::rotate(sidemul * 180.0f / nbsides, 0.f, 1.f, 0.f);
  514. mat3 smat2 = mat3::rotate(sidemul * -360.0f / nbsides, 0.f, 1.f, 0.f);
  515. vec3 p[8];
  516. p[0] = vec3(r1, h * .5f, 0.f);
  517. p[1] = rotmat * p[0];
  518. p[2] = smat1 * (rotmat * vec3(r1 + r12, h * .5f, 0.f));
  519. p[3] = smat2 * (rotmat * p[2]);
  520. p[4] = vec3(r2, h * -.5f, 0.f);
  521. p[5] = rotmat * p[4];
  522. p[6] = smat1 * (rotmat * vec3(r2 + r22, h * -.5f, 0.f));
  523. p[7] = smat2 * (rotmat * p[6]);
  524. if (offset & 1)
  525. for (int n = 0; n < 8; n++)
  526. p[n] = rotmat * p[n];
  527. rotmat = rotmat * rotmat;
  528. for (int i = 0; i < nbsides; i++)
  529. {
  530. /* Each vertex will share three faces, so three different
  531. * normals, therefore we add each vertex three times. */
  532. for (int n = 0; n < 24; n++)
  533. {
  534. AddVertex(p[n / 3]);
  535. if (n / 3 >= 4)
  536. SetCurVertColor(m_color2);
  537. }
  538. int j = 24 * i, k = 24 * ((i + 1) % nbsides);
  539. /* The top and bottom faces */
  540. AppendQuad(0, j + 2, j + 5, k + 2, vbase);
  541. AppendQuad(1, k + 14, j + 17, j + 14, vbase);
  542. AppendQuad(j + 5, j + 8, j + 11, k + 2, vbase);
  543. AppendQuad(k + 14, j + 23, j + 20, j + 17, vbase);
  544. /* The side quads */
  545. AppendQuad(j + 6, j + 3, j + 15, j + 18, vbase);
  546. AppendQuad(j + 9, j + 7, j + 19, j + 21, vbase);
  547. AppendQuad(j + 12, j + 10, j + 22, j + 24, vbase);
  548. AppendQuad(k + 4, j + 13, j + 25, k + 16, vbase);
  549. for (int n = 0; n < 8; n++)
  550. p[n] = rotmat * p[n];
  551. }
  552. ComputeNormals(ibase, m_indices.Count() - ibase);
  553. }
  554. void EasyMesh::Chamfer(float f)
  555. {
  556. int vlen = m_vert.Count() - m_cursors.Last().m1;
  557. int ilen = m_indices.Count() - m_cursors.Last().m2;
  558. /* Step 1: enumerate all faces. This is done by merging triangles
  559. * that are coplanar and share an edge. */
  560. int *triangle_classes = new int[ilen / 3];
  561. for (int i = 0; i < ilen / 3; i++)
  562. triangle_classes[i] = -1;
  563. for (int i = 0; i < ilen / 3; i++)
  564. {
  565. }
  566. /* Fun shit: reduce all triangles */
  567. int *vertices = new int[vlen];
  568. memset(vertices, 0, vlen * sizeof(int));
  569. for (int i = 0; i < ilen; i++)
  570. vertices[m_indices[i]]++;
  571. for (int i = 0; i < ilen / 3; i++)
  572. {
  573. #if 0
  574. if (vertices[m_indices[i * 3]] > 1)
  575. continue;
  576. if (vertices[m_indices[i * 3 + 1]] > 1)
  577. continue;
  578. if (vertices[m_indices[i * 3 + 2]] > 1)
  579. continue;
  580. #endif
  581. vec3 bary = 1.f / 3.f * (m_vert[m_indices[i * 3]].m1 +
  582. m_vert[m_indices[i * 3 + 1]].m1 +
  583. m_vert[m_indices[i * 3 + 2]].m1);
  584. for (int k = 0; k < 3; k++)
  585. {
  586. vec3 &p = m_vert[m_indices[i * 3 + k]].m1;
  587. p -= normalize(p - bary) * f;
  588. }
  589. }
  590. }
  591. } /* namespace lol */