2 #include <msp/core/application.h>
3 #include <msp/core/getopt.h>
4 #include <msp/datafile/directorysource.h>
5 #include <msp/datafile/packsource.h>
6 #include <msp/fs/stat.h>
7 #include <msp/fs/utils.h>
8 #include <msp/graphics/simplewindow.h>
9 #include <msp/gl/animatedobject.h>
10 #include <msp/gl/animation.h>
11 #include <msp/gl/animationplayer.h>
12 #include <msp/gl/blend.h>
13 #include <msp/gl/camera.h>
14 #include <msp/gl/framebuffer.h>
15 #include <msp/gl/light.h>
16 #include <msp/gl/lighting.h>
17 #include <msp/gl/mesh.h>
18 #include <msp/gl/object.h>
19 #include <msp/gl/pipeline.h>
20 #include <msp/gl/renderer.h>
21 #include <msp/gl/resources.h>
22 #include <msp/gl/simplescene.h>
23 #include <msp/gl/technique.h>
24 #include <msp/gl/tests.h>
25 #include <msp/gl/windowview.h>
26 #include <msp/input/mouse.h>
27 #include <msp/io/print.h>
28 #include <msp/time/timestamp.h>
29 #include <msp/time/utils.h>
34 class Viewer: public RegisteredApplication<Viewer>
37 class Resources: public GL::Resources
40 DataFile::DirectorySource dir_source;
41 DataFile::PackSource pack_source;
46 void add_directory(const string &);
47 void add_pack(const string &);
50 Graphics::SimpleGLWindow window;
54 GL::Pipeline pipeline;
55 GL::Renderable *renderable;
56 GL::AnimatedObject *anim_object;
57 GL::AnimationPlayer *anim_player;
59 GL::Lighting lighting;
67 Time::TimeStamp last_tick;
73 T *load(const string &);
81 void button_press(unsigned);
82 void button_release(unsigned);
83 void axis_motion(unsigned, float, float);
90 Viewer::Viewer(int argc, char **argv):
91 window(1024, 768, false),
93 view(window, window.get_gl_context()),
105 list<string> resource_locations;
106 string animation_name;
107 string renderable_name;
109 getopt.add_option('r', "resources", resource_locations, GetOpt::REQUIRED_ARG);
110 getopt.add_option('a', "animation", animation_name, GetOpt::REQUIRED_ARG);
111 getopt.add_argument("renderable", renderable_name);
114 for(list<string>::const_iterator i=resource_locations.begin(); i!=resource_locations.end(); ++i)
117 resources.add_directory(*i);
120 string ext = FS::extpart(*i);
122 resources.add_pack(*i);
124 DataFile::load(resources, *i);
128 GL::Object *object = 0;
130 string ext = FS::extpart(renderable_name);
134 if(FS::exists(renderable_name))
137 DataFile::load(*mesh, renderable_name);
138 resources.add("__"+renderable_name, mesh);
141 mesh = &resources.get<GL::Mesh>(renderable_name);
143 object = new GL::Object;
144 GL::Technique *tech = new GL::Technique;
146 object->set_mesh(mesh);
147 object->set_technique(tech);
150 resources.add("__.tech", tech);
151 resources.add("__.object", object);
153 else if(ext==".object")
154 renderable = load<GL::Object>(renderable_name);
155 else if(ext==".scene")
157 GL::SimpleScene *scene = new GL::SimpleScene;
158 DataFile::load(*scene, renderable_name, resources);
162 throw usage_error("Unknown renderable type");
164 if(!animation_name.empty())
167 throw usage_error("Must have an object to animate");
169 GL::Animation *anim = load<GL::Animation>(animation_name);
170 anim_player = new GL::AnimationPlayer;
171 anim_object = new GL::AnimatedObject(*object);
172 anim_player->play(*anim_object, *anim);
173 renderable = anim_object;
176 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Viewer::exit), 0));
177 mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Viewer::button_press), false));
178 mouse.signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &Viewer::button_release), false));
179 mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Viewer::axis_motion), false));
181 light.set_position(GL::Vector4(0, 0, 1, 0));
182 lighting.attach(0, light);
184 camera.set_up_direction(GL::Vector3(0, 0, 1));
187 GL::Pipeline::Pass &pass = pipeline.add_pass(0, *renderable);
188 pass.set_lighting(&lighting);
189 pass.set_depth_test(&GL::DepthTest::lequal());
190 pass.set_blend(&GL::Blend::alpha());
192 view.set_content(&pipeline);
193 view.set_camera(&camera);
197 T *Viewer::load(const string &name)
202 DataFile::load(*thing, name, resources);
203 resources.add("__"+name, thing);
207 return &resources.get<T>(name);
219 return Application::main();
226 Time::TimeStamp t = Time::now();
232 anim_player->tick(dt);
239 void Viewer::button_press(unsigned btn)
248 axis_motion(0, 0, 0);
262 void Viewer::button_release(unsigned btn)
268 void Viewer::axis_motion(unsigned axis, float, float delta)
272 float dx = (axis==0 ? delta : 0);
273 float dy = (axis==1 ? delta : 0);
291 float x = mouse.get_axis_value(0);
292 float y = mouse.get_axis_value(1);
294 light_yaw = yaw+x*M_PI;
295 light_pitch = pitch-y*M_PI;
301 void Viewer::update_camera()
305 float cp = cos(pitch);
306 float sp = sin(pitch);
307 camera.set_position(GL::Vector3(-cy*cp*distance, -sy*cp*distance, -sp*distance));
308 camera.set_depth_clip(distance*0.02, distance*50);
309 camera.look_at(GL::Vector3(0, 0, 0));
312 void Viewer::update_light()
314 float cy = cos(light_yaw);
315 float sy = sin(light_yaw);
316 float cp = cos(light_pitch);
317 float sp = sin(light_pitch);
318 light.set_position(GL::Vector4(-cy*cp, -sy*cp, -sp, 0));
322 Viewer::Resources::Resources()
324 add_source(dir_source);
325 add_source(pack_source);
328 void Viewer::Resources::add_directory(const string &dir)
330 dir_source.add_directory(dir);
333 void Viewer::Resources::add_pack(const string &pack)
335 pack_source.add_pack_file(pack);