X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fscr_mod.c;h=d3cbb407768499b3dae895f75e08cd22e1d0e0d3;hb=86aa9bdd85243207bbd7e888d73c2b865d805265;hp=0a177451c8bfb37ca36017ea1045351ac57605ea;hpb=15a4548ff09afb37dd33786cafd50b060a64f840;p=retroray
diff --git a/src/scr_mod.c b/src/scr_mod.c
index 0a17745..d3cbb40 100644
--- a/src/scr_mod.c
+++ b/src/scr_mod.c
@@ -20,16 +20,18 @@ along with this program. If not, see .
#include "app.h"
#include "rtk.h"
#include "scene.h"
-#include "rt.h"
+#include "geom.h"
#include "cmesh.h"
#include "meshgen.h"
+#include "font.h"
+#include "rend.h"
enum {
TBN_NEW, TBN_OPEN, TBN_SAVE, TBN_SEP1,
TBN_SEL, TBN_MOVE, TBN_ROT, TBN_SCALE, TBN_SEP2,
TBN_ADD, TBN_RM, TBN_SEP3,
TBN_UNION, TBN_ISECT, TBN_DIFF, TBN_SEP4,
- TBN_MTL, TBN_REND, TBN_VIEWREND, TBN_SEP5, TBN_CFG,
+ TBN_MTL, TBN_REND, TBN_REND_AREA, TBN_VIEWREND, TBN_SEP5, TBN_CFG,
NUM_TOOL_BUTTONS
};
@@ -38,21 +40,21 @@ static const char *tbn_icon_name[] = {
"sel", "move", "rot", "scale", 0,
"add", "remove", 0,
"union", "isect", "diff", 0,
- "mtl", "rend", "viewrend", 0, "cfg"
+ "mtl", "rend", "rend-area", "viewrend", 0, "cfg"
};
static int tbn_icon_pos[][2] = {
{0,0}, {16,0}, {32,0}, {-1,-1},
{48,0}, {64,0}, {80,0}, {96,0}, {-1,-1},
{112,0}, {112,16}, {-1,-1},
{0,16}, {16,16}, {32,16}, {-1,-1},
- {48,16}, {64,16}, {80,16}, {-1,-1}, {96,16}
+ {48,16}, {64,16}, {64, 32}, {80,16}, {-1,-1}, {96,16}
};
static int tbn_istool[] = {
0, 0, 0, 0,
1, 1, 1, 1, 0,
0, 0, 0,
1, 1, 1, 0,
- 0, 0, 0, 0, 0
+ 0, 0, 1, 0, 0, 0
};
static rtk_icon *tbn_icons[NUM_TOOL_BUTTONS];
static rtk_widget *tbn_buttons[NUM_TOOL_BUTTONS];
@@ -61,7 +63,7 @@ static rtk_widget *tbn_buttons[NUM_TOOL_BUTTONS];
enum {
TOOL_SEL, TOOL_MOVE, TOOL_ROT, TOOL_SCALE,
- TOOL_UNION, TOOL_ISECT, TOOL_DIFF,
+ TOOL_UNION, TOOL_ISECT, TOOL_DIFF, TOOL_REND_AREA,
NUM_TOOLS
};
static rtk_widget *tools[NUM_TOOLS];
@@ -78,11 +80,17 @@ 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);
+static void act_settool(int tidx);
+static void act_addobj(void);
+static void act_rmobj(void);
+
+static void fix_rect(rtk_rect *rect);
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);
struct app_screen scr_model = {
@@ -104,12 +112,15 @@ static float view_matrix_inv[16], proj_matrix_inv[16];
static int viewport[4];
static cgm_ray pickray;
-static int cur_tool;
+static int cur_tool, prev_tool = -1;
static int selobj = -1;
static rtk_rect rband;
static int rband_valid;
+static int rendering;
+
+
static int mdl_init(void)
{
int i, toolidx;
@@ -176,6 +187,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;
}
@@ -186,6 +199,7 @@ static void mdl_stop(void)
static void mdl_display(void)
{
int i, num;
+ static int frameno;
gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
@@ -202,10 +216,10 @@ static void mdl_display(void)
draw_grid();
- gaw_mtl_diffuse(0.5, 0.5, 0.5, 1);
-
num = scn_num_objects(scn);
for(i=0; iobjects[i]->mtl);
+
if(i == selobj) {
gaw_zoffset(1);
gaw_enable(GAW_POLYGON_OFFSET);
@@ -224,6 +238,19 @@ static void mdl_display(void)
}
}
+ if(rendering) {
+ if(render(framebuf)) {
+ app_redisplay();
+ } else {
+ rendering = 0;
+ }
+ }
+
+ 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();
}
@@ -233,7 +260,9 @@ static void draw_object(struct object *obj)
{
struct sphere *sph;
- calc_object_matrix(obj);
+ if(!obj->xform_valid) {
+ calc_object_matrix(obj);
+ }
gaw_push_matrix();
gaw_mult_matrix(obj->xform);
@@ -251,6 +280,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();
@@ -298,6 +334,30 @@ static void mdl_keyb(int key, int press)
app_redisplay();
return;
}
+
+ if(press) {
+ switch(key) {
+ case 27:
+ act_settool(TOOL_SEL);
+ break;
+ case 'g':
+ act_settool(TOOL_MOVE);
+ break;
+ case 'r':
+ act_settool(TOOL_ROT);
+ break;
+ case 's':
+ act_settool(TOOL_SCALE);
+ break;
+
+ case KEY_DEL:
+ act_rmobj();
+ break;
+
+ default:
+ break;
+ }
+ }
}
static int vpdrag;
@@ -311,6 +371,7 @@ static void mdl_mouse(int bn, int press, int x, int y)
}
if(press) {
+ rband_valid = 0;
rband.x = x;
rband.y = y;
vpdrag |= (1 << bn);
@@ -318,9 +379,18 @@ static void mdl_mouse(int bn, int press, int x, int y)
vpdrag &= ~(1 << bn);
if(rband_valid) {
- printf("rubber band: %d,%d %dx%d\n", rband.x, rband.y, rband.width, rband.height);
rband_valid = 0;
+ if(cur_tool == TOOL_REND_AREA) {
+ if(prev_tool >= 0) {
+ act_settool(prev_tool);
+ }
+ rendering = 1;
+ rend_size(win_width, win_height);
+ fix_rect(&rband);
+ rend_begin(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)) {
@@ -362,13 +432,28 @@ static void mdl_motion(int x, int y)
}
} else {
if(mouse_state[0]) {
- if(rband.x != x || rband.y != y) {
- rband.width = x - rband.x;
- rband.height = y - rband.y;
- rband_valid = 1;
+ switch(cur_tool) {
+ case TOOL_SEL:
+ case TOOL_REND_AREA:
+ if(rband.x != x || rband.y != y) {
+ rband.width = x - rband.x;
+ rband.height = y - rband.y;
+ rband_valid = 1;
+ }
+ break;
+
+ case TOOL_MOVE:
+ if(selobj >= 0) {
+ struct object *obj = scn->objects[selobj];
+ moveobj(obj, mouse_x, mouse_y, x, y);
+ }
+ break;
+
+ default:
+ break;
}
+ app_redisplay();
}
- app_redisplay();
}
}
@@ -384,41 +469,30 @@ static void add_sphere(void)
static void tbn_callback(rtk_widget *w, void *cls)
{
- int i, id = (intptr_t)cls;
- int idx;
+ int id = (intptr_t)cls;
switch(id) {
case TBN_SEL:
case TBN_MOVE:
case TBN_ROT:
case TBN_SCALE:
- idx = id - TBN_SEL;
- if(0) {
+ act_settool(id - TBN_SEL);
+ break;
case TBN_UNION:
case TBN_ISECT:
case TBN_DIFF:
- idx = id - TBN_UNION + TOOL_UNION;
- }
- cur_tool = idx;
- for(i=0; i= 0) {
- scn_rm_object(scn, selobj);
- selobj = -1;
- app_redisplay();
- }
+ act_rmobj();
break;
default:
@@ -426,10 +500,42 @@ static void tbn_callback(rtk_widget *w, void *cls)
}
}
-static void draw_rband(void)
+static void act_settool(int tidx)
{
- int i, x, y, w, h;
- uint32_t *fbptr, *bptr;
+ int i;
+ prev_tool = cur_tool;
+ cur_tool = tidx;
+ for(i=0; i= 0) {
+ scn_rm_object(scn, selobj);
+ selobj = -1;
+ app_redisplay();
+ }
+}
+
+static void fix_rect(rtk_rect *rect)
+{
+ int x, y, w, h;
x = rband.x;
y = rband.y;
@@ -447,22 +553,37 @@ static void draw_rband(void)
h = rband.height;
}
- fbptr = framebuf + y * win_width + x;
- bptr = fbptr + win_width * (h - 1);
+ rect->x = x;
+ rect->y = y;
+ rect->width = w;
+ rect->height = h;
+}
+
+static void draw_rband(void)
+{
+ int i;
+ rtk_rect rect;
+ uint32_t *fbptr, *bptr;
+
+ rect = rband;
+ fix_rect(&rect);
+
+ fbptr = framebuf + rect.y * win_width + rect.x;
+ bptr = fbptr + win_width * (rect.height - 1);
- for(i=0; idir.y = farpt.y - ray->origin.y;
ray->dir.z = farpt.z - ray->origin.z;
}
+
+static void moveobj(struct object *obj, int px0, int py0, int px1, int py1)
+{
+ cgm_ray ray;
+ float dist;
+ cgm_vec3 p0, p1;
+
+ primray(&ray, px0, py0);
+ cgm_vnormalize(&ray.dir);
+ dist = ray_object_dist(&ray, obj);
+ cgm_raypos(&p0, &ray, dist);
+ primray(&ray, px1, py1);
+ cgm_vnormalize(&ray.dir);
+ cgm_raypos(&p1, &ray, dist);
+
+ cgm_vsub(&p1, &p0);
+ cgm_vadd(&obj->pos, &p1);
+ obj->xform_valid = 0;
+}