]> git.tdb.fi Git - libs/gl.git/blob - tools/viewer.cpp
Use latest OpenGL version in viewer
[libs/gl.git] / tools / viewer.cpp
1 #include <cmath>
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>
30
31 using namespace std;
32 using namespace Msp;
33
34 class Viewer: public RegisteredApplication<Viewer>
35 {
36 private:
37         struct Options
38         {
39                 list<string> resource_locations;
40                 string animation_name;
41                 string renderable_name;
42                 Graphics::WindowOptions wnd_opts;
43                 Graphics::GLOptions gl_opts;
44
45                 Options(int, char **);
46         };
47
48         class Resources: public GL::Resources
49         {
50         private:
51                 DataFile::DirectorySource dir_source;
52                 DataFile::PackSource pack_source;
53
54         public:
55                 Resources();
56
57                 void add_directory(const string &);
58                 void add_pack(const string &);
59         };
60
61         Options opts;
62         Graphics::Display display;
63         Graphics::Window window;
64         Graphics::GLContext gl_ctx;
65         Input::Mouse mouse;
66         Resources resources;
67         GL::WindowView view;
68         GL::Pipeline pipeline;
69         GL::Renderable *renderable;
70         GL::AnimatedObject *anim_object;
71         GL::AnimationPlayer *anim_player;
72         GL::Light light;
73         GL::Lighting lighting;
74         GL::Camera camera;
75         float yaw;
76         float pitch;
77         float distance;
78         float light_yaw;
79         float light_pitch;
80         unsigned dragging;
81         Time::TimeStamp last_tick;
82
83 public:
84         Viewer(int, char **);
85 private:
86         template<typename T>
87         T *load(const string &);
88 public:
89         ~Viewer();
90
91         virtual int main();
92 private:
93         virtual void tick();
94
95         void button_press(unsigned);
96         void button_release(unsigned);
97         void axis_motion(unsigned, float, float);
98
99         void update_camera();
100         void update_light();
101 };
102
103
104 Viewer::Options::Options(int argc, char **argv)
105 {
106         GetOpt getopt;
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);
110         getopt(argc, argv);
111
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;
116 }
117
118 Viewer::Viewer(int argc, char **argv):
119         opts(argc, argv),
120         window(display, opts.wnd_opts),
121         gl_ctx(window, opts.gl_opts),
122         mouse(window),
123         view(window, gl_ctx),
124         pipeline(view),
125         renderable(0),
126         anim_object(0),
127         anim_player(0),
128         yaw(0),
129         pitch(0),
130         distance(10),
131         light_yaw(0),
132         light_pitch(0),
133         dragging(0)
134 {
135         for(list<string>::const_iterator i=opts.resource_locations.begin(); i!=opts.resource_locations.end(); ++i)
136         {
137                 if(FS::is_dir(*i))
138                         resources.add_directory(*i);
139                 else
140                 {
141                         string ext = FS::extpart(*i);
142                         if(ext==".mdp")
143                                 resources.add_pack(*i);
144                         else
145                                 DataFile::load(resources, *i);
146                 }
147         }
148
149         GL::Object *object = 0;
150
151         string ext = FS::extpart(opts.renderable_name);
152         if(ext==".mesh")
153         {
154                 GL::Mesh *mesh = 0;
155                 if(FS::exists(opts.renderable_name))
156                 {
157                         mesh = new GL::Mesh;
158                         DataFile::load(*mesh, opts.renderable_name);
159                         resources.add("__"+opts.renderable_name, mesh);
160                 }
161                 else
162                         mesh = &resources.get<GL::Mesh>(opts.renderable_name);
163
164                 object = new GL::Object;
165                 GL::Technique *tech = new GL::Technique;
166                 tech->add_pass(0);
167                 object->set_mesh(mesh);
168                 object->set_technique(tech);
169                 renderable = object;
170
171                 resources.add("__.tech", tech);
172                 resources.add("__.object", object);
173         }
174         else if(ext==".object")
175                 renderable = load<GL::Object>(opts.renderable_name);
176         else if(ext==".scene")
177         {
178                 GL::SimpleScene *scene = new GL::SimpleScene;
179                 DataFile::load(*scene, opts.renderable_name, resources);
180                 renderable = scene;
181         }
182         else
183                 throw usage_error("Unknown renderable type");
184
185         if(!opts.animation_name.empty())
186         {
187                 if(!object)
188                         throw usage_error("Must have an object to animate");
189
190                 GL::Animation *anim = load<GL::Animation>(opts.animation_name);
191                 anim_player = new GL::AnimationPlayer;
192                 anim_object = new GL::AnimatedObject(*object);
193                 anim_player->play(*anim_object, *anim);
194                 renderable = anim_object;
195         }
196
197         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Viewer::exit), 0));
198         mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Viewer::button_press), false));
199         mouse.signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &Viewer::button_release), false));
200         mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Viewer::axis_motion), false));
201
202         light.set_position(GL::Vector4(0, 0, 1, 0));
203         lighting.attach(0, light);
204
205         camera.set_up_direction(GL::Vector3(0, 0, 1));
206         update_camera();
207
208         GL::Pipeline::Pass &pass = pipeline.add_pass(0, *renderable);
209         pass.set_lighting(&lighting);
210         pass.set_depth_test(&GL::DepthTest::lequal());
211         pass.set_blend(&GL::Blend::alpha());
212
213         view.set_content(&pipeline);
214         view.set_camera(&camera);
215 }
216
217 template<typename T>
218 T *Viewer::load(const string &name)
219 {
220         if(FS::exists(name))
221         {
222                 T *thing = new T;
223                 DataFile::load(*thing, name, resources);
224                 resources.add("__"+name, thing);
225                 return thing;
226         }
227         else
228                 return &resources.get<T>(name);
229 }
230
231 Viewer::~Viewer()
232 {
233         delete anim_player;
234         delete anim_object;
235 }
236
237 int Viewer::main()
238 {
239         window.show();
240         return Application::main();
241 }
242
243 void Viewer::tick()
244 {
245         if(anim_player)
246         {
247                 Time::TimeStamp t = Time::now();
248                 Time::TimeDelta dt;
249                 if(last_tick)
250                         dt = t-last_tick;
251                 last_tick = t;
252
253                 anim_player->tick(dt);
254         }
255
256         display.tick();
257         view.render();
258 }
259
260 void Viewer::button_press(unsigned btn)
261 {
262         if(btn==1)
263         {
264                 dragging = 1;
265         }
266         else if(btn==3)
267         {
268                 dragging = 3;
269                 axis_motion(0, 0, 0);
270         }
271         else if(btn==4)
272         {
273                 distance *= 0.9;
274                 update_camera();
275         }
276         else if(btn==5)
277         {
278                 distance *= 1.1;
279                 update_camera();
280         }
281 }
282
283 void Viewer::button_release(unsigned btn)
284 {
285         if(btn==dragging)
286                 dragging = 0;
287 }
288
289 void Viewer::axis_motion(unsigned axis, float, float delta)
290 {
291         if(dragging==1)
292         {
293                 float dx = (axis==0 ? delta : 0);
294                 float dy = (axis==1 ? delta : 0);
295
296                 yaw -= dx*M_PI*2;
297                 while(yaw>M_PI)
298                         yaw -= M_PI*2;
299                 while(yaw<-M_PI)
300                         yaw += M_PI*2;
301
302                 pitch += dy*M_PI;
303                 if(pitch>M_PI*0.49)
304                         pitch = M_PI*0.49;
305                 if(pitch<-M_PI*0.49)
306                         pitch = -M_PI*0.49;
307
308                 update_camera();
309         }
310         else if(dragging==3)
311         {
312                 float x = mouse.get_axis_value(0);
313                 float y = mouse.get_axis_value(1);
314
315                 light_yaw = yaw+x*M_PI;
316                 light_pitch = pitch-y*M_PI;
317
318                 update_light();
319         }
320 }
321
322 void Viewer::update_camera()
323 {
324         float cy = cos(yaw);
325         float sy = sin(yaw);
326         float cp = cos(pitch);
327         float sp = sin(pitch);
328         camera.set_position(GL::Vector3(-cy*cp*distance, -sy*cp*distance, -sp*distance));
329         camera.set_depth_clip(distance*0.02, distance*50);
330         camera.look_at(GL::Vector3(0, 0, 0));
331 }
332
333 void Viewer::update_light()
334 {
335         float cy = cos(light_yaw);
336         float sy = sin(light_yaw);
337         float cp = cos(light_pitch);
338         float sp = sin(light_pitch);
339         light.set_position(GL::Vector4(-cy*cp, -sy*cp, -sp, 0));
340 }
341
342
343 Viewer::Resources::Resources()
344 {
345         add_source(dir_source);
346         add_source(pack_source);
347 }
348
349 void Viewer::Resources::add_directory(const string &dir)
350 {
351         dir_source.add_directory(dir);
352 }
353
354 void Viewer::Resources::add_pack(const string &pack)
355 {
356         pack_source.add_pack_file(pack);
357 }