You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.1 KiB

  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. #define USE_CUSTOM_SHADER 1
  18. #if USE_CUSTOM_SHADER
  19. LOLFX_RESOURCE_DECLARE(shiny);
  20. #endif //USE_CUSTOM_SHADER
  21. class EasyMeshTutorial : public WorldEntity
  22. {
  23. public:
  24. EasyMeshTutorial()
  25. {
  26. m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
  27. m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
  28. m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
  29. m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
  30. m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
  31. m_gears[0].m1.Compile("[sc#00f ab 8 1 8 ty -.25]"
  32. "[sc#f9f scb#f9f acg 12 10 5 5 20 20 5 5 0.1 0 s .1 .1 .1 ty -.1 csgu]"
  33. "[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]"
  34. "[sc#00f ab 5 3 9 tx 2.5 csgs]"
  35. "[[ sc#fff ab 3 1.4 2 tx -2 tz -2 "
  36. "[sc#fff ab 2.1 .7 1.1 ty .5 tx -1.4 tz -1.4 csgs] mz] csgu]");
  37. //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]]");
  38. 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");
  39. //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]");
  40. //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]");
  41. //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]");
  42. 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]]");
  43. 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]]");
  44. 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]]");
  45. m_angle = 0;
  46. m_camera = new Camera();
  47. m_camera->SetProjection(mat4::perspective(30.f, 960.f, 600.f, .1f, 1000.f));
  48. m_camera->SetView(mat4::lookat(vec3(-15.f, 5.f, 0.f),
  49. vec3(0.f, -1.f, 0.f),
  50. vec3(0.f, 1.f, 0.f)));
  51. g_scene->PushCamera(m_camera);
  52. /* Add a white directional light */
  53. m_light1 = new Light();
  54. m_light1->SetPosition(vec3(0.2f, 0.2f, 0.f));
  55. m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
  56. m_light1->SetType(LightType::Directional);
  57. Ticker::Ref(m_light1);
  58. /* Add an orangeish point light */
  59. m_light2 = new Light();
  60. m_light2->SetPosition(vec3(-15.f, 15.f, 15.f));
  61. m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
  62. m_light2->SetType(LightType::Point);
  63. Ticker::Ref(m_light2);
  64. m_ready = false;
  65. }
  66. ~EasyMeshTutorial()
  67. {
  68. g_scene->PopCamera(m_camera);
  69. Ticker::Unref(m_light1);
  70. Ticker::Unref(m_light2);
  71. }
  72. virtual void TickGame(float seconds)
  73. {
  74. WorldEntity::TickGame(seconds);
  75. m_angle += seconds * 70.0f;
  76. m_mat = mat4::rotate(10.0f, vec3(0, 0, 1))
  77. * mat4::rotate(100, vec3(0, 1, 0));
  78. // * mat4::rotate(m_angle, vec3(0, 1, 0));
  79. m_gears[0].m3 += seconds * 20.0f;
  80. m_gears[1].m3 += seconds * 20.0f * -2 / 9;
  81. m_gears[2].m3 += seconds * 20.0f * -2 / 3;
  82. m_gears[3].m3 += seconds * 20.0f * -2 / 3;
  83. m_gears[4].m3 += seconds * 20.0f * -2 / 3;
  84. m_gears[0].m2 = mat4::translate(vec3(0, -1, 0))
  85. * mat4::rotate(m_gears[0].m3 - 130.0f, vec3(0, 1, 0))
  86. * mat4::rotate(40.0f, vec3(0, 0, 1));
  87. m_gears[1].m2 = mat4::translate(vec3(0, 0, 0))
  88. * mat4::rotate(m_gears[1].m3, vec3(0, 1, 0));
  89. m_gears[2].m2 = mat4::translate(vec3(0, 0, 5.5f))
  90. * mat4::rotate(m_gears[2].m3 - 40.0f, vec3(0, 1, 0))
  91. * mat4::rotate(90.0f, vec3(0, 0, 1));
  92. m_gears[3].m2 = mat4::translate(vec3(5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
  93. * mat4::rotate(m_gears[3].m3 - 140.0f, vec3(0, 1, 0))
  94. * mat4::rotate(-70.0f, vec3(0, 0, 1));
  95. m_gears[4].m2 = mat4::translate(vec3(-5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
  96. * mat4::rotate(m_gears[4].m3 - 80.0f, vec3(0, 1, 0));
  97. }
  98. virtual void TickDraw(float seconds, Scene &scene)
  99. {
  100. WorldEntity::TickDraw(seconds, scene);
  101. if (!m_ready)
  102. {
  103. g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  104. /* Upload vertex data to GPU */
  105. for (int i = 0; i < m_gears.Count(); i++)
  106. m_gears[i].m1.MeshConvert();
  107. #if USE_CUSTOM_SHADER
  108. /* Custom Shader: Init the shader */
  109. Shader *custom_shader = Shader::Create(LOLFX_RESOURCE_NAME(shiny));
  110. // any other shader stuf here (Get uniform, mostly, and set texture)
  111. for (int i = 0; i < m_gears.Count(); i++)
  112. m_gears[i].m1.SetMaterial(custom_shader);
  113. #endif
  114. m_ready = true;
  115. }
  116. for (int i = 0; i < m_gears.Count(); i++)
  117. {
  118. scene.AddPrimitive(m_gears[i].m1, m_mat * m_gears[i].m2);
  119. }
  120. }
  121. private:
  122. Shader* m_custom_shader;
  123. Array<EasyMesh, mat4, float> m_gears;
  124. float m_angle;
  125. mat4 m_mat;
  126. Camera *m_camera;
  127. Light *m_light1, *m_light2;
  128. bool m_ready;
  129. };
  130. int main(int argc, char **argv)
  131. {
  132. System::Init(argc, argv);
  133. Application app("Tutorial 5: EasyMesh", ivec2(960, 600), 60.0f);
  134. new EasyMeshTutorial();
  135. app.Run();
  136. return EXIT_SUCCESS;
  137. }