]> git.tdb.fi Git - libs/gl.git/blob - viewer.cpp
Use SimpleWindow::tick in viewer
[libs/gl.git] / viewer.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <cmath>
9 #include <msp/core/application.h>
10 #include <msp/fs/utils.h>
11 #include <msp/gbase/simplewindow.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/tests.h>
20 #include <msp/io/print.h>
21
22 using namespace std;
23 using namespace Msp;
24
25 class Viewer: public Application
26 {
27 private:
28         Graphics::SimpleGLWindow window;
29         GL::Object *object;
30         GL::Mesh *mesh;
31         GL::Light light;
32         GL::Lighting lighting;
33         GL::Camera camera;
34         float yaw;
35         float pitch;
36         float distance;
37         float light_yaw;
38         float light_pitch;
39         unsigned dragging;
40         int pointer_x;
41         int pointer_y;
42
43         static Application::RegApp<Viewer> reg;
44
45 public:
46         Viewer(int, char **);
47         ~Viewer();
48
49         virtual int main();
50 private:
51         virtual void tick();
52
53         void button_press(int, int, unsigned, unsigned);
54         void button_release(int, int, unsigned, unsigned);
55         void pointer_motion(int, int);
56         void key_press(unsigned, unsigned, wchar_t);
57
58         void update_camera();
59         void update_light();
60
61 public:
62         static void usage(const char *, const char *, bool);
63 };
64
65
66 Application::RegApp<Viewer> Viewer::reg;
67
68 Viewer::Viewer(int argc, char **argv):
69         window(1024, 768, false),
70         object(0),
71         mesh(0),
72         yaw(0),
73         pitch(0),
74         distance(10),
75         light_yaw(0),
76         light_pitch(0),
77         dragging(0),
78         pointer_x(0),
79         pointer_y(0)
80 {
81         if(argc<2)
82                 throw UsageError("Filename must be provided");
83
84         string fn = argv[1];
85         string ext = FS::extpart(fn);
86
87         if(ext==".mesh")
88         {
89                 mesh = new GL::Mesh;
90                 DataFile::load(*mesh, fn);
91         }
92         else if(ext==".object")
93         {
94                 object = new GL::Object;
95                 DataFile::load(*object, fn);
96         }
97         else
98                 throw UsageError("Don't know how to view this file");
99
100         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Viewer::exit), 0));
101         window.signal_button_press.connect(sigc::mem_fun(this, &Viewer::button_press));
102         window.signal_button_release.connect(sigc::mem_fun(this, &Viewer::button_release));
103         window.signal_pointer_motion.connect(sigc::mem_fun(this, &Viewer::pointer_motion));
104         window.signal_key_press.connect(sigc::mem_fun(this, &Viewer::key_press));
105
106         light.set_position(GL::Vector4(0, 0, 1, 0));
107         lighting.attach(0, light);
108
109         camera.set_up_direction(GL::Vector3(0, 0, 1));
110         camera.set_position(GL::Vector3(distance, 0, 0));
111         camera.look_at(GL::Vector3(0, 0, 0));
112 }
113
114 Viewer::~Viewer()
115 {
116         delete object;
117         delete mesh;
118 }
119
120 int Viewer::main()
121 {
122         window.show();
123         return Application::main();
124 }
125
126 void Viewer::tick()
127 {
128         window.tick();
129
130         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
131
132         camera.apply();
133
134         GL::Bind bind_lighting(lighting);
135         GL::Bind bind_depth(GL::DepthTest::lequal());
136         GL::Bind bind_blend(GL::Blend::alpha());
137         if(object)
138                 object->render();
139         else if(mesh)
140                 mesh->draw();
141
142         window.swap_buffers();
143 }
144
145 void Viewer::usage(const char *err, const char *argv0, bool)
146 {
147         if(err)
148                 IO::print("%s\n", err);
149         IO::print("Usage: %s <filename>\n", argv0);
150 }
151
152 void Viewer::button_press(int x, int y, unsigned btn, unsigned)
153 {
154         if(btn==1)
155         {
156                 dragging = 1;
157                 pointer_x = x;
158                 pointer_y = y;
159         }
160         else if(btn==3)
161         {
162                 dragging = 3;
163                 pointer_motion(x, y);
164         }
165         else if(btn==4)
166         {
167                 distance *= 0.9;
168                 update_camera();
169         }
170         else if(btn==5)
171         {
172                 distance *= 1.1;
173                 update_camera();
174         }
175 }
176
177 void Viewer::button_release(int, int, unsigned btn, unsigned)
178 {
179         if(btn==dragging)
180                 dragging = 0;
181 }
182
183 void Viewer::pointer_motion(int x, int y)
184 {
185         if(dragging==1)
186         {
187                 int dx = x-pointer_x;
188                 int dy = pointer_y-y;
189
190                 yaw -= dx*M_PI*2/window.get_width();
191                 while(yaw>M_PI)
192                         yaw -= M_PI*2;
193                 while(yaw<-M_PI)
194                         yaw += M_PI*2;
195
196                 pitch += dy*M_PI/window.get_height();
197                 if(pitch>M_PI*0.49)
198                         pitch = M_PI*0.49;
199                 if(pitch<-M_PI*0.49)
200                         pitch = -M_PI*0.49;
201
202                 update_camera();
203
204                 pointer_x = x;
205                 pointer_y = y;
206         }
207         else if(dragging==3)
208         {
209                 int dx = x-window.get_width()/2;
210                 int dy = window.get_height()/2-y;
211
212                 light_yaw = yaw+dx*M_PI/window.get_width();
213                 light_pitch = pitch-dy*M_PI/window.get_height();
214
215                 update_light();
216         }
217 }
218
219 void Viewer::key_press(unsigned, unsigned, wchar_t)
220 {
221 }
222
223 void Viewer::update_camera()
224 {
225         float cy = cos(yaw);
226         float sy = sin(yaw);
227         float cp = cos(pitch);
228         float sp = sin(pitch);
229         camera.set_position(GL::Vector3(-cy*cp*distance, -sy*cp*distance, -sp*distance));
230         camera.look_at(GL::Vector3(0, 0, 0));
231 }
232
233 void Viewer::update_light()
234 {
235         float cy = cos(light_yaw);
236         float sy = sin(light_yaw);
237         float cp = cos(light_pitch);
238         float sp = sin(light_pitch);
239         light.set_position(GL::Vector4(-cy*cp, -sy*cp, -sp));
240 }