2 #include <msp/core/application.h>
3 #include <msp/core/getopt.h>
4 #include <msp/fs/utils.h>
5 #include <msp/graphics/simplewindow.h>
6 #include <msp/gl/blend.h>
7 #include <msp/gl/camera.h>
8 #include <msp/gl/framebuffer.h>
9 #include <msp/gl/light.h>
10 #include <msp/gl/lighting.h>
11 #include <msp/gl/mesh.h>
12 #include <msp/gl/object.h>
13 #include <msp/gl/tests.h>
14 #include <msp/input/mouse.h>
15 #include <msp/io/print.h>
20 class Viewer: public RegisteredApplication<Viewer>
23 Graphics::SimpleGLWindow window;
28 GL::Lighting lighting;
45 void button_press(unsigned);
46 void button_release(unsigned);
47 void axis_motion(unsigned, float, float);
54 Viewer::Viewer(int argc, char **argv):
55 window(1024, 768, false),
67 throw usage_error("Filename must be provided");
70 string ext = FS::extpart(fn);
75 DataFile::load(*mesh, fn);
77 else if(ext==".object")
79 object = new GL::Object;
80 DataFile::load(*object, fn);
83 throw usage_error("Don't know how to view this file");
85 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Viewer::exit), 0));
86 mouse.signal_button_press.connect(sigc::mem_fun(this, &Viewer::button_press));
87 mouse.signal_button_release.connect(sigc::mem_fun(this, &Viewer::button_release));
88 mouse.signal_axis_motion.connect(sigc::mem_fun(this, &Viewer::axis_motion));
90 light.set_position(GL::Vector4(0, 0, 1, 0));
91 lighting.attach(0, light);
93 camera.set_up_direction(GL::Vector3(0, 0, 1));
106 return Application::main();
113 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
117 GL::Bind bind_lighting(lighting);
118 GL::Bind bind_depth(GL::DepthTest::lequal());
119 GL::Bind bind_blend(GL::Blend::alpha());
125 window.swap_buffers();
128 void Viewer::button_press(unsigned btn)
137 axis_motion(0, 0, 0);
151 void Viewer::button_release(unsigned btn)
157 void Viewer::axis_motion(unsigned axis, float, float delta)
161 float dx = (axis==0 ? delta : 0);
162 float dy = (axis==1 ? delta : 0);
180 float x = mouse.get_axis_value(0);
181 float y = mouse.get_axis_value(1);
183 light_yaw = yaw+x*M_PI;
184 light_pitch = pitch-y*M_PI;
190 void Viewer::update_camera()
194 float cp = cos(pitch);
195 float sp = sin(pitch);
196 camera.set_position(GL::Vector3(-cy*cp*distance, -sy*cp*distance, -sp*distance));
197 camera.set_depth_clip(distance*0.02, distance*50);
198 camera.look_at(GL::Vector3(0, 0, 0));
201 void Viewer::update_light()
203 float cy = cos(light_yaw);
204 float sy = sin(light_yaw);
205 float cp = cos(light_pitch);
206 float sp = sin(light_pitch);
207 light.set_position(GL::Vector4(-cy*cp, -sy*cp, -sp, 0));