2408 linhas
89 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. LOLFX_RESOURCE_DECLARE(shiny);
  34. LOLFX_RESOURCE_DECLARE(shinydebugwireframe);
  35. LOLFX_RESOURCE_DECLARE(shinydebuglighting);
  36. LOLFX_RESOURCE_DECLARE(shinydebugnormal);
  37. LOLFX_RESOURCE_DECLARE(shinydebugUV);
  38. LOLFX_RESOURCE_DECLARE(shiny_SK);
  39. namespace lol
  40. {
  41. //-----------------------------------------------------------------------------
  42. GpuShaderData::GpuShaderData()
  43. {
  44. m_render_mode = DebugRenderMode::Default;
  45. }
  46. //-----------------------------------------------------------------------------
  47. GpuShaderData::GpuShaderData(uint16_t vert_decl_flags, Shader* shader, DebugRenderMode render_mode)
  48. {
  49. m_render_mode = render_mode;
  50. m_shader = shader;
  51. m_vert_decl_flags = vert_decl_flags;
  52. }
  53. //-----------------------------------------------------------------------------
  54. GpuShaderData::~GpuShaderData()
  55. {
  56. m_shader_uniform.Empty();
  57. m_shader_attrib.Empty();
  58. }
  59. //-----------------------------------------------------------------------------
  60. void GpuShaderData::AddUniform(const lol::String &new_uniform)
  61. {
  62. m_shader_uniform.Push(new_uniform, m_shader->GetUniformLocation(new_uniform.C()));
  63. }
  64. //-----------------------------------------------------------------------------
  65. void GpuShaderData::AddAttribute(const lol::String &new_attribute, VertexUsage usage, int index)
  66. {
  67. m_shader_attrib.Push(new_attribute, m_shader->GetAttribLocation(new_attribute.C(), usage, index));
  68. }
  69. //-----------------------------------------------------------------------------
  70. ShaderUniform const *GpuShaderData::GetUniform(const lol::String &uniform)
  71. {
  72. for (int i = 0; i < m_shader_uniform.Count(); ++i)
  73. if (m_shader_uniform[i].m1 == uniform)
  74. return &m_shader_uniform[i].m2;
  75. return nullptr;
  76. }
  77. //-----------------------------------------------------------------------------
  78. ShaderAttrib const *GpuShaderData::GetAttribute(const lol::String &attribute)
  79. {
  80. for (int i = 0; i < m_shader_attrib.Count(); ++i)
  81. if (m_shader_attrib[i].m1 == attribute)
  82. return &m_shader_attrib[i].m2;
  83. return nullptr;
  84. }
  85. //-----------------------------------------------------------------------------
  86. DefaultShaderData::DefaultShaderData(DebugRenderMode render_mode)
  87. {
  88. bool with_UV = false;
  89. m_render_mode = render_mode;
  90. m_vert_decl_flags = (1 << VertexUsage::Position) |
  91. (1 << VertexUsage::Normal) |
  92. (1 << VertexUsage::Color);
  93. if (render_mode == DebugRenderMode::Default)
  94. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(shiny));
  95. else if (render_mode == DebugRenderMode::Wireframe)
  96. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinydebugwireframe));
  97. else if (render_mode == DebugRenderMode::Lighting)
  98. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinydebuglighting));
  99. else if (render_mode == DebugRenderMode::Normal)
  100. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinydebugnormal));
  101. else if (render_mode == DebugRenderMode::UV)
  102. {
  103. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinydebugUV));
  104. m_vert_decl_flags |= (1 << VertexUsage::TexCoord);
  105. with_UV = true;
  106. }
  107. SetupDefaultData(with_UV);
  108. }
  109. //-----------------------------------------------------------------------------
  110. DefaultShaderData::DefaultShaderData(uint16_t vert_decl_flags, Shader* shader, bool with_UV)
  111. : GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default)
  112. {
  113. SetupDefaultData(with_UV);
  114. }
  115. //-----------------------------------------------------------------------------
  116. void DefaultShaderData::SetupDefaultData(bool with_UV)
  117. {
  118. AddUniform("in_ModelView");
  119. AddUniform("in_Inv_ModelView");
  120. AddUniform("in_View");
  121. AddUniform("in_Inv_View");
  122. AddUniform("in_Proj");
  123. AddUniform("in_NormalMat");
  124. AddUniform("in_Damage");
  125. AddUniform("u_Lights");
  126. }
  127. //-----------------------------------------------------------------------------
  128. void DefaultShaderData::SetupShaderDatas(mat4 const &model)
  129. {
  130. mat4 proj = Scene::GetDefault()->GetCamera()->GetProjection();
  131. mat4 view = Scene::GetDefault()->GetCamera()->GetView();
  132. mat4 modelview = view * model;
  133. mat3 normalmat = transpose(inverse(mat3(modelview)));
  134. /* FIXME: this should be hidden in the shader */
  135. /* FIXME: the 4th component of the position can be used for other things */
  136. /* FIXME: GetUniform("blabla") is costly */
  137. Array<Light *> const lights = Scene::GetDefault()->GetLights();
  138. Array<vec4> light_data;
  139. for (int i = 0; i < lights.Count(); ++i)
  140. light_data << lights[i]->GetPosition() << lights[i]->GetColor();
  141. while (light_data.Count() < 8)
  142. light_data << vec4(0.f) << vec4(0.f);
  143. m_shader->SetUniform(*GetUniform("u_Lights"), light_data);
  144. m_shader->SetUniform(*GetUniform("in_ModelView"), modelview);
  145. m_shader->SetUniform(*GetUniform("in_Inv_ModelView"), inverse(modelview));
  146. m_shader->SetUniform(*GetUniform("in_View"), view);
  147. m_shader->SetUniform(*GetUniform("in_Inv_View"), inverse(view));
  148. m_shader->SetUniform(*GetUniform("in_Proj"), proj);
  149. m_shader->SetUniform(*GetUniform("in_NormalMat"), normalmat);
  150. m_shader->SetUniform(*GetUniform("in_Damage"), 0);
  151. }
  152. //-----------------------------------------------------------------------------
  153. GpuEasyMeshData::GpuEasyMeshData()
  154. {
  155. m_vertexcount = 0;
  156. m_indexcount = 0;
  157. m_ibo = nullptr;
  158. }
  159. //-----------------------------------------------------------------------------
  160. GpuEasyMeshData::~GpuEasyMeshData()
  161. {
  162. m_gpudatas.Empty();
  163. m_vdatas.Empty();
  164. if (m_ibo)
  165. delete(m_ibo);
  166. }
  167. #define BUILD_VFLAG(bool_value, flag_value, check_flag) \
  168. bool bool_value = (check_flag & (1 << flag_value)) != 0; \
  169. check_flag &= ~(1 << flag_value);
  170. #define BUILD_VFLAG_OR(bool_value, flag_value, check_flag) \
  171. bool_value = (bool_value || (check_flag & (1 << flag_value)) != 0); \
  172. check_flag &= ~(1 << flag_value);
  173. #define BUILD_VFLAG_COUNT(bool_value, flag_value, check_flag, count_value) \
  174. BUILD_VFLAG(bool_value, flag_value, check_flag) \
  175. count_value += (int)bool_value;
  176. //-----------------------------------------------------------------------------
  177. void GpuEasyMeshData::AddGpuData(GpuShaderData* gpudata, EasyMesh* src_mesh)
  178. {
  179. uint16_t vflags = gpudata->m_vert_decl_flags;
  180. BUILD_VFLAG(has_position, VertexUsage::Position, vflags);
  181. BUILD_VFLAG(has_normal, VertexUsage::Normal, vflags);
  182. BUILD_VFLAG(has_color, VertexUsage::Color, vflags);
  183. BUILD_VFLAG(has_texcoord, VertexUsage::TexCoord, vflags);
  184. BUILD_VFLAG_OR(has_texcoord, VertexUsage::TexCoordExt, vflags);
  185. ASSERT(!vflags, String("Vertex Useage setup is not implemented for : ") + VertexUsage::GetNameList(vflags) + String(", feel free to do so."));
  186. if (has_position) gpudata->AddAttribute(gpudata->GetInVertexName(), VertexUsage::Position, 0);
  187. if (has_normal) gpudata->AddAttribute(gpudata->GetInNormalName(), VertexUsage::Normal, 0);
  188. if (has_color) gpudata->AddAttribute(gpudata->GetInColorName(), VertexUsage::Color, 0);
  189. if (has_texcoord) gpudata->AddAttribute(gpudata->GetInTexCoordName(), VertexUsage::TexCoord, 0);
  190. SetupVertexData(gpudata->m_vert_decl_flags, src_mesh);
  191. if (!m_ibo)
  192. {
  193. Array<uint16_t> indexlist;
  194. for (int i = 0; i < src_mesh->m_indices.Count(); i += 3)
  195. {
  196. indexlist << src_mesh->m_indices[i + 0];
  197. indexlist << src_mesh->m_indices[i + 1];
  198. indexlist << src_mesh->m_indices[i + 2];
  199. }
  200. m_ibo = new IndexBuffer(indexlist.Bytes());
  201. void *indices = m_ibo->Lock(0, 0);
  202. memcpy(indices, &indexlist[0], indexlist.Bytes());
  203. m_ibo->Unlock();
  204. m_indexcount = indexlist.Count();
  205. }
  206. if (m_gpudatas.Count() != DebugRenderMode::Max)
  207. {
  208. m_gpudatas.Reserve(DebugRenderMode::Max);
  209. for (int i = 0; i < DebugRenderMode::Max; i++)
  210. m_gpudatas << nullptr;
  211. }
  212. m_gpudatas[gpudata->m_render_mode] = gpudata;
  213. }
  214. //-----------------------------------------------------------------------------
  215. void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
  216. {
  217. for (int i = 0; i < m_vdatas.Count(); ++i)
  218. if (m_vdatas[i].m1 == vflags)
  219. return;
  220. VertexDeclaration* new_vdecl = nullptr;
  221. VertexBuffer* new_vbo = nullptr;
  222. void *vbo_data = nullptr;
  223. int vbo_bytes = 0;
  224. #define COPY_VBO \
  225. new_vbo = new VertexBuffer(vbo_bytes); \
  226. void *mesh = new_vbo->Lock(0, 0); \
  227. memcpy(mesh, vbo_data, vbo_bytes); \
  228. new_vbo->Unlock();
  229. //Keep a count of the flags
  230. uint16_t saveflags = vflags;
  231. int flagnb = 0;
  232. BUILD_VFLAG_COUNT(has_position, VertexUsage::Position, saveflags, flagnb);
  233. BUILD_VFLAG_COUNT(has_normal, VertexUsage::Normal, saveflags, flagnb);
  234. BUILD_VFLAG_COUNT(has_color, VertexUsage::Color, saveflags, flagnb);
  235. BUILD_VFLAG_COUNT(has_texcoord, VertexUsage::TexCoord, saveflags, flagnb);
  236. BUILD_VFLAG_COUNT(has_texcoordExt,VertexUsage::TexCoordExt, saveflags, flagnb);
  237. ASSERT(!saveflags, String("Vertex Declaration setup is not implemented for : ") + VertexUsage::GetNameList(vflags) + String(", feel free to do so."));
  238. if (has_position && has_normal && has_color && has_texcoord && has_texcoordExt && flagnb == 5)
  239. {
  240. new_vdecl = new VertexDeclaration(
  241. VertexStream<vec3,vec3,u8vec4,vec4>(
  242. VertexUsage::Position,
  243. VertexUsage::Normal,
  244. VertexUsage::Color,
  245. VertexUsage::TexCoord));
  246. Array<vec3, vec3, u8vec4, vec4> vertexlist;
  247. for (int i = 0; i < src_mesh->m_vert.Count(); i++)
  248. vertexlist.Push(src_mesh->m_vert[i].m_coord,
  249. src_mesh->m_vert[i].m_normal,
  250. (u8vec4)(src_mesh->m_vert[i].m_color * 255.f),
  251. src_mesh->m_vert[i].m_texcoord);
  252. vbo_data = &vertexlist[0];
  253. vbo_bytes = vertexlist.Bytes();
  254. m_vertexcount = vertexlist.Count();
  255. COPY_VBO;
  256. }
  257. else if (has_position && has_texcoord && has_texcoordExt && flagnb == 3)
  258. {
  259. new_vdecl = new VertexDeclaration(VertexStream<vec3,vec4>(VertexUsage::Position, VertexUsage::TexCoord));
  260. Array<vec3, vec4> vertexlist;
  261. for (int i = 0; i < src_mesh->m_vert.Count(); i++)
  262. vertexlist.Push(src_mesh->m_vert[i].m_coord, src_mesh->m_vert[i].m_texcoord);
  263. vbo_data = &vertexlist[0];
  264. vbo_bytes = vertexlist.Bytes();
  265. m_vertexcount = vertexlist.Count();
  266. COPY_VBO;
  267. }
  268. else if (has_position && has_texcoord && flagnb == 2)
  269. {
  270. new_vdecl = new VertexDeclaration(VertexStream<vec3,vec2>(VertexUsage::Position, VertexUsage::TexCoord));
  271. Array<vec3, vec2> vertexlist;
  272. for (int i = 0; i < src_mesh->m_vert.Count(); i++)
  273. vertexlist.Push(src_mesh->m_vert[i].m_coord, src_mesh->m_vert[i].m_texcoord.xy);
  274. vbo_data = &vertexlist[0];
  275. vbo_bytes = vertexlist.Bytes();
  276. m_vertexcount = vertexlist.Count();
  277. COPY_VBO;
  278. }
  279. else if (has_position && has_normal && has_color && has_texcoord && flagnb == 4)
  280. {
  281. new_vdecl = new VertexDeclaration(
  282. VertexStream<vec3,vec3,u8vec4,vec2>(
  283. VertexUsage::Position,
  284. VertexUsage::Normal,
  285. VertexUsage::Color,
  286. VertexUsage::TexCoord));
  287. Array<vec3, vec3, u8vec4, vec2> vertexlist;
  288. for (int i = 0; i < src_mesh->m_vert.Count(); i++)
  289. vertexlist.Push(src_mesh->m_vert[i].m_coord,
  290. src_mesh->m_vert[i].m_normal,
  291. (u8vec4)(src_mesh->m_vert[i].m_color * 255.f),
  292. src_mesh->m_vert[i].m_texcoord.xy);
  293. vbo_data = &vertexlist[0];
  294. vbo_bytes = vertexlist.Bytes();
  295. m_vertexcount = vertexlist.Count();
  296. COPY_VBO;
  297. }
  298. else if (has_position && has_normal && has_color && flagnb == 3)
  299. {
  300. new_vdecl = new VertexDeclaration(
  301. VertexStream<vec3,vec3,u8vec4>(
  302. VertexUsage::Position,
  303. VertexUsage::Normal,
  304. VertexUsage::Color));
  305. Array<vec3,vec3,u8vec4> vertexlist;
  306. for (int i = 0; i < src_mesh->m_vert.Count(); i++)
  307. vertexlist.Push(src_mesh->m_vert[i].m_coord,
  308. src_mesh->m_vert[i].m_normal,
  309. (u8vec4)(src_mesh->m_vert[i].m_color * 255.f));
  310. vbo_data = &vertexlist[0];
  311. vbo_bytes = vertexlist.Bytes();
  312. m_vertexcount = vertexlist.Count();
  313. COPY_VBO;
  314. }
  315. m_vdatas.Push(vflags, new_vdecl, new_vbo);
  316. }
  317. //-----------------------------------------------------------------------------
  318. void GpuEasyMeshData::RenderMeshData(mat4 const &model)
  319. {
  320. DebugRenderMode d = Video::GetDebugRenderMode();
  321. GpuShaderData& gpu_sd = *(m_gpudatas[d]);
  322. int vdecl_idx = 0;
  323. for (; vdecl_idx < m_vdatas.Count(); ++vdecl_idx)
  324. if (m_vdatas[vdecl_idx].m1 == gpu_sd.m_vert_decl_flags)
  325. break;
  326. if (vdecl_idx >= m_vdatas.Count())
  327. return;
  328. uint16_t vflags = m_vdatas[vdecl_idx].m1;
  329. VertexDeclaration* vdecl = m_vdatas[vdecl_idx].m2;
  330. VertexBuffer* vbo = m_vdatas[vdecl_idx].m3;
  331. gpu_sd.m_shader->Bind();
  332. gpu_sd.SetupShaderDatas(model);
  333. vdecl->Bind();
  334. BUILD_VFLAG(has_position, VertexUsage::Position, vflags);
  335. BUILD_VFLAG(has_normal, VertexUsage::Normal, vflags);
  336. BUILD_VFLAG(has_color, VertexUsage::Color, vflags);
  337. BUILD_VFLAG(has_texcoord, VertexUsage::TexCoord, vflags);
  338. BUILD_VFLAG_OR(has_texcoord,VertexUsage::TexCoordExt, vflags);
  339. ASSERT(!vflags, String("Vertex Stream setup is not implemented for : ") + VertexUsage::GetNameList(vflags) + String(", feel free to do so."));
  340. int idx = 0;
  341. ShaderAttrib Attribs[4] = { lol::ShaderAttrib(), lol::ShaderAttrib(), lol::ShaderAttrib(), lol::ShaderAttrib() };
  342. if (has_position) Attribs[idx++] = *gpu_sd.GetAttribute(gpu_sd.GetInVertexName());
  343. if (has_normal) Attribs[idx++] = *gpu_sd.GetAttribute(gpu_sd.GetInNormalName());
  344. if (has_color) Attribs[idx++] = *gpu_sd.GetAttribute(gpu_sd.GetInColorName());
  345. if (has_texcoord) Attribs[idx++] = *gpu_sd.GetAttribute(gpu_sd.GetInTexCoordName());
  346. vdecl->SetStream(vbo, Attribs[0], Attribs[1], Attribs[2], Attribs[3]);
  347. m_ibo->Bind();
  348. vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, m_vertexcount, 0, m_indexcount);
  349. m_ibo->Unbind();
  350. vdecl->Unbind();
  351. }
  352. //-----------------------------------------------------------------------------
  353. EasyMesh::EasyMesh()
  354. : m_build_data(nullptr)
  355. {
  356. m_cursors.Push(0, 0);
  357. }
  358. //-----------------------------------------------------------------------------
  359. bool EasyMesh::Compile(char const *command)
  360. {
  361. EasyMeshCompiler mc(*this);
  362. return mc.ParseString(command);
  363. }
  364. //-----------------------------------------------------------------------------
  365. void EasyMesh::OpenBrace()
  366. {
  367. m_cursors.Push(m_vert.Count(), m_indices.Count());
  368. }
  369. //-----------------------------------------------------------------------------
  370. void EasyMesh::CloseBrace()
  371. {
  372. m_cursors.Pop();
  373. }
  374. //-----------------------------------------------------------------------------
  375. void EasyMesh::MeshConvert(GpuShaderData* new_gpu_sdata)
  376. {
  377. delete(m_build_data);
  378. m_build_data = nullptr;
  379. if (new_gpu_sdata)
  380. {
  381. m_gpu_data.AddGpuData(new_gpu_sdata, this);
  382. for (int i = DebugRenderMode::Default + 1; i < DebugRenderMode::Max; i++)
  383. m_gpu_data.AddGpuData(new DefaultShaderData(DebugRenderMode(i)), this);
  384. }
  385. }
  386. //-----------------------------------------------------------------------------
  387. void EasyMesh::MeshConvert(Shader* provided_shader)
  388. {
  389. if (provided_shader)
  390. {
  391. GpuShaderData *new_gpu_sdata = new DefaultShaderData(((1 << VertexUsage::Position) |
  392. (1 << VertexUsage::Normal) |
  393. (1 << VertexUsage::Color)),
  394. provided_shader,
  395. false);
  396. m_gpu_data.AddGpuData(new_gpu_sdata, this);
  397. }
  398. else
  399. m_gpu_data.AddGpuData(new DefaultShaderData(DebugRenderMode::Default), this);
  400. for (int i = DebugRenderMode::Default + 1; i < DebugRenderMode::Max; i++)
  401. m_gpu_data.AddGpuData(new DefaultShaderData(DebugRenderMode(i)), this);
  402. }
  403. //-----------------------------------------------------------------------------
  404. void EasyMesh::Render(mat4 const &model)
  405. {
  406. m_gpu_data.RenderMeshData(model);
  407. }
  408. //-------------------
  409. // "Collisions" functions
  410. //-------------------
  411. #define VX_ALONE -2
  412. #define VX_MASTER -1
  413. //-----------------------------------------------------------------------------
  414. //helpers func to retrieve a vertex.
  415. int VertexDictionnary::FindVertexMaster(const int search_idx)
  416. {
  417. //Resolve current vertex idx in the dictionnary (if exist)
  418. for (int j = 0; j < vertex_list.Count(); j++)
  419. if (vertex_list[j].m1 == search_idx)
  420. return vertex_list[j].m3;
  421. return VDictType::DoesNotExist;
  422. }
  423. //-----------------------------------------------------------------------------
  424. //retrieve a list of matching vertices, doesn't include search_idx.
  425. bool VertexDictionnary::FindMatchingVertices(const int search_idx, Array<int> &matching_ids)
  426. {
  427. int cur_mast = FindVertexMaster(search_idx);
  428. if (cur_mast == VDictType::DoesNotExist || cur_mast == VDictType::Alone)
  429. return false;
  430. if (cur_mast == VDictType::Master)
  431. cur_mast = search_idx;
  432. else
  433. matching_ids << vertex_list[cur_mast].m1;
  434. for (int j = 0; j < vertex_list.Count(); j++)
  435. if (vertex_list[j].m3 == cur_mast && vertex_list[j].m1 != search_idx)
  436. matching_ids << vertex_list[j].m1;
  437. return (matching_ids.Count() > 0);
  438. }
  439. //-----------------------------------------------------------------------------
  440. //Will return connected vertices (through triangles), if returned vertex has matching ones, it only returns the master.
  441. bool VertexDictionnary::FindConnectedVertices(const int search_idx, const Array<uint16_t> &tri_list, const int tri0, Array<int> &connected_vert, Array<int> const *ignored_tri)
  442. {
  443. Array<int> connected_tri;
  444. FindConnectedTriangles(search_idx, tri_list, tri0, connected_tri, ignored_tri);
  445. for (int i = 0; i < connected_tri.Count(); i++)
  446. {
  447. for (int j = 0; j < 3; j++)
  448. {
  449. int v_indice = tri_list[connected_tri[i] + j];
  450. if (v_indice != search_idx)
  451. {
  452. int found_master = FindVertexMaster(tri_list[connected_tri[i] + j]);
  453. if (found_master == VDictType::Alone || found_master == VDictType::Master)
  454. found_master = v_indice;
  455. if (found_master != search_idx)
  456. {
  457. bool already_exist = false;
  458. for (int k = 0; !already_exist && k < connected_vert.Count(); k++)
  459. if (connected_vert[k] == found_master)
  460. already_exist = true;
  461. if (!already_exist)
  462. connected_vert << found_master;
  463. }
  464. }
  465. }
  466. }
  467. return (connected_vert.Count() > 0);
  468. }
  469. //-----------------------------------------------------------------------------
  470. bool VertexDictionnary::FindConnectedTriangles(const int search_idx, const Array<uint16_t> &tri_list, const int tri0, Array<int> &connected_tri, Array<int> const *ignored_tri)
  471. {
  472. return FindConnectedTriangles(ivec3(search_idx, search_idx, search_idx), tri_list, tri0, connected_tri, ignored_tri);
  473. }
  474. //-----------------------------------------------------------------------------
  475. bool VertexDictionnary::FindConnectedTriangles(const ivec2 &search_idx, const Array<uint16_t> &tri_list, const int tri0, Array<int> &connected_tri, Array<int> const *ignored_tri)
  476. {
  477. return FindConnectedTriangles(ivec3(search_idx, search_idx.x), tri_list, tri0, connected_tri, ignored_tri);
  478. }
  479. //-----------------------------------------------------------------------------
  480. bool VertexDictionnary::FindConnectedTriangles(const ivec3 &search_idx, const Array<uint16_t> &tri_list, const int tri0, Array<int> &connected_tri, Array<int> const *ignored_tri)
  481. {
  482. int needed_validation = 0;
  483. Array<int> vert_list[3];
  484. for (int i = 0; i < 3; i++)
  485. {
  486. //Small optim since above func will use this one
  487. if ((i == 1 && search_idx[0] == search_idx[1]) ||
  488. (i == 2 && (search_idx[0] == search_idx[2] || search_idx[1] == search_idx[2])))
  489. continue;
  490. else
  491. {
  492. //increment the validation info, hence empty list aren't taken into account.
  493. needed_validation++;
  494. vert_list[i] << search_idx[i];
  495. FindMatchingVertices(search_idx[i], vert_list[i]);
  496. }
  497. }
  498. for (int i = tri0; i < tri_list.Count(); i += 3)
  499. {
  500. if (ignored_tri)
  501. {
  502. bool should_pass = false;
  503. for (int j = 0; !should_pass && j < ignored_tri->Count(); j++)
  504. if ((*ignored_tri)[j] == i)
  505. should_pass = true;
  506. if (should_pass)
  507. continue;
  508. }
  509. int found_validation = 0;
  510. for (int j = 0; j < 3; j++)
  511. {
  512. bool validated = false;
  513. for (int k = 0; !validated && k < vert_list[j].Count(); k++)
  514. for (int l = 0; !validated && l < 3; l++)
  515. if (vert_list[j][k] == tri_list[i + l])
  516. validated = true;
  517. found_validation += (validated)?(1):(0);
  518. }
  519. //triangle is validated store it
  520. if (found_validation == needed_validation)
  521. connected_tri << i;
  522. }
  523. return (connected_tri.Count() > 0);
  524. }
  525. //-----------------------------------------------------------------------------
  526. //Will update the given list with all the vertices on the same spot.
  527. void VertexDictionnary::AddVertex(const int vert_id, const vec3 vert_coord)
  528. {
  529. for (int j = 0; j < vertex_list.Count(); j++)
  530. if (vertex_list[j].m1 == vert_id)
  531. return;
  532. //First, build the vertex Dictionnary
  533. int i = 0;
  534. for (; i < master_list.Count(); i++)
  535. {
  536. int cur_mast = master_list[i];
  537. int cur_id = vertex_list[cur_mast].m1;
  538. vec3 cur_loc = vertex_list[cur_mast].m2;
  539. int &cur_type = vertex_list[cur_mast].m3;
  540. if (cur_id == vert_id)
  541. return;
  542. if (sqlength(cur_loc - vert_coord) < CSG_EPSILON)
  543. {
  544. if (cur_type == VDictType::Alone)
  545. cur_type = VDictType::Master;
  546. vertex_list.Push(vert_id, vert_coord, cur_mast);
  547. return;
  548. }
  549. }
  550. //We're here because we couldn't find any matching vertex
  551. master_list.Push(vertex_list.Count());
  552. vertex_list.Push(vert_id, vert_coord, VDictType::Alone);
  553. }
  554. //-----------------------------------------------------------------------------
  555. void EasyMesh::MeshCsg(CSGUsage csg_operation)
  556. {
  557. //A vertex dictionnary for vertices on the same spot.
  558. Array< int, int > vertex_dict;
  559. //This list keeps track of the triangle that will need deletion at the end.
  560. Array< int > triangle_to_kill;
  561. //Listing for each triangle of the vectors intersecting it. <tri_Id, <Point0, Point1, tri_isec_Normal>>
  562. Array< int, Array< vec3, vec3, vec3 > > triangle_isec;
  563. //keep a track of the intersection point on the triangle. <pos, side_id>
  564. Array< vec3, int > triangle_vertex;
  565. for (int k = 0; k < 10; k++)
  566. triangle_vertex.Push(vec3(.0f), 0);
  567. //bsp infos
  568. CsgBsp mesh_bsp_0;
  569. CsgBsp mesh_bsp_1;
  570. if (m_cursors.Count() == 0)
  571. return;
  572. //BSP BUILD : We use the brace logic, csg should be used as : "[ exp .... [exp .... csg]]"
  573. int cursor_start = (m_cursors.Count() < 2)?(0):(m_cursors[(m_cursors.Count() - 2)].m2);
  574. for (int mesh_id = 0; mesh_id < 2; mesh_id++)
  575. {
  576. int start_point = (mesh_id == 0)?(cursor_start):(m_cursors.Last().m2);
  577. int end_point = (mesh_id == 0)?(m_cursors.Last().m2):(m_indices.Count());
  578. CsgBsp &mesh_bsp = (mesh_id == 0)?(mesh_bsp_0):(mesh_bsp_1);
  579. for (int i = start_point; i < end_point; i += 3)
  580. mesh_bsp.AddTriangleToTree(i, m_vert[m_indices[i]].m_coord,
  581. m_vert[m_indices[i + 1]].m_coord,
  582. m_vert[m_indices[i + 2]].m_coord);
  583. }
  584. //BSP Useage : let's crunch all triangles on the correct BSP
  585. int indices_count = m_indices.Count();
  586. for (int mesh_id = 0; mesh_id < 2; mesh_id++)
  587. {
  588. int start_point = (mesh_id == 0)?(cursor_start):(m_cursors.Last().m2);
  589. int end_point = (mesh_id == 0)?(m_cursors.Last().m2):(indices_count);
  590. CsgBsp &mesh_bsp = (mesh_id == 0)?(mesh_bsp_1):(mesh_bsp_0);
  591. Array< vec3, int, int, float > vert_list;
  592. Array< int, int, int, int > tri_list;
  593. vec3 n0(.0f); vec3 n1(.0f);
  594. vec4 c0(.0f); vec4 c1(.0f);
  595. //Reserve some memory
  596. vert_list.Reserve(3);
  597. tri_list.Reserve(3);
  598. for (int i = start_point; i < end_point; i += 3)
  599. {
  600. int Result = mesh_bsp.TestTriangleToTree(m_vert[m_indices[i]].m_coord,
  601. m_vert[m_indices[i + 1]].m_coord,
  602. m_vert[m_indices[i + 2]].m_coord, vert_list, tri_list);
  603. int tri_base_idx = m_indices.Count();
  604. //one split has been done, we need to had the new vertices & the new triangles.
  605. if (Result == 1)
  606. {
  607. triangle_to_kill.Push(i);
  608. #if 1
  609. int base_idx = m_vert.Count();
  610. for (int k = 3; k < vert_list.Count(); k++)
  611. {
  612. int P0 = (vert_list[k].m2 < 3)?(m_indices[i + vert_list[k].m2]):(base_idx + vert_list[k].m2 - 3);
  613. int P1 = (vert_list[k].m3 < 3)?(m_indices[i + vert_list[k].m3]):(base_idx + vert_list[k].m3 - 3);
  614. AddVertex(vert_list[k].m1);
  615. //Normal : bad calculations there.
  616. n0 = m_vert[P0].m_normal;
  617. n1 = m_vert[P1].m_normal;
  618. SetCurVertNormal(normalize(n0 + (n1 - n0) * vert_list[k].m4));
  619. #if 1
  620. //Color
  621. c0 = m_vert[P0].m_color;
  622. c1 = m_vert[P1].m_color;
  623. vec4 res = c0 + ((c1 - c0) * vert_list[k].m4);
  624. SetCurVertColor(res);
  625. #else
  626. if (mesh_id == 0)
  627. SetCurVertColor(vec4(1.0f, .0f, .0f, 1.0f));
  628. else
  629. SetCurVertColor(vec4(.0f, 1.0f, 1.0f, 1.0f));
  630. #endif
  631. }
  632. for (int k = 0; k < tri_list.Count(); k++)
  633. {
  634. int P0 = (tri_list[k].m2 < 3)?(m_indices[i + tri_list[k].m2]):(base_idx + (tri_list[k].m2 - 3));
  635. int P1 = (tri_list[k].m3 < 3)?(m_indices[i + tri_list[k].m3]):(base_idx + (tri_list[k].m3 - 3));
  636. int P2 = (tri_list[k].m4 < 3)?(m_indices[i + tri_list[k].m4]):(base_idx + (tri_list[k].m4 - 3));
  637. AppendTriangle(P0, P1, P2, 0);
  638. }
  639. #endif
  640. }
  641. #if 1
  642. //Main case
  643. if (Result >= 0)
  644. {
  645. for (int k = 0; k < tri_list.Count(); k++)
  646. {
  647. int tri_idx = ((tri_list.Count() == 1)?(i):(tri_base_idx + k * 3));
  648. //Triangle Kill Test
  649. if (//csgu : CSGUnion() -> m0_Outside + m1_Outside
  650. (csg_operation == CSGUsage::Union && tri_list[k].m1 == LEAF_BACK) ||
  651. //csgs : CSGSubstract() -> m0_Outside + m1_Inside-inverted
  652. (csg_operation == CSGUsage::Substract &&
  653. ((mesh_id == 0 && tri_list[k].m1 == LEAF_BACK) ||
  654. (mesh_id == 1 && tri_list[k].m1 == LEAF_FRONT))) ||
  655. //csgs : CSGSubstractLoss() -> m0_Outside
  656. (csg_operation == CSGUsage::SubstractLoss &&
  657. ((mesh_id == 0 && tri_list[k].m1 == LEAF_BACK) || mesh_id == 1)) ||
  658. //csga : CSGAnd() -> m0_Inside + m1_Inside
  659. (csg_operation == CSGUsage::And && tri_list[k].m1 == LEAF_FRONT))
  660. {
  661. triangle_to_kill.Push(tri_idx);
  662. }
  663. //Triangle Invert Test
  664. if (//csgs : CSGSubstract() -> m0_Outside + m1_Inside-inverted
  665. (csg_operation == CSGUsage::Substract && mesh_id == 1 && tri_list[k].m1 == LEAF_BACK) ||
  666. //csgx : CSGXor() -> m0_Outside/m0_Inside-inverted + m1_Outside/m1_Inside-inverted
  667. (csg_operation == CSGUsage::Xor && tri_list[k].m1 == LEAF_BACK))
  668. {
  669. //a Xor means we will share vertices with the outside, so duplicate the vertices.
  670. //TODO : This operation disconnect all triangle, in some cases, not a good thing.
  671. if (csg_operation == CSGUsage::Xor)
  672. {
  673. for (int l = 0; l < 3; l++)
  674. {
  675. AddDuplicateVertex(m_indices[tri_idx + l]);
  676. m_indices[tri_idx + l] = m_vert.Count() - 1;
  677. }
  678. }
  679. m_indices[tri_idx + 1] += m_indices[tri_idx + 2];
  680. m_indices[tri_idx + 2] = m_indices[tri_idx + 1] - m_indices[tri_idx + 2];
  681. m_indices[tri_idx + 1] = m_indices[tri_idx + 1] - m_indices[tri_idx + 2];
  682. ComputeNormals(tri_idx, 3);
  683. }
  684. }
  685. }
  686. #endif
  687. vert_list.Empty();
  688. tri_list.Empty();
  689. }
  690. }
  691. for (int i = 0; i < m_vert.Count(); i++)
  692. if (length(m_vert[i].m_normal) < 1.0f)
  693. i = i;
  694. int dir = 1;
  695. for (int i = 0; i >= 0 && i < triangle_to_kill.Count() - 1; i += dir)
  696. {
  697. if (triangle_to_kill[i] < triangle_to_kill[i + 1] && dir < 0)
  698. dir = 1;
  699. if (triangle_to_kill[i] == triangle_to_kill[i + 1])
  700. {
  701. triangle_to_kill.Remove(i);
  702. dir = -1;
  703. }
  704. if (triangle_to_kill[i] > triangle_to_kill[i + 1])
  705. {
  706. triangle_to_kill[i] += triangle_to_kill[i + 1];
  707. triangle_to_kill[i + 1] = triangle_to_kill[i] - triangle_to_kill[i + 1];
  708. triangle_to_kill[i] = triangle_to_kill[i] - triangle_to_kill[i + 1];
  709. dir = -1;
  710. }
  711. if (i == 0 && dir == -1)
  712. dir = 1;
  713. }
  714. for (int i = triangle_to_kill.Count() - 1; i >= 0; i--)
  715. m_indices.Remove(triangle_to_kill[i], 3);
  716. m_cursors.Last().m1 = m_vert.Count();
  717. m_cursors.Last().m2 = m_indices.Count();
  718. //DONE for the splitting !
  719. }
  720. //-----------------------------------------------------------------------------
  721. void EasyMesh::ToggleScaleWinding()
  722. {
  723. BD()->Toggle(MeshBuildOperation::Scale_Winding);
  724. }
  725. //-----------------------------------------------------------------------------
  726. void EasyMesh::SetCurColor(vec4 const &color)
  727. {
  728. BD()->Color() = color;
  729. }
  730. //-----------------------------------------------------------------------------
  731. void EasyMesh::SetCurColor2(vec4 const &color)
  732. {
  733. BD()->Color2() = color;
  734. }
  735. //-----------------------------------------------------------------------------
  736. void EasyMesh::AddVertex(vec3 const &coord)
  737. {
  738. m_vert.Push(VertexData(coord, vec3(0.f, 1.f, 0.f), BD()->Color()));
  739. }
  740. //-----------------------------------------------------------------------------
  741. void EasyMesh::AddDuplicateVertex(int i)
  742. {
  743. m_vert << m_vert[i];
  744. }
  745. //-----------------------------------------------------------------------------
  746. void EasyMesh::AddLerpVertex(int i, int j, float alpha)
  747. {
  748. m_vert.Push(VertexData(
  749. lol::lerp(m_vert[i].m_coord, m_vert[j].m_coord, alpha),
  750. lol::lerp(m_vert[i].m_normal, m_vert[j].m_normal, alpha),
  751. lol::lerp(m_vert[i].m_color, m_vert[j].m_color, alpha),
  752. lol::lerp(m_vert[i].m_texcoord, m_vert[j].m_texcoord, alpha),
  753. ((alpha < .5f) ? (m_vert[i].m_bone_id) : (m_vert[j].m_bone_id)), /* FIXME ? */
  754. lol::lerp(m_vert[i].m_bone_weight, m_vert[j].m_bone_weight, alpha)));
  755. }
  756. //-----------------------------------------------------------------------------
  757. void EasyMesh::AppendQuad(int i1, int i2, int i3, int i4, int base)
  758. {
  759. m_indices << base + i1;
  760. m_indices << base + i2;
  761. m_indices << base + i3;
  762. m_indices << base + i4;
  763. m_indices << base + i1;
  764. m_indices << base + i3;
  765. }
  766. //-----------------------------------------------------------------------------
  767. void EasyMesh::AppendQuadDuplicateVerts(int i1, int i2, int i3, int i4, int base)
  768. {
  769. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  770. m_indices << m_vert.Count(); AddDuplicateVertex(base + i2);
  771. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  772. m_indices << m_vert.Count(); AddDuplicateVertex(base + i4);
  773. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  774. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  775. }
  776. //-----------------------------------------------------------------------------
  777. void EasyMesh::AppendTriangle(int i1, int i2, int i3, int base)
  778. {
  779. m_indices << base + i1;
  780. m_indices << base + i2;
  781. m_indices << base + i3;
  782. }
  783. //-----------------------------------------------------------------------------
  784. void EasyMesh::AppendTriangleDuplicateVerts(int i1, int i2, int i3, int base)
  785. {
  786. m_indices << m_vert.Count(); AddDuplicateVertex(base + i1);
  787. m_indices << m_vert.Count(); AddDuplicateVertex(base + i2);
  788. m_indices << m_vert.Count(); AddDuplicateVertex(base + i3);
  789. }
  790. //-----------------------------------------------------------------------------
  791. void EasyMesh::ComputeNormals(int start, int vcount)
  792. {
  793. for (int i = 0; i < vcount; i += 3)
  794. {
  795. vec3 v0 = m_vert[m_indices[start + i + 2]].m_coord
  796. - m_vert[m_indices[start + i + 0]].m_coord;
  797. vec3 v1 = m_vert[m_indices[start + i + 1]].m_coord
  798. - m_vert[m_indices[start + i + 0]].m_coord;
  799. vec3 n = normalize(cross(v1, v0));
  800. for (int j = 0; j < 3; j++)
  801. m_vert[m_indices[start + i + j]].m_normal = n;
  802. }
  803. }
  804. //-----------------------------------------------------------------------------
  805. void EasyMesh::ComputeTexCoord(float uv_scale, int uv_offset)
  806. {
  807. UNUSED(uv_scale, uv_offset);
  808. #if 0
  809. VertexDictionnary vert_dict;
  810. Array<int> tri_list;
  811. tri_list.Reserve(m_indices.Count() - m_cursors.Last().m2);
  812. for (int i = m_cursors.Last().m2; i < m_indices.Count(); i++)
  813. {
  814. vert_dict.AddVertex(m_indices[i], m_vert[m_indices[i]].m_coord);
  815. tri_list << m_indices[i];
  816. }
  817. //full triangle count
  818. Array<int> tri_done;
  819. Array<int> tri_check;
  820. int tri_count = (m_indices.Count() - m_cursors.Last().m2) / 3;
  821. tri_check << tri_list[0];
  822. while (tri_check.Count())
  823. {
  824. int cur_tri = tri_check[0];
  825. int v[3] = { tri_list[cur_tri + uv_offset % 3], tri_list[cur_tri + (1 + uv_offset) % 3], tri_list[cur_tri + (2 + uv_offset) % 3] };
  826. vec2 uv[3] = { m_vert[tri_list[cur_tri]].m_texcoord.xy, m_vert[tri_list[cur_tri + 1]].m_texcoord.xy, m_vert[tri_list[cur_tri + 2]].m_texcoord.xy };
  827. for (int j = 0; j < 3; j++)
  828. {
  829. if (uv[j] != vec2(-1.0f) && uv[j] == uv[(j + 1) % 3])
  830. {
  831. uv[0] = vec2(-1.0f);
  832. uv[1] = vec2(-1.0f);
  833. uv[2] = vec2(-1.0f);
  834. break;
  835. }
  836. }
  837. int uv_set = 0;
  838. for (int j = 0; j < 3; j++)
  839. uv_set += (uv[j].x < 0.f)?(0):(1);
  840. //this case shouldn't happen.
  841. if (uv_set == 1)
  842. {
  843. /*
  844. for (int j = 0; j < 3; j++)
  845. {
  846. if (uv[j] != vec2(-1.0f))
  847. {
  848. uv[(j + 1) % 2] = uv[j] + vec2(.0f, uv_scale * length(m_vert[v[j]].m1 - m_vert[v[(j + 1) % 3]].m1));
  849. uv_set = 2;
  850. break;
  851. }
  852. }
  853. */
  854. }
  855. //No UV is set, let's do the arbitrary set and use the basic method.
  856. if (uv_set == 0)
  857. {
  858. float new_dot = FLT_MAX;
  859. int base_i = 0;
  860. for (int j = 0; j < 3; j++)
  861. {
  862. float tmp_dot = abs(dot(normalize(m_vert[v[(j + 1) % 3]].m_coord - m_vert[v[j]].m_coord),
  863. normalize(m_vert[v[(j + 2) % 3]].m_coord - m_vert[v[j]].m_coord)));
  864. if (tmp_dot < new_dot)
  865. {
  866. base_i = j;
  867. new_dot = tmp_dot;
  868. }
  869. }
  870. uv[base_i] = vec2(.0f);
  871. uv[(base_i + 1) % 3] = vec2(.0f, uv_scale * length(m_vert[v[base_i]].m_coord - m_vert[v[(base_i + 1) % 3]].m_coord));
  872. uv_set = 2;
  873. }
  874. //2 points have been set, let's figure the third
  875. if (uv_set == 2)
  876. {
  877. {
  878. //invert values so the two set uv are in [0, 1] slots.
  879. int new_v[3];
  880. vec2 new_uv[3];
  881. bool ignore_set = false;
  882. if (uv[0].x >= 0.f && uv[1].x < 0.f)
  883. {
  884. new_v[0] = v[2]; new_v[1] = v[0]; new_v[2] = v[1];
  885. new_uv[0] = uv[2]; new_uv[1] = uv[0]; new_uv[2] = uv[1];
  886. }
  887. else if (uv[0].x < 0.f && uv[1].x >= 0.f)
  888. {
  889. new_v[0] = v[1]; new_v[1] = v[2]; new_v[2] = v[0];
  890. new_uv[0] = uv[1]; new_uv[1] = uv[2]; new_uv[2] = uv[0];
  891. }
  892. else
  893. ignore_set = true;
  894. if (!ignore_set)
  895. {
  896. v[0] = new_v[0]; v[1] = new_v[1]; v[2] = new_v[2];
  897. uv[0] = new_uv[0]; uv[1] = new_uv[1]; uv[2] = new_uv[2];
  898. }
  899. }
  900. //Do this to be sure the normal is OK.
  901. ComputeNormals(cur_tri, 3);
  902. vec3 v01 = normalize(m_vert[v[1]].m_coord - m_vert[v[0]].m_coord);
  903. vec3 v02 = m_vert[v[2]].m_coord - m_vert[v[0]].m_coord;
  904. vec3 v_dir = normalize(cross(m_vert[m_indices[cur_tri]].m_normal, v01));
  905. vec2 texu_dir = uv[1] - uv[0];
  906. vec2 texv_dir = vec2(texu_dir.y, texu_dir.x);
  907. //Final calculations
  908. uv[2] = texu_dir * dot(v01, v02) + texv_dir * dot(v_dir, v02);
  909. //Set UV on ALL matching vertices!
  910. Array<int> matching_vert;
  911. for (int i = 0; i < 3; i++)
  912. {
  913. #if 1
  914. //This marks all same position UV to the same values
  915. //Deactivation is a test.
  916. matching_vert << v[i];
  917. vert_dict.FindMatchingVertices(v[i], matching_vert);
  918. for (int j = 0; j < matching_vert.Count(); j++)
  919. if (m_vert[matching_vert[j]].m_texcoord.xy == vec2(-1.0f))
  920. m_vert[matching_vert[j]].m_texcoord = vec4(abs(uv[i]), m_vert[matching_vert[j]].m_texcoord.zw);
  921. #else
  922. m_vert[v[i]].m_texcoord = abs(uv[i]);
  923. #endif
  924. }
  925. tri_done << cur_tri;
  926. tri_check.Remove(0);
  927. //Get connected triangles and go from there.
  928. for (int j = 0; j < 3; j++)
  929. {
  930. #if 1
  931. //This finds triangle that are connected to this triangle
  932. vert_dict.FindConnectedTriangles(ivec2(v[j], v[(j + 1) % 3]), tri_list, tri_check, &tri_done);
  933. #else
  934. //This finds triangle that are connected to the vertices of this triangle
  935. vert_dict.FindConnectedTriangles(v[j], tri_list, tri_check, &tri_done);
  936. #endif
  937. }
  938. }
  939. else if (uv_set == 3)
  940. {
  941. for (int j = 0; j < 3; j++)
  942. {
  943. m_vert[tri_list[cur_tri]].m_texcoord = vec4(vec2(-1.0f), m_vert[tri_list[cur_tri]].m_texcoord.zw);
  944. m_vert[tri_list[cur_tri + 1]].m_texcoord = vec4(vec2(-1.0f), m_vert[tri_list[cur_tri + 1]].m_texcoord.zw);
  945. m_vert[tri_list[cur_tri + 2]].m_texcoord = vec4(vec2(-1.0f), m_vert[tri_list[cur_tri + 2]].m_texcoord.zw);
  946. }
  947. //uv[0] = vec2(-1.0f);
  948. //uv[1] = vec2(-1.0f);
  949. //uv[2] = vec2(-1.0f);
  950. /*
  951. bool tri_present = false;
  952. for (int j = 0; j < tri_done.Count(); j++)
  953. if (cur_tri == tri_done[j])
  954. tri_present = true;
  955. if (!tri_present)
  956. tri_done << cur_tri;
  957. tri_check.Remove(0);
  958. */
  959. }
  960. if (tri_check.Count() == 0 && tri_done.Count() != tri_count)
  961. {
  962. //look for unset triangle
  963. for (int i = 0; !tri_check.Count() && i < tri_list.Count(); i += 3)
  964. {
  965. bool tri_present = false;
  966. for (int j = 0; j < tri_done.Count(); j++)
  967. if (i == tri_done[j])
  968. tri_present = true;
  969. if (!tri_present)
  970. tri_check << i;
  971. }
  972. }
  973. }
  974. #endif
  975. }
  976. //-----------------------------------------------------------------------------
  977. void EasyMesh::SetVertColor(vec4 const &color)
  978. {
  979. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  980. m_vert[i].m_color = color;
  981. }
  982. //-----------------------------------------------------------------------------
  983. void EasyMesh::SetTexCoordData(vec2 const &new_offset, vec2 const &new_scale)
  984. {
  985. BD()->TexCoordOffset() = new_offset;
  986. BD()->TexCoordScale() = new_scale;
  987. }
  988. //-----------------------------------------------------------------------------
  989. void EasyMesh::SetTexCoordData2(vec2 const &new_offset, vec2 const &new_scale)
  990. {
  991. BD()->TexCoordOffset2() = new_offset;
  992. BD()->TexCoordScale2() = new_scale;
  993. }
  994. //-----------------------------------------------------------------------------
  995. void EasyMesh::SetCurVertNormal(vec3 const &normal)
  996. {
  997. m_vert[m_vert.Count() - 1].m_normal = normal;
  998. }
  999. //-----------------------------------------------------------------------------
  1000. void EasyMesh::SetCurVertColor(vec4 const &color)
  1001. {
  1002. m_vert[m_vert.Count() - 1].m_color = color;
  1003. }
  1004. //-----------------------------------------------------------------------------
  1005. void EasyMesh::SetCurVertTexCoord(vec2 const &texcoord)
  1006. {
  1007. m_vert[m_vert.Count() - 1].m_texcoord = vec4(texcoord, m_vert[m_vert.Count() - 1].m_texcoord.zw);
  1008. }
  1009. //-----------------------------------------------------------------------------
  1010. void EasyMesh::SetCurVertTexCoord2(vec2 const &texcoord)
  1011. {
  1012. m_vert[m_vert.Count() - 1].m_texcoord = vec4(m_vert[m_vert.Count() - 1].m_texcoord.xy, texcoord);
  1013. }
  1014. //-----------------------------------------------------------------------------
  1015. void EasyMesh::Translate(vec3 const &v)
  1016. {
  1017. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  1018. m_vert[i].m_coord += v;
  1019. }
  1020. //-----------------------------------------------------------------------------
  1021. void EasyMesh::RotateX(float angle) { Rotate(angle, vec3(1, 0, 0)); }
  1022. void EasyMesh::RotateY(float angle) { Rotate(angle, vec3(0, 1, 0)); }
  1023. void EasyMesh::RotateZ(float angle) { Rotate(angle, vec3(0, 0, 1)); }
  1024. //-----------------------------------------------------------------------------
  1025. void EasyMesh::Rotate(float angle, vec3 const &axis)
  1026. {
  1027. mat3 m = mat3::rotate(angle, axis);
  1028. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  1029. {
  1030. m_vert[i].m_coord = m * m_vert[i].m_coord;
  1031. m_vert[i].m_normal = m * m_vert[i].m_normal;
  1032. }
  1033. }
  1034. //-----------------------------------------------------------------------------
  1035. void EasyMesh::RadialJitter(float r)
  1036. {
  1037. Array<int> Welded;
  1038. Welded.Push(-1);
  1039. for (int i = m_cursors.Last().m1 + 1; i < m_vert.Count(); i++)
  1040. {
  1041. int j, k;
  1042. for (j = m_cursors.Last().m1, k = 0; j < i; j++, k++)
  1043. {
  1044. if(Welded[k] < 0)
  1045. {
  1046. vec3 diff = m_vert[i].m_coord - m_vert[j].m_coord;
  1047. if(diff.x > 0.1f || diff.x < -0.1f)
  1048. continue;
  1049. if(diff.y > 0.1f || diff.y < -0.1f)
  1050. continue;
  1051. if(diff.z > 0.1f || diff.z < -0.1f)
  1052. continue;
  1053. break;
  1054. }
  1055. }
  1056. if(j == i)
  1057. Welded.Push(-1);
  1058. else
  1059. Welded.Push(j);
  1060. }
  1061. int i, j;
  1062. for (i = m_cursors.Last().m1, j = 0; i < m_vert.Count(); i++, j++)
  1063. {
  1064. if(Welded[j] == -1)
  1065. m_vert[i].m_coord *= 1.0f + RandF(r);
  1066. else
  1067. m_vert[i].m_coord = m_vert[Welded[j]].m_coord;
  1068. }
  1069. ComputeNormals(m_cursors.Last().m2, m_indices.Count() - m_cursors.Last().m2);
  1070. }
  1071. //-----------------------------------------------------------------------------
  1072. void EasyMesh::TaperX(float ny, float nz, float xoff, int absolute) { DoMeshTransform(MeshTransform::Taper, Axis::X, Axis::X, ny, nz, xoff, absolute); }
  1073. void EasyMesh::TaperY(float nx, float nz, float yoff, int absolute) { DoMeshTransform(MeshTransform::Taper, Axis::Y, Axis::Y, nz, nx, yoff, absolute); }
  1074. void EasyMesh::TaperZ(float nx, float ny, float zoff, int absolute) { DoMeshTransform(MeshTransform::Taper, Axis::Z, Axis::Z, nx, ny, zoff, absolute); }
  1075. //-----------------------------------------------------------------------------
  1076. void EasyMesh::TwistX(float t, float toff) { DoMeshTransform(MeshTransform::Twist, Axis::X, Axis::X, t, t, toff, 0); }
  1077. void EasyMesh::TwistY(float t, float toff) { DoMeshTransform(MeshTransform::Twist, Axis::Y, Axis::Y, t, t, toff, 0); }
  1078. void EasyMesh::TwistZ(float t, float toff) { DoMeshTransform(MeshTransform::Twist, Axis::Z, Axis::Z, t, t, toff, 0); }
  1079. //-----------------------------------------------------------------------------
  1080. void EasyMesh::ShearX(float ny, float nz, float xoff, int absolute) { DoMeshTransform(MeshTransform::Shear, Axis::X, Axis::X, ny, nz, xoff, absolute); }
  1081. void EasyMesh::ShearY(float nx, float nz, float yoff, int absolute) { DoMeshTransform(MeshTransform::Shear, Axis::Y, Axis::Y, nz, nx, yoff, absolute); }
  1082. void EasyMesh::ShearZ(float nx, float ny, float zoff, int absolute) { DoMeshTransform(MeshTransform::Shear, Axis::Z, Axis::Z, nx, ny, zoff, absolute); }
  1083. //-----------------------------------------------------------------------------
  1084. void EasyMesh::StretchX(float ny, float nz, float xoff) { DoMeshTransform(MeshTransform::Stretch, Axis::X, Axis::X, ny, nz, xoff, 0); }
  1085. void EasyMesh::StretchY(float nx, float nz, float yoff) { DoMeshTransform(MeshTransform::Stretch, Axis::Y, Axis::Y, nz, nx, yoff, 0); }
  1086. void EasyMesh::StretchZ(float nx, float ny, float zoff) { DoMeshTransform(MeshTransform::Stretch, Axis::Z, Axis::Z, nx, ny, zoff, 0); }
  1087. //-----------------------------------------------------------------------------
  1088. void EasyMesh::BendXY(float t, float toff) { DoMeshTransform(MeshTransform::Bend, Axis::X, Axis::Y, t, t, toff, 0); }
  1089. void EasyMesh::BendXZ(float t, float toff) { DoMeshTransform(MeshTransform::Bend, Axis::X, Axis::Z, t, t, toff, 0); }
  1090. void EasyMesh::BendYX(float t, float toff) { DoMeshTransform(MeshTransform::Bend, Axis::Y, Axis::X, t, t, toff, 0); }
  1091. void EasyMesh::BendYZ(float t, float toff) { DoMeshTransform(MeshTransform::Bend, Axis::Y, Axis::Z, t, t, toff, 0); }
  1092. void EasyMesh::BendZX(float t, float toff) { DoMeshTransform(MeshTransform::Bend, Axis::Z, Axis::X, t, t, toff, 0); }
  1093. void EasyMesh::BendZY(float t, float toff) { DoMeshTransform(MeshTransform::Bend, Axis::Z, Axis::Y, t, t, toff, 0); }
  1094. //-----------------------------------------------------------------------------
  1095. void EasyMesh::DoMeshTransform(MeshTransform ct, Axis axis0, Axis axis1, float n0, float n1, float noff, int absolute)
  1096. {
  1097. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  1098. {
  1099. switch (ct)
  1100. {
  1101. case MeshTransform::Taper:
  1102. {
  1103. float value = m_vert[i].m_coord[axis0];
  1104. if (absolute) value = abs(value);
  1105. m_vert[i].m_coord[(axis0 + 1) % 3] *= max(0.f, 1.f + (n0 * value + noff));
  1106. m_vert[i].m_coord[(axis0 + 2) % 3] *= max(0.f, 1.f + (n1 * value + noff));
  1107. break;
  1108. }
  1109. case MeshTransform::Twist:
  1110. {
  1111. vec3 rotaxis = vec3(1.f); rotaxis[(axis0 + 1) % 3] = .0f; rotaxis[(axis0 + 2) % 3] = .0f;
  1112. m_vert[i].m_coord = mat3::rotate(m_vert[i].m_coord[axis0] * n0 + noff, rotaxis) * m_vert[i].m_coord;
  1113. break;
  1114. }
  1115. case MeshTransform::Shear:
  1116. {
  1117. float value = m_vert[i].m_coord[axis0];
  1118. if (absolute) value = abs(value);
  1119. m_vert[i].m_coord[(axis0 + 1) % 3] += (n0 * value + noff);
  1120. m_vert[i].m_coord[(axis0 + 2) % 3] += (n1 * value + noff);
  1121. break;
  1122. }
  1123. case MeshTransform::Stretch:
  1124. {
  1125. //float value = abs(m_vert[i].m1[axis0]);
  1126. //m_vert[i].m1[(axis0 + 1) % 3] += (lol::pow(value, n0) + noff);
  1127. //m_vert[i].m1[(axis0 + 2) % 3] += (lol::pow(value, n1) + noff);
  1128. break;
  1129. }
  1130. case MeshTransform::Bend:
  1131. {
  1132. vec3 rotaxis = vec3(1.f); rotaxis[(axis1 + 1) % 3] = .0f; rotaxis[(axis1 + 2) % 3] = .0f;
  1133. m_vert[i].m_coord = mat3::rotate(m_vert[i].m_coord[axis0] * n0 + noff, rotaxis) * m_vert[i].m_coord;
  1134. break;
  1135. }
  1136. }
  1137. }
  1138. ComputeNormals(m_cursors.Last().m2, m_indices.Count() - m_cursors.Last().m2);
  1139. }
  1140. //-----------------------------------------------------------------------------
  1141. void EasyMesh::Scale(vec3 const &s)
  1142. {
  1143. vec3 const invs = vec3(1) / s;
  1144. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  1145. {
  1146. m_vert[i].m_coord *= s;
  1147. m_vert[i].m_normal = normalize(m_vert[i].m_normal * invs);
  1148. }
  1149. /* Flip winding if the scaling involves mirroring */
  1150. if (!BD()->IsEnabled(MeshBuildOperation::Scale_Winding) && s.x * s.y * s.z < 0)
  1151. {
  1152. for (int i = m_cursors.Last().m2; i < m_indices.Count(); i += 3)
  1153. {
  1154. uint16_t tmp = m_indices[i + 0];
  1155. m_indices[i + 0] = m_indices[i + 1];
  1156. m_indices[i + 1] = tmp;
  1157. }
  1158. }
  1159. }
  1160. //-----------------------------------------------------------------------------
  1161. void EasyMesh::MirrorX() { DupAndScale(vec3(-1, 1, 1)); }
  1162. void EasyMesh::MirrorY() { DupAndScale(vec3(1, -1, 1)); }
  1163. void EasyMesh::MirrorZ() { DupAndScale(vec3(1, 1, -1)); }
  1164. //-----------------------------------------------------------------------------
  1165. void EasyMesh::DupAndScale(vec3 const &s)
  1166. {
  1167. int vlen = m_vert.Count() - m_cursors.Last().m1;
  1168. int tlen = m_indices.Count() - m_cursors.Last().m2;
  1169. for (int i = 0; i < vlen; i++)
  1170. AddDuplicateVertex(m_cursors.Last().m1++);
  1171. for (int i = 0; i < tlen; i++)
  1172. m_indices << m_indices[m_cursors.Last().m2++] + vlen;
  1173. Scale(s);
  1174. m_cursors.Last().m1 -= vlen;
  1175. m_cursors.Last().m2 -= tlen;
  1176. }
  1177. //-----------------------------------------------------------------------------
  1178. void EasyMesh::AppendCylinder(int nsides, float h, float d1, float d2,
  1179. int dualside, int smooth, int close)
  1180. {
  1181. //XXX : This operation is done to convert radius to diameter without changing all the code.
  1182. float r1 = d1 * .5f;
  1183. float r2 = d2 * .5f;
  1184. //SAVE
  1185. vec4 Saved_Color = BD()->Color();
  1186. vec4 Saved_Color2 = BD()->Color2();
  1187. vec2 Save_texcoord_offset = BD()->TexCoordOffset();
  1188. vec2 Save_texcoord_scale = BD()->TexCoordScale();
  1189. int vbase = m_vert.Count();
  1190. mat3 rotmat = mat3::rotate(360.0f / (float)nsides, 0.f, 1.f, 0.f);
  1191. vec3 p1(r1, -h * .5f, 0.f), p2(r2, h * .5f, 0.f), n;
  1192. vec2 uv1(.0f, .0f), uv2(.0f, 1.0f), uvadd(1.0f / (float)nsides, .0f);
  1193. if (close)
  1194. SetTexCoordData(vec2(.0f), vec2(1.0f, .5f));
  1195. /* Construct normal */
  1196. if (r2 != .0f)
  1197. n = vec3(r2, h * .5f, 0.f);
  1198. else
  1199. n = vec3(r1, h * .5f, 0.f);
  1200. n.y = r1 * (r1 - r2) / h;
  1201. if (!smooth)
  1202. n = mat3::rotate(180.0f / nsides, 0.f, 1.f, 0.f) * n;
  1203. n = normalize(n);
  1204. /* FIXME: normals should be flipped in two-sided mode, but that
  1205. * means duplicating the vertices again... */
  1206. for (int i = 0; i < nsides; i++)
  1207. {
  1208. AddVertex(p1); SetCurVertNormal(n); SetCurVertTexCoord(uv1); SetCurVertTexCoord2(uv1);
  1209. AddVertex(p2); SetCurVertNormal(n); SetCurVertTexCoord(uv2); SetCurVertTexCoord2(uv2);
  1210. SetCurVertColor(BD()->Color2());
  1211. if (smooth)
  1212. {
  1213. int j = (i + 1) % nsides;
  1214. AppendQuad(j * 2, j * 2 + 1, i * 2 + 1, i * 2, vbase);
  1215. if (dualside)
  1216. AppendQuad(i * 2, i * 2 + 1, j * 2 + 1, j * 2, vbase);
  1217. }
  1218. p1 = rotmat * p1; uv1 += uvadd;
  1219. p2 = rotmat * p2; uv2 += uvadd;
  1220. if (!smooth)
  1221. {
  1222. AddVertex(p1); SetCurVertNormal(n); SetCurVertTexCoord(uv1); SetCurVertTexCoord2(uv1);
  1223. AddVertex(p2); SetCurVertNormal(n); SetCurVertTexCoord(uv2); SetCurVertTexCoord2(uv2);
  1224. SetCurVertColor(BD()->Color2());
  1225. AppendQuad(i * 4 + 2, i * 4 + 3, i * 4 + 1, i * 4, vbase);
  1226. if (dualside)
  1227. AppendQuad(i * 4, i * 4 + 1, i * 4 + 3, i * 4 + 2, vbase);
  1228. }
  1229. n = rotmat * n;
  1230. }
  1231. if (close)
  1232. {
  1233. //START
  1234. OpenBrace();
  1235. //LOWER DISC
  1236. SetTexCoordData(vec2(.0f, .5f), vec2(.5f, .5f));
  1237. SetCurColor(BD()->Color());
  1238. AppendDisc(nsides, d1);
  1239. Translate(vec3(.0f, h, .0f));
  1240. RotateX(180.0f);
  1241. //UPPER DISC
  1242. SetTexCoordData(vec2(.5f, .5f), vec2(.5f, .5f));
  1243. SetCurColor(BD()->Color2());
  1244. AppendDisc(nsides, d2);
  1245. Translate(vec3(.0f, h * .5f, .0f));
  1246. CloseBrace();
  1247. }
  1248. //RESTORE
  1249. SetCurColor(Saved_Color);
  1250. SetCurColor2(Saved_Color2);
  1251. SetTexCoordData(Save_texcoord_offset, Save_texcoord_scale);
  1252. }
  1253. //-----------------------------------------------------------------------------
  1254. void EasyMesh::AppendSphere(int ndivisions, float d)
  1255. {
  1256. AppendCapsule(ndivisions, 0.f, d);
  1257. }
  1258. //-----------------------------------------------------------------------------
  1259. void EasyMesh::AppendCapsule(int ndivisions, float h, float d)
  1260. {
  1261. //XXX : This operation is done to convert radius to diameter without changing all the code.
  1262. float r = d * .5f;
  1263. int ibase = m_indices.Count();
  1264. Array<vec3> vertices;
  1265. float uv_h = 0;
  1266. float uv_r = 0;
  1267. /* FIXME: we don't know how to handle even-divided capsules, so we
  1268. * force the count to be odd. */
  1269. if (h)
  1270. {
  1271. ndivisions |= 1;
  1272. //calculate uv h&r percents
  1273. uv_h = (float)h / (float)(h + r * 2);
  1274. uv_r = (float)r / (float)(h + r * 2);
  1275. }
  1276. /* Fill in the icosahedron vertices, rotating them so that there
  1277. * is a vertex at [0 1 0] and [0 -1 0] after normalisation. */
  1278. float phi = 0.5f + 0.5f * sqrt(5.f);
  1279. mat3 mat = mat3::rotate(asin(1.f / sqrt(2.f + phi)) * (180.f / (float)M_PI),
  1280. vec3(0.f, 0.f, 1.f));
  1281. for (int i = 0; i < 4; i++)
  1282. {
  1283. float x = (i & 1) ? 0.5f : -0.5f;
  1284. float y = (i & 2) ? phi * 0.5f : phi * -0.5f;
  1285. vertices << mat * vec3(x, y, 0.f);
  1286. vertices << mat * vec3(0.f, x, y);
  1287. vertices << mat * vec3(y, 0.f, x);
  1288. }
  1289. static int const trilist[] =
  1290. {
  1291. 0, 1, 2, 2, 4, 6, 3, 8, 1, 9, 4, 8,
  1292. 7, 0, 5, 7, 11, 3, 10, 5, 6, 10, 9, 11,
  1293. 0, 3, 1, 7, 3, 0, 1, 4, 2, 8, 4, 1,
  1294. 2, 5, 0, 6, 5, 2, 6, 9, 10, 4, 9, 6,
  1295. 7, 10, 11, 5, 10, 7, 8, 11, 9, 3, 11, 8
  1296. };
  1297. for (unsigned i = 0; i < sizeof(trilist) / sizeof(*trilist); i += 3)
  1298. {
  1299. vec3 const &a = vertices[trilist[i]];
  1300. vec3 const &b = vertices[trilist[i + 1]];
  1301. vec3 const &c = vertices[trilist[i + 2]];
  1302. vec3 const vb = 1.f / ndivisions * (b - a);
  1303. vec3 const vc = 1.f / ndivisions * (c - a);
  1304. int line = ndivisions + 1;
  1305. for (int v = 0, x = 0, y = 0; x < ndivisions + 1; v++)
  1306. {
  1307. vec3 p[] = { a + (float)x * vb + (float)y * vc,
  1308. p[0] + vb,
  1309. p[0] + vc,
  1310. p[0] + vb + vc };
  1311. vec2 uv[4];
  1312. /* FIXME: when we normalise here, we get a volume that is slightly
  1313. * smaller than the sphere of radius 1, since we are not using
  1314. * the midradius. */
  1315. for (int k = 0; k < 4; k++)
  1316. {
  1317. //keep normalized until the end of the UV calculations
  1318. p[k] = normalize(p[k]);
  1319. uv[k].x = (lol::atan2(p[k].z, p[k].x) + (float)M_PI) / ((float)M_PI * 2.f);
  1320. if (abs(p[k].y) >= 1.0f)
  1321. uv[k].x = -1.f;
  1322. uv[k].y = lol::atan2(p[k].y, dot(p[k], normalize(p[k] * vec3(1.f,0.f,1.f)))) / (float)M_PI + 0.5f;
  1323. if (h)
  1324. {
  1325. if (uv[k].y > .5f)
  1326. uv[k].y = uv_r + uv_h + (uv[k].y - .5f) * uv_r * 2.f;
  1327. else
  1328. uv[k].y *= uv_r * 2.f;
  1329. }
  1330. p[k] *= r;
  1331. }
  1332. /* If this is a capsule, grow in the Y direction */
  1333. if (h > 0.f)
  1334. {
  1335. for (int k = 0; k < 4; k++)
  1336. p[k].y += (p[k].y > 0.f) ? 0.5f * h : -0.5f * h;
  1337. }
  1338. /* Add zero, one or two triangles */
  1339. int id[] = { 0, 1, 2,
  1340. 1, 3 ,2 };
  1341. int l = 6;
  1342. while ((l -= 3) >= 0)
  1343. {
  1344. if ((l == 0 && y < line - 1) || (l == 3 && y < line - 2))
  1345. {
  1346. int k = -1;
  1347. while (++k < 3)
  1348. {
  1349. int rid[] = { id[k + l], id[(k + 1) % 3 + l] };
  1350. if (uv[rid[0]].x >= .0f &&
  1351. uv[rid[1]].x >= .0f &&
  1352. abs(uv[rid[0]].x - uv[rid[1]].x) > .5f)
  1353. {
  1354. if (uv[rid[0]].x < uv[rid[1]].x)
  1355. uv[rid[0]].x += 1.0f;
  1356. else
  1357. uv[rid[1]].x += 1.0f;
  1358. }
  1359. }
  1360. k = -1;
  1361. while (++k < 3)
  1362. {
  1363. int rid[] = { id[k + l], id[(k + 1) % 3 + l], id[(k + 2) % 3 + l] };
  1364. AddVertex(p[rid[0]]);
  1365. vec2 new_uv;
  1366. if (uv[rid[0]].x < .0f)
  1367. new_uv = vec2((uv[rid[1]].x + uv[rid[2]].x) * .5f, uv[rid[0]].y);
  1368. else
  1369. new_uv = uv[rid[0]];
  1370. SetCurVertTexCoord(vec2(0.f, 1.f) - new_uv);
  1371. SetCurVertTexCoord2(vec2(0.f, 1.f) - new_uv);
  1372. }
  1373. AppendTriangle(0, 2, 1, m_vert.Count() - 3);
  1374. }
  1375. }
  1376. y++;
  1377. if (y == line)
  1378. {
  1379. x++;
  1380. y = 0;
  1381. line--;
  1382. }
  1383. }
  1384. }
  1385. ComputeNormals(ibase, m_indices.Count() - ibase);
  1386. }
  1387. //-----------------------------------------------------------------------------
  1388. void EasyMesh::AppendTorus(int ndivisions, float d1, float d2)
  1389. {
  1390. //XXX : This operation is done to convert radius to diameter without changing all the code.
  1391. float r1 = d1 * .5f;
  1392. float r2 = d2 * .5f;
  1393. int ibase = m_indices.Count();
  1394. int nidiv = ndivisions; /* Cross-section */
  1395. int njdiv = ndivisions; /* Full circumference */
  1396. for (int j = 0; j < njdiv; j++)
  1397. for (int i = 0; i < 2 * nidiv; i++)
  1398. {
  1399. for (int di = 0; di < 2; di++)
  1400. for (int dj = 0; dj < 2; dj++)
  1401. {
  1402. int i2 = (i + di) % nidiv;
  1403. int j2 = (j + dj) % njdiv;
  1404. //Location on the donut
  1405. float x = 0.5f * (r2 - r1) * (float)lol::cos(2.0 * M_PI * i2 / nidiv) + 0.5f * (r1 + r2);
  1406. float y = 0.5f * (r2 - r1) * (float)lol::sin(2.0 * M_PI * i2 / nidiv);
  1407. float z = 0.0f;
  1408. //Center circle
  1409. float ca = (float)lol::cos(2.0 * M_PI * j2 / njdiv);
  1410. float sa = (float)lol::sin(2.0 * M_PI * j2 / njdiv);
  1411. //Actual location
  1412. float x2 = x * ca - z * sa;
  1413. float z2 = z * ca + x * sa;
  1414. AddVertex(vec3(x2, y, z2));
  1415. SetCurVertTexCoord(vec2((float)(i + di) / (float)nidiv, (float)(j + dj) / (float)nidiv));
  1416. SetCurVertTexCoord2(vec2((float)(i + di) / (float)nidiv, (float)(j + dj) / (float)nidiv));
  1417. }
  1418. AppendTriangle(0, 2, 3, m_vert.Count() - 4);
  1419. AppendTriangle(0, 3, 1, m_vert.Count() - 4);
  1420. }
  1421. ComputeNormals(ibase, m_indices.Count() - ibase);
  1422. }
  1423. //-----------------------------------------------------------------------------
  1424. void EasyMesh::AppendBox(vec3 const &size, float chamf)
  1425. {
  1426. AppendBox(size, chamf, false);
  1427. }
  1428. //-----------------------------------------------------------------------------
  1429. void EasyMesh::AppendSmoothChamfBox(vec3 const &size, float chamf)
  1430. {
  1431. AppendBox(size, chamf, true);
  1432. }
  1433. //-----------------------------------------------------------------------------
  1434. void EasyMesh::AppendFlatChamfBox(vec3 const &size, float chamf)
  1435. {
  1436. AppendBox(size, chamf, false);
  1437. }
  1438. //-----------------------------------------------------------------------------
  1439. void EasyMesh::AppendBox(vec3 const &size, float chamf, bool smooth)
  1440. {
  1441. if (chamf < 0.0f)
  1442. {
  1443. AppendBox(size + vec3(chamf * 2.0f), -chamf, smooth);
  1444. return;
  1445. }
  1446. int vbase = m_vert.Count();
  1447. int ibase = m_indices.Count();
  1448. vec3 d = size * 0.5f;
  1449. MeshType mt = MeshType::Box;
  1450. TexCoordPos bl = TexCoordPos::BL;
  1451. TexCoordPos br = TexCoordPos::BR;
  1452. TexCoordPos tl = TexCoordPos::TL;
  1453. TexCoordPos tr = TexCoordPos::TR;
  1454. //--
  1455. //Side vertices
  1456. //--
  1457. MeshFaceType mft = MeshFaceType::BoxFront;
  1458. AddVertex(vec3(-d.x, -d.y, -d.z - chamf));
  1459. SetCurVertTexCoord(BD()->TexCoord(mt, tr, mft));
  1460. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1461. //--
  1462. AddVertex(vec3(-d.x, +d.y, -d.z - chamf));
  1463. SetCurVertTexCoord(BD()->TexCoord(mt, br, mft));
  1464. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1465. //--
  1466. AddVertex(vec3(+d.x, +d.y, -d.z - chamf));
  1467. SetCurVertTexCoord(BD()->TexCoord(mt, bl, mft));
  1468. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1469. //--
  1470. AddVertex(vec3(+d.x, -d.y, -d.z - chamf));
  1471. SetCurVertTexCoord(BD()->TexCoord(mt, tl, mft));
  1472. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1473. //--
  1474. mft = MeshFaceType::BoxLeft;
  1475. AddVertex(vec3(-d.x - chamf, -d.y, +d.z));
  1476. SetCurVertTexCoord(BD()->TexCoord(mt, tr, mft));
  1477. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1478. //--
  1479. AddVertex(vec3(-d.x - chamf, +d.y, +d.z));
  1480. SetCurVertTexCoord(BD()->TexCoord(mt, br, mft));
  1481. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1482. //--
  1483. AddVertex(vec3(-d.x - chamf, +d.y, -d.z));
  1484. SetCurVertTexCoord(BD()->TexCoord(mt, bl, mft));
  1485. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1486. //--
  1487. AddVertex(vec3(-d.x - chamf, -d.y, -d.z));
  1488. SetCurVertTexCoord(BD()->TexCoord(mt, tl, mft));
  1489. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1490. //--
  1491. mft = MeshFaceType::BoxBack;
  1492. AddVertex(vec3(+d.x, -d.y, +d.z + chamf));
  1493. SetCurVertTexCoord(BD()->TexCoord(mt, tr, mft));
  1494. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1495. //--
  1496. AddVertex(vec3(+d.x, +d.y, +d.z + chamf));
  1497. SetCurVertTexCoord(BD()->TexCoord(mt, br, mft));
  1498. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1499. //--
  1500. AddVertex(vec3(-d.x, +d.y, +d.z + chamf));
  1501. SetCurVertTexCoord(BD()->TexCoord(mt, bl, mft));
  1502. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1503. //--
  1504. AddVertex(vec3(-d.x, -d.y, +d.z + chamf));
  1505. SetCurVertTexCoord(BD()->TexCoord(mt, tl, mft));
  1506. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1507. //--
  1508. mft = MeshFaceType::BoxRight;
  1509. AddVertex(vec3(+d.x + chamf, -d.y, -d.z));
  1510. SetCurVertTexCoord(BD()->TexCoord(mt, tr, mft));
  1511. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1512. //--
  1513. AddVertex(vec3(+d.x + chamf, +d.y, -d.z));
  1514. SetCurVertTexCoord(BD()->TexCoord(mt, br, mft));
  1515. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1516. //--
  1517. AddVertex(vec3(+d.x + chamf, +d.y, +d.z));
  1518. SetCurVertTexCoord(BD()->TexCoord(mt, bl, mft));
  1519. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1520. //--
  1521. AddVertex(vec3(+d.x + chamf, -d.y, +d.z));
  1522. SetCurVertTexCoord(BD()->TexCoord(mt, tl, mft));
  1523. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1524. //--
  1525. //Bottom vertices
  1526. //--
  1527. mft = MeshFaceType::BoxBottom;
  1528. AddVertex(vec3(-d.x, -d.y - chamf, +d.z));
  1529. SetCurVertTexCoord(BD()->TexCoord(mt, tr, mft));
  1530. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1531. //--
  1532. AddVertex(vec3(-d.x, -d.y - chamf, -d.z));
  1533. SetCurVertTexCoord(BD()->TexCoord(mt, br, mft));
  1534. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1535. //--
  1536. AddVertex(vec3(+d.x, -d.y - chamf, -d.z));
  1537. SetCurVertTexCoord(BD()->TexCoord(mt, bl, mft));
  1538. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1539. //--
  1540. AddVertex(vec3(+d.x, -d.y - chamf, +d.z));
  1541. SetCurVertTexCoord(BD()->TexCoord(mt, tl, mft));
  1542. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1543. //--
  1544. //Top vertices
  1545. //--
  1546. mft = MeshFaceType::BoxTop;
  1547. AddVertex(vec3(-d.x, +d.y + chamf, -d.z));
  1548. SetCurVertTexCoord(BD()->TexCoord(mt, tr, mft));
  1549. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1550. //--
  1551. AddVertex(vec3(-d.x, +d.y + chamf, +d.z));
  1552. SetCurVertTexCoord(BD()->TexCoord(mt, br, mft));
  1553. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1554. //--
  1555. AddVertex(vec3(+d.x, +d.y + chamf, +d.z));
  1556. SetCurVertTexCoord(BD()->TexCoord(mt, bl, mft));
  1557. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1558. //--
  1559. AddVertex(vec3(+d.x, +d.y + chamf, -d.z));
  1560. SetCurVertTexCoord(BD()->TexCoord(mt, tl, mft));
  1561. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1562. /* The 6 quads on each side of the box */
  1563. for (int i = 0; i < 24; i += 4)
  1564. AppendQuad(i, i + 1, i + 2, i + 3, vbase);
  1565. ComputeNormals(ibase, m_indices.Count() - ibase);
  1566. ibase = m_indices.Count();
  1567. /* The 8 quads at each edge of the box */
  1568. if (chamf)
  1569. {
  1570. static int const quadlist[48] =
  1571. {
  1572. 0, 3, 18, 17, 4, 7, 17, 16, 8, 11, 16, 19, 12, 15, 19, 18,
  1573. 2, 1, 20, 23, 6, 5, 21, 20, 10, 9, 22, 21, 14, 13, 23, 22,
  1574. 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2,
  1575. };
  1576. for (int i = 0; i < 48; i += 4)
  1577. {
  1578. if (smooth)
  1579. AppendQuad(quadlist[i], quadlist[i + 1],
  1580. quadlist[i + 2], quadlist[i + 3], vbase);
  1581. else
  1582. AppendQuadDuplicateVerts(quadlist[i], quadlist[i + 1],
  1583. quadlist[i + 2], quadlist[i + 3], vbase);
  1584. }
  1585. }
  1586. /* The 8 triangles at each corner of the box */
  1587. if (chamf)
  1588. {
  1589. static int const trilist[24] =
  1590. {
  1591. 3, 12, 18, 15, 8, 19, 11, 4, 16, 7, 0, 17,
  1592. 2, 23, 13, 14, 22, 9, 10, 21, 5, 6, 20, 1,
  1593. };
  1594. for (int i = 0; i < 24; i += 3)
  1595. {
  1596. if (smooth)
  1597. AppendTriangle(trilist[i], trilist[i + 1],
  1598. trilist[i + 2], vbase);
  1599. else
  1600. AppendTriangleDuplicateVerts(trilist[i], trilist[i + 1],
  1601. trilist[i + 2], vbase);
  1602. }
  1603. }
  1604. if (!smooth)
  1605. ComputeNormals(ibase, m_indices.Count() - ibase);
  1606. }
  1607. //-----------------------------------------------------------------------------
  1608. void EasyMesh::AppendStar(int nbranches, float d1, float d2,
  1609. int fade, int fade2)
  1610. {
  1611. //XXX : This operation is done to convert radius to diameter without changing all the code.
  1612. float r1 = d1 * .5f;
  1613. float r2 = d2 * .5f;
  1614. //TODO: It would probably be good to think of another way of UV painting this, like "branch repeating"
  1615. int vbase = m_vert.Count();
  1616. float maxr = max(r1, r2);
  1617. AddVertex(vec3(0.f, 0.f, 0.f)); SetCurVertTexCoord(vec2(.5f, .5f)); SetCurVertTexCoord2(vec2(.5f, .5f));
  1618. mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f);
  1619. vec3 p1(r1, 0.f, 0.f), p2(r2, 0.f, 0.f);
  1620. vec3 uv1(0.f, 0.f, -.5f * ((float)r1 / maxr)),
  1621. uv2(0.f, 0.f, -.5f * ((float)r2 / maxr));
  1622. p2 = rotmat * p2; uv2 = rotmat * uv2;
  1623. rotmat = rotmat * rotmat;
  1624. for (int i = 0; i < nbranches; i++)
  1625. {
  1626. AddVertex(p1); SetCurVertTexCoord(uv1.xz + vec2(.5f)); SetCurVertTexCoord2(uv1.xz + vec2(.5f));
  1627. if (fade2)
  1628. SetCurVertColor(BD()->Color2());
  1629. AddVertex(p2); SetCurVertTexCoord(uv2.xz + vec2(.5f)); SetCurVertTexCoord2(uv2.xz + vec2(.5f));
  1630. if (fade)
  1631. SetCurVertColor(BD()->Color2());
  1632. AppendQuad(0, 2 * i + 1, 2 * i + 2, (2 * i + 3) % (2 * nbranches),
  1633. vbase);
  1634. p1 = rotmat * p1; uv1 = rotmat * uv1;
  1635. p2 = rotmat * p2; uv2 = rotmat * uv2;
  1636. }
  1637. }
  1638. //-----------------------------------------------------------------------------
  1639. void EasyMesh::AppendExpandedStar(int nbranches, float d1, float d2, float extrad)
  1640. {
  1641. //XXX : This operation is done to convert radius to diameter without changing all the code.
  1642. float r1 = d1 * .5f;
  1643. float r2 = d2 * .5f;
  1644. float extrar = extrad * .5f;
  1645. int vbase = m_vert.Count();
  1646. float maxr = (float)max(max(r1, r2), max(r1 + extrar, r2 + extrar));
  1647. AddVertex(vec3(0.f, 0.f, 0.f)); SetCurVertTexCoord(vec2(.5f, .5f)); SetCurVertTexCoord2(vec2(.5f, .5f));
  1648. mat3 rotmat = mat3::rotate(180.0f / nbranches, 0.f, 1.f, 0.f);
  1649. vec3 p1(r1, 0.f, 0.f), p2(r2, 0.f, 0.f),
  1650. p3(r1 + extrar, 0.f, 0.f), p4(r2 + extrar, 0.f, 0.f);;
  1651. vec3 uv1(0.f, 0.f, -.5f * ((float)r1 / maxr)),
  1652. uv2(0.f, 0.f, -.5f * ((float)r2 / maxr)),
  1653. uv3(0.f, 0.f, -.5f * ((float)(r1 + extrar) / maxr)),
  1654. uv4(0.f, 0.f, -.5f * ((float)(r2 + extrar) / maxr));
  1655. p2 = rotmat * p2; uv2 = rotmat * uv2;
  1656. p4 = rotmat * p4; uv4 = rotmat * uv4;
  1657. rotmat = rotmat * rotmat;
  1658. for (int i = 0; i < nbranches; i++)
  1659. {
  1660. AddVertex(p1); SetCurVertTexCoord(uv1.xz + vec2(.5f)); SetCurVertTexCoord2(uv1.xz + vec2(.5f));
  1661. AddVertex(p2); SetCurVertTexCoord(uv2.xz + vec2(.5f)); SetCurVertTexCoord2(uv2.xz + vec2(.5f));
  1662. AddVertex(p3); SetCurVertTexCoord(uv3.xz + vec2(.5f)); SetCurVertTexCoord2(uv3.xz + vec2(.5f)); SetCurVertColor(BD()->Color2());
  1663. AddVertex(p4); SetCurVertTexCoord(uv4.xz + vec2(.5f)); SetCurVertTexCoord2(uv4.xz + vec2(.5f)); SetCurVertColor(BD()->Color2());
  1664. int j = (i + 1) % nbranches;
  1665. AppendQuad(0, 4 * i + 1, 4 * i + 2, 4 * j + 1, vbase);
  1666. AppendQuad(4 * i + 1, 4 * i + 3, 4 * i + 4, 4 * i + 2, vbase);
  1667. AppendQuad(4 * j + 1, 4 * i + 2, 4 * i + 4, 4 * j + 3, vbase);
  1668. p1 = rotmat * p1; uv1 = rotmat * uv1;
  1669. p2 = rotmat * p2; uv2 = rotmat * uv2;
  1670. p3 = rotmat * p3; uv3 = rotmat * uv3;
  1671. p4 = rotmat * p4; uv4 = rotmat * uv4;
  1672. }
  1673. }
  1674. //-----------------------------------------------------------------------------
  1675. void EasyMesh::AppendDisc(int nsides, float d, int fade)
  1676. {
  1677. //XXX : This operation is done to convert radius to diameter without changing all the code.
  1678. float r = d * .5f;
  1679. int vbase = m_vert.Count();
  1680. AddVertex(vec3(0.f, 0.f, 0.f)); SetCurVertTexCoord(vec2(.5f, .5f)); SetCurVertTexCoord2(vec2(.5f, .5f));
  1681. mat3 rotmat = mat3::rotate(360.0f / nsides, 0.f, 1.f, 0.f);
  1682. vec3 p1(r, 0.f, 0.f);
  1683. vec3 uv(.5f, .0f, .0f);
  1684. for (int i = 0; i < nsides; i++)
  1685. {
  1686. AddVertex(p1); SetCurVertTexCoord(uv.xz + vec2(.5f, .5f)); SetCurVertTexCoord2(uv.xz + vec2(.5f, .5f));
  1687. if (fade)
  1688. SetCurVertColor(BD()->Color2());
  1689. AppendTriangle(0, i + 1, ((i + 1) % nsides) + 1, vbase);
  1690. p1 = rotmat * p1;
  1691. uv = rotmat * uv;
  1692. }
  1693. }
  1694. //-----------------------------------------------------------------------------
  1695. void EasyMesh::AppendSimpleTriangle(float d, int fade)
  1696. {
  1697. //XXX : This operation is done to convert radius to diameter without changing all the code.
  1698. float size = d * .5f;
  1699. mat3 m = mat3::rotate(120.f, 0.f, 1.f, 0.f);
  1700. vec3 p(0.f, 0.f, size);
  1701. AddVertex(p); SetCurVertTexCoord(vec2(.5f, 0.133975f)); SetCurVertTexCoord2(vec2(.5f, 0.133975f));
  1702. p = m * p;
  1703. AddVertex(p); SetCurVertTexCoord(vec2(0.f, 1.f)); SetCurVertTexCoord2(vec2(0.f, 1.f));
  1704. if (fade)
  1705. SetCurVertColor(BD()->Color2());
  1706. p = m * p;
  1707. AddVertex(p); SetCurVertTexCoord(vec2(1.f, 1.f)); SetCurVertTexCoord2(vec2(1.f, 1.f));
  1708. if (fade)
  1709. SetCurVertColor(BD()->Color2());
  1710. AppendTriangle(0, 1, 2, m_vert.Count() - 3);
  1711. }
  1712. //-----------------------------------------------------------------------------
  1713. void EasyMesh::AppendSimpleQuad(float size, int fade)
  1714. {
  1715. AppendSimpleQuad(vec2(size * .5f), vec2(size * -.5f), 0.f, fade);
  1716. }
  1717. //-----------------------------------------------------------------------------
  1718. void EasyMesh::AppendSimpleQuad(vec2 p1, vec2 p2, float z, int fade)
  1719. {
  1720. MeshType mt = MeshType::Quad;
  1721. MeshFaceType mft = MeshFaceType::QuadDefault;
  1722. //--
  1723. AddVertex(vec3(p2.x, z, -p1.y));
  1724. TexCoordPos br = TexCoordPos::BR;
  1725. SetCurVertTexCoord(BD()->TexCoord(mt, br, mft));
  1726. SetCurVertTexCoord2(BD()->TexCoord2(mt, br, mft));
  1727. //--
  1728. AddVertex(vec3(p2.x, z, -p2.y));
  1729. TexCoordPos bl = TexCoordPos::BL;
  1730. SetCurVertTexCoord(BD()->TexCoord(mt, bl, mft));
  1731. SetCurVertTexCoord2(BD()->TexCoord2(mt, bl, mft));
  1732. //--
  1733. AddVertex(vec3(p1.x, z, -p2.y));
  1734. TexCoordPos tl = TexCoordPos::TL;
  1735. SetCurVertTexCoord(BD()->TexCoord(mt, tl, mft));
  1736. SetCurVertTexCoord2(BD()->TexCoord2(mt, tl, mft));
  1737. if (fade) SetCurVertColor(BD()->Color2());
  1738. //--
  1739. AddVertex(vec3(p1.x, z, -p1.y));
  1740. TexCoordPos tr = TexCoordPos::TR;
  1741. SetCurVertTexCoord(BD()->TexCoord(mt, tr, mft));
  1742. SetCurVertTexCoord2(BD()->TexCoord2(mt, tr, mft));
  1743. if (fade) SetCurVertColor(BD()->Color2());
  1744. AppendQuad(0, 1, 2, 3, m_vert.Count() - 4);
  1745. ComputeNormals(m_indices.Count() - 6, 6);
  1746. }
  1747. //-----------------------------------------------------------------------------
  1748. void EasyMesh::AppendCog(int nbsides, float h, float d10, float d20,
  1749. float d1, float d2, float d12, float d22,
  1750. float sidemul, int offset)
  1751. {
  1752. //XXX : This operation is done to convert radius to diameter without changing all the code.
  1753. float r10 = d10 * .5f;
  1754. float r20 = d20 * .5f;
  1755. float r1 = d1 * .5f;
  1756. float r2 = d2 * .5f;
  1757. float r12 = d12 * .5f;
  1758. float r22 = d22 * .5f;
  1759. int ibase = m_indices.Count();
  1760. int vbase = m_vert.Count();
  1761. /* FIXME: enforce this some other way */
  1762. if (r12 < 0)
  1763. h = -h;
  1764. mat3 rotmat = mat3::rotate(180.0f / nbsides, 0.f, 1.f, 0.f);
  1765. mat3 smat1 = mat3::rotate(sidemul * 180.0f / nbsides, 0.f, 1.f, 0.f);
  1766. mat3 smat2 = mat3::rotate(sidemul * -360.0f / nbsides, 0.f, 1.f, 0.f);
  1767. vec3 p[12];
  1768. //Upper points
  1769. p[0] = vec3(r10, h * .5f, 0.f);
  1770. p[1] = rotmat * p[0];
  1771. p[2] = vec3(r1, h * .5f, 0.f);
  1772. p[3] = rotmat * p[2];
  1773. p[4] = smat1 * (rotmat * vec3(r1 + r12, h * .5f, 0.f));
  1774. p[5] = smat2 * (rotmat * p[4]);
  1775. //Lower points
  1776. p[6] = vec3(r20, h * -.5f, 0.f);
  1777. p[7] = rotmat * p[6];
  1778. p[8] = vec3(r2, h * -.5f, 0.f);
  1779. p[9] = rotmat * p[8];
  1780. p[10] = smat1 * (rotmat * vec3(r2 + r22, h * -.5f, 0.f));
  1781. p[11] = smat2 * (rotmat * p[10]);
  1782. if (offset & 1)
  1783. for (int n = 0; n < 12; n++)
  1784. p[n] = rotmat * p[n];
  1785. rotmat = rotmat * rotmat;
  1786. //UV base computation
  1787. float maxr = max(max(r1 + r12, r2 + r22), max(r10, r20));
  1788. float InLn = length(p[1] - p[0]);
  1789. float CogLn[8] = { .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f };
  1790. for (int i = 0; i < 3; i++)
  1791. {
  1792. for (int j = 0, k = 2; j < 8 && k < 12; j += 4, k += 6)
  1793. {
  1794. CogLn[j + i] = length(p[k + i + 1] - p[k + i]);
  1795. CogLn[j + 3] += CogLn[j + i];
  1796. if (i == 1) //Add 3to4 twice since it's automatically completed by +1 loop.
  1797. CogLn[j + 3] += CogLn[j + i];
  1798. }
  1799. }
  1800. //Choose the biggest cog length
  1801. int CogSrc = (CogLn[7] > CogLn[3])?(4):(0);
  1802. CogLn[3] = CogLn[CogSrc + 3];
  1803. for (int i = 0; i < 3; i++)
  1804. CogLn[i] = CogLn[CogSrc + i] / CogLn[CogSrc + 3];
  1805. //Calculate Cog Modifiers
  1806. vec2 InUV[2] = { vec2(.0f), vec2(.5f) };
  1807. vec2 CogUV[2] = { vec2(.0f), vec2(.5f) };
  1808. vec2 upadd = vec2(.25f, .75f);
  1809. vec2 lowadd = vec2(.75f, .75f);
  1810. {
  1811. if (h < InLn)
  1812. {
  1813. InUV[0].x = 1.0f;
  1814. InUV[0].y = h / InLn;
  1815. InUV[1].x = .0f;
  1816. InUV[1].y -= InUV[0].y * .5f;
  1817. }
  1818. else
  1819. {
  1820. InUV[0].x = InLn / h;
  1821. InUV[0].y = 1.0f;
  1822. InUV[1].x -= InUV[0].x * .5f;
  1823. InUV[1].y = .0f;
  1824. }
  1825. if (h < CogLn[3])
  1826. {
  1827. CogUV[0].x = 1.0f;
  1828. CogUV[0].y = h / CogLn[3];
  1829. CogUV[1].x = .0f;
  1830. CogUV[1].y -= CogUV[0].y * .5f;
  1831. }
  1832. else
  1833. {
  1834. CogUV[0].x = CogLn[3] / h;
  1835. CogUV[0].y = 1.0f;
  1836. CogUV[1].x -= CogUV[0].x * .5f;
  1837. CogUV[1].y = .0f;
  1838. }
  1839. if (InUV[0].x + CogUV[0].x < .5f)
  1840. {
  1841. InUV[1].x = .0f;
  1842. CogUV[1].x = .5f - CogUV[0].x;
  1843. upadd = vec2(.75f, .25f);
  1844. lowadd = vec2(.75f, .75f);
  1845. }
  1846. else if (InUV[0].y + CogUV[0].y < .5f)
  1847. {
  1848. InUV[1].y = .0f;
  1849. CogUV[1].y = .5f - CogUV[0].y;
  1850. }
  1851. else
  1852. {
  1853. InUV[0] *= .5f;
  1854. InUV[1] *= .5f;
  1855. CogUV[0] *= .5f;
  1856. CogUV[1] *= .5f;
  1857. InUV[1] += vec2(.5f, .0f);
  1858. }
  1859. }
  1860. //Build UV tab
  1861. vec2 uv[12]; float CogSz;
  1862. //Upper points
  1863. CogSz = 1.0f - CogLn[1];
  1864. uv[0] = vec2(0.f, 0.f) * InUV[0] + InUV[1];
  1865. uv[1] = vec2(1.f, 0.f) * InUV[0] + InUV[1];
  1866. uv[5] = vec2(CogSz, 0.f) * CogUV[0] + CogUV[1]; CogSz -= CogLn[2];
  1867. uv[4] = vec2(CogSz, 0.f) * CogUV[0] + CogUV[1]; CogSz -= CogLn[1];
  1868. uv[3] = vec2(CogSz, 0.f) * CogUV[0] + CogUV[1]; CogSz -= CogLn[0];
  1869. uv[2] = vec2(0.f, 0.f) * CogUV[0] + CogUV[1];
  1870. //Lower points
  1871. CogSz = 1.0f - CogLn[1];
  1872. uv[6] = vec2(0.f, 1.f) * InUV[0] + InUV[1];
  1873. uv[7] = vec2(1.f, 1.f) * InUV[0] + InUV[1];
  1874. uv[11] = vec2(CogSz, 1.f) * CogUV[0] + CogUV[1]; CogSz -= CogLn[2];
  1875. uv[10] = vec2(CogSz, 1.f) * CogUV[0] + CogUV[1]; CogSz -= CogLn[1];
  1876. uv[ 9] = vec2(CogSz, 1.f) * CogUV[0] + CogUV[1]; CogSz -= CogLn[0];
  1877. uv[ 8] = vec2(0.f, 1.f) * CogUV[0] + CogUV[1];
  1878. //Gear generation loop
  1879. for (int i = 0; i < nbsides; i++)
  1880. {
  1881. int j = 3 * 12 * i,
  1882. k = 3 * 12 * ((i + 1) % nbsides);
  1883. int q[] = { /* The top and bottom faces */
  1884. j, j, j, j,
  1885. j, j, j, j,
  1886. j, j, k, k,
  1887. k, k, j, j,
  1888. j, j, j, k,
  1889. k, j, j, j,
  1890. /* The inner side quads */
  1891. j, j, j, j,
  1892. j, k, k, j,
  1893. /* The outer side quads */
  1894. j, j, j, j,
  1895. j, j, j, j,
  1896. j, j, j, j,
  1897. k, j, j, k
  1898. };
  1899. int m[] = { /* The top and bottom faces */
  1900. 0, 2, 3, 1,
  1901. 7, 9, 8, 6,
  1902. 1, 3, 2, 0,
  1903. 6, 8, 9, 7,
  1904. 3, 4, 5, 2,
  1905. 8, 11, 10, 9,
  1906. /* The inner side quads */
  1907. 0, 1, 7, 6,
  1908. 1, 0, 6, 7,
  1909. /* The outer side quads */
  1910. 3, 2, 8, 9,
  1911. 4, 3, 9, 10,
  1912. 5, 4, 10, 11,
  1913. 2, 5, 11, 8
  1914. };
  1915. int a[] = { /* The top and bottom faces */
  1916. 0, 0, 0, 0,
  1917. 0, 0, 0, 0,
  1918. 0, 0, 0, 0,
  1919. 0, 0, 0, 0,
  1920. 0, 0, 0, 0,
  1921. 0, 0, 0, 0,
  1922. /* The inner side quads */
  1923. 1, 1, 1, 1,
  1924. 2, 2, 2, 2,
  1925. /* The outer side quads */
  1926. 1, 1, 1, 1,
  1927. 1, 2, 2, 1,
  1928. 1, 2, 2, 1,
  1929. 2, 2, 2, 2
  1930. };
  1931. /* Each vertex will share three faces, so three different
  1932. * normals, therefore we add each vertex three times. */
  1933. for (int n = 0; n < 3 * 12; n++)
  1934. {
  1935. int d = n / 3;
  1936. int e = d % 6;
  1937. AddVertex(p[d]);
  1938. if (n % 3 == 0) //Top-Bottom logic
  1939. {
  1940. vec2 tmp = (p[d].xz / maxr);
  1941. vec2 add;
  1942. if (d >= 6)
  1943. {
  1944. tmp *= -1.0f;
  1945. add = lowadd;
  1946. }
  1947. else
  1948. add = upadd;
  1949. SetCurVertTexCoord(tmp * vec2(.25f) + add);
  1950. SetCurVertTexCoord2(tmp * vec2(.25f) + add);
  1951. }
  1952. else if (e == 0 || e == 1) //inner Logic
  1953. {
  1954. SetCurVertTexCoord(uv[d]);
  1955. SetCurVertTexCoord2(uv[d]);
  1956. }
  1957. else //Cog logic
  1958. {
  1959. if (e == 2 && n % 3 == 2)
  1960. {
  1961. SetCurVertTexCoord(vec2(1.f, (d == 2)?(0.f):(1.f)) * CogUV[0] + CogUV[1]);
  1962. SetCurVertTexCoord2(vec2(1.f, (d == 2)?(0.f):(1.f)) * CogUV[0] + CogUV[1]);
  1963. }
  1964. else
  1965. {
  1966. SetCurVertTexCoord(uv[d]);
  1967. SetCurVertTexCoord2(uv[d]);
  1968. }
  1969. }
  1970. if (d >= 6)
  1971. SetCurVertColor(BD()->Color2());
  1972. }
  1973. int l = -4;
  1974. while ((l += 4) < 48)
  1975. AppendQuad(q[l + 0] + m[l + 0] * 3 + a[l + 0],
  1976. q[l + 1] + m[l + 1] * 3 + a[l + 1],
  1977. q[l + 2] + m[l + 2] * 3 + a[l + 2],
  1978. q[l + 3] + m[l + 3] * 3 + a[l + 3],
  1979. vbase);
  1980. for (int n = 0; n < 12; n++)
  1981. p[n] = rotmat * p[n];
  1982. }
  1983. ComputeNormals(ibase, m_indices.Count() - ibase);
  1984. }
  1985. //-----------------------------------------------------------------------------
  1986. void EasyMesh::Chamfer(float f)
  1987. {
  1988. int vlen = m_vert.Count() - m_cursors.Last().m1;
  1989. int ilen = m_indices.Count() - m_cursors.Last().m2;
  1990. /* Step 1: enumerate all faces. This is done by merging triangles
  1991. * that are coplanar and share an edge. */
  1992. int *triangle_classes = new int[ilen / 3];
  1993. for (int i = 0; i < ilen / 3; i++)
  1994. triangle_classes[i] = -1;
  1995. for (int i = 0; i < ilen / 3; i++)
  1996. {
  1997. }
  1998. /* Fun shit: reduce all triangles */
  1999. int *vertices = new int[vlen];
  2000. memset(vertices, 0, vlen * sizeof(int));
  2001. for (int i = 0; i < ilen; i++)
  2002. vertices[m_indices[i]]++;
  2003. for (int i = 0; i < ilen / 3; i++)
  2004. {
  2005. #if 0
  2006. if (vertices[m_indices[i * 3]] > 1)
  2007. continue;
  2008. if (vertices[m_indices[i * 3 + 1]] > 1)
  2009. continue;
  2010. if (vertices[m_indices[i * 3 + 2]] > 1)
  2011. continue;
  2012. #endif
  2013. vec3 bary = 1.f / 3.f * (m_vert[m_indices[i * 3]].m_coord +
  2014. m_vert[m_indices[i * 3 + 1]].m_coord +
  2015. m_vert[m_indices[i * 3 + 2]].m_coord);
  2016. for (int k = 0; k < 3; k++)
  2017. {
  2018. vec3 &p = m_vert[m_indices[i * 3 + k]].m_coord;
  2019. p -= normalize(p - bary) * f;
  2020. }
  2021. }
  2022. }
  2023. //-----------------------------------------------------------------------------
  2024. void EasyMesh::SplitTriangles(int pass) { SplitTriangles(pass, nullptr); }
  2025. //-----------------------------------------------------------------------------
  2026. void EasyMesh::SplitTriangles(int pass, VertexDictionnary *vert_dict)
  2027. {
  2028. while (pass--)
  2029. {
  2030. int trimax = m_indices.Count();
  2031. for (int i = m_cursors.Last().m2; i < trimax; i += 3)
  2032. {
  2033. int vbase = m_vert.Count();
  2034. int j = -1;
  2035. while (++j < 3)
  2036. {
  2037. AddLerpVertex(m_indices[i + j], m_indices[i + (j + 1) % 3], .5f);
  2038. if (vert_dict)
  2039. vert_dict->AddVertex(vbase + j, m_vert[vbase + j].m_coord);
  2040. }
  2041. //Add new triangles
  2042. AppendTriangle(vbase, m_indices[i + 1], vbase + 1, 0);
  2043. AppendTriangle(vbase + 2, vbase + 1, m_indices[i + 2], 0);
  2044. AppendTriangle(vbase, vbase + 1, vbase + 2, 0);
  2045. //Change current triangle
  2046. m_indices[i + 1] = vbase;
  2047. m_indices[i + 2] = vbase + 2;
  2048. }
  2049. }
  2050. ComputeNormals(m_cursors.Last().m2, m_indices.Count() - m_cursors.Last().m2);
  2051. }
  2052. //-----------------------------------------------------------------------------
  2053. //TODO : Add an half-edges implementation to refine smooth.
  2054. //TODO : Smooth should only use connected vertices that are on edges of the mesh (See box).
  2055. void EasyMesh::SmoothMesh(int main_pass, int split_per_main_pass, int smooth_per_main_pass)
  2056. {
  2057. VertexDictionnary vert_dict;
  2058. Array<vec3> smooth_buf[2];
  2059. Array<int> master_list;
  2060. Array<int> matching_ids;
  2061. Array<int> connected_vert;
  2062. int smbuf = 0;
  2063. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  2064. vert_dict.AddVertex(i, m_vert[i].m_coord);
  2065. while (main_pass--)
  2066. {
  2067. int split_pass = split_per_main_pass;
  2068. int smooth_pass = smooth_per_main_pass;
  2069. SplitTriangles(split_pass, &vert_dict);
  2070. matching_ids.Reserve(m_vert.Count() - m_cursors.Last().m1);
  2071. connected_vert.Reserve(m_vert.Count() - m_cursors.Last().m1);
  2072. smooth_buf[0].Resize(m_vert.Count() - m_cursors.Last().m1);
  2073. smooth_buf[1].Resize(m_vert.Count() - m_cursors.Last().m1);
  2074. for (int i = m_cursors.Last().m1; i < m_vert.Count(); i++)
  2075. smooth_buf[smbuf][i - m_cursors.Last().m1] = m_vert[i].m_coord;
  2076. while (smooth_pass--)
  2077. {
  2078. master_list.Empty();
  2079. if (vert_dict.GetMasterList(master_list))
  2080. {
  2081. for (int i = 0; i < master_list.Count(); i++)
  2082. {
  2083. connected_vert.Empty();
  2084. if (vert_dict.FindConnectedVertices(master_list[i], m_indices, m_cursors.Last().m2, connected_vert))
  2085. {
  2086. //Calculate vertices sum
  2087. vec3 vert_sum = vec3(.0f);
  2088. for (int j = 0; j < connected_vert.Count(); j++)
  2089. vert_sum += smooth_buf[smbuf][connected_vert[j] - m_cursors.Last().m1];
  2090. //Calculate new master vertex
  2091. float n = (float)connected_vert.Count();
  2092. //b(n) = 5/4 - pow(3 + 2 * cos(2 * M_PI / n), 2) / 32
  2093. float beta = 3.f + 2.f * cos(2.f * (float)M_PI / n);
  2094. beta = 5.f / 4.f - beta * beta / 32.f;
  2095. //a(n) = n * (1 - b(n)) / b(n)
  2096. float alpha = (n * (1 - beta)) / beta;
  2097. //V = (a(n) * v + v1 + ... + vn) / (a(n) + n)
  2098. vec3 new_vert = (alpha * smooth_buf[smbuf][master_list[i] - m_cursors.Last().m1] + vert_sum) / (alpha + n);
  2099. //Set all matching vertices to new value
  2100. matching_ids.Empty();
  2101. matching_ids << master_list[i];
  2102. vert_dict.FindMatchingVertices(master_list[i], matching_ids);
  2103. for (int j = 0; j < matching_ids.Count(); j++)
  2104. smooth_buf[1 - smbuf][matching_ids[j] - m_cursors.Last().m1] = new_vert;
  2105. }
  2106. }
  2107. }
  2108. smbuf = 1 - smbuf;
  2109. }
  2110. for (int i = 0; i < smooth_buf[smbuf].Count(); i++)
  2111. m_vert[i + m_cursors.Last().m1].m_coord = smooth_buf[smbuf][i];
  2112. }
  2113. }
  2114. } /* namespace lol */