您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

175 行
6.2 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& scene = Scene::GetScene();
  56. scene.PushCamera(m_camera);
  57. /* Add a white directional light */
  58. m_light1 = new Light();
  59. m_light1->SetPosition(vec3(0.2f, 0.2f, 0.f));
  60. m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
  61. m_light1->SetType(LightType::Directional);
  62. Ticker::Ref(m_light1);
  63. /* Add an orangeish point light */
  64. m_light2 = new Light();
  65. m_light2->SetPosition(vec3(-15.f, 15.f, 15.f));
  66. m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
  67. m_light2->SetType(LightType::Point);
  68. Ticker::Ref(m_light2);
  69. m_ready = false;
  70. }
  71. ~EasyMeshTutorial()
  72. {
  73. Scene& scene = Scene::GetScene();
  74. scene.PopCamera(m_camera);
  75. Ticker::Unref(m_light1);
  76. Ticker::Unref(m_light2);
  77. }
  78. virtual void TickGame(float seconds)
  79. {
  80. WorldEntity::TickGame(seconds);
  81. m_angle += seconds * 70.0f;
  82. m_mat = mat4::rotate(10.0f, vec3(0, 0, 1))
  83. * mat4::rotate(100, vec3(0, 1, 0));
  84. // * mat4::rotate(m_angle, vec3(0, 1, 0));
  85. m_gears[0].m3 += seconds * 20.0f;
  86. m_gears[1].m3 += seconds * 20.0f * -2 / 9;
  87. m_gears[2].m3 += seconds * 20.0f * -2 / 3;
  88. m_gears[3].m3 += seconds * 20.0f * -2 / 3;
  89. m_gears[4].m3 += seconds * 20.0f * -2 / 3;
  90. m_gears[0].m2 = mat4::translate(vec3(0, -1, 0))
  91. * mat4::rotate(m_gears[0].m3 - 130.0f, vec3(0, 1, 0))
  92. * mat4::rotate(40.0f, vec3(0, 0, 1));
  93. m_gears[1].m2 = mat4::translate(vec3(0, 0, 0))
  94. * mat4::rotate(m_gears[1].m3, vec3(0, 1, 0));
  95. m_gears[2].m2 = mat4::translate(vec3(0, 0, 5.5f))
  96. * mat4::rotate(m_gears[2].m3 - 40.0f, vec3(0, 1, 0))
  97. * mat4::rotate(90.0f, vec3(0, 0, 1));
  98. m_gears[3].m2 = mat4::translate(vec3(5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
  99. * mat4::rotate(m_gears[3].m3 - 140.0f, vec3(0, 1, 0))
  100. * mat4::rotate(-70.0f, vec3(0, 0, 1));
  101. m_gears[4].m2 = mat4::translate(vec3(-5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
  102. * mat4::rotate(m_gears[4].m3 - 80.0f, vec3(0, 1, 0));
  103. }
  104. virtual void TickDraw(float seconds, Scene &scene)
  105. {
  106. WorldEntity::TickDraw(seconds, scene);
  107. if (!m_ready)
  108. {
  109. Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  110. /* Upload vertex data to GPU */
  111. for (int i = 0; i < m_gears.count(); i++)
  112. m_gears[i].m1.MeshConvert();
  113. #if USE_CUSTOM_SHADER
  114. /* Custom Shader: Init the shader */
  115. Shader *custom_shader = Shader::Create(LOLFX_RESOURCE_NAME(shiny));
  116. // any other shader stuf here (Get uniform, mostly, and set texture)
  117. for (int i = 0; i < m_gears.count(); i++)
  118. m_gears[i].m1.SetMaterial(custom_shader);
  119. #endif
  120. m_ready = true;
  121. }
  122. for (int i = 0; i < m_gears.count(); i++)
  123. {
  124. m_gears[i].m1.Render(scene, m_mat * m_gears[i].m2);
  125. }
  126. }
  127. private:
  128. Shader* m_custom_shader;
  129. array<EasyMesh, mat4, float> m_gears;
  130. float m_angle;
  131. mat4 m_mat;
  132. Camera *m_camera;
  133. Light *m_light1, *m_light2;
  134. bool m_ready;
  135. };
  136. int main(int argc, char **argv)
  137. {
  138. System::Init(argc, argv);
  139. Application app("Tutorial 5: EasyMesh", ivec2(960, 600), 60.0f);
  140. new EasyMeshTutorial();
  141. app.Run();
  142. return EXIT_SUCCESS;
  143. }