fixed some UI glitches
[retroray] / src / scr_mod.c
index 47f0119..efa4cb0 100644 (file)
@@ -20,7 +20,7 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #include "app.h"
 #include "rtk.h"
 #include "scene.h"
-#include "rt.h"
+#include "geom.h"
 #include "cmesh.h"
 #include "meshgen.h"
 #include "font.h"
@@ -68,6 +68,8 @@ enum {
 };
 static rtk_widget *tools[NUM_TOOLS];
 
+static int vpdirty;
+
 
 static int mdl_init(void);
 static void mdl_destroy(void);
@@ -80,6 +82,7 @@ static void mdl_mouse(int bn, int press, int x, int y);
 static void mdl_motion(int x, int y);
 
 static void draw_object(struct object *obj);
+static void setup_material(struct material *mtl);
 static void draw_grid(void);
 static void tbn_callback(rtk_widget *w, void *cls);
 
@@ -87,10 +90,10 @@ static void act_settool(int tidx);
 static void act_addobj(void);
 static void act_rmobj(void);
 
-static void draw_rband(void);
-static void primray(cgm_ray *ray, int x, int y);
 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1);
 
+static void inval_vport(void);
+
 
 struct app_screen scr_model = {
        "modeller",
@@ -118,6 +121,7 @@ static rtk_rect rband;
 static int rband_valid;
 
 static int rendering;
+static rtk_rect rendrect;
 
 
 static int mdl_init(void)
@@ -169,6 +173,8 @@ static int mdl_init(void)
                return -1;
        }
        gen_sphere(mesh_sph, 1.0f, 16, 8, 1.0f, 1.0f);
+
+       vpdirty = 1;
        return 0;
 }
 
@@ -186,6 +192,8 @@ static int mdl_start(void)
        gaw_enable(GAW_CULL_FACE);
        gaw_enable(GAW_LIGHTING);
        gaw_enable(GAW_LIGHT0);
+
+       rend_pan(0, -TOOLBAR_HEIGHT);
        return 0;
 }
 
