Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

05_easymesh.cpp 6.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Lol Engine — EasyMesh tutorial
  3. //
  4. // Copyright © 2011—2015 Sam Hocevar <sam@hocevar.net>
  5. // © 2012—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #if HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif
  16. #include <lol/engine.h>
  17. using namespace lol;
  18. #define USE_CUSTOM_SHADER 1
  19. #if USE_CUSTOM_SHADER
  20. LOLFX_RESOURCE_DECLARE(easymesh_shiny);
  21. #endif //USE_CUSTOM_SHADER
  22. class EasyMeshTutorial : public WorldEntity
  23. {
  24. public:
  25. EasyMeshTutorial()
  26. {
  27. EasyMeshLuaLoader EzMhLoader;
  28. EzMhLoader.ExecLuaFile("05_easymesh.lua");
  29. EasyMeshLuaObject* gears0 = EzMhLoader.GetPtr<EasyMeshLuaObject>("g0");
  30. EasyMeshLuaObject* gears1 = EzMhLoader.GetPtr<EasyMeshLuaObject>("g1");
  31. EasyMeshLuaObject* gears2 = EzMhLoader.GetPtr<EasyMeshLuaObject>("g2");
  32. EasyMeshLuaObject* gears3 = EzMhLoader.GetPtr<EasyMeshLuaObject>("g3");
  33. EasyMeshLuaObject* gears4 = EzMhLoader.GetPtr<EasyMeshLuaObject>("g4");
  34. m_gears.push(gears0->GetMesh(), mat4(1.0f), 0.0f);
  35. m_gears.push(gears1->GetMesh(), mat4(1.0f), 0.0f);
  36. m_gears.push(gears2->GetMesh(), mat4(1.0f), 180.0f / 18);
  37. m_gears.push(gears3->GetMesh(), mat4(1.0f), 180.0f / 18);
  38. m_gears.push(gears4->GetMesh(), mat4(1.0f), 180.0f / 18);
  39. /*
  40. m_gears[0].m1.Compile("[sc#00f ab 8 1 8 ty -.25]"
  41. "[sc#f9f scb#f9f acg 12 10 5 5 20 20 5 5 0.1 0 s .1 .1 .1 ty -.1 csgu]"
  42. "[sc#fff scb#000 acg 12 10 10 10 20 20 5 5 0.1 0 s .05 .05 .05 tx -1.5 ty .3 csgu]"
  43. "[sc#00f ab 5 3 9 tx 2.5 csgs]"
  44. "[[ sc#fff ab 3 1.4 2 tx -2 tz -2 "
  45. "[sc#fff ab 2.1 .7 1.1 ty .5 tx -1.4 tz -1.4 csgs] mz] csgu]");
  46. m_gears[1].m1.Compile("sc#ff9 scb#ff9 acg 54 10 95 95 90 90 -5 -5 0.1 0 s .1 .1 .1");
  47. m_gears[2].m1.Compile("[sc#0f0 ab 2 2 2 t .8 .8 .8 rx 20 ry 20 [sc#00f ab 2 2 2 tx 0 csgu]]");
  48. m_gears[3].m1.Compile("[sc#0f0 ab 2 2 2 t .8 .8 .8 rx 20 ry 20 [sc#00f ab 2 2 2 tx 0 csgs]]");
  49. m_gears[4].m1.Compile("[sc#0f0 ab 2 2 2 t .8 .8 .8 rx 20 ry 20 [sc#00f ab 2 2 2 tx 0 csga]]");
  50. */
  51. m_angle = 0;
  52. m_camera = new Camera();
  53. m_camera->SetProjection(mat4::perspective(radians(30.f), 960.f, 600.f, .1f, 1000.f));
  54. m_camera->SetView(mat4::lookat(vec3(-15.f, 5.f, 0.f),
  55. vec3(0.f, -1.f, 0.f),
  56. vec3(0.f, 1.f, 0.f)));
  57. Scene& scene = Scene::GetScene();
  58. scene.PushCamera(m_camera);
  59. /* Add a white directional light */
  60. m_light1 = new Light();
  61. m_light1->SetPosition(vec3(0.2f, 0.2f, 0.f));
  62. m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
  63. m_light1->SetType(LightType::Directional);
  64. Ticker::Ref(m_light1);
  65. /* Add an orangeish point light */
  66. m_light2 = new Light();
  67. m_light2->SetPosition(vec3(-15.f, 15.f, 15.f));
  68. m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
  69. m_light2->SetType(LightType::Point);
  70. Ticker::Ref(m_light2);
  71. m_ready = false;
  72. }
  73. ~EasyMeshTutorial()
  74. {
  75. Scene& scene = Scene::GetScene();
  76. scene.PopCamera(m_camera);
  77. Ticker::Unref(m_light1);
  78. Ticker::Unref(m_light2);
  79. }
  80. virtual void TickGame(float seconds)
  81. {
  82. WorldEntity::TickGame(seconds);
  83. m_angle += seconds * radians(70.0f);
  84. m_mat = mat4::rotate(radians(10.0f), vec3(0, 0, 1))
  85. * mat4::rotate(radians(100.f), vec3(0, 1, 0));
  86. // * mat4::rotate(m_angle, vec3(0, 1, 0));
  87. m_gears[0].m3 += seconds * radians(20.0f);
  88. m_gears[1].m3 += seconds * radians(20.0f) * -2 / 9;
  89. m_gears[2].m3 += seconds * radians(20.0f) * -2 / 3;
  90. m_gears[3].m3 += seconds * radians(20.0f) * -2 / 3;
  91. m_gears[4].m3 += seconds * radians(20.0f) * -2 / 3;
  92. m_gears[0].m2 = mat4::translate(vec3(0, -1, 0))
  93. * mat4::rotate(m_gears[0].m3 - 130.0f, vec3(0, 1, 0))
  94. * mat4::rotate(40.0f, vec3(0, 0, 1));
  95. m_gears[1].m2 = mat4::translate(vec3(0, 0, 0))
  96. * mat4::rotate(m_gears[1].m3, vec3(0, 1, 0));
  97. m_gears[2].m2 = mat4::translate(vec3(0, 0, 5.5f))
  98. * mat4::rotate(m_gears[2].m3 - 40.0f, vec3(0, 1, 0))
  99. * mat4::rotate(90.0f, vec3(0, 0, 1));
  100. m_gears[3].m2 = mat4::translate(vec3(5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
  101. * mat4::rotate(m_gears[3].m3 - 140.0f, vec3(0, 1, 0))
  102. * mat4::rotate(-70.0f, vec3(0, 0, 1));
  103. m_gears[4].m2 = mat4::translate(vec3(-5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
  104. * mat4::rotate(m_gears[4].m3 - 80.0f, vec3(0, 1, 0));
  105. }
  106. virtual void TickDraw(float seconds, Scene &scene)
  107. {
  108. WorldEntity::TickDraw(seconds, scene);
  109. if (!m_ready)
  110. {
  111. Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  112. /* Upload vertex data to GPU */
  113. for (int i = 0; i < m_gears.count(); i++)
  114. m_gears[i].m1.MeshConvert();
  115. #if USE_CUSTOM_SHADER
  116. /* Custom Shader: Init the shader */
  117. Shader *custom_shader = Shader::Create(LOLFX_RESOURCE_NAME(easymesh_shiny));
  118. // any other shader stuf here (Get uniform, mostly, and set texture)
  119. for (int i = 0; i < m_gears.count(); i++)
  120. m_gears[i].m1.SetMaterial(custom_shader);
  121. #endif
  122. m_ready = true;
  123. }
  124. for (int i = 0; i < m_gears.count(); i++)
  125. {
  126. m_gears[i].m1.Render(scene, m_mat * m_gears[i].m2);
  127. }
  128. }
  129. private:
  130. Shader* m_custom_shader;
  131. array<EasyMesh, mat4, float> m_gears;
  132. float m_angle;
  133. mat4 m_mat;
  134. Camera *m_camera;
  135. Light *m_light1, *m_light2;
  136. bool m_ready;
  137. };
  138. int main(int argc, char **argv)
  139. {
  140. sys::init(argc, argv);
  141. Application app("Tutorial 5: EasyMesh", ivec2(960, 600), 60.0f);
  142. new EasyMeshTutorial();
  143. app.Run();
  144. return EXIT_SUCCESS;
  145. }