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 static rtk_rect totalrend;
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);
43 static void draw_object(struct object *obj);
44 static void setup_material(struct material *mtl);
45 static void draw_grid(void);
47 static void act_settool(int tidx);
48 static void act_addobj(void);
49 static void act_rmobj(void);
51 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1);
53 static void inval_vport(void);
56 struct app_screen scr_model = {
58 mdl_init, mdl_destroy,
60 mdl_display, mdl_reshape,
61 mdl_keyb, mdl_mouse, mdl_motion
65 static struct cmesh *mesh_sph;
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;
73 static int cur_tool, prev_tool = -1;
74 static int selobj = -1;
76 static rtk_rect rband;
77 static int rband_valid;
80 static rtk_rect rendrect;
83 static int mdl_init(void)
85 if(modui_init() == -1) {
86 errormsg("failed to initialize modeller UI\n");
90 if(!(mesh_sph = cmesh_alloc())) {
91 errormsg("failed to allocate sphere vis mesh\n");
94 gen_sphere(mesh_sph, 1.0f, 16, 8, 1.0f, 1.0f);
100 static void mdl_destroy(void)
102 cmesh_free(mesh_sph);
106 static int mdl_start(void)
108 gaw_clear_color(0.125, 0.125, 0.125, 1);
110 gaw_enable(GAW_DEPTH_TEST);
111 gaw_enable(GAW_CULL_FACE);
112 gaw_enable(GAW_LIGHTING);
113 gaw_enable(GAW_LIGHT0);
115 rend_pan(0, -TOOLBAR_HEIGHT);
119 static void mdl_stop(void)
123 static void mdl_display(void)
129 gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
131 gaw_matrix_mode(GAW_MODELVIEW);
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);
142 num = scn_num_objects(scn);
143 for(i=0; i<num; i++) {
144 setup_material(scn->objects[i]->mtl);
148 gaw_enable(GAW_POLYGON_OFFSET);
149 draw_object(scn->objects[i]);
150 gaw_disable(GAW_POLYGON_OFFSET);
153 gaw_disable(GAW_LIGHTING);
155 gaw_color3f(0, 1, 0);
156 draw_object(scn->objects[i]);
160 draw_object(scn->objects[i]);
165 /* dirty all GUI windows */
166 rtk_invalidate(toolbar);
167 rtk_invalidate(mtlwin);
172 if(!render(framebuf)) {
175 app_redisplay(rendrect.x, rendrect.y, rendrect.width, rendrect.height);
179 rtk_draw_widget(toolbar);
180 rtk_draw_widget(mtlwin);
183 static void draw_object(struct object *obj)
187 if(!obj->xform_valid) {
188 calc_object_matrix(obj);
191 gaw_mult_matrix(obj->xform);
195 sph = (struct sphere*)obj;
196 gaw_scale(sph->rad, sph->rad, sph->rad);
197 cmesh_draw(mesh_sph);
207 static void setup_material(struct material *mtl)
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);
214 static void draw_grid(void)
217 gaw_disable(GAW_LIGHTING);
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);
235 static void mdl_reshape(int x, int y)
237 float aspect = (float)x / (float)(y - TOOLBAR_HEIGHT);
240 viewport[1] = TOOLBAR_HEIGHT;
242 viewport[3] = y - TOOLBAR_HEIGHT;
243 gaw_viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
245 gaw_matrix_mode(GAW_PROJECTION);
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);
252 rtk_resize(toolbar, win_width, TOOLBAR_HEIGHT);
257 static void mdl_keyb(int key, int press)
259 if(rtk_input_key(toolbar, key, press)) {
266 act_settool(TOOL_SEL);
269 act_settool(TOOL_MOVE);
272 act_settool(TOOL_ROT);
275 act_settool(TOOL_SCALE);
279 act_settool(TOOL_REND_AREA);
294 static void mdl_mouse(int bn, int press, int x, int y)
297 if(!vpdrag && rtk_input_mbutton(toolbar, bn, press, x, y)) {
307 vpdrag &= ~(1 << bn);
311 app_rband(0, 0, 0, 0);
313 if(cur_tool == TOOL_REND_AREA) {
314 if(rband.width && rband.height) {
316 rend_size(win_width, win_height);
317 rtk_fix_rect(&rband);
319 rend_begin(rband.x, rband.y, rband.width, rband.height);
320 app_redisplay(rband.x, rband.y, rband.width, rband.height);
322 if(totalrend.width) {
323 rtk_rect_union(&totalrend, &rband);
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) {
348 static void mdl_motion(int x, int y)
352 if(!vpdrag && rtk_input_mmotion(toolbar, x, y)) {
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;
370 cam_dist += dy * 0.1f;
371 if(cam_dist < 0) cam_dist = 0;
379 if(rband.x != x || rband.y != y) {
380 rband.width = x - rband.x;
381 rband.height = y - rband.y;
383 app_rband(rband.x, rband.y, rband.width, rband.height);
389 struct object *obj = scn->objects[selobj];
390 moveobj(obj, mouse_x, mouse_y, x, y);
401 static void add_sphere(void)
405 if(!(obj = create_object(OBJ_SPHERE))) {
408 scn_add_object(scn, obj);
411 void tbn_callback(rtk_widget *w, void *cls)
413 int id = (intptr_t)cls;
425 act_settool(id - TBN_SEL);
430 act_settool(id - TBN_UNION + TOOL_UNION);
434 if(rtk_visible(mtlwin)) {
443 act_settool(TOOL_REND_AREA);
459 static void act_settool(int tidx)
464 if(tidx == cur_tool) return;
466 if(cur_tool == TOOL_REND_AREA) {
468 app_redisplay(totalrend.x, totalrend.y, totalrend.width, totalrend.height);
472 prev_tool = cur_tool;
474 for(i=0; i<NUM_TOOLS; i++) {
476 if(!rtk_get_value(tools[i])) {
477 rtk_set_value(tools[i], 1);
478 rtk_get_rect(tools[i], &r);
481 if(rtk_get_value(tools[i])) {
482 rtk_set_value(tools[i], 0);
483 rtk_get_rect(tools[i], &r);
489 static void act_addobj(void)
491 int idx = scn_num_objects(scn);
498 static void act_rmobj(void)
501 scn_rm_object(scn, selobj);
508 void primray(cgm_ray *ray, int x, int y)
511 cgm_vec3 npos, farpt;
515 nx = (float)(x - viewport[0]) / (float)viewport[2];
516 ny = (float)(y - viewport[1]) / (float)viewport[3];
518 cgm_mcopy(inv_pv, proj_matrix_inv);
519 cgm_mmul(inv_pv, view_matrix_inv);
521 cgm_vcons(&npos, nx, ny, 0.0f);
522 cgm_unproject(&ray->origin, &npos, inv_pv);
524 cgm_unproject(&farpt, &npos, inv_pv);
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;
531 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1)
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);
546 cgm_vadd(&obj->pos, &p1);
547 obj->xform_valid = 0;
552 static void inval_vport(void)
555 app_redisplay(0, 0, 0, 0);