@@ -196,61 +204,59 @@ static void mdl_stop(void)
 static void mdl_display(void)
 {
        int i, num;
-       static int frameno;
-
-       gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
 
-       rtk_draw_widget(toolbar);
-
-       gaw_matrix_mode(GAW_MODELVIEW);
-       gaw_load_identity();
-       gaw_translate(0, 0, -cam_dist);
-       gaw_rotate(cam_phi, 1, 0, 0);
-       gaw_rotate(cam_theta, 0, 1, 0);
-       gaw_get_modelview(view_matrix);
-       cgm_mcopy(view_matrix_inv, view_matrix);
-       cgm_minverse(view_matrix_inv);
-
-       draw_grid();
-
-       gaw_mtl_diffuse(0.5, 0.5, 0.5, 1);
-
-       num = scn_num_objects(scn);
-       for(i=0; i<num; i++) {
-               if(i == selobj) {
-                       gaw_zoffset(1);
-                       gaw_enable(GAW_POLYGON_OFFSET);
-                       draw_object(scn->objects[i]);
-                       gaw_disable(GAW_POLYGON_OFFSET);
-
-                       gaw_save();
-                       gaw_disable(GAW_LIGHTING);
-                       gaw_poly_wire();
-                       gaw_color3f(0, 1, 0);
-                       draw_object(scn->objects[i]);
-                       gaw_poly_gouraud();
-                       gaw_restore();
-               } else {
-                       draw_object(scn->objects[i]);
+       /* viewport */
+       if(vpdirty) {
+               gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
+
+               gaw_matrix_mode(GAW_MODELVIEW);
+               gaw_load_identity();
+               gaw_translate(0, 0, -cam_dist);
+               gaw_rotate(cam_phi, 1, 0, 0);
+               gaw_rotate(cam_theta, 0, 1, 0);
+               gaw_get_modelview(view_matrix);
+               cgm_mcopy(view_matrix_inv, view_matrix);
+               cgm_minverse(view_matrix_inv);
+
+               draw_grid();
+
+               num = scn_num_objects(scn);
+               for(i=0; i<num; i++) {
+                       setup_material(scn->objects[i]->mtl);
+
+                       if(i == selobj) {
+                               gaw_zoffset(1);
+                               gaw_enable(GAW_POLYGON_OFFSET);
+                               draw_object(scn->objects[i]);
+                               gaw_disable(GAW_POLYGON_OFFSET);
+
+                               gaw_save();
+                               gaw_disable(GAW_LIGHTING);
+                               gaw_poly_wire();
+                               gaw_color3f(0, 1, 0);
+                               draw_object(scn->objects[i]);
+                               gaw_poly_gouraud();
+                               gaw_restore();
+                       } else {
+                               draw_object(scn->objects[i]);
+                       }
                }
+               vpdirty = 0;
+
+               /* dirty all GUI windows */
+               rtk_invalidate(toolbar);
        }
 
+       /* render layer */
        if(rendering) {
-               if(render(framebuf)) {
-                       app_redisplay();
-               } else {
+               if(!render(framebuf)) {
                        rendering = 0;
                }
+               app_redisplay(rendrect.x, rendrect.y, rendrect.width, rendrect.height);
        }
 
-       use_font(uifont);
-       dtx_position(550, 475);
-       dtx_color(0.3, 0.3, 0.1, 1);
-       dtx_printf("update: %ld", frameno++);
-
-       if(rband_valid) {
-               draw_rband();
-       }
+       /* GUI */
+       rtk_draw_widget(toolbar);
 }
 
 static void draw_object(struct object *obj)
@@ -277,6 +283,13 @@ static void draw_object(struct object *obj)
        gaw_pop_matrix();
 }
 
+static void setup_material(struct material *mtl)
+{
+       gaw_mtl_diffuse(mtl->kd.x, mtl->kd.y, mtl->kd.z, 1.0f);
+       gaw_mtl_specular(mtl->ks.x, mtl->ks.y, mtl->ks.z, mtl->shin);
+       gaw_mtl_emission(mtl->ke.x, mtl->ke.y, mtl->ke.z);
+}
+
 static void draw_grid(void)
 {
        gaw_save();
@@ -321,7 +334,6 @@ static void mdl_reshape(int x, int y)
 static void mdl_keyb(int key, int press)
 {
        if(rtk_input_key(toolbar, key, press)) {
-               app_redisplay();
                return;
        }
 
@@ -340,6 +352,10 @@ static void mdl_keyb(int key, int press)
                        act_settool(TOOL_SCALE);
                        break;
 
+               case KEY_F6:
+                       act_settool(TOOL_REND_AREA);
+                       break;
+
                case KEY_DEL:
                        act_rmobj();
                        break;
@@ -356,7 +372,6 @@ static void mdl_mouse(int bn, int press, int x, int y)
 {
        struct rayhit hit;
        if(!vpdrag && rtk_input_mbutton(toolbar, bn, press, x, y)) {
-               app_redisplay();
                return;
        }
 
@@ -370,6 +385,7 @@ static void mdl_mouse(int bn, int press, int x, int y)
 
                if(rband_valid) {
                        rband_valid = 0;
+                       app_rband(0, 0, 0, 0);
 
                        if(cur_tool == TOOL_REND_AREA) {
                                if(prev_tool >= 0) {
@@ -377,18 +393,27 @@ static void mdl_mouse(int bn, int press, int x, int y)
                                }
                                rendering = 1;
                                rend_size(win_width, win_height);
+                               rtk_fix_rect(&rband);
+                               rendrect = rband;
                                rend_begin(rband.x, rband.y, rband.width, rband.height);
+                               app_redisplay(rband.x, rband.y, rband.width, rband.height);
                        }
 
                } else if(bn == 0 && x == rband.x && y == rband.y) {
                        primray(&pickray, x, y);
                        if(scn_intersect(scn, &pickray, &hit)) {
-                               selobj = scn_object_index(scn, hit.obj);
+                               int newsel = scn_object_index(scn, hit.obj);
+                               if(newsel != selobj) {
+                                       selobj = newsel;
+                                       inval_vport();
+                               }
                        } else {
+                               if(selobj != -1) {
+                                       inval_vport();
+                               }
                                selobj = -1;
                        }
                }
-               app_redisplay();
        }
 }
 
@@ -397,7 +422,6 @@ static void mdl_motion(int x, int y)
        int dx, dy;
 
        if(!vpdrag && rtk_input_mmotion(toolbar, x, y)) {
-               app_redisplay();
                return;
        }
 
@@ -411,13 +435,13 @@ static void mdl_motion(int x, int y)
                        cam_phi += dy * 0.5f;
                        if(cam_phi < -90) cam_phi = -90;
                        if(cam_phi > 90) cam_phi = 90;
-                       app_redisplay();
+                       inval_vport();
                }
 
                if(mouse_state[2]) {
                        cam_dist += dy * 0.1f;
                        if(cam_dist < 0) cam_dist = 0;
-                       app_redisplay();
+                       inval_vport();
                }
        } else {
                if(mouse_state[0]) {
@@ -428,6 +452,7 @@ static void mdl_motion(int x, int y)
                                        rband.width = x - rband.x;
                                        rband.height = y - rband.y;
                                        rband_valid = 1;
+                                       app_rband(rband.x, rband.y, rband.width, rband.height);
                                }
                                break;
 
@@ -441,7 +466,6 @@ static void mdl_motion(int x, int y)
                        default:
                                break;
                        }
-                       app_redisplay();
                }
        }
 }
