1  // 

2  // Lol Engine  EasyMesh tutorial 

3  // 

4  // Copyright: (c) 20112013 Sam Hocevar <sam@hocevar.net> 

5  // (c) 20122013 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  

12  #if defined HAVE_CONFIG_H 

13  # include "config.h" 

14  #endif 

15  

16  #include "core.h" 

17  

18  using namespace std; 

19  using namespace lol; 

20  #define MESH_DIST 5.0f 

21  

22  class MeshViewer : public WorldEntity 

23  { 

24  public: 

25  MeshViewer() 

26  { 

27  int i=10; 

28  while (i) 

29  { 

30  m_meshes.Push(EasyMesh()); 

31  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]]"); 

32  } 

33  

34  m_angle = 0; 

35  

36  m_camera = new Camera(vec3(0.f, 600.f, 0.f), 

37  vec3(0.f, 0.f, 0.f), 

38  vec3(0, 1, 0)); 

39  m_camera>SetPerspective(60.f, 16, 9, .1f, 1000.f); 

40  m_camera>SetTarget(vec3(0.f, 0.f, 0.f)); 

41  m_camera>SetPosition(vec3(0.f, 0.f, 5.f)); 

42  m_camera>ForceSceneUpdate(); 

43  Ticker::Ref(m_camera); 

44  min_pos = vec3(FLT_MAX); 

45  max_pos = vec3(FLT_MAX); 

46  

47  m_ready = false; 

48  } 

49  

50  ~MeshViewer() 

51  { 

52  Ticker::Unref(m_camera); 

53  } 

54  

55  virtual void TickGame(float seconds) 

56  { 

57  WorldEntity::TickGame(seconds); 

58  //vec4 vertex = in_ModelView * vec4(in_Vertex, 1.0); 

59  // gl_Position = in_Proj * vertex; 

60  

61  

62  m_angle += seconds * 70.0f; 

63  m_mat = mat4::rotate(m_angle, vec3(0, 1, 0)); 

64  

65  //mat4 screen_matrix = Scene::GetDefault()>GetProjMatrix() * Scene::GetDefault()>GetViewMatrix(); 

66  mat4 world_view_matrix = m_camera>GetViewMatrix() * m_mat; 

67  mat4 world_screen_matrix = m_camera>GetProjMatrix() * world_view_matrix; 

68  mat4 view_world_matrix = inverse(m_camera>GetViewMatrix()); 

69  mat4 screen_view_matrix = inverse(m_camera>GetProjMatrix()); 

70  vec4 a(0, 2, 0, 1.0f); 

71  vec4 b(0,2, 0, 1.0f); 

72  vec4 c(0, 0, 2, 1.0f); 

73  vec4 d(1, 1, 1, 1.0f); 

74  

75  //vec2 toto; 

76  //near plane : vec4(toto, 1.f, 1.f);


77  //far plane : vec4(toto, 1.f, 1.f); 

78  

79  a = vec4((world_screen_matrix * a).xyz / a.w, a.w); 

80  b = vec4((world_screen_matrix * b).xyz / b.w, b.w); 

81  c = vec4((inverse(world_screen_matrix) * c).xyz / c.w, c.w); 

82  d = vec4((inverse(world_screen_matrix) * d).xyz / d.w, d.w); 

83  a = b;


84  c = d; 

85  

86  //this is the algorithm for a camera that must keep n target in the screen 

87  { 

88  //Get the Min/Max needed 

89  vec3 new_min_pos(FLT_MAX); 

90  vec3 new_max_pos(FLT_MAX); 

91  for (int i = 0; i < m_meshes.Last().GetVertexCount(); i++) 

92  { 

93  vec4 vpos = world_view_matrix * vec4(m_meshes.Last().GetVertexLocation(i), 1.0f); 

94  new_min_pos = min(vpos.xyz, new_min_pos); 

95  new_max_pos = max(vpos.xyz, new_max_pos); 

96  } 

97  

98  //Actual camera algorithm 

99  { 

100  vec4 BottomLeft = m_camera>GetProjMatrix() * vec4(new_min_pos.xy, new_min_pos.z, 1.0f); 

101  vec4 TopRight = m_camera>GetProjMatrix() * vec4(new_max_pos.xy, new_min_pos.z, 1.0f); 

102  BottomLeft = vec4(BottomLeft.xyz / BottomLeft.w, BottomLeft.w); 

103  TopRight = vec4(TopRight.xyz / TopRight.w, TopRight.w); 

104  //vec2 NewSize = TopRight.xy  BottomLeft.xy; 

105  //NewSize.x = max(NewSize.x, NewSize.y) * 1.5f; 

106  vec4 DistantPoint = screen_view_matrix * vec4(vec2(1.0f, 1.0f), TopRight.z * TopRight.w, TopRight.w); 

107  

108  vec3 vcenter = vec3(new_min_pos.xy + new_max_pos.xy, .0f) * .5f; 

109  vec4 new_pos = screen_view_matrix * vec4(.0f, .0f, new_min_pos.z, 1.0f); 

110  //vcenter.z += (new_pos.z  new_pos.z * NewSize.x); 

111  vcenter = (view_world_matrix * vec4(vcenter, 1.0f)).xyz; 

112  

113  m_camera>SetPosition(damp(m_camera>GetPosition(), vcenter, 0.4f, seconds)); 

114  //m_camera>SetPosition(vcenter); 

115  m_camera>SetTarget(m_camera>GetPosition() + vec3(0, 0, 5.0f)); 

116  } 

117  // 

118  } 

119  } 

120  

121  virtual void TickDraw(float seconds) 

122  { 

123  WorldEntity::TickDraw(seconds); 

124  

125  if (!m_ready) 

126  { 

127  for (int i = 0; i < m_meshes.Count(); i++) 

128  m_meshes[i].MeshConvert(); 

129  m_ready = true; 

130  } 

131  

132  Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); 

133  

134  m_mat = mat4::translate(vec3(m_meshes.Count() * MESH_DIST * 0.5f, 0, m_meshes.Count() * MESH_DIST) * 1.0f) * m_mat; 

135  for (int i = 0; i < m_meshes.Count(); i++) 

136  { 

137  m_mat = mat4::translate(vec3(MESH_DIST * 0.5f, 0, MESH_DIST)) * m_mat; 

138  m_meshes[i].Render(m_mat); 

139  Video::Clear(ClearMask::Depth); 

140  } 

141  //m_mat = mat4::translate(vec3(.0f)); 

142  //m_meshes.Last().Render(m_mat); 

143  } 

144  

145  private: 

146  //Array<EasyMesh, mat4, float> m_gears; 

147  float m_angle; 

148  mat4 m_mat; 

149  vec3 min_pos; 

150  vec3 max_pos; 

151  

152  Array<EasyMesh> m_meshes; 

153  Camera *m_camera; 

154  

155  bool m_ready; 

156  }; 

157  

158  int main(int argc, char **argv) 

159  { 

160  System::Init(argc, argv); 

161  

162  Application app("MeshViewer", ivec2(960, 600), 60.0f); 

163  new MeshViewer(); 

164  app.Run(); 

165  

166  return EXIT_SUCCESS; 

167  } 

168  
