dos: forgot to reshape at least once, to allocate the framebuffer
[retroray] / src / scr_mod.c
1 /*
2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <assert.h>
19 #include "gaw/gaw.h"
20 #include "app.h"
21 #include "rtk.h"
22 #include "scene.h"
23 #include "rt.h"
24 #include "cmesh.h"
25 #include "meshgen.h"
26
27 enum {
28         TBN_NEW, TBN_OPEN, TBN_SAVE, TBN_SEP1,
29         TBN_SEL, TBN_MOVE, TBN_ROT, TBN_SCALE, TBN_SEP2,
30         TBN_ADD, TBN_RM, TBN_SEP3,
31         TBN_UNION, TBN_ISECT, TBN_DIFF, TBN_SEP4,
32         TBN_MTL, TBN_REND, TBN_VIEWREND, TBN_SEP5, TBN_CFG,
33
34         NUM_TOOL_BUTTONS
35 };
36 static const char *tbn_icon_name[] = {
37         "new", "open", "save", 0,
38         "sel", "move", "rot", "scale", 0,
39         "add", "remove", 0,
40         "union", "isect", "diff", 0,
41         "mtl", "rend", "viewrend", 0, "cfg"
42 };
43 static int tbn_icon_pos[][2] = {
44         {0,0}, {16,0}, {32,0}, {-1,-1},
45         {48,0}, {64,0}, {80,0}, {96,0}, {-1,-1},
46         {112,0}, {112,16}, {-1,-1},
47         {0,16}, {16,16}, {32,16}, {-1,-1},
48         {48,16}, {64,16}, {80,16}, {-1,-1}, {96,16}
49 };
50 static int tbn_istool[] = {
51         0, 0, 0, 0,
52         1, 1, 1, 1, 0,
53         0, 0, 0,
54         1, 1, 1, 0,
55         0, 0, 0, 0, 0
56 };
57 static rtk_icon *tbn_icons[NUM_TOOL_BUTTONS];
58 static rtk_widget *tbn_buttons[NUM_TOOL_BUTTONS];
59
60 #define TOOLBAR_HEIGHT  26
61
62 enum {
63         TOOL_SEL, TOOL_MOVE, TOOL_ROT, TOOL_SCALE,
64         TOOL_UNION, TOOL_ISECT, TOOL_DIFF,
65         NUM_TOOLS
66 };
67 static rtk_widget *tools[NUM_TOOLS];
68
69
70 static int mdl_init(void);
71 static void mdl_destroy(void);
72 static int mdl_start(void);
73 static void mdl_stop(void);
74 static void mdl_display(void);
75 static void mdl_reshape(int x, int y);
76 static void mdl_keyb(int key, int press);
77 static void mdl_mouse(int bn, int press, int x, int y);
78 static void mdl_motion(int x, int y);
79
80 static void draw_object(struct object *obj);
81 static void draw_grid(void);
82 static void tbn_callback(rtk_widget *w, void *cls);
83
84 static void draw_rband(void);
85 static void primray(cgm_ray *ray, int x, int y);
86 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1);
87
88
89 struct app_screen scr_model = {
90         "modeller",
91         mdl_init, mdl_destroy,
92         mdl_start, mdl_stop,
93         mdl_display, mdl_reshape,
94         mdl_keyb, mdl_mouse, mdl_motion
95 };
96
97 static rtk_widget *toolbar;
98 static rtk_iconsheet *icons;
99
100 static struct cmesh *mesh_sph;
101
102 static float cam_theta, cam_phi = 20, cam_dist = 8;
103 static float view_matrix[16], proj_matrix[16];
104 static float view_matrix_inv[16], proj_matrix_inv[16];
105 static int viewport[4];
106 static cgm_ray pickray;
107
108 static int cur_tool;
109 static int selobj = -1;
110
111 static rtk_rect rband;
112 static int rband_valid;
113
114
115 static int mdl_init(void)
116 {
117         int i, toolidx;
118         rtk_widget *w;
119
120         if(!(icons = rtk_load_iconsheet("data/icons.png"))) {
121                 errormsg("failed to load iconsheet\n");
122                 return -1;
123         }
124         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
125                 if(tbn_icon_name[i]) {
126                         tbn_icons[i] = rtk_define_icon(icons, tbn_icon_name[i],
127                                         tbn_icon_pos[i][0], tbn_icon_pos[i][1], 16, 16);
128                 } else {
129                         tbn_icons[i] = 0;
130                 }
131         }
132
133         if(!(toolbar = rtk_create_window(0, "toolbar", 0, 0, win_width, TOOLBAR_HEIGHT))) {
134                 return -1;
135         }
136         rtk_win_layout(toolbar, RTK_HBOX);
137
138         toolidx = 0;
139         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
140                 if(!tbn_icons[i]) {
141                         rtk_create_separator(toolbar);
142                 } else {
143                         if(!(w = rtk_create_iconbutton(toolbar, tbn_icons[i], 0))) {
144                                 return -1;
145                         }
146                         tbn_buttons[i] = w;
147                         rtk_set_callback(w, tbn_callback, (void*)(intptr_t)i);
148                         if(tbn_istool[i]) {
149                                 rtk_bn_mode(w, RTK_TOGGLEBN);
150                                 tools[toolidx++] = w;
151                         }
152                         if(i == TBN_SEL) {
153                                 rtk_set_value(w, 1);
154                         }
155                 }
156         }
157         assert(toolidx == NUM_TOOLS);
158
159         if(!(mesh_sph = cmesh_alloc())) {
160                 errormsg("failed to allocate sphere vis mesh\n");
161                 return -1;
162         }
163         gen_sphere(mesh_sph, 1.0f, 16, 8, 1.0f, 1.0f);
164         return 0;
165 }
166
167 static void mdl_destroy(void)
168 {
169         cmesh_free(mesh_sph);
170         rtk_free_iconsheet(icons);
171 }
172
173 static int mdl_start(void)
174 {
175         gaw_clear_color(0.125, 0.125, 0.125, 1);
176
177         gaw_enable(GAW_DEPTH_TEST);
178         gaw_enable(GAW_CULL_FACE);
179         gaw_enable(GAW_LIGHTING);
180         gaw_enable(GAW_LIGHT0);
181         return 0;
182 }
183
184 static void mdl_stop(void)
185 {
186 }
187
188 static void mdl_display(void)
189 {
190         int i, num;
191
192         gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
193
194         rtk_draw_widget(toolbar);
195
196         gaw_matrix_mode(GAW_MODELVIEW);
197         gaw_load_identity();
198         gaw_translate(0, 0, -cam_dist);
199         gaw_rotate(cam_phi, 1, 0, 0);
200         gaw_rotate(cam_theta, 0, 1, 0);
201         gaw_get_modelview(view_matrix);
202         cgm_mcopy(view_matrix_inv, view_matrix);
203         cgm_minverse(view_matrix_inv);
204
205         draw_grid();
206
207         gaw_mtl_diffuse(0.5, 0.5, 0.5, 1);
208
209         num = scn_num_objects(scn);
210         for(i=0; i<num; i++) {
211                 if(i == selobj) {
212                         gaw_zoffset(1);
213                         gaw_enable(GAW_POLYGON_OFFSET);
214                         draw_object(scn->objects[i]);
215                         gaw_disable(GAW_POLYGON_OFFSET);
216
217                         gaw_save();
218                         gaw_disable(GAW_LIGHTING);
219                         gaw_poly_wire();
220                         gaw_color3f(0, 1, 0);
221                         draw_object(scn->objects[i]);
222                         gaw_poly_gouraud();
223                         gaw_restore();
224                 } else {
225                         draw_object(scn->objects[i]);
226                 }
227         }
228
229         if(rband_valid) {
230                 draw_rband();
231         }
232 }
233
234 static void draw_object(struct object *obj)
235 {
236         struct sphere *sph;
237
238         if(!obj->xform_valid) {
239                 calc_object_matrix(obj);
240         }
241         gaw_push_matrix();
242         gaw_mult_matrix(obj->xform);
243
244         switch(obj->type) {
245         case OBJ_SPHERE:
246                 sph = (struct sphere*)obj;
247                 gaw_scale(sph->rad, sph->rad, sph->rad);
248                 cmesh_draw(mesh_sph);
249                 break;
250
251         default:
252                 break;
253         }
254
255         gaw_pop_matrix();
256 }
257
258 static void draw_grid(void)
259 {
260         gaw_save();
261         gaw_disable(GAW_LIGHTING);
262
263         gaw_begin(GAW_LINES);
264         gaw_color3f(0.5, 0, 0);
265         gaw_vertex3f(0, 0, 0);
266         gaw_vertex3f(-100, 0, 0);
267         gaw_vertex3f(0, 0, 0);
268         gaw_vertex3f(100, 0, 0);
269         gaw_color3f(0, 0.5, 0);
270         gaw_vertex3f(0, 0, 0);
271         gaw_vertex3f(0, 0, -100);
272         gaw_vertex3f(0, 0, 0);
273         gaw_vertex3f(0, 0, 100);
274         gaw_end();
275
276         gaw_restore();
277 }
278
279 static void mdl_reshape(int x, int y)
280 {
281         float aspect = (float)x / (float)(y - TOOLBAR_HEIGHT);
282
283         viewport[0] = 0;
284         viewport[1] = TOOLBAR_HEIGHT;
285         viewport[2] = x;
286         viewport[3] = y - TOOLBAR_HEIGHT;
287         gaw_viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
288
289         gaw_matrix_mode(GAW_PROJECTION);
290         gaw_load_identity();
291         gaw_perspective(50, aspect, 0.5, 100.0);
292         gaw_get_projection(proj_matrix);
293         cgm_mcopy(proj_matrix_inv, proj_matrix);
294         cgm_minverse(proj_matrix_inv);
295
296         rtk_resize(toolbar, win_width, TOOLBAR_HEIGHT);
297 }
298
299 static void mdl_keyb(int key, int press)
300 {
301         if(rtk_input_key(toolbar, key, press)) {
302                 app_redisplay();
303                 return;
304         }
305 }
306
307 static int vpdrag;
308
309 static void mdl_mouse(int bn, int press, int x, int y)
310 {
311         struct rayhit hit;
312         if(!vpdrag && rtk_input_mbutton(toolbar, bn, press, x, y)) {
313                 app_redisplay();
314                 return;
315         }
316
317         if(press) {
318                 rband.x = x;
319                 rband.y = y;
320                 vpdrag |= (1 << bn);
321         } else {
322                 vpdrag &= ~(1 << bn);
323
324                 if(rband_valid) {
325                         printf("rubber band: %d,%d %dx%d\n", rband.x, rband.y, rband.width, rband.height);
326                         rband_valid = 0;
327
328                 } else if(bn == 0 && x == rband.x && y == rband.y) {
329                         primray(&pickray, x, y);
330                         if(scn_intersect(scn, &pickray, &hit)) {
331                                 selobj = scn_object_index(scn, hit.obj);
332                         } else {
333                                 selobj = -1;
334                         }
335                 }
336                 app_redisplay();
337         }
338 }
339
340 static void mdl_motion(int x, int y)
341 {
342         int dx, dy;
343
344         if(!vpdrag && rtk_input_mmotion(toolbar, x, y)) {
345                 app_redisplay();
346                 return;
347         }
348
349         dx = x - mouse_x;
350         dy = y - mouse_y;
351
352         if(modkeys) {
353                 /* navigation */
354                 if(mouse_state[0]) {
355                         cam_theta += dx * 0.5f;
356                         cam_phi += dy * 0.5f;
357                         if(cam_phi < -90) cam_phi = -90;
358                         if(cam_phi > 90) cam_phi = 90;
359                         app_redisplay();
360                 }
361
362                 if(mouse_state[2]) {
363                         cam_dist += dy * 0.1f;
364                         if(cam_dist < 0) cam_dist = 0;
365                         app_redisplay();
366                 }
367         } else {
368                 if(mouse_state[0]) {
369                         switch(cur_tool) {
370                         case TOOL_SEL:
371                                 if(rband.x != x || rband.y != y) {
372                                         rband.width = x - rband.x;
373                                         rband.height = y - rband.y;
374                                         rband_valid = 1;
375                                 }
376                                 break;
377
378                         case TOOL_MOVE:
379                                 if(selobj >= 0) {
380                                         struct object *obj = scn->objects[selobj];
381                                         moveobj(obj, mouse_x, mouse_y, x, y);
382                                 }
383                                 break;
384
385                         default:
386                                 break;
387                         }
388                 }
389                 app_redisplay();
390         }
391 }
392
393 static void add_sphere(void)
394 {
395         struct object *obj;
396
397         if(!(obj = create_object(OBJ_SPHERE))) {
398                 return;
399         }
400         scn_add_object(scn, obj);
401 }
402
403 static void tbn_callback(rtk_widget *w, void *cls)
404 {
405         int i, id = (intptr_t)cls;
406         int idx;
407
408         switch(id) {
409         case TBN_SEL:
410         case TBN_MOVE:
411         case TBN_ROT:
412         case TBN_SCALE:
413                 idx = id - TBN_SEL;
414                 if(0) {
415         case TBN_UNION:
416         case TBN_ISECT:
417         case TBN_DIFF:
418                         idx = id - TBN_UNION + TOOL_UNION;
419                 }
420                 cur_tool = idx;
421                 for(i=0; i<NUM_TOOLS; i++) {
422                         if(i != cur_tool) {
423                                 rtk_set_value(tools[i], 0);
424                         }
425                 }
426                 break;
427
428         case TBN_ADD:
429                 idx = scn_num_objects(scn);
430                 add_sphere();
431                 selobj = idx;
432                 break;
433
434         case TBN_RM:
435                 if(selobj >= 0) {
436                         scn_rm_object(scn, selobj);
437                         selobj = -1;
438                         app_redisplay();
439                 }
440                 break;
441
442         default:
443                 break;
444         }
445 }
446
447 static void draw_rband(void)
448 {
449         int i, x, y, w, h;
450         uint32_t *fbptr, *bptr;
451
452         x = rband.x;
453         y = rband.y;
454
455         if(rband.width < 0) {
456                 w = -rband.width;
457                 x += rband.width;
458         } else {
459                 w = rband.width;
460         }
461         if(rband.height < 0) {
462                 h = -rband.height;
463                 y += rband.height;
464         } else {
465                 h = rband.height;
466         }
467
468         fbptr = framebuf + y * win_width + x;
469         bptr = fbptr + win_width * (h - 1);
470
471         for(i=0; i<w; i++) {
472                 fbptr[i] ^= 0xffffff;
473                 bptr[i] ^= 0xffffff;
474         }
475         fbptr += win_width;
476         for(i=0; i<h-2; i++) {
477                 fbptr[0] ^= 0xffffff;
478                 fbptr[w - 1] ^= 0xffffff;
479                 fbptr += win_width;
480         }
481 }
482
483 static void primray(cgm_ray *ray, int x, int y)
484 {
485         float nx, ny;
486         cgm_vec3 npos, farpt;
487         float inv_pv[16];
488
489         y = win_height - y;
490         nx = (float)(x - viewport[0]) / (float)viewport[2];
491         ny = (float)(y - viewport[1]) / (float)viewport[3];
492
493         cgm_mcopy(inv_pv, proj_matrix_inv);
494         cgm_mmul(inv_pv, view_matrix_inv);
495
496         cgm_vcons(&npos, nx, ny, 0.0f);
497         cgm_unproject(&ray->origin, &npos, inv_pv);
498         npos.z = 1.0f;
499         cgm_unproject(&farpt, &npos, inv_pv);
500
501         ray->dir.x = farpt.x - ray->origin.x;
502         ray->dir.y = farpt.y - ray->origin.y;
503         ray->dir.z = farpt.z - ray->origin.z;
504 }
505
506 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1)
507 {
508         cgm_ray ray;
509         float dist;
510         cgm_vec3 p0, p1;
511
512         primray(&ray, px0, py0);
513         cgm_vnormalize(&ray.dir);
514         dist = ray_object_dist(&ray, obj);
515         cgm_raypos(&p0, &ray, dist);
516         primray(&ray, px1, py1);
517         cgm_vnormalize(&ray.dir);
518         cgm_raypos(&p1, &ray, dist);
519
520         cgm_vsub(&p1, &p0);
521         cgm_vadd(&obj->pos, &p1);
522         obj->xform_valid = 0;
523 }