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