added dos timer and double-esc to quit
[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 "geom.h"
24 #include "cmesh.h"
25 #include "meshgen.h"
26 #include "font.h"
27 #include "rend.h"
28
29 enum {
30         TBN_NEW, TBN_OPEN, TBN_SAVE, TBN_SEP1,
31         TBN_SEL, TBN_MOVE, TBN_ROT, TBN_SCALE, TBN_SEP2,
32         TBN_ADD, TBN_RM, TBN_SEP3,
33         TBN_UNION, TBN_ISECT, TBN_DIFF, TBN_SEP4,
34         TBN_MTL, TBN_REND, TBN_REND_AREA, TBN_VIEWREND, TBN_SEP5, TBN_CFG,
35
36         NUM_TOOL_BUTTONS
37 };
38 static const char *tbn_icon_name[] = {
39         "new", "open", "save", 0,
40         "sel", "move", "rot", "scale", 0,
41         "add", "remove", 0,
42         "union", "isect", "diff", 0,
43         "mtl", "rend", "rend-area", "viewrend", 0, "cfg"
44 };
45 static int tbn_icon_pos[][2] = {
46         {0,0}, {16,0}, {32,0}, {-1,-1},
47         {48,0}, {64,0}, {80,0}, {96,0}, {-1,-1},
48         {112,0}, {112,16}, {-1,-1},
49         {0,16}, {16,16}, {32,16}, {-1,-1},
50         {48,16}, {64,16}, {64, 32}, {80,16}, {-1,-1}, {96,16}
51 };
52 static int tbn_istool[] = {
53         0, 0, 0, 0,
54         1, 1, 1, 1, 0,
55         0, 0, 0,
56         1, 1, 1, 0,
57         0, 0, 1, 0, 0, 0
58 };
59 static rtk_icon *tbn_icons[NUM_TOOL_BUTTONS];
60 static rtk_widget *tbn_buttons[NUM_TOOL_BUTTONS];
61
62 #define TOOLBAR_HEIGHT  26
63
64 enum {
65         TOOL_SEL, TOOL_MOVE, TOOL_ROT, TOOL_SCALE,
66         TOOL_UNION, TOOL_ISECT, TOOL_DIFF, TOOL_REND_AREA,
67         NUM_TOOLS
68 };
69 static rtk_widget *tools[NUM_TOOLS];
70
71 static int vpdirty;
72 static rtk_rect totalrend;
73
74
75 static int mdl_init(void);
76 static void mdl_destroy(void);
77 static int mdl_start(void);
78 static void mdl_stop(void);
79 static void mdl_display(void);
80 static void mdl_reshape(int x, int y);
81 static void mdl_keyb(int key, int press);
82 static void mdl_mouse(int bn, int press, int x, int y);
83 static void mdl_motion(int x, int y);
84
85 static void draw_object(struct object *obj);
86 static void setup_material(struct material *mtl);
87 static void draw_grid(void);
88 static void tbn_callback(rtk_widget *w, void *cls);
89
90 static void act_settool(int tidx);
91 static void act_addobj(void);
92 static void act_rmobj(void);
93
94 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1);
95
96 static void inval_vport(void);
97
98
99 struct app_screen scr_model = {
100         "modeller",
101         mdl_init, mdl_destroy,
102         mdl_start, mdl_stop,
103         mdl_display, mdl_reshape,
104         mdl_keyb, mdl_mouse, mdl_motion
105 };
106
107 static rtk_widget *toolbar;
108 static rtk_iconsheet *icons;
109
110 static struct cmesh *mesh_sph;
111
112 static float cam_theta, cam_phi = 20, cam_dist = 8;
113 static float view_matrix[16], proj_matrix[16];
114 static float view_matrix_inv[16], proj_matrix_inv[16];
115 static int viewport[4];
116 static cgm_ray pickray;
117
118 static int cur_tool, prev_tool = -1;
119 static int selobj = -1;
120
121 static rtk_rect rband;
122 static int rband_valid;
123
124 static int rendering;
125 static rtk_rect rendrect;
126
127
128 static int mdl_init(void)
129 {
130         int i, toolidx;
131         rtk_widget *w;
132
133         if(!(icons = rtk_load_iconsheet("data/icons.png"))) {
134                 errormsg("failed to load iconsheet\n");
135                 return -1;
136         }
137         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
138                 if(tbn_icon_name[i]) {
139                         tbn_icons[i] = rtk_define_icon(icons, tbn_icon_name[i],
140                                         tbn_icon_pos[i][0], tbn_icon_pos[i][1], 16, 16);
141                 } else {
142                         tbn_icons[i] = 0;
143                 }
144         }
145
146         if(!(toolbar = rtk_create_window(0, "toolbar", 0, 0, win_width, TOOLBAR_HEIGHT))) {
147                 return -1;
148         }
149         rtk_win_layout(toolbar, RTK_HBOX);
150
151         toolidx = 0;
152         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
153                 if(!tbn_icons[i]) {
154                         rtk_create_separator(toolbar);
155                 } else {
156                         if(!(w = rtk_create_iconbutton(toolbar, tbn_icons[i], 0))) {
157                                 return -1;
158                         }
159                         tbn_buttons[i] = w;
160                         rtk_set_callback(w, tbn_callback, (void*)(intptr_t)i);
161                         if(tbn_istool[i]) {
162                                 rtk_bn_mode(w, RTK_TOGGLEBN);
163                                 tools[toolidx++] = w;
164                         }
165                         if(i == TBN_SEL) {
166                                 rtk_set_value(w, 1);
167                         }
168                 }
169         }
170         assert(toolidx == NUM_TOOLS);
171
172         if(!(mesh_sph = cmesh_alloc())) {
173                 errormsg("failed to allocate sphere vis mesh\n");
174                 return -1;
175         }
176         gen_sphere(mesh_sph, 1.0f, 16, 8, 1.0f, 1.0f);
177
178         vpdirty = 1;
179         return 0;
180 }
181
182 static void mdl_destroy(void)
183 {
184         cmesh_free(mesh_sph);
185         rtk_free_iconsheet(icons);
186 }
187
188 static int mdl_start(void)
189 {
190         gaw_clear_color(0.125, 0.125, 0.125, 1);
191
192         gaw_enable(GAW_DEPTH_TEST);
193         gaw_enable(GAW_CULL_FACE);
194         gaw_enable(GAW_LIGHTING);
195         gaw_enable(GAW_LIGHT0);
196
197         rend_pan(0, -TOOLBAR_HEIGHT);
198         return 0;
199 }
200
201 static void mdl_stop(void)
202 {
203 }
204
205 static void mdl_display(void)
206 {
207         int i, num;
208
209         /* viewport */
210         if(vpdirty) {
211                 gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
212
213                 gaw_matrix_mode(GAW_MODELVIEW);
214                 gaw_load_identity();
215                 gaw_translate(0, 0, -cam_dist);
216                 gaw_rotate(cam_phi, 1, 0, 0);
217                 gaw_rotate(cam_theta, 0, 1, 0);
218                 gaw_get_modelview(view_matrix);
219                 cgm_mcopy(view_matrix_inv, view_matrix);
220                 cgm_minverse(view_matrix_inv);
221
222                 draw_grid();
223
224                 num = scn_num_objects(scn);
225                 for(i=0; i<num; i++) {
226                         setup_material(scn->objects[i]->mtl);
227
228                         if(i == selobj) {
229                                 gaw_zoffset(1);
230                                 gaw_enable(GAW_POLYGON_OFFSET);
231                                 draw_object(scn->objects[i]);
232                                 gaw_disable(GAW_POLYGON_OFFSET);
233
234                                 gaw_save();
235                                 gaw_disable(GAW_LIGHTING);
236                                 gaw_poly_wire();
237                                 gaw_color3f(0, 1, 0);
238                                 draw_object(scn->objects[i]);
239                                 gaw_poly_gouraud();
240                                 gaw_restore();
241                         } else {
242                                 draw_object(scn->objects[i]);
243                         }
244                 }
245                 vpdirty = 0;
246
247                 /* dirty all GUI windows */
248                 rtk_invalidate(toolbar);
249         }
250
251         /* render layer */
252         if(rendering) {
253                 if(!render(framebuf)) {
254                         rendering = 0;
255                 }
256                 app_redisplay(rendrect.x, rendrect.y, rendrect.width, rendrect.height);
257         }
258
259         /* GUI */
260         rtk_draw_widget(toolbar);
261 }
262
263 static void draw_object(struct object *obj)
264 {
265         struct sphere *sph;
266
267         if(!obj->xform_valid) {
268                 calc_object_matrix(obj);
269         }
270         gaw_push_matrix();
271         gaw_mult_matrix(obj->xform);
272
273         switch(obj->type) {
274         case OBJ_SPHERE:
275                 sph = (struct sphere*)obj;
276                 gaw_scale(sph->rad, sph->rad, sph->rad);
277                 cmesh_draw(mesh_sph);
278                 break;
279
280         default:
281                 break;
282         }
283
284         gaw_pop_matrix();
285 }
286
287 static void setup_material(struct material *mtl)
288 {
289         gaw_mtl_diffuse(mtl->kd.x, mtl->kd.y, mtl->kd.z, 1.0f);
290         gaw_mtl_specular(mtl->ks.x, mtl->ks.y, mtl->ks.z, mtl->shin);
291         gaw_mtl_emission(mtl->ke.x, mtl->ke.y, mtl->ke.z);
292 }
293
294 static void draw_grid(void)
295 {
296         gaw_save();
297         gaw_disable(GAW_LIGHTING);
298
299         gaw_begin(GAW_LINES);
300         gaw_color3f(0.5, 0, 0);
301         gaw_vertex3f(0, 0, 0);
302         gaw_vertex3f(-100, 0, 0);
303         gaw_vertex3f(0, 0, 0);
304         gaw_vertex3f(100, 0, 0);
305         gaw_color3f(0, 0.5, 0);
306         gaw_vertex3f(0, 0, 0);
307         gaw_vertex3f(0, 0, -100);
308         gaw_vertex3f(0, 0, 0);
309         gaw_vertex3f(0, 0, 100);
310         gaw_end();
311
312         gaw_restore();
313 }
314
315 static void mdl_reshape(int x, int y)
316 {
317         float aspect = (float)x / (float)(y - TOOLBAR_HEIGHT);
318
319         viewport[0] = 0;
320         viewport[1] = TOOLBAR_HEIGHT;
321         viewport[2] = x;
322         viewport[3] = y - TOOLBAR_HEIGHT;
323         gaw_viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
324
325         gaw_matrix_mode(GAW_PROJECTION);
326         gaw_load_identity();
327         gaw_perspective(50, aspect, 0.5, 100.0);
328         gaw_get_projection(proj_matrix);
329         cgm_mcopy(proj_matrix_inv, proj_matrix);
330         cgm_minverse(proj_matrix_inv);
331
332         rtk_resize(toolbar, win_width, TOOLBAR_HEIGHT);
333 }
334
335 static void mdl_keyb(int key, int press)
336 {
337         if(rtk_input_key(toolbar, key, press)) {
338                 return;
339         }
340
341         if(press) {
342                 switch(key) {
343                 case 27:
344                         act_settool(TOOL_SEL);
345                         break;
346                 case 'g':
347                         act_settool(TOOL_MOVE);
348                         break;
349                 case 'r':
350                         act_settool(TOOL_ROT);
351                         break;
352                 case 's':
353                         act_settool(TOOL_SCALE);
354                         break;
355
356                 case KEY_F6:
357                         act_settool(TOOL_REND_AREA);
358                         break;
359
360                 case KEY_DEL:
361                         act_rmobj();
362                         break;
363
364                 default:
365                         break;
366                 }
367         }
368 }
369
370 static int vpdrag;
371
372 static void mdl_mouse(int bn, int press, int x, int y)
373 {
374         struct rayhit hit;
375         if(!vpdrag && rtk_input_mbutton(toolbar, bn, press, x, y)) {
376                 return;
377         }
378
379         if(press) {
380                 rband_valid = 0;
381                 rband.x = x;
382                 rband.y = y;
383                 vpdrag |= (1 << bn);
384         } else {
385                 vpdrag &= ~(1 << bn);
386
387                 if(rband_valid) {
388                         rband_valid = 0;
389                         app_rband(0, 0, 0, 0);
390
391                         if(cur_tool == TOOL_REND_AREA) {
392                                 if(rband.width && rband.height) {
393                                         rendering = 1;
394                                         rend_size(win_width, win_height);
395                                         rtk_fix_rect(&rband);
396                                         rendrect = rband;
397                                         rend_begin(rband.x, rband.y, rband.width, rband.height);
398                                         app_redisplay(rband.x, rband.y, rband.width, rband.height);
399
400                                         if(totalrend.width) {
401                                                 rtk_rect_union(&totalrend, &rband);
402                                         } else {
403                                                 totalrend = rband;
404                                         }
405                                 }
406                         }
407
408                 } else if(bn == 0 && x == rband.x && y == rband.y) {
409                         primray(&pickray, x, y);
410                         if(scn_intersect(scn, &pickray, &hit)) {
411                                 int newsel = scn_object_index(scn, hit.obj);
412                                 if(newsel != selobj) {
413                                         selobj = newsel;
414                                         inval_vport();
415                                 }
416                         } else {
417                                 if(selobj != -1) {
418                                         inval_vport();
419                                 }
420                                 selobj = -1;
421                         }
422                 }
423         }
424 }
425
426 static void mdl_motion(int x, int y)
427 {
428         int dx, dy;
429
430         if(!vpdrag && rtk_input_mmotion(toolbar, x, y)) {
431                 return;
432         }
433
434         dx = x - mouse_x;
435         dy = y - mouse_y;
436
437         if(modkeys) {
438                 /* navigation */
439                 if(mouse_state[0]) {
440                         cam_theta += dx * 0.5f;
441                         cam_phi += dy * 0.5f;
442                         if(cam_phi < -90) cam_phi = -90;
443                         if(cam_phi > 90) cam_phi = 90;
444                         inval_vport();
445                 }
446
447                 if(mouse_state[2]) {
448                         cam_dist += dy * 0.1f;
449                         if(cam_dist < 0) cam_dist = 0;
450                         inval_vport();
451                 }
452         } else {
453                 if(mouse_state[0]) {
454                         switch(cur_tool) {
455                         case TOOL_SEL:
456                         case TOOL_REND_AREA:
457                                 if(rband.x != x || rband.y != y) {
458                                         rband.width = x - rband.x;
459                                         rband.height = y - rband.y;
460                                         rband_valid = 1;
461                                         app_rband(rband.x, rband.y, rband.width, rband.height);
462                                 }
463                                 break;
464
465                         case TOOL_MOVE:
466                                 if(selobj >= 0) {
467                                         struct object *obj = scn->objects[selobj];
468                                         moveobj(obj, mouse_x, mouse_y, x, y);
469                                 }
470                                 break;
471
472                         default:
473                                 break;
474                         }
475                 }
476         }
477 }
478
479 static void add_sphere(void)
480 {
481         struct object *obj;
482
483         if(!(obj = create_object(OBJ_SPHERE))) {
484                 return;
485         }
486         scn_add_object(scn, obj);
487 }
488
489 static void tbn_callback(rtk_widget *w, void *cls)
490 {
491         int id = (intptr_t)cls;
492
493         switch(id) {
494         case TBN_SEL:
495         case TBN_MOVE:
496         case TBN_ROT:
497         case TBN_SCALE:
498                 act_settool(id - TBN_SEL);
499                 break;
500         case TBN_UNION:
501         case TBN_ISECT:
502         case TBN_DIFF:
503                 act_settool(id - TBN_UNION + TOOL_UNION);
504                 break;
505         case TBN_REND_AREA:
506                 act_settool(TOOL_REND_AREA);
507                 break;
508
509         case TBN_ADD:
510                 act_addobj();
511                 break;
512
513         case TBN_RM:
514                 act_rmobj();
515                 break;
516
517         default:
518                 break;
519         }
520 }
521
522 static void act_settool(int tidx)
523 {
524         int i;
525         rtk_rect r;
526
527         if(tidx == prev_tool) return;
528
529         if(prev_tool == TOOL_REND_AREA) {
530                 app_redisplay(totalrend.x, totalrend.y, totalrend.width, totalrend.height);
531                 totalrend.width = 0;
532         }
533
534         prev_tool = cur_tool;
535         cur_tool = tidx;
536         for(i=0; i<NUM_TOOLS; i++) {
537                 if(i == cur_tool) {
538                         if(!rtk_get_value(tools[i])) {
539                                 rtk_set_value(tools[i], 1);
540                                 rtk_get_rect(tools[i], &r);
541                         }
542                 } else {
543                         if(rtk_get_value(tools[i])) {
544                                 rtk_set_value(tools[i], 0);
545                                 rtk_get_rect(tools[i], &r);
546                         }
547                 }
548         }
549 }
550
551 static void act_addobj(void)
552 {
553         int idx = scn_num_objects(scn);
554         add_sphere();
555         selobj = idx;
556
557         inval_vport();
558 }
559
560 static void act_rmobj(void)
561 {
562         if(selobj >= 0) {
563                 scn_rm_object(scn, selobj);
564                 selobj = -1;
565                 inval_vport();
566         }
567 }
568
569
570 void primray(cgm_ray *ray, int x, int y)
571 {
572         float nx, ny;
573         cgm_vec3 npos, farpt;
574         float inv_pv[16];
575
576         y = win_height - y;
577         nx = (float)(x - viewport[0]) / (float)viewport[2];
578         ny = (float)(y - viewport[1]) / (float)viewport[3];
579
580         cgm_mcopy(inv_pv, proj_matrix_inv);
581         cgm_mmul(inv_pv, view_matrix_inv);
582
583         cgm_vcons(&npos, nx, ny, 0.0f);
584         cgm_unproject(&ray->origin, &npos, inv_pv);
585         npos.z = 1.0f;
586         cgm_unproject(&farpt, &npos, inv_pv);
587
588         ray->dir.x = farpt.x - ray->origin.x;
589         ray->dir.y = farpt.y - ray->origin.y;
590         ray->dir.z = farpt.z - ray->origin.z;
591 }
592
593 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1)
594 {
595         cgm_ray ray;
596         float dist;
597         cgm_vec3 p0, p1;
598
599         primray(&ray, px0, py0);
600         cgm_vnormalize(&ray.dir);
601         dist = ray_object_dist(&ray, obj);
602         cgm_raypos(&p0, &ray, dist);
603         primray(&ray, px1, py1);
604         cgm_vnormalize(&ray.dir);
605         cgm_raypos(&p1, &ray, dist);
606
607         cgm_vsub(&p1, &p0);
608         cgm_vadd(&obj->pos, &p1);
609         obj->xform_valid = 0;
610
611         inval_vport();
612 }
613
614 static void inval_vport(void)
615 {
616         vpdirty = 1;
617         app_redisplay(0, 0, 0, 0);
618 }