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