Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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