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>
39 list<string> resource_locations;
40 string animation_name;
41 string renderable_name;
42 Graphics::WindowOptions wnd_opts;
43 Graphics::GLOptions gl_opts;
45 Options(int, char **);
48 class Resources: public GL::Resources
51 DataFile::DirectorySource dir_source;
52 DataFile::PackSource pack_source;
57 void add_directory(const string &);
58 void add_pack(const string &);
62 Graphics::Display display;
63 Graphics::Window window;
64 Graphics::GLContext gl_ctx;
68 GL::Pipeline pipeline;
69 GL::Renderable *renderable;
70 GL::AnimatedObject *anim_object;
71 GL::AnimationPlayer *anim_player;
73 GL::Lighting lighting;
81 Time::TimeStamp last_tick;
87 T *load(const string &);
95 void button_press(unsigned);
96 void button_release(unsigned);
97 void axis_motion(unsigned, float, float);
104 Viewer::Options::Options(int argc, char **argv)
107 getopt.add_option('r', "resources", resource_locations, GetOpt::REQUIRED_ARG);
108 getopt.add_option('a', "animation", animation_name, GetOpt::REQUIRED_ARG);
109 getopt.add_argument("renderable", renderable_name);
112 wnd_opts.width = 1024;
113 wnd_opts.height = 768;
114 gl_opts.gl_version_major = Graphics::GLOptions::LATEST_VERSION;
115 gl_opts.core_profile = true;
118 Viewer::Viewer(int argc, char **argv):
120 window(display, opts.wnd_opts),
121 gl_ctx(window, opts.gl_opts),
123 view(window, gl_ctx),
135 for(list<string>::const_iterator i=opts.resource_locations.begin(); i!=opts.resource_locations.end(); ++i)
138 resources.add_directory(*i);
141 string ext = FS::extpart(*i);
143 resources.add_pack(*i);
145 DataFile::load(resources, *i);
149 GL::Object *object = 0;
151 string ext = FS::extpart(opts.renderable_name);
155 if(FS::exists(opts.renderable_name))
158 DataFile::load(*mesh, opts.renderable_name);
159 resources.add("__"+opts.renderable_name, mesh);
162 mesh = &resources.get<GL::Mesh>(opts.renderable_name);
164 object = new GL::Object;
165 GL::Technique *tech = new GL::Technique;
167 object->set_mesh(mesh);
168 object->set_technique(tech);
171 resources.add("__.tech", tech);
172 resources.add("__.object", object);
174 else if(ext==".object")
175 renderable = load<GL::Object>(opts.renderable_name);
176 else if(ext==".scene")
178 if(FS::exists(opts.renderable_name))
180 GL::Scene::GenericLoader ldr(resources);
181 IO::BufferedFile in(opts.renderable_name);
182 DataFile::Parser parser(in, opts.renderable_name);
184 renderable = ldr.get_scene();
187 renderable = &resources.get<GL::Scene>(opts.renderable_name);
190 throw usage_error("Unknown renderable type");
192 if(!opts.animation_name.empty())
195 throw usage_error("Must have an object to animate");
197 GL::Animation *anim = load<GL::Animation>(opts.animation_name);
198 anim_player = new GL::AnimationPlayer;
199 anim_object = new GL::AnimatedObject(*object);
200 anim_player->play(*anim_object, *anim);
201 renderable = anim_object;
204 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Viewer::exit), 0));
205 mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Viewer::button_press), false));
206 mouse.signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &Viewer::button_release), false));
207 mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Viewer::axis_motion), false));
209 light.set_position(GL::Vector4(0, 0, 1, 0));
210 lighting.attach(0, light);
212 camera.set_up_direction(GL::Vector3(0, 0, 1));
215 GL::Pipeline::Pass &pass = pipeline.add_pass(0, *renderable);
216 pass.set_lighting(&lighting);
217 pass.set_depth_test(&GL::DepthTest::lequal());
218 pass.set_blend(&GL::Blend::alpha());
220 view.set_content(&pipeline);
221 view.set_camera(&camera);
225 T *Viewer::load(const string &name)
230 DataFile::load(*thing, name, resources);
231 resources.add("__"+name, thing);
235 return &resources.get<T>(name);
247 return Application::main();
254 Time::TimeStamp t = Time::now();
260 anim_player->tick(dt);
267 void Viewer::button_press(unsigned btn)
276 axis_motion(0, 0, 0);
290 void Viewer::button_release(unsigned btn)
296 void Viewer::axis_motion(unsigned axis, float, float delta)
300 float dx = (axis==0 ? delta : 0);
301 float dy = (axis==1 ? delta : 0);
319 float x = mouse.get_axis_value(0);
320 float y = mouse.get_axis_value(1);
322 light_yaw = yaw+x*M_PI;
323 light_pitch = pitch-y*M_PI;
329 void Viewer::update_camera()
333 float cp = cos(pitch);
334 float sp = sin(pitch);
335 camera.set_position(GL::Vector3(-cy*cp*distance, -sy*cp*distance, -sp*distance));
336 camera.set_depth_clip(distance*0.02, distance*50);
337 camera.look_at(GL::Vector3(0, 0, 0));
340 void Viewer::update_light()
342 float cy = cos(light_yaw);
343 float sy = sin(light_yaw);
344 float cp = cos(light_pitch);
345 float sp = sin(light_pitch);
346 light.set_position(GL::Vector4(-cy*cp, -sy*cp, -sp, 0));
350 Viewer::Resources::Resources()
352 add_source(dir_source);
353 add_source(pack_source);
356 void Viewer::Resources::add_directory(const string &dir)
358 dir_source.add_directory(dir);
361 void Viewer::Resources::add_pack(const string &pack)
363 pack_source.add_pack_file(pack);