Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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