test_render.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*************************************************************************/
  2. /* test_render.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "test_render.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/math/quick_hull.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/os/main_loop.h"
  35. #include "core/os/os.h"
  36. #include "core/print_string.h"
  37. #include "servers/visual_server.h"
  38. #define OBJECT_COUNT 50
  39. namespace TestRender {
  40. class TestMainLoop : public MainLoop {
  41. RID test_cube;
  42. RID instance;
  43. RID camera;
  44. RID viewport;
  45. RID light;
  46. RID scenario;
  47. struct InstanceInfo {
  48. RID instance;
  49. Transform base;
  50. Vector3 rot_axis;
  51. };
  52. List<InstanceInfo> instances;
  53. float ofs;
  54. bool quit;
  55. protected:
  56. public:
  57. virtual void input_event(const Ref<InputEvent> &p_event) {
  58. if (p_event->is_pressed())
  59. quit = true;
  60. }
  61. virtual void init() {
  62. print_line("INITIALIZING TEST RENDER");
  63. VisualServer *vs = VisualServer::get_singleton();
  64. test_cube = vs->get_test_cube();
  65. scenario = vs->scenario_create();
  66. Vector<Vector3> vts;
  67. /*
  68. PoolVector<Plane> sp = Geometry::build_sphere_planes(2,5,5);
  69. Geometry::MeshData md2 = Geometry::build_convex_mesh(sp);
  70. vts=md2.vertices;
  71. */
  72. /*
  73. static const int s = 20;
  74. for(int i=0;i<s;i++) {
  75. Basis rot(Vector3(0,1,0),i*Math_PI/s);
  76. for(int j=0;j<s;j++) {
  77. Vector3 v;
  78. v.x=Math::sin(j*Math_PI*2/s);
  79. v.y=Math::cos(j*Math_PI*2/s);
  80. vts.push_back( rot.xform(v*2 ) );
  81. }
  82. }*/
  83. /*for(int i=0;i<100;i++) {
  84. vts.push_back( Vector3(Math::randf()*2-1.0,Math::randf()*2-1.0,Math::randf()*2-1.0).normalized()*2);
  85. }*/
  86. /*
  87. vts.push_back(Vector3(0,0,1));
  88. vts.push_back(Vector3(0,0,-1));
  89. vts.push_back(Vector3(0,1,0));
  90. vts.push_back(Vector3(0,-1,0));
  91. vts.push_back(Vector3(1,0,0));
  92. vts.push_back(Vector3(-1,0,0));*/
  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. vts.push_back(Vector3(-1, -1, -1));
  101. Geometry::MeshData md;
  102. Error err = QuickHull::build(vts, md);
  103. print_line("ERR: " + itos(err));
  104. test_cube = vs->mesh_create();
  105. vs->mesh_add_surface_from_mesh_data(test_cube, md);
  106. //vs->scenario_set_debug(scenario,VS::SCENARIO_DEBUG_WIREFRAME);
  107. /*
  108. RID sm = vs->shader_create();
  109. //vs->shader_set_fragment_code(sm,"OUT_ALPHA=mod(TIME,1);");
  110. //vs->shader_set_vertex_code(sm,"OUT_VERTEX=IN_VERTEX*mod(TIME,1);");
  111. vs->shader_set_fragment_code(sm,"OUT_DIFFUSE=vec3(1,0,1);OUT_GLOW=abs(sin(TIME));");
  112. RID tcmat = vs->mesh_surface_get_material(test_cube,0);
  113. vs->material_set_shader(tcmat,sm);
  114. */
  115. List<String> cmdline = OS::get_singleton()->get_cmdline_args();
  116. int object_count = OBJECT_COUNT;
  117. if (cmdline.size() > 0 && cmdline[cmdline.size() - 1].to_int()) {
  118. object_count = cmdline[cmdline.size() - 1].to_int();
  119. };
  120. for (int i = 0; i < object_count; i++) {
  121. InstanceInfo ii;
  122. ii.instance = vs->instance_create2(test_cube, scenario);
  123. ii.base.translate(Math::random(-20, 20), Math::random(-20, 20), Math::random(-20, 18));
  124. ii.base.rotate(Vector3(0, 1, 0), Math::randf() * Math_PI);
  125. ii.base.rotate(Vector3(1, 0, 0), Math::randf() * Math_PI);
  126. vs->instance_set_transform(ii.instance, ii.base);
  127. ii.rot_axis = Vector3(Math::random(-1, 1), Math::random(-1, 1), Math::random(-1, 1)).normalized();
  128. instances.push_back(ii);
  129. }
  130. camera = vs->camera_create();
  131. // vs->camera_set_perspective( camera, 60.0,0.1, 100.0 );
  132. viewport = vs->viewport_create();
  133. Size2i screen_size = OS::get_singleton()->get_window_size();
  134. vs->viewport_set_size(viewport, screen_size.x, screen_size.y);
  135. vs->viewport_attach_to_screen(viewport, Rect2(Vector2(), screen_size));
  136. vs->viewport_set_active(viewport, true);
  137. vs->viewport_attach_camera(viewport, camera);
  138. vs->viewport_set_scenario(viewport, scenario);
  139. vs->camera_set_transform(camera, Transform(Basis(), Vector3(0, 3, 30)));
  140. vs->camera_set_perspective(camera, 60, 0.1, 1000);
  141. /*
  142. RID lightaux = vs->light_create( VisualServer::LIGHT_OMNI );
  143. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_RADIUS, 80 );
  144. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ATTENUATION, 1 );
  145. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ENERGY, 1.5 );
  146. light = vs->instance_create( lightaux );
  147. */
  148. RID lightaux;
  149. lightaux = vs->directional_light_create();
  150. //vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
  151. vs->light_set_color(lightaux, Color(1.0, 1.0, 1.0));
  152. //vs->light_set_shadow( lightaux, true );
  153. light = vs->instance_create2(lightaux, scenario);
  154. Transform lla;
  155. //lla.set_look_at(Vector3(),Vector3(1,-1,1),Vector3(0,1,0));
  156. lla.set_look_at(Vector3(), Vector3(-0.000000, -0.836026, -0.548690), Vector3(0, 1, 0));
  157. vs->instance_set_transform(light, lla);
  158. lightaux = vs->omni_light_create();
  159. //vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,1.0) );
  160. vs->light_set_color(lightaux, Color(1.0, 1.0, 0.0));
  161. vs->light_set_param(lightaux, VisualServer::LIGHT_PARAM_RANGE, 4);
  162. vs->light_set_param(lightaux, VisualServer::LIGHT_PARAM_ENERGY, 8);
  163. //vs->light_set_shadow( lightaux, true );
  164. //light = vs->instance_create( lightaux );
  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(Basis(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. } // namespace TestRender