1 /*************************************************************************/
2 /* test_detailer.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 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 "geometry.h"
31 #include "math_funcs.h"
32 #include "os/main_loop.h"
33 #include "print_string.h"
34 #include "quick_hull.h"
35 #include "servers/visual_server.h"
36 namespace TestMultiMesh {
37
38 class TestMainLoop : public MainLoop {
39
40 RID instance;
41 RID camera;
42 RID viewport;
43 RID light;
44 RID mesh;
45 RID scenario;
46
47 #define MULTIMESH_COUNT 1500
48
49 float ofs_x, ofs_y;
50 bool quit;
51
52 public:
_update_qh()53 virtual void _update_qh() {
54
55 VisualServer *vs = VisualServer::get_singleton();
56 Vector<Vector3> vts;
57 /*
58
59 static const int s = 20;
60 for(int i=0;i<s;i++) {
61 Matrix3 rot(Vector3(0,1,0),i*Math_PI/s);
62
63 for(int j=0;j<s;j++) {
64 Vector3 v;
65 v.x=Math::sin(j*Math_PI*2/s);
66 v.y=Math::cos(j*Math_PI*2/s);
67
68 vts.push_back( rot.xform(v*2 ) );
69 }
70 }*/
71 /*
72 Math::seed(0);
73 for(int i=0;i<50;i++) {
74
75 vts.push_back( Vector3(Math::randf()*2-1.0,Math::randf()*2-1.0,Math::randf()*2-1.0).normalized()*2);
76 }*/
77 /*
78 vts.push_back(Vector3(0,0,1));
79 vts.push_back(Vector3(0,0,-1));
80 vts.push_back(Vector3(0,1,0));
81 vts.push_back(Vector3(0,-1,0));
82 vts.push_back(Vector3(1,0,0));
83 vts.push_back(Vector3(-1,0,0));*/
84 /*
85 vts.push_back(Vector3(1,1,1));
86 vts.push_back(Vector3(1,-1,1));
87 vts.push_back(Vector3(-1,1,1));
88 vts.push_back(Vector3(-1,-1,1));
89 vts.push_back(Vector3(1,1,-1));
90 vts.push_back(Vector3(1,-1,-1));
91 vts.push_back(Vector3(-1,1,-1));
92 vts.push_back(Vector3(-1,-1,-1));
93 */
94
95 DVector<Plane> convex_planes = Geometry::build_cylinder_planes(0.5, 0.7, 4, Vector3::AXIS_Z);
96 Geometry::MeshData convex_data = Geometry::build_convex_mesh(convex_planes);
97 vts = convex_data.vertices;
98
99 Geometry::MeshData md;
100 Error err = QuickHull::build(vts, md);
101 print_line("ERR: " + itos(err));
102
103 vs->mesh_remove_surface(mesh, 0);
104 vs->mesh_add_surface_from_mesh_data(mesh, md);
105
106 //vs->scenario_set_debug(scenario,VS::SCENARIO_DEBUG_WIREFRAME);
107
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 }
117
input_event(const InputEvent & p_event)118 virtual void input_event(const InputEvent &p_event) {
119
120 if (p_event.type == InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask & 4) {
121
122 ofs_x += p_event.mouse_motion.relative_y / 200.0;
123 ofs_y += p_event.mouse_motion.relative_x / 200.0;
124 }
125 if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index == 1) {
126
127 QuickHull::debug_stop_after++;
128 _update_qh();
129 }
130 if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index == 2) {
131
132 if (QuickHull::debug_stop_after > 0)
133 QuickHull::debug_stop_after--;
134 _update_qh();
135 }
136 }
137
request_quit()138 virtual void request_quit() {
139
140 quit = true;
141 }
142
init()143 virtual void init() {
144
145 VisualServer *vs = VisualServer::get_singleton();
146
147 mesh = vs->mesh_create();
148
149 scenario = vs->scenario_create();
150
151 QuickHull::debug_stop_after = 0;
152 _update_qh();
153
154 instance = vs->instance_create2(mesh, scenario);
155
156 camera = vs->camera_create();
157
158 vs->camera_set_perspective(camera, 60.0, 0.1, 100.0);
159 viewport = vs->viewport_create();
160 vs->viewport_attach_camera(viewport, camera);
161 vs->viewport_attach_to_screen(viewport);
162 vs->viewport_set_scenario(viewport, scenario);
163
164 vs->camera_set_transform(camera, Transform(Matrix3(), Vector3(0, 0, 2)));
165
166 RID lightaux = vs->light_create(VisualServer::LIGHT_DIRECTIONAL);
167 //vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.3,0.3,0.3) );
168 light = vs->instance_create2(lightaux, scenario);
169 vs->instance_set_transform(light, Transform(Matrix3(Vector3(0.1, 0.4, 0.7).normalized(), 0.9)));
170
171 ofs_x = 0;
172 ofs_y = 0;
173 quit = false;
174 }
175
idle(float p_time)176 virtual bool idle(float p_time) {
177 return false;
178 }
179
iteration(float p_time)180 virtual bool iteration(float p_time) {
181
182 VisualServer *vs = VisualServer::get_singleton();
183
184 Transform tr_camera;
185 tr_camera.rotate(Vector3(0, 1, 0), ofs_y);
186 tr_camera.rotate(Vector3(1, 0, 0), ofs_x);
187 tr_camera.translate(0, 0, 10);
188
189 vs->camera_set_transform(camera, tr_camera);
190
191 return quit;
192 }
finish()193 virtual void finish() {
194 }
195 };
196
test()197 MainLoop *test() {
198
199 return memnew(TestMainLoop);
200 }
201 } // namespace TestMultiMesh
202