No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

MeshViewer.cpp 6.0 KiB

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