Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

169 рядки
5.4 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 MESH_DIST 5.0f
  18. class MeshViewer : public WorldEntity
  19. {
  20. public:
  21. MeshViewer()
  22. {
  23. int i=10;
  24. while (i--)
  25. {
  26. m_meshes.Push(EasyMesh());
  27. m_meshes.Last().Compile("[sc#0f0 ab 2 2 2 t .8 .8 .8 rx 20 ry 20 [sc#00f ab 2 2 2 tx 0 csgu]]");
  28. }
  29. m_angle = 0;
  30. m_camera = new Camera(vec3(0.f, 600.f, 0.f),
  31. vec3(0.f, 0.f, 0.f),
  32. vec3(0, 1, 0));
  33. m_camera->SetPerspective(60.f, 16, 9, .1f, 1000.f);
  34. m_camera->SetTarget(vec3(0.f, 0.f, 0.f));
  35. m_camera->SetPosition(vec3(0.f, 0.f, 5.f));
  36. m_camera->ForceSceneUpdate();
  37. Ticker::Ref(m_camera);
  38. min_pos = vec3(FLT_MAX);
  39. max_pos = vec3(-FLT_MAX);
  40. m_ready = false;
  41. }
  42. ~MeshViewer()
  43. {
  44. Ticker::Unref(m_camera);
  45. }
  46. virtual void TickGame(float seconds)
  47. {
  48. WorldEntity::TickGame(seconds);
  49. //vec4 vertex = in_ModelView * vec4(in_Vertex, 1.0);
  50. // gl_Position = in_Proj * vertex;
  51. m_angle += seconds * 70.0f;
  52. m_mat = mat4::rotate(m_angle, vec3(0, 1, 0));
  53. //mat4 screen_matrix = Scene::GetDefault()->GetProjMatrix() * Scene::GetDefault()->GetViewMatrix();
  54. mat4 world_view_matrix = m_camera->GetViewMatrix() * m_mat;
  55. mat4 world_screen_matrix = m_camera->GetProjMatrix() * world_view_matrix;
  56. mat4 view_world_matrix = inverse(m_camera->GetViewMatrix());
  57. mat4 screen_view_matrix = inverse(m_camera->GetProjMatrix());
  58. vec4 a(0, 2, 0, 1.0f);
  59. vec4 b(0,-2, 0, 1.0f);
  60. vec4 c(0, 0, -2, 1.0f);
  61. vec4 d(-1, -1, 1, 1.0f);
  62. //vec2 toto;
  63. //near plane : vec4(toto, 1.f, 1.f);
  64. //far plane : vec4(toto, -1.f, 1.f);
  65. a = vec4((world_screen_matrix * a).xyz / a.w, a.w);
  66. b = vec4((world_screen_matrix * b).xyz / b.w, b.w);
  67. c = vec4((inverse(world_screen_matrix) * c).xyz / c.w, c.w);
  68. d = vec4((inverse(world_screen_matrix) * d).xyz / d.w, d.w);
  69. a = b;
  70. c = d;
  71. //this is the algorithm for a camera that must keep n target in the screen
  72. {
  73. //Get the Min/Max needed
  74. vec3 new_min_pos(FLT_MAX);
  75. vec3 new_max_pos(-FLT_MAX);
  76. for (int i = 0; i < m_meshes.Last().GetVertexCount(); i++)
  77. {
  78. vec4 vpos = world_view_matrix * vec4(m_meshes.Last().GetVertexLocation(i), 1.0f);
  79. new_min_pos = min(vpos.xyz, new_min_pos);
  80. new_max_pos = max(vpos.xyz, new_max_pos);
  81. }
  82. //Actual camera algorithm
  83. {
  84. vec4 BottomLeft = m_camera->GetProjMatrix() * vec4(new_min_pos.xy, new_min_pos.z, 1.0f);
  85. vec4 TopRight = m_camera->GetProjMatrix() * vec4(new_max_pos.xy, new_min_pos.z, 1.0f);
  86. BottomLeft = vec4(BottomLeft.xyz / BottomLeft.w, BottomLeft.w);
  87. TopRight = vec4(TopRight.xyz / TopRight.w, TopRight.w);
  88. //vec2 NewSize = TopRight.xy - BottomLeft.xy;
  89. //NewSize.x = max(NewSize.x, NewSize.y) * 1.5f;
  90. vec4 DistantPoint = screen_view_matrix * vec4(vec2(1.0f, 1.0f), TopRight.z * TopRight.w, TopRight.w);
  91. vec3 vcenter = vec3(new_min_pos.xy + new_max_pos.xy, .0f) * .5f;
  92. vec4 new_pos = screen_view_matrix * vec4(.0f, .0f, new_min_pos.z, 1.0f);
  93. //vcenter.z += (new_pos.z - new_pos.z * NewSize.x);
  94. vcenter = (view_world_matrix * vec4(vcenter, 1.0f)).xyz;
  95. m_camera->SetPosition(damp(m_camera->GetPosition(), vcenter, 0.4f, seconds));
  96. //m_camera->SetPosition(vcenter);
  97. m_camera->SetTarget(m_camera->GetPosition() + vec3(0, 0, -5.0f));
  98. }
  99. //
  100. }
  101. }
  102. virtual void TickDraw(float seconds)
  103. {
  104. WorldEntity::TickDraw(seconds);
  105. if (!m_ready)
  106. {
  107. for (int i = 0; i < m_meshes.Count(); i++)
  108. m_meshes[i].MeshConvert();
  109. m_ready = true;
  110. }
  111. Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  112. m_mat = mat4::translate(vec3(m_meshes.Count() * MESH_DIST * 0.5f, 0, m_meshes.Count() * MESH_DIST) * -1.0f) * m_mat;
  113. for (int i = 0; i < m_meshes.Count(); i++)
  114. {
  115. m_mat = mat4::translate(vec3(MESH_DIST * 0.5f, 0, MESH_DIST)) * m_mat;
  116. m_meshes[i].Render(m_mat);
  117. Video::Clear(ClearMask::Depth);
  118. }
  119. //m_mat = mat4::translate(vec3(.0f));
  120. //m_meshes.Last().Render(m_mat);
  121. }
  122. private:
  123. //Array<EasyMesh, mat4, float> m_gears;
  124. float m_angle;
  125. mat4 m_mat;
  126. vec3 min_pos;
  127. vec3 max_pos;
  128. Array<EasyMesh> m_meshes;
  129. Camera *m_camera;
  130. bool m_ready;
  131. };
  132. int main(int argc, char **argv)
  133. {
  134. System::Init(argc, argv);
  135. Application app("MeshViewer", ivec2(960, 600), 60.0f);
  136. new MeshViewer();
  137. app.Run();
  138. return EXIT_SUCCESS;
  139. }