2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023 John Tsiombikas <nuclear@mutantstargoat.com>
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.
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.
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/>.
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,
38 static const char *tbn_icon_name[] = {
39 "new", "open", "save", 0,
40 "sel", "move", "rot", "scale", 0,
42 "union", "isect", "diff", 0,
43 "mtl", "rend", "rend-area", "viewrend", 0, "cfg"
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}
52 static int tbn_istool[] = {
59 static rtk_icon *tbn_icons[NUM_TOOL_BUTTONS];
60 static rtk_widget *tbn_buttons[NUM_TOOL_BUTTONS];
62 #define TOOLBAR_HEIGHT 26
65 TOOL_SEL, TOOL_MOVE, TOOL_ROT, TOOL_SCALE,
66 TOOL_UNION, TOOL_ISECT, TOOL_DIFF, TOOL_REND_AREA,
69 static rtk_widget *tools[NUM_TOOLS];
72 static rtk_rect totalrend;
75 static int mdl_init(void);
76 static void mdl_destroy(void);
77 static int mdl_start(void);
78 static void mdl_stop(void);
79 static void mdl_display(void);
80 static void mdl_reshape(int x, int y);
81 static void mdl_keyb(int key, int press);
82 static void mdl_mouse(int bn, int press, int x, int y);
83 static void mdl_motion(int x, int y);
85 static void draw_object(struct object *obj);
86 static void setup_material(struct material *mtl);
87 static void draw_grid(void);
88 static void tbn_callback(rtk_widget *w, void *cls);
90 static void act_settool(int tidx);
91 static void act_addobj(void);
92 static void act_rmobj(void);
94 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1);
96 static void inval_vport(void);
99 struct app_screen scr_model = {
101 mdl_init, mdl_destroy,
103 mdl_display, mdl_reshape,
104 mdl_keyb, mdl_mouse, mdl_motion
107 static rtk_widget *toolbar;
108 static rtk_iconsheet *icons;
110 static struct cmesh *mesh_sph;
112 static float cam_theta, cam_phi = 20, cam_dist = 8;
113 static float view_matrix[16], proj_matrix[16];
114 static float view_matrix_inv[16], proj_matrix_inv[16];
115 static int viewport[4];
116 static cgm_ray pickray;
118 static int cur_tool, prev_tool = -1;
119 static int selobj = -1;
121 static rtk_rect rband;
122 static int rband_valid;
124 static int rendering;
125 static rtk_rect rendrect;
128 static int mdl_init(void)
133 if(!(icons = rtk_load_iconsheet("data/icons.png"))) {
134 errormsg("failed to load iconsheet\n");
137 for(i=0; i<NUM_TOOL_BUTTONS; i++) {
138 if(tbn_icon_name[i]) {
139 tbn_icons[i] = rtk_define_icon(icons, tbn_icon_name[i],
140 tbn_icon_pos[i][0], tbn_icon_pos[i][1], 16, 16);
146 if(!(toolbar = rtk_create_window(0, "toolbar", 0, 0, win_width, TOOLBAR_HEIGHT))) {
149 rtk_win_layout(toolbar, RTK_HBOX);
152 for(i=0; i<NUM_TOOL_BUTTONS; i++) {
154 rtk_create_separator(toolbar);
156 if(!(w = rtk_create_iconbutton(toolbar, tbn_icons[i], 0))) {
160 rtk_set_callback(w, tbn_callback, (void*)(intptr_t)i);
162 rtk_bn_mode(w, RTK_TOGGLEBN);
163 tools[toolidx++] = w;
170 assert(toolidx == NUM_TOOLS);
172 if(!(mesh_sph = cmesh_alloc())) {
173 errormsg("failed to allocate sphere vis mesh\n");
176 gen_sphere(mesh_sph, 1.0f, 16, 8, 1.0f, 1.0f);
182 static void mdl_destroy(void)
184 cmesh_free(mesh_sph);
185 rtk_free_iconsheet(icons);
188 static int mdl_start(void)
190 gaw_clear_color(0.125, 0.125, 0.125, 1);
192 gaw_enable(GAW_DEPTH_TEST);
193 gaw_enable(GAW_CULL_FACE);
194 gaw_enable(GAW_LIGHTING);
195 gaw_enable(GAW_LIGHT0);
197 rend_pan(0, -TOOLBAR_HEIGHT);
201 static void mdl_stop(void)
205 static void mdl_display(void)
211 gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
213 gaw_matrix_mode(GAW_MODELVIEW);
215 gaw_translate(0, 0, -cam_dist);
216 gaw_rotate(cam_phi, 1, 0, 0);
217 gaw_rotate(cam_theta, 0, 1, 0);
218 gaw_get_modelview(view_matrix);
219 cgm_mcopy(view_matrix_inv, view_matrix);
220 cgm_minverse(view_matrix_inv);
224 num = scn_num_objects(scn);
225 for(i=0; i<num; i++) {
226 setup_material(scn->objects[i]->mtl);
230 gaw_enable(GAW_POLYGON_OFFSET);
231 draw_object(scn->objects[i]);
232 gaw_disable(GAW_POLYGON_OFFSET);
235 gaw_disable(GAW_LIGHTING);
237 gaw_color3f(0, 1, 0);
238 draw_object(scn->objects[i]);
242 draw_object(scn->objects[i]);
247 /* dirty all GUI windows */
248 rtk_invalidate(toolbar);
253 if(!render(framebuf)) {
256 app_redisplay(rendrect.x, rendrect.y, rendrect.width, rendrect.height);
260 rtk_draw_widget(toolbar);
263 static void draw_object(struct object *obj)
267 if(!obj->xform_valid) {
268 calc_object_matrix(obj);
271 gaw_mult_matrix(obj->xform);
275 sph = (struct sphere*)obj;
276 gaw_scale(sph->rad, sph->rad, sph->rad);
277 cmesh_draw(mesh_sph);
287 static void setup_material(struct material *mtl)
289 gaw_mtl_diffuse(mtl->kd.x, mtl->kd.y, mtl->kd.z, 1.0f);
290 gaw_mtl_specular(mtl->ks.x, mtl->ks.y, mtl->ks.z, mtl->shin);
291 gaw_mtl_emission(mtl->ke.x, mtl->ke.y, mtl->ke.z);
294 static void draw_grid(void)
297 gaw_disable(GAW_LIGHTING);
299 gaw_begin(GAW_LINES);
300 gaw_color3f(0.5, 0, 0);
301 gaw_vertex3f(0, 0, 0);
302 gaw_vertex3f(-100, 0, 0);
303 gaw_vertex3f(0, 0, 0);
304 gaw_vertex3f(100, 0, 0);
305 gaw_color3f(0, 0.5, 0);
306 gaw_vertex3f(0, 0, 0);
307 gaw_vertex3f(0, 0, -100);
308 gaw_vertex3f(0, 0, 0);
309 gaw_vertex3f(0, 0, 100);
315 static void mdl_reshape(int x, int y)
317 float aspect = (float)x / (float)(y - TOOLBAR_HEIGHT);
320 viewport[1] = TOOLBAR_HEIGHT;
322 viewport[3] = y - TOOLBAR_HEIGHT;
323 gaw_viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
325 gaw_matrix_mode(GAW_PROJECTION);
327 gaw_perspective(50, aspect, 0.5, 100.0);
328 gaw_get_projection(proj_matrix);
329 cgm_mcopy(proj_matrix_inv, proj_matrix);
330 cgm_minverse(proj_matrix_inv);
332 rtk_resize(toolbar, win_width, TOOLBAR_HEIGHT);
337 static void mdl_keyb(int key, int press)
339 if(rtk_input_key(toolbar, key, press)) {
346 act_settool(TOOL_SEL);
349 act_settool(TOOL_MOVE);
352 act_settool(TOOL_ROT);
355 act_settool(TOOL_SCALE);
359 act_settool(TOOL_REND_AREA);
374 static void mdl_mouse(int bn, int press, int x, int y)
377 if(!vpdrag && rtk_input_mbutton(toolbar, bn, press, x, y)) {
387 vpdrag &= ~(1 << bn);
391 app_rband(0, 0, 0, 0);
393 if(cur_tool == TOOL_REND_AREA) {
394 if(rband.width && rband.height) {
396 rend_size(win_width, win_height);
397 rtk_fix_rect(&rband);
399 rend_begin(rband.x, rband.y, rband.width, rband.height);
400 app_redisplay(rband.x, rband.y, rband.width, rband.height);
402 if(totalrend.width) {
403 rtk_rect_union(&totalrend, &rband);
410 } else if(bn == 0 && x == rband.x && y == rband.y) {
411 primray(&pickray, x, y);
412 if(scn_intersect(scn, &pickray, &hit)) {
413 int newsel = scn_object_index(scn, hit.obj);
414 if(newsel != selobj) {
428 static void mdl_motion(int x, int y)
432 if(!vpdrag && rtk_input_mmotion(toolbar, x, y)) {
442 cam_theta += dx * 0.5f;
443 cam_phi += dy * 0.5f;
444 if(cam_phi < -90) cam_phi = -90;
445 if(cam_phi > 90) cam_phi = 90;
450 cam_dist += dy * 0.1f;
451 if(cam_dist < 0) cam_dist = 0;
459 if(rband.x != x || rband.y != y) {
460 rband.width = x - rband.x;
461 rband.height = y - rband.y;
463 app_rband(rband.x, rband.y, rband.width, rband.height);
469 struct object *obj = scn->objects[selobj];
470 moveobj(obj, mouse_x, mouse_y, x, y);
481 static void add_sphere(void)
485 if(!(obj = create_object(OBJ_SPHERE))) {
488 scn_add_object(scn, obj);
491 static void tbn_callback(rtk_widget *w, void *cls)
493 int id = (intptr_t)cls;
505 act_settool(id - TBN_SEL);
510 act_settool(id - TBN_UNION + TOOL_UNION);
513 act_settool(TOOL_REND_AREA);
529 static void act_settool(int tidx)
534 if(tidx == cur_tool) return;
536 if(cur_tool == TOOL_REND_AREA) {
538 app_redisplay(totalrend.x, totalrend.y, totalrend.width, totalrend.height);
542 prev_tool = cur_tool;
544 for(i=0; i<NUM_TOOLS; i++) {
546 if(!rtk_get_value(tools[i])) {
547 rtk_set_value(tools[i], 1);
548 rtk_get_rect(tools[i], &r);
551 if(rtk_get_value(tools[i])) {
552 rtk_set_value(tools[i], 0);
553 rtk_get_rect(tools[i], &r);
559 static void act_addobj(void)
561 int idx = scn_num_objects(scn);
568 static void act_rmobj(void)
571 scn_rm_object(scn, selobj);
578 void primray(cgm_ray *ray, int x, int y)
581 cgm_vec3 npos, farpt;
585 nx = (float)(x - viewport[0]) / (float)viewport[2];
586 ny = (float)(y - viewport[1]) / (float)viewport[3];
588 cgm_mcopy(inv_pv, proj_matrix_inv);
589 cgm_mmul(inv_pv, view_matrix_inv);
591 cgm_vcons(&npos, nx, ny, 0.0f);
592 cgm_unproject(&ray->origin, &npos, inv_pv);
594 cgm_unproject(&farpt, &npos, inv_pv);
596 ray->dir.x = farpt.x - ray->origin.x;
597 ray->dir.y = farpt.y - ray->origin.y;
598 ray->dir.z = farpt.z - ray->origin.z;
601 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1)
607 primray(&ray, px0, py0);
608 cgm_vnormalize(&ray.dir);
609 dist = ray_object_dist(&ray, obj);
610 cgm_raypos(&p0, &ray, dist);
611 primray(&ray, px1, py1);
612 cgm_vnormalize(&ray.dir);
613 cgm_raypos(&p1, &ray, dist);
616 cgm_vadd(&obj->pos, &p1);
617 obj->xform_valid = 0;
622 static void inval_vport(void)
625 app_redisplay(0, 0, 0, 0);