Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

05_easymesh.cpp 5.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Lol Engine - EasyMesh tutorial
  3. //
  4. // Copyright: (c) 2011-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2012-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #if defined HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include "core.h"
  15. using namespace std;
  16. using namespace lol;
  17. class EasyMeshTutorial : public WorldEntity
  18. {
  19. public:
  20. EasyMeshTutorial()
  21. {
  22. m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
  23. m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
  24. m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
  25. m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
  26. m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
  27. m_gears[0].m1.Compile("[sc#00f ab 8 1 8 ty -.25]"
  28. "[sc#f9f scb#f9f acg 12 10 5 5 20 20 5 5 0.1 0 s .1 .1 .1 ty -.1 csgu]"
  29. "[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]"
  30. "[sc#00f ab 5 3 9 tx 2.5 csgs]"
  31. "[[ sc#fff ab 3 1.4 2 tx -2 tz -2 "
  32. "[sc#fff ab 2.1 .7 1.1 ty .5 tx -1.4 tz -1.4 csgs] mz] csgu]");
  33. //m_gears[0].m1.Compile("[sc#f9f scb#f9f acg 12 10 5 5 20 20 5 5 0.1 0 s .1 .1 .1 [sc#00f ab 3 1 2 ty .25 tx 1 csgs]]");
  34. 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");
  35. //m_gears[2].m1.Compile("sc#9ff scb#9ff acg 18 10 5 5 30 30 5 5 0.1 0 s .1 .1 .1 [sc#00f scb#00f ab 2 2 2 tx 1.5]");
  36. //m_gears[3].m1.Compile("sc#9ff scb#9ff acg 18 10 5 5 30 30 5 5 0.1 0 s .1 .1 .1 [sc#00f scb#00f ab 2 2 2 tx 1.5]");
  37. //m_gears[4].m1.Compile("sc#9ff scb#9ff acg 18 10 5 5 30 30 5 5 0.1 0 s .1 .1 .1 [sc#00f scb#00f ab 2 2 2 tx 1.5]");
  38. 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]]");
  39. 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]]");
  40. 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]]");
  41. m_angle = 0;
  42. m_camera = new Camera(vec3(0.f, 600.f, 0.f),
  43. vec3(0.f, 0.f, 0.f),
  44. vec3(0, 1, 0));
  45. m_camera->SetPerspective(30.f, 960.f, 600.f, .1f, 1000.f);
  46. m_camera->SetTarget(vec3(0.f, -1.f, 0.f));
  47. m_camera->SetPosition(vec3(-15.f, 5.f, 0.f));
  48. Ticker::Ref(m_camera);
  49. /* Add a white directional light */
  50. m_light1 = new Light();
  51. m_light1->SetPosition(vec4(0.2f, 0.2f, 0.f, 0.f));
  52. m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
  53. Ticker::Ref(m_light1);
  54. /* Add an orangeish point light */
  55. m_light2 = new Light();
  56. m_light2->SetPosition(vec4(-15.f, 15.f, 15.f, 1.f));
  57. m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
  58. Ticker::Ref(m_light2);
  59. m_ready = false;
  60. }
  61. ~EasyMeshTutorial()
  62. {
  63. Ticker::Unref(m_camera);
  64. Ticker::Unref(m_light1);
  65. Ticker::Unref(m_light2);
  66. }
  67. virtual void TickGame(float seconds)
  68. {
  69. WorldEntity::TickGame(seconds);
  70. m_angle += seconds * 70.0f;
  71. m_mat = mat4::rotate(10.0f, vec3(0, 0, 1))
  72. * mat4::rotate(100, vec3(0, 1, 0));
  73. // * mat4::rotate(m_angle, vec3(0, 1, 0));
  74. m_gears[0].m3 += seconds * 20.0f;
  75. m_gears[1].m3 += seconds * 20.0f * -2 / 9;
  76. m_gears[2].m3 += seconds * 20.0f * -2 / 3;
  77. m_gears[3].m3 += seconds * 20.0f * -2 / 3;
  78. m_gears[4].m3 += seconds * 20.0f * -2 / 3;
  79. m_gears[0].m2 = mat4::translate(vec3(0, -1, 0))
  80. * mat4::rotate(m_gears[0].m3 - 130.0f, vec3(0, 1, 0))
  81. * mat4::rotate(40.0f, vec3(0, 0, 1));
  82. m_gears[1].m2 = mat4::translate(vec3(0, 0, 0))
  83. * mat4::rotate(m_gears[1].m3, vec3(0, 1, 0));
  84. m_gears[2].m2 = mat4::translate(vec3(0, 0, 5.5f))
  85. * mat4::rotate(m_gears[2].m3 - 40.0f, vec3(0, 1, 0))
  86. * mat4::rotate(90.0f, vec3(0, 0, 1));
  87. m_gears[3].m2 = mat4::translate(vec3(5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
  88. * mat4::rotate(m_gears[3].m3 - 140.0f, vec3(0, 1, 0))
  89. * mat4::rotate(-70.0f, vec3(0, 0, 1));
  90. m_gears[4].m2 = mat4::translate(vec3(-5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
  91. * mat4::rotate(m_gears[4].m3 - 80.0f, vec3(0, 1, 0));
  92. }
  93. virtual void TickDraw(float seconds)
  94. {
  95. WorldEntity::TickDraw(seconds);
  96. if (!m_ready)
  97. {
  98. for (int i = 0; i < m_gears.Count(); i++)
  99. m_gears[i].m1.MeshConvert();
  100. m_ready = true;
  101. }
  102. Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  103. for (int i = 0; i < m_gears.Count(); i++)
  104. m_gears[i].m1.Render(m_mat * m_gears[i].m2);
  105. }
  106. private:
  107. Array<EasyMesh, mat4, float> m_gears;
  108. float m_angle;
  109. mat4 m_mat;
  110. Camera *m_camera;
  111. Light *m_light1, *m_light2;
  112. bool m_ready;
  113. };
  114. int main(int argc, char **argv)
  115. {
  116. System::Init(argc, argv);
  117. Application app("Tutorial 5: EasyMesh", ivec2(960, 600), 60.0f);
  118. new EasyMeshTutorial();
  119. app.Run();
  120. return EXIT_SUCCESS;
  121. }