test_render.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*************************************************************************/
  2. /* test_render.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_render.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 "os/os.h"
  35. #include "quick_hull.h"
  36. #include "os/keyboard.h"
  37. #define OBJECT_COUNT 50
  38. namespace TestRender {
  39. class TestMainLoop : public MainLoop {
  40. RID test_cube;
  41. RID instance;
  42. RID camera;
  43. RID viewport;
  44. RID light;
  45. RID scenario;
  46. struct InstanceInfo {
  47. RID instance;
  48. Transform base;
  49. Vector3 rot_axis;
  50. };
  51. List<InstanceInfo> instances;
  52. float ofs;
  53. bool quit;
  54. protected:
  55. public:
  56. virtual void input_event(const InputEvent& p_event) {
  57. if (p_event.type==InputEvent::KEY && p_event.key.pressed)
  58. quit=true;
  59. }
  60. virtual void init() {
  61. print_line("INITIALIZING TEST RENDER");
  62. VisualServer *vs=VisualServer::get_singleton();
  63. test_cube = vs->get_test_cube();
  64. scenario = vs->scenario_create();
  65. Vector<Vector3> vts;
  66. /*
  67. DVector<Plane> sp = Geometry::build_sphere_planes(2,5,5);
  68. Geometry::MeshData md2 = Geometry::build_convex_mesh(sp);
  69. vts=md2.vertices;
  70. */
  71. /*
  72. static const int s = 20;
  73. for(int i=0;i<s;i++) {
  74. Matrix3 rot(Vector3(0,1,0),i*Math_PI/s);
  75. for(int j=0;j<s;j++) {
  76. Vector3 v;
  77. v.x=Math::sin(j*Math_PI*2/s);
  78. v.y=Math::cos(j*Math_PI*2/s);
  79. vts.push_back( rot.xform(v*2 ) );
  80. }
  81. }*/
  82. /*for(int i=0;i<100;i++) {
  83. vts.push_back( Vector3(Math::randf()*2-1.0,Math::randf()*2-1.0,Math::randf()*2-1.0).normalized()*2);
  84. }*/
  85. /*
  86. vts.push_back(Vector3(0,0,1));
  87. vts.push_back(Vector3(0,0,-1));
  88. vts.push_back(Vector3(0,1,0));
  89. vts.push_back(Vector3(0,-1,0));
  90. vts.push_back(Vector3(1,0,0));
  91. vts.push_back(Vector3(-1,0,0));*/
  92. vts.push_back(Vector3(1,1,1));
  93. vts.push_back(Vector3(1,-1,1));
  94. vts.push_back(Vector3(-1,1,1));
  95. vts.push_back(Vector3(-1,-1,1));
  96. vts.push_back(Vector3(1,1,-1));
  97. vts.push_back(Vector3(1,-1,-1));
  98. vts.push_back(Vector3(-1,1,-1));
  99. vts.push_back(Vector3(-1,-1,-1));
  100. Geometry::MeshData md;
  101. Error err = QuickHull::build(vts,md);
  102. print_line("ERR: "+itos(err));
  103. test_cube = vs->mesh_create();
  104. vs->mesh_add_surface_from_mesh_data(test_cube,md);
  105. //vs->scenario_set_debug(scenario,VS::SCENARIO_DEBUG_WIREFRAME);
  106. /*
  107. RID sm = vs->shader_create();
  108. //vs->shader_set_fragment_code(sm,"OUT_ALPHA=mod(TIME,1);");
  109. //vs->shader_set_vertex_code(sm,"OUT_VERTEX=IN_VERTEX*mod(TIME,1);");
  110. vs->shader_set_fragment_code(sm,"OUT_DIFFUSE=vec3(1,0,1);OUT_GLOW=abs(sin(TIME));");
  111. RID tcmat = vs->mesh_surface_get_material(test_cube,0);
  112. vs->material_set_shader(tcmat,sm);
  113. */
  114. List<String> cmdline = OS::get_singleton()->get_cmdline_args();
  115. int object_count = OBJECT_COUNT;
  116. if (cmdline.size() > 0 && cmdline[cmdline.size()-1].to_int()) {
  117. object_count = cmdline[cmdline.size()-1].to_int();
  118. };
  119. for (int i=0;i<object_count;i++) {
  120. InstanceInfo ii;
  121. ii.instance = vs->instance_create2( test_cube, scenario );
  122. ii.base.translate( Math::random(-20,20), Math::random(-20,20),Math::random(-20,18) );
  123. ii.base.rotate( Vector3(0,1,0), Math::randf() * Math_PI );
  124. ii.base.rotate( Vector3(1,0,0), Math::randf() * Math_PI );
  125. vs->instance_set_transform( ii.instance, ii.base );
  126. ii.rot_axis = Vector3( Math::random(-1,1), Math::random(-1,1), Math::random(-1,1) ).normalized();
  127. instances.push_back(ii);
  128. }
  129. camera = vs->camera_create();
  130. // vs->camera_set_perspective( camera, 60.0,0.1, 100.0 );
  131. viewport = vs->viewport_create();
  132. vs->viewport_attach_to_screen(viewport);
  133. vs->viewport_attach_camera( viewport, camera );
  134. vs->viewport_set_scenario( viewport, scenario );
  135. vs->camera_set_transform(camera, Transform( Matrix3(), Vector3(0,3,30 ) ) );
  136. vs->camera_set_perspective( camera, 60, 0.1, 1000);
  137. /*
  138. RID lightaux = vs->light_create( VisualServer::LIGHT_OMNI );
  139. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_RADIUS, 80 );
  140. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ATTENUATION, 1 );
  141. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ENERGY, 1.5 );
  142. light = vs->instance_create( lightaux );
  143. */
  144. RID lightaux;
  145. //*
  146. lightaux = vs->light_create( VisualServer::LIGHT_DIRECTIONAL );
  147. //vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
  148. vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_DIFFUSE, Color(1.0,1.0,1.0) );
  149. //vs->light_set_shadow( lightaux, true );
  150. light = vs->instance_create2( lightaux, scenario );
  151. Transform lla;
  152. //lla.set_look_at(Vector3(),Vector3(1,-1,1),Vector3(0,1,0));
  153. lla.set_look_at(Vector3(),Vector3(-0.000000,-0.836026,-0.548690),Vector3(0,1,0));
  154. vs->instance_set_transform( light, lla );
  155. // */
  156. //*
  157. lightaux = vs->light_create( VisualServer::LIGHT_OMNI );
  158. // vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,1.0) );
  159. vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_DIFFUSE, Color(1.0,1.0,0.0) );
  160. vs->light_set_param( lightaux, VisualServer::LIGHT_PARAM_RADIUS, 4 );
  161. vs->light_set_param( lightaux, VisualServer::LIGHT_PARAM_ENERGY, 8 );
  162. //vs->light_set_shadow( lightaux, true );
  163. //light = vs->instance_create( lightaux );
  164. // */
  165. ofs=0;
  166. quit=false;
  167. }
  168. virtual bool iteration(float p_time) {
  169. VisualServer *vs=VisualServer::get_singleton();
  170. //Transform t;
  171. //t.rotate(Vector3(0, 1, 0), ofs);
  172. //t.translate(Vector3(0,0,20 ));
  173. //vs->camera_set_transform(camera, t);
  174. ofs+=p_time*0.05;
  175. //return quit;
  176. for(List<InstanceInfo>::Element *E=instances.front();E;E=E->next()) {
  177. Transform pre( Matrix3(E->get().rot_axis, ofs), Vector3() );
  178. vs->instance_set_transform( E->get().instance, pre * E->get().base );
  179. /*
  180. if( !E->next() ) {
  181. vs->free( E->get().instance );
  182. instances.erase(E );
  183. }*/
  184. }
  185. return quit;
  186. }
  187. virtual bool idle(float p_time) {
  188. return quit;
  189. }
  190. virtual void finish() {
  191. }
  192. };
  193. MainLoop* test() {
  194. return memnew( TestMainLoop );
  195. }
  196. }