test_render.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*************************************************************************/
  2. /* test_render.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/convex_hull.h"
  32. #include "core/math/math_funcs.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. }
  62. virtual void init() {
  63. print_line("INITIALIZING TEST RENDER");
  64. VisualServer *vs = VisualServer::get_singleton();
  65. test_cube = vs->get_test_cube();
  66. scenario = vs->scenario_create();
  67. Vector<Vector3> vts;
  68. /*
  69. PoolVector<Plane> sp = Geometry::build_sphere_planes(2,5,5);
  70. Geometry::MeshData md2 = Geometry::build_convex_mesh(sp);
  71. vts=md2.vertices;
  72. */
  73. /*
  74. static const int s = 20;
  75. for(int i=0;i<s;i++) {
  76. Basis rot(Vector3(0,1,0),i*Math_PI/s);
  77. for(int j=0;j<s;j++) {
  78. Vector3 v;
  79. v.x=Math::sin(j*Math_PI*2/s);
  80. v.y=Math::cos(j*Math_PI*2/s);
  81. vts.push_back( rot.xform(v*2 ) );
  82. }
  83. }*/
  84. /*for(int i=0;i<100;i++) {
  85. vts.push_back( Vector3(Math::randf()*2-1.0,Math::randf()*2-1.0,Math::randf()*2-1.0).normalized()*2);
  86. }*/
  87. /*
  88. vts.push_back(Vector3(0,0,1));
  89. vts.push_back(Vector3(0,0,-1));
  90. vts.push_back(Vector3(0,1,0));
  91. vts.push_back(Vector3(0,-1,0));
  92. vts.push_back(Vector3(1,0,0));
  93. vts.push_back(Vector3(-1,0,0));*/
  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. vts.push_back(Vector3(-1, -1, -1));
  102. Geometry::MeshData md;
  103. Error err = ConvexHullComputer::convex_hull(vts, md);
  104. print_line("ERR: " + itos(err));
  105. test_cube = vs->mesh_create();
  106. vs->mesh_add_surface_from_mesh_data(test_cube, md);
  107. //vs->scenario_set_debug(scenario,VS::SCENARIO_DEBUG_WIREFRAME);
  108. /*
  109. RID sm = vs->shader_create();
  110. //vs->shader_set_fragment_code(sm,"OUT_ALPHA=mod(TIME,1);");
  111. //vs->shader_set_vertex_code(sm,"OUT_VERTEX=IN_VERTEX*mod(TIME,1);");
  112. vs->shader_set_fragment_code(sm,"OUT_DIFFUSE=vec3(1,0,1);OUT_GLOW=abs(sin(TIME));");
  113. RID tcmat = vs->mesh_surface_get_material(test_cube,0);
  114. vs->material_set_shader(tcmat,sm);
  115. */
  116. List<String> cmdline = OS::get_singleton()->get_cmdline_args();
  117. int object_count = OBJECT_COUNT;
  118. if (cmdline.size() > 0 && cmdline[cmdline.size() - 1].to_int()) {
  119. object_count = cmdline[cmdline.size() - 1].to_int();
  120. };
  121. for (int i = 0; i < object_count; i++) {
  122. InstanceInfo ii;
  123. ii.instance = vs->instance_create2(test_cube, scenario);
  124. ii.base.translate(Math::random(-20, 20), Math::random(-20, 20), Math::random(-20, 18));
  125. ii.base.rotate(Vector3(0, 1, 0), Math::randf() * Math_PI);
  126. ii.base.rotate(Vector3(1, 0, 0), Math::randf() * Math_PI);
  127. vs->instance_set_transform(ii.instance, ii.base);
  128. ii.rot_axis = Vector3(Math::random(-1, 1), Math::random(-1, 1), Math::random(-1, 1)).normalized();
  129. instances.push_back(ii);
  130. }
  131. camera = vs->camera_create();
  132. // vs->camera_set_perspective( camera, 60.0,0.1, 100.0 );
  133. viewport = vs->viewport_create();
  134. Size2i screen_size = OS::get_singleton()->get_window_size();
  135. vs->viewport_set_size(viewport, screen_size.x, screen_size.y);
  136. vs->viewport_attach_to_screen(viewport, Rect2(Vector2(), screen_size));
  137. vs->viewport_set_active(viewport, true);
  138. vs->viewport_attach_camera(viewport, camera);
  139. vs->viewport_set_scenario(viewport, scenario);
  140. vs->camera_set_transform(camera, Transform(Basis(), Vector3(0, 3, 30)));
  141. vs->camera_set_perspective(camera, 60, 0.1, 1000);
  142. /*
  143. RID lightaux = vs->light_create( VisualServer::LIGHT_OMNI );
  144. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_RADIUS, 80 );
  145. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ATTENUATION, 1 );
  146. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ENERGY, 1.5 );
  147. light = vs->instance_create( lightaux );
  148. */
  149. RID lightaux;
  150. lightaux = vs->directional_light_create();
  151. //vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
  152. vs->light_set_color(lightaux, Color(1.0, 1.0, 1.0));
  153. //vs->light_set_shadow( lightaux, true );
  154. light = vs->instance_create2(lightaux, scenario);
  155. Transform lla;
  156. //lla.set_look_at(Vector3(),Vector3(1,-1,1),Vector3(0,1,0));
  157. lla.set_look_at(Vector3(), Vector3(-0.000000, -0.836026, -0.548690), Vector3(0, 1, 0));
  158. vs->instance_set_transform(light, lla);
  159. lightaux = vs->omni_light_create();
  160. //vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,1.0) );
  161. vs->light_set_color(lightaux, Color(1.0, 1.0, 0.0));
  162. vs->light_set_param(lightaux, VisualServer::LIGHT_PARAM_RANGE, 4);
  163. vs->light_set_param(lightaux, VisualServer::LIGHT_PARAM_ENERGY, 8);
  164. //vs->light_set_shadow( lightaux, true );
  165. //light = vs->instance_create( lightaux );
  166. ofs = 0;
  167. quit = false;
  168. }
  169. virtual bool iteration(float p_time) {
  170. VisualServer *vs = VisualServer::get_singleton();
  171. //Transform t;
  172. //t.rotate(Vector3(0, 1, 0), ofs);
  173. //t.translate(Vector3(0,0,20 ));
  174. //vs->camera_set_transform(camera, t);
  175. ofs += p_time * 0.05;
  176. //return quit;
  177. for (List<InstanceInfo>::Element *E = instances.front(); E; E = E->next()) {
  178. Transform pre(Basis(E->get().rot_axis, ofs), Vector3());
  179. vs->instance_set_transform(E->get().instance, pre * E->get().base);
  180. /*
  181. if( !E->next() ) {
  182. vs->free( E->get().instance );
  183. instances.erase(E );
  184. }*/
  185. }
  186. return quit;
  187. }
  188. virtual bool idle(float p_time) {
  189. return quit;
  190. }
  191. virtual void finish() {
  192. }
  193. };
  194. MainLoop *test() {
  195. return memnew(TestMainLoop);
  196. }
  197. } // namespace TestRender