picking and brute force ray intersections
[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 draw_rband(void);
85 static void primray(cgm_ray *ray, int x, int y);
86
87
88 struct app_screen scr_model = {
89         "modeller",
90         mdl_init, mdl_destroy,
91         mdl_start, mdl_stop,
92         mdl_display, mdl_reshape,
93         mdl_keyb, mdl_mouse, mdl_motion
94 };
95
96 static rtk_widget *toolbar;
97 static rtk_iconsheet *icons;
98
99 static struct cmesh *mesh_sph;
100
101 static float cam_theta, cam_phi = 20, cam_dist = 8;
102 static float view_matrix[16], proj_matrix[16];
103 static float view_matrix_inv[16], proj_matrix_inv[16];
104 static int viewport[4];
105 static cgm_ray pickray;
106
107 static int cur_tool;
108 static int selobj = -1;
109
110 static rtk_rect rband;
111 static int rband_valid;
112
113 static int mdl_init(void)
114 {
115         int i, toolidx;
116         rtk_widget *w;
117
118         if(!(icons = rtk_load_iconsheet("data/icons.png"))) {
119                 errormsg("failed to load iconsheet\n");
120                 return -1;
121         }
122         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
123                 if(tbn_icon_name[i]) {
124                         tbn_icons[i] = rtk_define_icon(icons, tbn_icon_name[i],
125                                         tbn_icon_pos[i][0], tbn_icon_pos[i][1], 16, 16);
126                 } else {
127                         tbn_icons[i] = 0;
128                 }
129         }
130
131         if(!(toolbar = rtk_create_window(0, "toolbar", 0, 0, win_width, TOOLBAR_HEIGHT))) {
132                 return -1;
133         }
134         rtk_win_layout(toolbar, RTK_HBOX);
135
136         toolidx = 0;
137         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
138                 if(!tbn_icons[i]) {
139                         rtk_create_separator(toolbar);
140                 } else {
141                         if(!(w = rtk_create_iconbutton(toolbar, tbn_icons[i], 0))) {
142                                 return -1;
143                         }
144                         tbn_buttons[i] = w;
145                         rtk_set_callback(w, tbn_callback, (void*)(intptr_t)i);
146                         if(tbn_istool[i]) {
147                                 rtk_bn_mode(w, RTK_TOGGLEBN);
148                                 tools[toolidx++] = w;
149                         }
150                         if(i == TBN_SEL) {
151                                 rtk_set_value(w, 1);
152                         }
153                 }
154         }
155         assert(toolidx == NUM_TOOLS);
156
157         if(!(mesh_sph = cmesh_alloc())) {
158                 errormsg("failed to allocate sphere vis mesh\n");
159                 return -1;
160         }
161         gen_sphere(mesh_sph, 1.0f, 16, 8, 1.0f, 1.0f);
162         return 0;
163 }
164
165 static void mdl_destroy(void)
166 {
167         cmesh_free(mesh_sph);
168         rtk_free_iconsheet(icons);
169 }
170
171 static int mdl_start(void)
172 {
173         gaw_clear_color(0.125, 0.125, 0.125, 1);
174
175         gaw_enable(GAW_DEPTH_TEST);
176         gaw_enable(GAW_CULL_FACE);
177         gaw_enable(GAW_LIGHTING);
178         gaw_enable(GAW_LIGHT0);
179         return 0;
180 }
181
182 static void mdl_stop(void)
183 {
184 }
185
186 static void mdl_display(void)
187 {
188         int i, num;
189
190         gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
191
192         rtk_draw_widget(toolbar);
193
194         gaw_matrix_mode(GAW_MODELVIEW);
195         gaw_load_identity();
196         gaw_translate(0, 0, -cam_dist);
197         gaw_rotate(cam_phi, 1, 0, 0);
198         gaw_rotate(cam_theta, 0, 1, 0);
199         gaw_get_modelview(view_matrix);
200         cgm_mcopy(view_matrix_inv, view_matrix);
201         cgm_minverse(view_matrix_inv);
202
203         draw_grid();
204
205         gaw_mtl_diffuse(0.5, 0.5, 0.5, 1);
206
207         num = scn_num_objects(scn);
208         for(i=0; i<num; i++) {
209                 if(i == selobj) {
210                         gaw_zoffset(1);
211                         gaw_enable(GAW_POLYGON_OFFSET);
212                         draw_object(scn->objects[i]);
213                         gaw_disable(GAW_POLYGON_OFFSET);
214
215                         gaw_save();
216                         gaw_disable(GAW_LIGHTING);
217                         gaw_poly_wire();
218                         gaw_color3f(0, 1, 0);
219                         draw_object(scn->objects[i]);
220                         gaw_poly_gouraud();
221                         gaw_restore();
222                 } else {
223                         draw_object(scn->objects[i]);
224                 }
225         }
226
227         if(rband_valid) {
228                 draw_rband();
229         }
230 }
231
232 static void draw_object(struct object *obj)
233 {
234         struct sphere *sph;
235
236         calc_object_matrix(obj);
237         gaw_push_matrix();
238         gaw_mult_matrix(obj->xform);
239
240         switch(obj->type) {
241         case OBJ_SPHERE:
242                 sph = (struct sphere*)obj;
243                 gaw_scale(sph->rad, sph->rad, sph->rad);
244                 cmesh_draw(mesh_sph);
245                 break;
246
247         default:
248                 break;
249         }
250
251         gaw_pop_matrix();
252 }
253
254 static void draw_grid(void)
255 {
256         gaw_save();
257         gaw_disable(GAW_LIGHTING);
258
259         gaw_begin(GAW_LINES);
260         gaw_color3f(0.5, 0, 0);
261         gaw_vertex3f(0, 0, 0);
262         gaw_vertex3f(-100, 0, 0);
263         gaw_vertex3f(0, 0, 0);
264         gaw_vertex3f(100, 0, 0);
265         gaw_color3f(0, 0.5, 0);
266         gaw_vertex3f(0, 0, 0);
267         gaw_vertex3f(0, 0, -100);
268         gaw_vertex3f(0, 0, 0);
269         gaw_vertex3f(0, 0, 100);
270         gaw_end();
271
272         gaw_restore();
273 }
274
275 static void mdl_reshape(int x, int y)
276 {
277         float aspect = (float)x / (float)(y - TOOLBAR_HEIGHT);
278
279         viewport[0] = 0;
280         viewport[1] = TOOLBAR_HEIGHT;
281         viewport[2] = x;
282         viewport[3] = y - TOOLBAR_HEIGHT;
283         gaw_viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
284
285         gaw_matrix_mode(GAW_PROJECTION);
286         gaw_load_identity();
287         gaw_perspective(50, aspect, 0.5, 100.0);
288         gaw_get_projection(proj_matrix);
289         cgm_mcopy(proj_matrix_inv, proj_matrix);
290         cgm_minverse(proj_matrix_inv);
291
292         rtk_resize(toolbar, win_width, TOOLBAR_HEIGHT);
293 }
294
295 static void mdl_keyb(int key, int press)
296 {
297         if(rtk_input_key(toolbar, key, press)) {
298                 app_redisplay();
299                 return;
300         }
301 }
302
303 static int vpdrag;
304
305 static void mdl_mouse(int bn, int press, int x, int y)
306 {
307         struct rayhit hit;
308         if(!vpdrag && rtk_input_mbutton(toolbar, bn, press, x, y)) {
309                 app_redisplay();
310                 return;
311         }
312
313         if(press) {
314                 rband.x = x;
315                 rband.y = y;
316                 vpdrag |= (1 << bn);
317         } else {
318                 vpdrag &= ~(1 << bn);
319
320                 if(rband_valid) {
321                         printf("rubber band: %d,%d %dx%d\n", rband.x, rband.y, rband.width, rband.height);
322                         rband_valid = 0;
323
324                 } else if(bn == 0 && x == rband.x && y == rband.y) {
325                         primray(&pickray, x, y);
326                         if(scn_intersect(scn, &pickray, &hit)) {
327                                 selobj = scn_object_index(scn, hit.obj);
328                         } else {
329                                 selobj = -1;
330                         }
331                 }
332                 app_redisplay();
333         }
334 }
335
336 static void mdl_motion(int x, int y)
337 {
338         int dx, dy;
339
340         if(!vpdrag && rtk_input_mmotion(toolbar, x, y)) {
341                 app_redisplay();
342                 return;
343         }
344
345         dx = x - mouse_x;
346         dy = y - mouse_y;
347
348         if(modkeys) {
349                 /* navigation */
350                 if(mouse_state[0]) {
351                         cam_theta += dx * 0.5f;
352                         cam_phi += dy * 0.5f;
353                         if(cam_phi < -90) cam_phi = -90;
354                         if(cam_phi > 90) cam_phi = 90;
355                         app_redisplay();
356                 }
357
358                 if(mouse_state[2]) {
359                         cam_dist += dy * 0.1f;
360                         if(cam_dist < 0) cam_dist = 0;
361                         app_redisplay();
362                 }
363         } else {
364                 if(mouse_state[0]) {
365                         if(rband.x != x || rband.y != y) {
366                                 rband.width = x - rband.x;
367                                 rband.height = y - rband.y;
368                                 rband_valid = 1;
369                         }
370                 }
371                 app_redisplay();
372         }
373 }
374
375 static void add_sphere(void)
376 {
377         struct object *obj;
378
379         if(!(obj = create_object(OBJ_SPHERE))) {
380                 return;
381         }
382         scn_add_object(scn, obj);
383 }
384
385 static void tbn_callback(rtk_widget *w, void *cls)
386 {
387         int i, id = (intptr_t)cls;
388         int idx;
389
390         switch(id) {
391         case TBN_SEL:
392         case TBN_MOVE:
393         case TBN_ROT:
394         case TBN_SCALE:
395                 idx = id - TBN_SEL;
396                 if(0) {
397         case TBN_UNION:
398         case TBN_ISECT:
399         case TBN_DIFF:
400                         idx = id - TBN_UNION + TOOL_UNION;
401                 }
402                 cur_tool = idx;
403                 for(i=0; i<NUM_TOOLS; i++) {
404                         if(i != cur_tool) {
405                                 rtk_set_value(tools[i], 0);
406                         }
407                 }
408                 break;
409
410         case TBN_ADD:
411                 idx = scn_num_objects(scn);
412                 add_sphere();
413                 selobj = idx;
414                 break;
415
416         case TBN_RM:
417                 if(selobj >= 0) {
418                         scn_rm_object(scn, selobj);
419                         selobj = -1;
420                         app_redisplay();
421                 }
422                 break;
423
424         default:
425                 break;
426         }
427 }
428
429 static void draw_rband(void)
430 {
431         int i, x, y, w, h;
432         uint32_t *fbptr, *bptr;
433
434         x = rband.x;
435         y = rband.y;
436
437         if(rband.width < 0) {
438                 w = -rband.width;
439                 x += rband.width;
440         } else {
441                 w = rband.width;
442         }
443         if(rband.height < 0) {
444                 h = -rband.height;
445                 y += rband.height;
446         } else {
447                 h = rband.height;
448         }
449
450         fbptr = framebuf + y * win_width + x;
451         bptr = fbptr + win_width * (h - 1);
452
453         for(i=0; i<w; i++) {
454                 fbptr[i] ^= 0xffffff;
455                 bptr[i] ^= 0xffffff;
456         }
457         fbptr += win_width;
458         for(i=0; i<h-2; i++) {
459                 fbptr[0] ^= 0xffffff;
460                 fbptr[w - 1] ^= 0xffffff;
461                 fbptr += win_width;
462         }
463 }
464
465 static void primray(cgm_ray *ray, int x, int y)
466 {
467         float nx, ny;
468         cgm_vec3 npos, farpt;
469         float inv_pv[16];
470
471         y = win_height - y;
472         nx = (float)(x - viewport[0]) / (float)viewport[2];
473         ny = (float)(y - viewport[1]) / (float)viewport[3];
474
475         cgm_mcopy(inv_pv, proj_matrix_inv);
476         cgm_mmul(inv_pv, view_matrix_inv);
477
478         cgm_vcons(&npos, nx, ny, 0.0f);
479         cgm_unproject(&ray->origin, &npos, inv_pv);
480         npos.z = 1.0f;
481         cgm_unproject(&farpt, &npos, inv_pv);
482
483         ray->dir.x = farpt.x - ray->origin.x;
484         ray->dir.y = farpt.y - ray->origin.y;
485         ray->dir.z = farpt.z - ray->origin.z;
486 }