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