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