test_detailer.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*************************************************************************/
  2. /* test_detailer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "test_detailer.h"
  30. #include "servers/visual_server.h"
  31. #include "os/main_loop.h"
  32. #include "math_funcs.h"
  33. #include "print_string.h"
  34. #include "geometry.h"
  35. #include "quick_hull.h"
  36. namespace TestMultiMesh {
  37. class TestMainLoop : public MainLoop {
  38. RID instance;
  39. RID camera;
  40. RID viewport;
  41. RID light;
  42. RID mesh;
  43. RID scenario;
  44. #define MULTIMESH_COUNT 1500
  45. float ofs_x,ofs_y;
  46. bool quit;
  47. public:
  48. virtual void _update_qh() {
  49. VisualServer *vs=VisualServer::get_singleton();
  50. Vector<Vector3> vts;
  51. /*
  52. static const int s = 20;
  53. for(int i=0;i<s;i++) {
  54. Matrix3 rot(Vector3(0,1,0),i*Math_PI/s);
  55. for(int j=0;j<s;j++) {
  56. Vector3 v;
  57. v.x=Math::sin(j*Math_PI*2/s);
  58. v.y=Math::cos(j*Math_PI*2/s);
  59. vts.push_back( rot.xform(v*2 ) );
  60. }
  61. }*/
  62. /*
  63. Math::seed(0);
  64. for(int i=0;i<50;i++) {
  65. vts.push_back( Vector3(Math::randf()*2-1.0,Math::randf()*2-1.0,Math::randf()*2-1.0).normalized()*2);
  66. }*/
  67. /*
  68. vts.push_back(Vector3(0,0,1));
  69. vts.push_back(Vector3(0,0,-1));
  70. vts.push_back(Vector3(0,1,0));
  71. vts.push_back(Vector3(0,-1,0));
  72. vts.push_back(Vector3(1,0,0));
  73. vts.push_back(Vector3(-1,0,0));*/
  74. /*
  75. vts.push_back(Vector3(1,1,1));
  76. vts.push_back(Vector3(1,-1,1));
  77. vts.push_back(Vector3(-1,1,1));
  78. vts.push_back(Vector3(-1,-1,1));
  79. vts.push_back(Vector3(1,1,-1));
  80. vts.push_back(Vector3(1,-1,-1));
  81. vts.push_back(Vector3(-1,1,-1));
  82. vts.push_back(Vector3(-1,-1,-1));
  83. */
  84. DVector<Plane> convex_planes = Geometry::build_cylinder_planes(0.5,0.7,4,Vector3::AXIS_Z);
  85. Geometry::MeshData convex_data = Geometry::build_convex_mesh(convex_planes);
  86. vts=convex_data.vertices;
  87. Geometry::MeshData md;
  88. Error err = QuickHull::build(vts,md);
  89. print_line("ERR: "+itos(err));
  90. vs->mesh_remove_surface(mesh,0);
  91. vs->mesh_add_surface_from_mesh_data(mesh,md);
  92. //vs->scenario_set_debug(scenario,VS::SCENARIO_DEBUG_WIREFRAME);
  93. /*
  94. RID sm = vs->shader_create();
  95. //vs->shader_set_fragment_code(sm,"OUT_ALPHA=mod(TIME,1);");
  96. //vs->shader_set_vertex_code(sm,"OUT_VERTEX=IN_VERTEX*mod(TIME,1);");
  97. vs->shader_set_fragment_code(sm,"OUT_DIFFUSE=vec3(1,0,1);OUT_GLOW=abs(sin(TIME));");
  98. RID tcmat = vs->mesh_surface_get_material(test_cube,0);
  99. vs->material_set_shader(tcmat,sm);
  100. */
  101. }
  102. virtual void input_event(const InputEvent& p_event) {
  103. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&4) {
  104. ofs_x+=p_event.mouse_motion.relative_y/200.0;
  105. ofs_y+=p_event.mouse_motion.relative_x/200.0;
  106. }
  107. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index==1) {
  108. QuickHull::debug_stop_after++;
  109. _update_qh();
  110. }
  111. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index==2) {
  112. if (QuickHull::debug_stop_after>0)
  113. QuickHull::debug_stop_after--;
  114. _update_qh();
  115. }
  116. }
  117. virtual void request_quit() {
  118. quit=true;
  119. }
  120. virtual void init() {
  121. VisualServer *vs=VisualServer::get_singleton();
  122. mesh = vs->mesh_create();
  123. scenario = vs->scenario_create();
  124. QuickHull::debug_stop_after=0;
  125. _update_qh();
  126. instance = vs->instance_create2(mesh,scenario);
  127. camera = vs->camera_create();
  128. vs->camera_set_perspective( camera, 60.0,0.1, 100.0 );
  129. viewport = vs->viewport_create();
  130. vs->viewport_attach_camera( viewport, camera );
  131. vs->viewport_attach_to_screen(viewport);
  132. vs->viewport_set_scenario( viewport, scenario );
  133. vs->camera_set_transform(camera, Transform( Matrix3(), Vector3(0,0,2 ) ) );
  134. RID lightaux = vs->light_create( VisualServer::LIGHT_DIRECTIONAL );
  135. //vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.3,0.3,0.3) );
  136. light = vs->instance_create2( lightaux,scenario );
  137. vs->instance_set_transform(light,Transform(Matrix3(Vector3(0.1,0.4,0.7).normalized(),0.9)));
  138. ofs_x=0;
  139. ofs_y=0;
  140. quit=false;
  141. }
  142. virtual bool idle(float p_time) {
  143. return false;
  144. }
  145. virtual bool iteration(float p_time) {
  146. VisualServer *vs=VisualServer::get_singleton();
  147. Transform tr_camera;
  148. tr_camera.rotate( Vector3(0,1,0), ofs_y );
  149. tr_camera.rotate( Vector3(1,0,0),ofs_x );
  150. tr_camera.translate(0,0,10);
  151. vs->camera_set_transform( camera, tr_camera );
  152. return quit;
  153. }
  154. virtual void finish() {
  155. }
  156. };
  157. MainLoop* test() {
  158. return memnew(TestMainLoop);
  159. }
  160. }