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