選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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