@@ -492,16 +516,23 @@ static void tbn_callback(rtk_widget *w, void *cls)
 static void act_settool(int tidx)
 {
        int i;
+       rtk_rect r;
+
        prev_tool = cur_tool;
        cur_tool = tidx;
        for(i=0; i<NUM_TOOLS; i++) {
                if(i == cur_tool) {
-                       rtk_set_value(tools[i], 1);
+                       if(!rtk_get_value(tools[i])) {
+                               rtk_set_value(tools[i], 1);
+                               rtk_get_rect(tools[i], &r);
+                       }
                } else {
-                       rtk_set_value(tools[i], 0);
+                       if(rtk_get_value(tools[i])) {
+                               rtk_set_value(tools[i], 0);
+                               rtk_get_rect(tools[i], &r);
+                       }
                }
        }
-       app_redisplay();
 }
 
 static void act_addobj(void)
@@ -510,7 +541,7 @@ static void act_addobj(void)
        add_sphere();
        selobj = idx;
 
-       app_redisplay();
+       inval_vport();
 }
 
 static void act_rmobj(void)
@@ -518,47 +549,12 @@ static void act_rmobj(void)
        if(selobj >= 0) {
                scn_rm_object(scn, selobj);
                selobj = -1;
-               app_redisplay();
+               inval_vport();
        }
 }
 
-static void draw_rband(void)
-{
-       int i, x, y, w, h;
-       uint32_t *fbptr, *bptr;
 
-       x = rband.x;
-       y = rband.y;
-
-       if(rband.width < 0) {
-               w = -rband.width;
-               x += rband.width;
-       } else {
-               w = rband.width;
-       }
-       if(rband.height < 0) {
-               h = -rband.height;
-               y += rband.height;
-       } else {
-               h = rband.height;
-       }
-
-       fbptr = framebuf + y * win_width + x;
-       bptr = fbptr + win_width * (h - 1);
-
-       for(i=0; i<w; i++) {
-               fbptr[i] ^= 0xffffff;
-               bptr[i] ^= 0xffffff;
-       }
-       fbptr += win_width;
-       for(i=0; i<h-2; i++) {
-               fbptr[0] ^= 0xffffff;
-               fbptr[w - 1] ^= 0xffffff;
-               fbptr += win_width;
-       }
-}
-
-static void primray(cgm_ray *ray, int x, int y)
+void primray(cgm_ray *ray, int x, int y)
 {
        float nx, ny;
        cgm_vec3 npos, farpt;
@@ -598,4 +594,12 @@ static void moveobj(struct object *obj, int px0, int py0, int px1, int py1)
        cgm_vsub(&p1, &p0);
        cgm_vadd(&obj->pos, &p1);
        obj->xform_valid = 0;
+
+       inval_vport();
+}
+
+static void inval_vport(void)
+{
+       vpdirty = 1;
+       app_redisplay(0, 0, 0, 0);
 }