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