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