Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

906 řádky
26 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. #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()
  55. {
  56. m_gpu.shader = Shader::Create(lolfx_shiny);
  57. m_gpu.modelview = m_gpu.shader->GetUniformLocation("in_ModelView");
  58. m_gpu.view = m_gpu.shader->GetUniformLocation("in_View");
  59. m_gpu.invview = m_gpu.shader->GetUniformLocation("in_Inv_View");
  60. m_gpu.proj = m_gpu.shader->GetUniformLocation("in_Proj");
  61. m_gpu.normalmat = m_gpu.shader->GetUniformLocation("in_NormalMat");
  62. m_gpu.damage = m_gpu.shader->GetUniformLocation("in_Damage");
  63. m_gpu.coord = m_gpu.shader->GetAttribLocation("in_Vertex",
  64. VertexUsage::Position, 0);
  65. m_gpu.norm = m_gpu.shader->GetAttribLocation("in_Normal",
  66. VertexUsage::Normal, 0);
  67. m_gpu.color = m_gpu.shader->GetAttribLocation("in_Color",
  68. VertexUsage::Color, 0);
  69. m_gpu.vdecl = new VertexDeclaration(
  70. VertexStream<vec3,vec3,u8vec4>(VertexUsage::Position,
  71. VertexUsage::Normal,
  72. VertexUsage::Color));
  73. Array<vec3,vec3,u8vec4> vertexlist;
  74. for (int i = 0; i < m_vert.Count(); i++)
  75. vertexlist.Push(m_vert[i].m1,
  76. m_vert[i].m2,
  77. (u8vec4)(m_vert[i].m3 * 255.f));
  78. Array<uint16_t> indexlist;
  79. for (int i = 0; i < m_indices.Count(); i += 3)
  80. {
  81. indexlist << m_indices[i + 0];
  82. indexlist << m_indices[i + 1];
  83. indexlist << m_indices[i + 2];
  84. }
  85. m_gpu.vbo = new VertexBuffer(vertexlist.Bytes());
  86. void *mesh = m_gpu.vbo->Lock(0, 0);
  87. memcpy(mesh, &vertexlist[0], vertexlist.Bytes());
  88. m_gpu.vbo->Unlock();
  89. m_gpu.ibo = new IndexBuffer(indexlist.Bytes());
  90. void *indices = m_gpu.ibo->Lock(0, 0);
  91. memcpy(indices, &indexlist[0], indexlist.Bytes());
  92. m_gpu.ibo->Unlock();
  93. m_gpu.vertexcount = vertexlist.Count();
  94. m_gpu.indexcount = indexlist.Count();
  95. }
  96. void EasyMesh::Render(mat4 const &model, float damage)
  97. {
  98. mat4 modelview = Scene::GetDefault()->GetViewMatrix() * model;
  99. mat3 normalmat = transpose(inverse(mat3(modelview)));
  100. m_gpu.shader->Bind();
  101. m_gpu.shader->SetUniform(m_gpu.modelview, modelview);
  102. m_gpu.shader->SetUniform(m_gpu.view, Scene::GetDefault()->GetViewMatrix());
  103. m_gpu.shader->SetUniform(m_gpu.invview, inverse(Scene::GetDefault()->GetViewMatrix()));
  104. m_gpu.shader->SetUniform(m_gpu.proj, Scene::GetDefault()->GetProjMatrix());
  105. m_gpu.shader->SetUniform(m_gpu.normalmat, normalmat);
  106. m_gpu.shader->SetUniform(m_gpu.damage, damage);
  107. m_gpu.vdecl->Bind();
  108. m_gpu.vdecl->SetStream(m_gpu.vbo, m_gpu.coord, m_gpu.norm, m_gpu.color);
  109. m_gpu.ibo->Bind();
  110. m_gpu.vdecl->DrawIndexedElements(MeshPrimitive::Triangles,
  111. 0, 0, m_gpu.vertexcount,
  112. 0, m_gpu.indexcount);
  113. m_gpu.ibo->Unbind();
  114. m_gpu.vdecl->Unbind();
  115. }
  116. void EasyMesh::ToggleScaleWinding()
  117. {
  118. m_ignore_winding_on_scale = !m_ignore_winding_on_scale;
  119. }
  120. void EasyMesh::SetCurColor(vec4 const &color)
  121. {
  122. m_color = color;
  123. }
  124. void EasyMesh::SetCurColor2(vec4 const &color)
  125. {
  126. m_color2 = color;
  127. }
  128. void EasyMesh::AddVertex(vec3 const &coord)
  129. {
  130. m_vert.Push(coord, vec3(0.f, 1.f, 0.f), m_color);
  131. }
  132. void EasyMesh::AddDuplicateVertex(int i)
  133. {
  134. m_vert.Push(m_vert[i].m1, vec3(0.f, 1.f, 0.f), m_vert[i].m3);
  135. }
  136. void EasyMesh::AppendQuad(int i1, int i2, int i3, int i4, int base)
  137. {
  138. m_indices << base + i1;
  139. m_indices << base + i2;
  140. m_indices << base + i3;
  141. m_indices << base + i4;
  142. m_indices << base + i1;
  143. m_indices << base + i3;
  144. }
  145. void EasyMesh::AppendQuadDuplicateVerts(int i1, int i2, int i3, int i4, int base)
  146. {
  147. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  148. m_indices << m_vert.Count(); AddDuplicateVertex(base + i2);
  149. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  150. m_indices << m_vert.Count(); AddDuplicateVertex(base + i4);
  151. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  152. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  153. }
  154. void EasyMesh::AppendTriangle(int i1, int i2, int i3, int base)
  155. {
  156. m_indices << base + i1;
  157. m_indices << base + i2;
  158. m_indices << base + i3;
  159. }
  160. void EasyMesh::AppendTriangleDuplicateVerts(int i1, int i2, int i3, int base)
  161. {
  162. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  163. m_indices << m_vert.Count(); AddDuplicateVertex(base + i2);
  164. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  165. }
  166. void EasyMesh::ComputeNormals(int start, int vcount)
  167. {
  168. for (int i = 0; i < vcount; i += 3)
  169. {
  170. vec3 v0 = m_vert[m_indices[start + i + 2]].m1
  171. - m_vert[m_indices[start + i + 0]].m1;
  172. vec3 v1 = m_vert[m_indices[start + i + 1]].m1
  173. - m_vert[m_indices[start + i + 0]].m1;
  174. vec3 n = normalize(cross(v1, v0));
  175. for (int j = 0; j < 3; j++)
  176. m_vert[m_indices[start + i + j]].m2 = n;
  177. }
  178. }
  179. void EasyMesh::SetVertColor(vec4 const &color)
  180. {
  181. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  182. m_vert[i].m3 = color;
  183. }
  184. void EasyMesh::SetCurVertNormal(vec3 const &normal)
  185. {
  186. m_vert[m_vert.Count() - 1].m2 = normal;
  187. }
  188. void EasyMesh::SetCurVertColor(vec4 const &color)
  189. {
  190. m_vert[m_vert.Count() - 1].m3 = color;
  191. }
  192. void EasyMesh::Translate(vec3 const &v)
  193. {
  194. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  195. m_vert[i].m1 += v;
  196. }
  197. void EasyMesh::RotateX(float t) { Rotate(t, vec3(1, 0, 0)); }
  198. void EasyMesh::RotateY(float t) { Rotate(t, vec3(0, 1, 0)); }
  199. void EasyMesh::RotateZ(float t) { Rotate(t, vec3(0, 0, 1)); }
  200. void EasyMesh::Rotate(float t, vec3 const &axis)
  201. {
  202. mat3 m = mat3::rotate(t, axis);
  203. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  204. {
  205. m_vert[i].m1 = m * m_vert[i].m1;
  206. m_vert[i].m2 = m * m_vert[i].m2;
  207. }
  208. }
  209. void EasyMesh::RadialJitter(float r)
  210. {
  211. Array<int> Welded;
  212. Welded.Push(-1);
  213. for (int i = m_cursors.Last().m1 + 1; i < m_vert.Count(); i++)
  214. {
  215. int j, k;
  216. for (j = m_cursors.Last().m1, k = 0; j < i; j++, k++)
  217. {
  218. if(Welded[k] < 0)
  219. {
  220. vec3 diff = m_vert[i].m1 - m_vert[j].m1;
  221. if(diff.x > 0.1f || diff.x < -0.1f)
  222. continue;
  223. if(diff.y > 0.1f || diff.y < -0.1f)
  224. continue;
  225. if(diff.z > 0.1f || diff.z < -0.1f)
  226. continue;
  227. break;
  228. }
  229. }
  230. if(j == i)
  231. Welded.Push(-1);
  232. else
  233. Welded.Push(j);
  234. }
  235. int i, j;
  236. for (i = m_cursors.Last().m1, j = 0; i < m_vert.Count(); i++, j++)
  237. {
  238. if(Welded[j] == -1)
  239. m_vert[i].m1 *= 1.0f + RandF(r);
  240. else
  241. m_vert[i].m1 = m_vert[Welded[j]].m1;
  242. }
  243. ComputeNormals(m_cursors.Last().m2, m_indices.Count() - m_cursors.Last().m2);
  244. }
  245. void EasyMesh::TaperX(float y, float z, float xoff)
  246. {
  247. /* FIXME: this code breaks normals! */
  248. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  249. {
  250. m_vert[i].m1.y *= 1.f + (y * m_vert[i].m1.x + xoff);
  251. m_vert[i].m1.z *= 1.f + (z * m_vert[i].m1.x + xoff);
  252. }
  253. }
  254. void EasyMesh::TaperY(float x, float z, float yoff)
  255. {
  256. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  257. {
  258. m_vert[i].m1.x *= 1.f + (x * m_vert[i].m1.y + yoff);
  259. m_vert[i].m1.z *= 1.f + (z * m_vert[i].m1.y + yoff);
  260. }
  261. }
  262. void EasyMesh::TaperZ(float x, float y, float zoff)
  263. {
  264. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  265. {
  266. m_vert[i].m1.x *= 1.f + (x * m_vert[i].m1.z + zoff);
  267. m_vert[i].m1.y *= 1.f + (y * m_vert[i].m1.z + zoff);
  268. }
  269. }
  270. void EasyMesh::Scale(vec3 const &s)
  271. {
  272. vec3 const invs = vec3(1) / s;
  273. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  274. {
  275. m_vert[i].m1 *= s;
  276. m_vert[i].m2 = normalize(m_vert[i].m2 * invs);
  277. }
  278. /* Flip winding if the scaling involves mirroring */
  279. if (!m_ignore_winding_on_scale && s.x * s.y * s.z < 0)
  280. {
  281. for (int i = m_cursors.Last().m2; i < m_indices.Count(); i += 3)
  282. {
  283. uint16_t tmp = m_indices[i + 0];
  284. m_indices[i + 0] = m_indices[i + 1];
  285. m_indices[i + 1] = tmp;
  286. }
  287. }
  288. }
  289. void EasyMesh::MirrorX() { DupAndScale(vec3(-1, 1, 1)); }
  290. void EasyMesh::MirrorY() { DupAndScale(vec3(1, -1, 1)); }
  291. void EasyMesh::MirrorZ() { DupAndScale(vec3(1, 1, -1)); }
  292. void EasyMesh::DupAndScale(vec3 const &s)
  293. {
  294. int vlen = m_vert.Count() - m_cursors.Last().m1;
  295. int tlen = m_indices.Count() - m_cursors.Last().m2;
  296. for (int i = 0; i < vlen; i++)
  297. m_vert << m_vert[m_cursors.Last().m1++];
  298. for (int i = 0; i < tlen; i++)
  299. m_indices << m_indices[m_cursors.Last().m2++] + vlen;
  300. Scale(s);
  301. m_cursors.Last().m1 -= vlen;
  302. m_cursors.Last().m2 -= tlen;
  303. }
  304. void EasyMesh::AppendCylinder(int nsides, float h, float r1, float r2,
  305. int dualside, int smooth)
  306. {
  307. int vbase = m_vert.Count();
  308. mat3 rotmat = mat3::rotate(360.0f / nsides, 0.f, 1.f, 0.f);
  309. vec3 p1(r1, -h * .5f, 0.f), p2(r2, h * .5f, 0.f), n;
  310. /* Construct normal */
  311. if (r2 != .0f)
  312. n = vec3(r2, h * .5f, 0.f);
  313. else
  314. n = vec3(r1, h * .5f, 0.f);
  315. n.y = r1 * (r1 - r2) / h;
  316. if (!smooth)
  317. n = mat3::rotate(180.0f / nsides, 0.f, 1.f, 0.f) * n;
  318. n = normalize(n);
  319. /* FIXME: normals should be flipped in two-sided mode, but that
  320. * means duplicating the vertices again... */
  321. for (int i = 0; i < nsides; i++)
  322. {
  323. AddVertex(p1); SetCurVertNormal(n);
  324. AddVertex(p2); SetCurVertNormal(n);
  325. SetCurVertColor(m_color2);
  326. if (smooth)
  327. {
  328. int j = (i + 1) % nsides;
  329. AppendQuad(j * 2, j * 2 + 1, i * 2 + 1, i * 2, vbase);
  330. if (dualside)
  331. AppendQuad(i * 2, i * 2 + 1, j * 2 + 1, j * 2, vbase);
  332. }
  333. p1 = rotmat * p1;
  334. p2 = rotmat * p2;
  335. if (!smooth)
  336. {
  337. AddVertex(p1); SetCurVertNormal(n);
  338. AddVertex(p2); SetCurVertNormal(n);
  339. SetCurVertColor(m_color2);
  340. AppendQuad(i * 4 + 2, i * 4 + 3, i * 4 + 1, i * 4, vbase);
  341. if (dualside)
  342. AppendQuad(i * 4, i * 4 + 1, i * 4 + 3, i * 4 + 2, vbase);
  343. }
  344. n = rotmat * n;
  345. }
  346. }
  347. void EasyMesh::AppendCapsule(int ndivisions, float h, float r)
  348. {
  349. int ibase = m_indices.Count();
  350. Array<vec3> vertices;
  351. /* FIXME: we don't know how to handle even-divided capsules, so we
  352. * force the count to be odd. */
  353. if (h)
  354. ndivisions |= 1;
  355. /* Fill in the icosahedron vertices, rotating them so that there
  356. * is a vertex at [0 1 0] and [0 -1 0] after normalisation. */
  357. float phi = 0.5f + 0.5f * sqrt(5.f);
  358. mat3 mat = mat3::rotate(asin(1.f / sqrt(2.f + phi)) * (180.f / M_PI),
  359. vec3(0.f, 0.f, 1.f));
  360. for (int i = 0; i < 4; i++)
  361. {
  362. float x = (i & 1) ? 0.5f : -0.5f;
  363. float y = (i & 2) ? phi * 0.5f : phi * -0.5f;
  364. vertices << mat * vec3(x, y, 0.f);
  365. vertices << mat * vec3(0.f, x, y);
  366. vertices << mat * vec3(y, 0.f, x);
  367. }
  368. static int const trilist[] =
  369. {
  370. 0, 1, 2, 2, 4, 6, 3, 8, 1, 9, 4, 8,
  371. 7, 0, 5, 7, 11, 3, 10, 5, 6, 10, 9, 11,
  372. 0, 3, 1, 7, 3, 0, 1, 4, 2, 8, 4, 1,
  373. 2, 5, 0, 6, 5, 2, 6, 9, 10, 4, 9, 6,
  374. 7, 10, 11, 5, 10, 7, 8, 11, 9, 3, 11, 8
  375. };
  376. for (unsigned i = 0; i < sizeof(trilist) / sizeof(*trilist); i += 3)
  377. {
  378. vec3 const &a = vertices[trilist[i]];
  379. vec3 const &b = vertices[trilist[i + 1]];
  380. vec3 const &c = vertices[trilist[i + 2]];
  381. vec3 const vb = 1.f / ndivisions * (b - a);
  382. vec3 const vc = 1.f / ndivisions * (c - a);
  383. int line = ndivisions + 1;
  384. for (int v = 0, x = 0, y = 0; x < ndivisions + 1; v++)
  385. {
  386. vec3 p[] = { a + x * vb + y * vc,
  387. p[0] + vb,
  388. p[0] + vc,
  389. p[0] + vb + vc };
  390. /* FIXME: when we normalise here, we get a volume that is slightly
  391. * smaller than the sphere of radius 1, since we are not using
  392. * the midradius. */
  393. for (int k = 0; k < 4; k++)
  394. p[k] = normalize(p[k]) * r;
  395. /* If this is a capsule, grow in the Z direction */
  396. if (h > 0.f)
  397. {
  398. for (int k = 0; k < 4; k++)
  399. p[k].y += (p[k].y > 0.f) ? 0.5f * h : -0.5f * h;
  400. }
  401. /* Add zero, one or two triangles */
  402. if (y < line - 1)
  403. {
  404. AddVertex(p[0]);
  405. AddVertex(p[1]);
  406. AddVertex(p[2]);
  407. AppendTriangle(0, 2, 1, m_vert.Count() - 3);
  408. }
  409. if (y < line - 2)
  410. {
  411. AddVertex(p[1]);
  412. AddVertex(p[3]);
  413. AddVertex(p[2]);
  414. AppendTriangle(0, 2, 1, m_vert.Count() - 3);
  415. }
  416. y++;
  417. if (y == line)
  418. {
  419. x++;
  420. y = 0;
  421. line--;
  422. }
  423. }
  424. }
  425. ComputeNormals(ibase, m_indices.Count() - ibase);
  426. }
  427. void EasyMesh::AppendSphere(int ndivisions, vec3 const &size)
  428. {
  429. OpenBrace();
  430. AppendCapsule(ndivisions, 0.f, 1.f);
  431. Scale(size);
  432. CloseBrace();
  433. }
  434. void EasyMesh::AppendTorus(int ndivisions, float r1, float r2)
  435. {
  436. int ibase = m_indices.Count();
  437. int nidiv = ndivisions; /* Cross-section */
  438. int njdiv = ndivisions; /* Full circumference */
  439. for (int j = 0; j < njdiv; j++)
  440. for (int i = 0; i < 2 * nidiv; i++)
  441. {
  442. for (int di = 0; di < 2; di++)
  443. for (int dj = 0; dj < 2; dj++)
  444. {
  445. int i2 = (i + di) % nidiv;
  446. int j2 = (j + dj) % njdiv;
  447. float x = 0.5f * (r1 + r2) + 0.5 * (r2 - r1) * lol::cos(2.0 * M_PI * i2 / nidiv);
  448. float y = 0.5f * (r2 - r1) * lol::sin(2.0 * M_PI * i2 / nidiv);
  449. float z = 0.0f;
  450. float ca = lol::cos(2.0 * M_PI * j2 / njdiv);
  451. float sa = lol::sin(2.0 * M_PI * j2 / njdiv);
  452. float x2 = x * ca - z * sa;
  453. float z2 = z * ca + x * sa;
  454. AddVertex(vec3(x2, y, z2));
  455. }
  456. AppendTriangle(0, 2, 3, m_vert.Count() - 4);
  457. AppendTriangle(0, 3, 1, m_vert.Count() - 4);
  458. }
  459. ComputeNormals(ibase, m_indices.Count() - ibase);
  460. }
  461. void EasyMesh::AppendBox(vec3 const &size, float chamf)
  462. {
  463. AppendBox(size, chamf, false);
  464. }
  465. void EasyMesh::AppendSmoothChamfBox(vec3 const &size, float chamf)
  466. {
  467. AppendBox(size, chamf, true);
  468. }
  469. void EasyMesh::AppendFlatChamfBox(vec3 const &size, float chamf)
  470. {
  471. AppendBox(size, chamf, false);
  472. }
  473. void EasyMesh::AppendBox(vec3 const &size, float chamf, bool smooth)
  474. {
  475. if (chamf < 0.0f)
  476. {
  477. AppendBox(size + vec3(chamf * 2.0f), -chamf, smooth);
  478. return;
  479. }
  480. int vbase = m_vert.Count();
  481. int ibase = m_indices.Count();
  482. vec3 d = size * 0.5f;
  483. AddVertex(vec3(-d.x, -d.y, -d.z - chamf));
  484. AddVertex(vec3(-d.x, +d.y, -d.z - chamf));
  485. AddVertex(vec3(+d.x, +d.y, -d.z - chamf));
  486. AddVertex(vec3(+d.x, -d.y, -d.z - chamf));
  487. AddVertex(vec3(-d.x - chamf, -d.y, +d.z));
  488. AddVertex(vec3(-d.x - chamf, +d.y, +d.z));
  489. AddVertex(vec3(-d.x - chamf, +d.y, -d.z));
  490. AddVertex(vec3(-d.x - chamf, -d.y, -d.z));
  491. AddVertex(vec3(+d.x, -d.y, +d.z + chamf));
  492. AddVertex(vec3(+d.x, +d.y, +d.z + chamf));
  493. AddVertex(vec3(-d.x, +d.y, +d.z + chamf));
  494. AddVertex(vec3(-d.x, -d.y, +d.z + chamf));
  495. AddVertex(vec3(+d.x + chamf, -d.y, -d.z));
  496. AddVertex(vec3(+d.x + chamf, +d.y, -d.z));
  497. AddVertex(vec3(+d.x + chamf, +d.y, +d.z));
  498. AddVertex(vec3(+d.x + chamf, -d.y, +d.z));
  499. AddVertex(vec3(-d.x, -d.y - chamf, +d.z));
  500. AddVertex(vec3(-d.x, -d.y - chamf, -d.z));
  501. AddVertex(vec3(+d.x, -d.y - chamf, -d.z));
  502. AddVertex(vec3(+d.x, -d.y - chamf, +d.z));
  503. AddVertex(vec3(-d.x, +d.y + chamf, -d.z));
  504. AddVertex(vec3(-d.x, +d.y + chamf, +d.z));
  505. AddVertex(vec3(+d.x, +d.y + chamf, +d.z));
  506. AddVertex(vec3(+d.x, +d.y + chamf, -d.z));
  507. /* The 6 quads on each side of the box */
  508. for (int i = 0; i < 24; i += 4)
  509. AppendQuad(i, i + 1, i + 2, i + 3, vbase);
  510. ComputeNormals(ibase, m_indices.Count() - ibase);
  511. ibase = m_indices.Count();
  512. /* The 8 quads at each edge of the box */
  513. if (chamf)
  514. {
  515. static int const quadlist[48] =
  516. {
  517. 0, 3, 18, 17, 4, 7, 17, 16, 8, 11, 16, 19, 12, 15, 19, 18,
  518. 2, 1, 20, 23, 6, 5, 21, 20, 10, 9, 22, 21, 14, 13, 23, 22,
  519. 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2,
  520. };
  521. for (int i = 0; i < 48; i += 4)
  522. {
  523. if (smooth)
  524. AppendQuad(quadlist[i], quadlist[i + 1],
  525. quadlist[i + 2], quadlist[i + 3], vbase);
  526. else
  527. AppendQuadDuplicateVerts(quadlist[i], quadlist[i + 1],
  528. quadlist[i + 2], quadlist[i + 3], vbase);
  529. }
  530. }
  531. /* The 8 triangles at each corner of the box */
  532. if (chamf)
  533. {
  534. static int const trilist[24] =
  535. {
  536. 3, 12, 18, 15, 8, 19, 11, 4, 16, 7, 0, 17,
  537. 2, 23, 13, 14, 22, 9, 10, 21, 5, 6, 20, 1,
  538. };
  539. for (int i = 0; i < 24; i += 3)
  540. {
  541. if (smooth)
  542. AppendTriangle(trilist[i], trilist[i + 1],
  543. trilist[i + 2], vbase);
  544. else
  545. AppendTriangleDuplicateVerts(trilist[i], trilist[i + 1],
  546. trilist[i + 2], vbase);
  547. }
  548. }
  549. if (!smooth)
  550. ComputeNormals(ibase, m_indices.Count() - ibase);
  551. }
  552. void EasyMesh::AppendStar(int nbranches, float r1, float r2,
  553. int fade, int fade2)
  554. {
  555. int vbase = m_vert.Count();
  556. AddVertex(vec3(0.f, 0.f, 0.f));
  557. mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f);
  558. vec3 p1(r1, 0.f, 0.f), p2(r2, 0.f, 0.f);
  559. p2 = rotmat * p2;
  560. rotmat = rotmat * rotmat;
  561. for (int i = 0; i < nbranches; i++)
  562. {
  563. AddVertex(p1);
  564. if (fade2)
  565. SetCurVertColor(m_color2);
  566. AddVertex(p2);
  567. if (fade)
  568. SetCurVertColor(m_color2);
  569. AppendQuad(0, 2 * i + 1, 2 * i + 2, (2 * i + 3) % (2 * nbranches),
  570. vbase);
  571. p1 = rotmat * p1;
  572. p2 = rotmat * p2;
  573. }
  574. }
  575. void EasyMesh::AppendExpandedStar(int nbranches, float r1,
  576. float r2, float extrar)
  577. {
  578. int vbase = m_vert.Count();
  579. AddVertex(vec3(0.f, 0.f, 0.f));
  580. mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f);
  581. vec3 p1(r1, 0.f, 0.f), p2(r2, 0.f, 0.f),
  582. p3(r1 + extrar, 0.f, 0.f), p4(r2 + extrar, 0.f, 0.f);;
  583. p2 = rotmat * p2;
  584. p4 = rotmat * p4;
  585. rotmat = rotmat * rotmat;
  586. for (int i = 0; i < nbranches; i++)
  587. {
  588. AddVertex(p1);
  589. AddVertex(p2);
  590. AddVertex(p3); SetCurVertColor(m_color2);
  591. AddVertex(p4); SetCurVertColor(m_color2);
  592. int j = (i + 1) % nbranches;
  593. AppendQuad(0, 4 * i + 1, 4 * i + 2, 4 * j + 1, vbase);
  594. AppendQuad(4 * i + 1, 4 * i + 3, 4 * i + 4, 4 * i + 2, vbase);
  595. AppendQuad(4 * j + 1, 4 * i + 2, 4 * i + 4, 4 * j + 3, vbase);
  596. p1 = rotmat * p1;
  597. p2 = rotmat * p2;
  598. p3 = rotmat * p3;
  599. p4 = rotmat * p4;
  600. }
  601. }
  602. void EasyMesh::AppendDisc(int nsides, float r, int fade)
  603. {
  604. int vbase = m_vert.Count();
  605. AddVertex(vec3(0.f, 0.f, 0.f));
  606. mat3 rotmat = mat3::rotate(360.0f / nsides, 0.f, 1.f, 0.f);
  607. vec3 p1(r, 0.f, 0.f);
  608. for (int i = 0; i < nsides; i++)
  609. {
  610. AddVertex(p1);
  611. if (fade)
  612. SetCurVertColor(m_color2);
  613. AppendTriangle(0, i + 1, ((i + 1) % nsides) + 1, vbase);
  614. p1 = rotmat * p1;
  615. }
  616. }
  617. void EasyMesh::AppendSimpleTriangle(float size, int fade)
  618. {
  619. mat3 m = mat3::rotate(120.f, 0.f, 1.f, 0.f);
  620. vec3 p(0.f, 0.f, size);
  621. AddVertex(p);
  622. p = m * p;
  623. AddVertex(p);
  624. if (fade)
  625. SetCurVertColor(m_color2);
  626. p = m * p;
  627. AddVertex(p);
  628. if (fade)
  629. SetCurVertColor(m_color2);
  630. AppendTriangle(0, 1, 2, m_vert.Count() - 3);
  631. }
  632. void EasyMesh::AppendSimpleQuad(float size, int fade)
  633. {
  634. AppendSimpleQuad(vec2(size * .5f), vec2(size * -.5f), 0.f, fade);
  635. }
  636. void EasyMesh::AppendSimpleQuad(vec2 p1, vec2 p2, float z, int fade)
  637. {
  638. AddVertex(vec3(p2.x, z, -p1.y));
  639. AddVertex(vec3(p2.x, z, -p2.y));
  640. AddVertex(vec3(p1.x, z, -p2.y));
  641. if (fade)
  642. SetCurVertColor(m_color2);
  643. AddVertex(vec3(p1.x, z, -p1.y));
  644. if (fade)
  645. SetCurVertColor(m_color2);
  646. AppendQuad(3, 2, 1, 0, m_vert.Count() - 4);
  647. ComputeNormals(m_indices.Count() - 6, 6);
  648. }
  649. void EasyMesh::AppendCog(int nbsides, float h, float r10, float r20,
  650. float r1, float r2, float r12, float r22,
  651. float sidemul, int offset)
  652. {
  653. int ibase = m_indices.Count();
  654. int vbase = m_vert.Count();
  655. /* FIXME: enforce this some other way */
  656. if (r12 < 0)
  657. h = -h;
  658. mat3 rotmat = mat3::rotate(180.0f / nbsides, 0.f, 1.f, 0.f);
  659. mat3 smat1 = mat3::rotate(sidemul * 180.0f / nbsides, 0.f, 1.f, 0.f);
  660. mat3 smat2 = mat3::rotate(sidemul * -360.0f / nbsides, 0.f, 1.f, 0.f);
  661. vec3 p[12];
  662. p[0] = vec3(r10, h * .5f, 0.f);
  663. p[1] = rotmat * p[0];
  664. p[2] = vec3(r1, h * .5f, 0.f);
  665. p[3] = rotmat * p[2];
  666. p[4] = smat1 * (rotmat * vec3(r1 + r12, h * .5f, 0.f));
  667. p[5] = smat2 * (rotmat * p[4]);
  668. p[6] = vec3(r20, h * -.5f, 0.f);
  669. p[7] = rotmat * p[6];
  670. p[8] = vec3(r2, h * -.5f, 0.f);
  671. p[9] = rotmat * p[8];
  672. p[10] = smat1 * (rotmat * vec3(r2 + r22, h * -.5f, 0.f));
  673. p[11] = smat2 * (rotmat * p[10]);
  674. if (offset & 1)
  675. for (int n = 0; n < 12; n++)
  676. p[n] = rotmat * p[n];
  677. rotmat = rotmat * rotmat;
  678. for (int i = 0; i < nbsides; i++)
  679. {
  680. /* Each vertex will share three faces, so three different
  681. * normals, therefore we add each vertex three times. */
  682. for (int n = 0; n < 3 * 12; n++)
  683. {
  684. AddVertex(p[n / 3]);
  685. if (n / 3 >= 6)
  686. SetCurVertColor(m_color2);
  687. }
  688. int j = 3 * 12 * i, k = 3 * 12 * ((i + 1) % nbsides);
  689. /* The top and bottom faces */
  690. AppendQuad(j, j + 6, j + 9, j + 3, vbase);
  691. AppendQuad(j + 21, j + 27, j + 24, j + 18, vbase);
  692. AppendQuad(j + 3, j + 9, k + 6, k, vbase);
  693. AppendQuad(k + 18, k + 24, j + 27, j + 21, vbase);
  694. AppendQuad(j + 9, j + 12, j + 15, k + 6, vbase);
  695. AppendQuad(k + 24, j + 33, j + 30, j + 27, vbase);
  696. /* The inner side quads */
  697. AppendQuad(j + 1, j + 4, j + 22, j + 19, vbase);
  698. AppendQuad(j + 5, k + 2, k + 20, j + 23, vbase);
  699. /* The outer side quads */
  700. AppendQuad(j + 10, j + 7, j + 25, j + 28, vbase);
  701. AppendQuad(j + 13, j + 11, j + 29, j + 31, vbase);
  702. AppendQuad(j + 16, j + 14, j + 32, j + 34, vbase);
  703. AppendQuad(k + 8, j + 17, j + 35, k + 26, vbase);
  704. for (int n = 0; n < 12; n++)
  705. p[n] = rotmat * p[n];
  706. }
  707. ComputeNormals(ibase, m_indices.Count() - ibase);
  708. }
  709. void EasyMesh::Chamfer(float f)
  710. {
  711. int vlen = m_vert.Count() - m_cursors.Last().m1;
  712. int ilen = m_indices.Count() - m_cursors.Last().m2;
  713. /* Step 1: enumerate all faces. This is done by merging triangles
  714. * that are coplanar and share an edge. */
  715. int *triangle_classes = new int[ilen / 3];
  716. for (int i = 0; i < ilen / 3; i++)
  717. triangle_classes[i] = -1;
  718. for (int i = 0; i < ilen / 3; i++)
  719. {
  720. }
  721. /* Fun shit: reduce all triangles */
  722. int *vertices = new int[vlen];
  723. memset(vertices, 0, vlen * sizeof(int));
  724. for (int i = 0; i < ilen; i++)
  725. vertices[m_indices[i]]++;
  726. for (int i = 0; i < ilen / 3; i++)
  727. {
  728. #if 0
  729. if (vertices[m_indices[i * 3]] > 1)
  730. continue;
  731. if (vertices[m_indices[i * 3 + 1]] > 1)
  732. continue;
  733. if (vertices[m_indices[i * 3 + 2]] > 1)
  734. continue;
  735. #endif
  736. vec3 bary = 1.f / 3.f * (m_vert[m_indices[i * 3]].m1 +
  737. m_vert[m_indices[i * 3 + 1]].m1 +
  738. m_vert[m_indices[i * 3 + 2]].m1);
  739. for (int k = 0; k < 3; k++)
  740. {
  741. vec3 &p = m_vert[m_indices[i * 3 + k]].m1;
  742. p -= normalize(p - bary) * f;
  743. }
  744. }
  745. }
  746. } /* namespace lol */