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