2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* test_render.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "test_render.h"
|
2017-03-05 14:47:28 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/math/math_funcs.h"
|
|
|
|
#include "core/math/quick_hull.h"
|
|
|
|
#include "core/os/keyboard.h"
|
|
|
|
#include "core/os/main_loop.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/print_string.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "servers/visual_server.h"
|
2015-04-20 22:38:02 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#define OBJECT_COUNT 50
|
|
|
|
|
|
|
|
namespace TestRender {
|
|
|
|
|
|
|
|
class TestMainLoop : public MainLoop {
|
|
|
|
|
|
|
|
RID test_cube;
|
|
|
|
RID instance;
|
|
|
|
RID camera;
|
|
|
|
RID viewport;
|
|
|
|
RID light;
|
|
|
|
RID scenario;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
struct InstanceInfo {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
RID instance;
|
|
|
|
Transform base;
|
|
|
|
Vector3 rot_axis;
|
|
|
|
};
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
List<InstanceInfo> instances;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
float ofs;
|
|
|
|
bool quit;
|
2015-04-20 22:38:02 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
protected:
|
2014-02-10 01:10:30 +00:00
|
|
|
public:
|
2017-05-20 15:38:03 +00:00
|
|
|
virtual void input_event(const Ref<InputEvent> &p_event) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (p_event->is_pressed())
|
2017-03-05 15:44:50 +00:00
|
|
|
quit = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void init() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
print_line("INITIALIZING TEST RENDER");
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualServer *vs = VisualServer::get_singleton();
|
2014-02-10 01:10:30 +00:00
|
|
|
test_cube = vs->get_test_cube();
|
|
|
|
scenario = vs->scenario_create();
|
|
|
|
|
|
|
|
Vector<Vector3> vts;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
/*
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<Plane> sp = Geometry::build_sphere_planes(2,5,5);
|
2014-02-10 01:10:30 +00:00
|
|
|
Geometry::MeshData md2 = Geometry::build_convex_mesh(sp);
|
|
|
|
vts=md2.vertices;
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
/*
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
static const int s = 20;
|
|
|
|
for(int i=0;i<s;i++) {
|
2017-01-11 03:52:51 +00:00
|
|
|
Basis rot(Vector3(0,1,0),i*Math_PI/s);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
for(int j=0;j<s;j++) {
|
|
|
|
Vector3 v;
|
|
|
|
v.x=Math::sin(j*Math_PI*2/s);
|
|
|
|
v.y=Math::cos(j*Math_PI*2/s);
|
|
|
|
|
|
|
|
vts.push_back( rot.xform(v*2 ) );
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
/*for(int i=0;i<100;i++) {
|
|
|
|
|
|
|
|
vts.push_back( Vector3(Math::randf()*2-1.0,Math::randf()*2-1.0,Math::randf()*2-1.0).normalized()*2);
|
|
|
|
}*/
|
|
|
|
/*
|
|
|
|
vts.push_back(Vector3(0,0,1));
|
|
|
|
vts.push_back(Vector3(0,0,-1));
|
|
|
|
vts.push_back(Vector3(0,1,0));
|
|
|
|
vts.push_back(Vector3(0,-1,0));
|
|
|
|
vts.push_back(Vector3(1,0,0));
|
|
|
|
vts.push_back(Vector3(-1,0,0));*/
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
vts.push_back(Vector3(1, 1, 1));
|
|
|
|
vts.push_back(Vector3(1, -1, 1));
|
|
|
|
vts.push_back(Vector3(-1, 1, 1));
|
|
|
|
vts.push_back(Vector3(-1, -1, 1));
|
|
|
|
vts.push_back(Vector3(1, 1, -1));
|
|
|
|
vts.push_back(Vector3(1, -1, -1));
|
|
|
|
vts.push_back(Vector3(-1, 1, -1));
|
|
|
|
vts.push_back(Vector3(-1, -1, -1));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Geometry::MeshData md;
|
2017-03-05 15:44:50 +00:00
|
|
|
Error err = QuickHull::build(vts, md);
|
|
|
|
print_line("ERR: " + itos(err));
|
2014-02-10 01:10:30 +00:00
|
|
|
test_cube = vs->mesh_create();
|
2017-03-05 15:44:50 +00:00
|
|
|
vs->mesh_add_surface_from_mesh_data(test_cube, md);
|
2014-02-10 01:10:30 +00:00
|
|
|
//vs->scenario_set_debug(scenario,VS::SCENARIO_DEBUG_WIREFRAME);
|
|
|
|
|
|
|
|
/*
|
|
|
|
RID sm = vs->shader_create();
|
|
|
|
//vs->shader_set_fragment_code(sm,"OUT_ALPHA=mod(TIME,1);");
|
|
|
|
//vs->shader_set_vertex_code(sm,"OUT_VERTEX=IN_VERTEX*mod(TIME,1);");
|
|
|
|
vs->shader_set_fragment_code(sm,"OUT_DIFFUSE=vec3(1,0,1);OUT_GLOW=abs(sin(TIME));");
|
|
|
|
RID tcmat = vs->mesh_surface_get_material(test_cube,0);
|
|
|
|
vs->material_set_shader(tcmat,sm);
|
|
|
|
*/
|
|
|
|
|
|
|
|
List<String> cmdline = OS::get_singleton()->get_cmdline_args();
|
|
|
|
int object_count = OBJECT_COUNT;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cmdline.size() > 0 && cmdline[cmdline.size() - 1].to_int()) {
|
|
|
|
object_count = cmdline[cmdline.size() - 1].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < object_count; i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
InstanceInfo ii;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ii.instance = vs->instance_create2(test_cube, scenario);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ii.base.translate(Math::random(-20, 20), Math::random(-20, 20), Math::random(-20, 18));
|
|
|
|
ii.base.rotate(Vector3(0, 1, 0), Math::randf() * Math_PI);
|
|
|
|
ii.base.rotate(Vector3(1, 0, 0), Math::randf() * Math_PI);
|
|
|
|
vs->instance_set_transform(ii.instance, ii.base);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ii.rot_axis = Vector3(Math::random(-1, 1), Math::random(-1, 1), Math::random(-1, 1)).normalized();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
instances.push_back(ii);
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
camera = vs->camera_create();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
// vs->camera_set_perspective( camera, 60.0,0.1, 100.0 );
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
viewport = vs->viewport_create();
|
2016-10-19 14:14:41 +00:00
|
|
|
Size2i screen_size = OS::get_singleton()->get_window_size();
|
2017-03-05 15:44:50 +00:00
|
|
|
vs->viewport_set_size(viewport, screen_size.x, screen_size.y);
|
|
|
|
vs->viewport_attach_to_screen(viewport, Rect2(Vector2(), screen_size));
|
|
|
|
vs->viewport_set_active(viewport, true);
|
|
|
|
vs->viewport_attach_camera(viewport, camera);
|
|
|
|
vs->viewport_set_scenario(viewport, scenario);
|
|
|
|
vs->camera_set_transform(camera, Transform(Basis(), Vector3(0, 3, 30)));
|
|
|
|
vs->camera_set_perspective(camera, 60, 0.1, 1000);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
RID lightaux = vs->light_create( VisualServer::LIGHT_OMNI );
|
|
|
|
vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_RADIUS, 80 );
|
|
|
|
vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ATTENUATION, 1 );
|
|
|
|
vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ENERGY, 1.5 );
|
|
|
|
light = vs->instance_create( lightaux );
|
|
|
|
*/
|
|
|
|
RID lightaux;
|
|
|
|
|
2017-11-10 02:34:01 +00:00
|
|
|
lightaux = vs->directional_light_create();
|
2014-06-28 02:21:45 +00:00
|
|
|
//vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
|
2017-03-05 15:44:50 +00:00
|
|
|
vs->light_set_color(lightaux, Color(1.0, 1.0, 1.0));
|
2014-02-10 01:10:30 +00:00
|
|
|
//vs->light_set_shadow( lightaux, true );
|
2017-03-05 15:44:50 +00:00
|
|
|
light = vs->instance_create2(lightaux, scenario);
|
2014-02-10 01:10:30 +00:00
|
|
|
Transform lla;
|
|
|
|
//lla.set_look_at(Vector3(),Vector3(1,-1,1),Vector3(0,1,0));
|
2017-03-05 15:44:50 +00:00
|
|
|
lla.set_look_at(Vector3(), Vector3(-0.000000, -0.836026, -0.548690), Vector3(0, 1, 0));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
vs->instance_set_transform(light, lla);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-10 02:34:01 +00:00
|
|
|
lightaux = vs->omni_light_create();
|
2017-01-14 11:26:56 +00:00
|
|
|
//vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,1.0) );
|
2017-03-05 15:44:50 +00:00
|
|
|
vs->light_set_color(lightaux, Color(1.0, 1.0, 0.0));
|
|
|
|
vs->light_set_param(lightaux, VisualServer::LIGHT_PARAM_RANGE, 4);
|
|
|
|
vs->light_set_param(lightaux, VisualServer::LIGHT_PARAM_ENERGY, 8);
|
2014-02-10 01:10:30 +00:00
|
|
|
//vs->light_set_shadow( lightaux, true );
|
|
|
|
//light = vs->instance_create( lightaux );
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs = 0;
|
|
|
|
quit = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
virtual bool iteration(float p_time) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualServer *vs = VisualServer::get_singleton();
|
2014-02-10 01:10:30 +00:00
|
|
|
//Transform t;
|
|
|
|
//t.rotate(Vector3(0, 1, 0), ofs);
|
|
|
|
//t.translate(Vector3(0,0,20 ));
|
|
|
|
//vs->camera_set_transform(camera, t);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs += p_time * 0.05;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//return quit;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<InstanceInfo>::Element *E = instances.front(); E; E = E->next()) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Transform pre(Basis(E->get().rot_axis, ofs), Vector3());
|
|
|
|
vs->instance_set_transform(E->get().instance, pre * E->get().base);
|
2014-02-10 01:10:30 +00:00
|
|
|
/*
|
|
|
|
if( !E->next() ) {
|
|
|
|
|
|
|
|
vs->free( E->get().instance );
|
|
|
|
instances.erase(E );
|
|
|
|
}*/
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool idle(float p_time) {
|
|
|
|
return quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void finish() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
MainLoop *test() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return memnew(TestMainLoop);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-12-06 20:36:34 +00:00
|
|
|
} // namespace TestRender
|