From d9f0528f47736efdec8c3cee0d0e0515f776b821 Mon Sep 17 00:00:00 2001 From: Spoike Date: Wed, 8 Feb 2012 01:56:30 +0000 Subject: [PATCH] All jogi's work. git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@3977 fc73d0e0-1445-4013-8a0c-d673dee63da5 --- quakec/csaddon/src/cam.qc | 454 ++++++++++++++++++++++++++++++++++---- 1 file changed, 413 insertions(+), 41 deletions(-) diff --git a/quakec/csaddon/src/cam.qc b/quakec/csaddon/src/cam.qc index 5640a854b..68f6bfeb8 100644 --- a/quakec/csaddon/src/cam.qc +++ b/quakec/csaddon/src/cam.qc @@ -1,8 +1,12 @@ typedef struct { int numpoints; + int type; vector pos[64]; -float totallength; + float totallength; + vector color; + int selected_point; + float position; } spline_t; typedef struct @@ -11,16 +15,20 @@ typedef struct float endtime; float startfov; float endfov; - spline_t view; - spline_t focus; + spline_t spline; } cam_t; + typedef struct { - int numcams; - int selcam; int ver; - cam_t cam[64]; + int position_count; + int position_selected; + int view_count; + int view_selected; + cam_t position[64]; + cam_t view[64]; } camdata_t; + static camdata_t *camdata; static var float splinefile = -1; @@ -57,6 +65,10 @@ static void spline_calclength(spline_t *s) static vector(spline_t *s, float frac) spline_pos = { vector v1, v2; + if (s->numpoints < 2) + { + return; + } frac *= s->numpoints; if (frac < 0) { @@ -95,49 +107,209 @@ static vector(vector pos, vector dir, vector view) beamdir = return result; }; +void(vector p1, vector p2, vector color) fakeline = { + R_BeginPolygon("camsplineshader"); + R_PolygonVertex(p1+'0 0 1', '0 0', color,1); + R_PolygonVertex(p1-'0 0 1', '0 1', color,1); + R_PolygonVertex(p2-'0 0 1', '1 1', color,1); + R_PolygonVertex(p2+'0 0 1', '1 0', color,1); + R_PolygonVertex(p1-'0 0 1', '0 0', color,1); + R_PolygonVertex(p1+'0 0 1', '0 1', color,1); + R_PolygonVertex(p2+'0 0 1', '1 1', color,1); + R_PolygonVertex(p2-'0 0 1', '1 0', color,1); + + R_EndPolygon(); +}; + +void (vector pos, vector color) draw_point +{ + local vector pos1, pos2; + //x + pos1 = pos - '10 0 0'; + pos2 = pos + '10 0 0'; + fakeline(pos1, pos2, color); + //y + pos1 = pos - '0 10 0'; + pos2 = pos + '0 10 0'; + fakeline(pos1, pos2, color); + //z -- this isnt drawing right for some reason + pos1 = pos - '0 0 10'; + pos2 = pos + '0 0 10'; + fakeline(pos1, pos2, color); +}; + +// time has to be between 0 and 1 +vector(spline_t *s, float frac_time) spline_calculate_position = +{ + local vector r, point1, point2, point3, point4; + local float t1, t2, t3, t4, t5, t_scaled; + local int i; + + if (s->numpoints == 0) + { + return '0 0 0'; + } + + // point + if (s->numpoints == 1) + { + return s->pos[0]; + } + + // straight line + if (s->numpoints == 2) + { + r = s->pos[1] - s->pos[0]; + r = r * frac_time; + r = r + s->pos[0]; + return r; + } + + // cubic spline + if (s->numpoints == 3)// || (s->numpoints > 3 && s->type == 1)) + { + t1 = 1-frac_time; + t2 = pow(t1, s->numpoints - 1); + t3 = pow(frac_time, s->numpoints - 1); + + for (i=0; inumpoints; i++) + { + if (i == 0) + { + r = s->pos[i] * t2; + } + else if (i == s->numpoints - 1) + { + r = r + s->pos[i] * t3; + } + else + { + t4 = (s->numpoints-1) * pow(frac_time, i) * pow(t1, s->numpoints - 1 - i); + r = r + s->pos[i] * t4; + } + } + return r; + } + + // catmull rom + if (s->numpoints > 3) + { + // getting the start point -- might change if we add times to points + // first point is always an entry point last point also + t5 = 1 / (s->numpoints - 3); + for (i=1, t4=t5; t4 s->numpoints) + { + print(sprintf("something went wrong while getting the start point!\n")); + return s->pos[0]; + } + + + point1 = s->pos[i - 1i]; + point2 = s->pos[i]; + point3 = s->pos[i+1i]; + point4 = s->pos[i+2i]; + + // scale the time so its 0 -> 1 between point1 and 2 + /* + 1 2 3 4 5 6 + 0 1 + 0 1 + */ + + t_scaled = (frac_time - (t4 - t5))/t5; // hope this works :P + + // would be really nice if i could access vector components via [0] [1] ... here + // warning RETARDED! + t1 = point2_x; + t2 = -0.5 * point1_x + 0.5 * point3_x; + t3 = point1_x + (-2.5 * point2_x) + 2.0* point3_x +(-0.5 *point4_x); + t4 = -0.5 * point1_x + 1.5 * point2_x + (-1.5 * point3_x) + 0.5 * point4_x; + r_x = ((t4 * t_scaled + t3)*t_scaled + t2)*t_scaled + t1; + + t1 = point2_y; + t2 = -0.5 * point1_y + 0.5 * point3_y; + t3 = point1_y + (-2.5 * point2_y) + 2.0* point3_y +(-0.5 *point4_y); + t4 = -0.5 * point1_y + 1.5 * point2_y + (-1.5 * point3_y) + 0.5 * point4_y; + r_y = ((t4 * t_scaled + t3)*t_scaled + t2)*t_scaled + t1; + + t1 = point2_z; + t2 = -0.5 * point1_z + 0.5 * point3_z; + t3 = point1_z + (-2.5 * point2_z) + 2.0* point3_z +(-0.5 *point4_z); + t4 = -0.5 * point1_z + 1.5 * point2_z + (-1.5 * point3_z) + 0.5 * point4_z; + r_z = ((t4 * t_scaled + t3)*t_scaled + t2)*t_scaled + t1; + + + + + return r; + } + + return s->pos[0]; + +}; + static void(spline_t *s) spline_draw = { local float step, frac; local vector pos, pos1, pos2, vpos; local vector bdir; + local int i; + local vector color; /*example of drawing convex polygons*/ R_BeginPolygon("camsplineshader"); vpos = getviewprop(VF_ORIGIN); - /*calc the initial beam positions*/ - pos = spline_pos(s, 0); - bdir = beamdir(pos, spline_dir(s, 0), vpos)*8; - pos1 = pos + bdir; - pos2 = pos - bdir; - - for (frac = 0; frac < 1; ) + if (s->numpoints < 3) { - frac += step; - if (frac > 1) - frac = 1; - - pos = spline_pos(s, frac); - - /*emit prior verts*/ - R_PolygonVertex(pos1, '0 0', '1 1 1', 1); - R_PolygonVertex(pos2, '1 0', '1 1 1', 1); - - /*calc intersitial beam position*/ - pos2 = spline_dir(s, frac); - bdir = beamdir(pos, spline_dir(s, frac), vpos)*8; - pos1 = pos + bdir; - pos2 = pos - bdir; - - /*emit current verts*/ - R_PolygonVertex(pos2, '1 1', '1 1 1', 1); - R_PolygonVertex(pos1, '0 1', '1 1 1', 1); - R_EndPolygon(); + step = 1; // should be fine } + else + { + step = 0.01; + } + + color = '1 0 1'; + + for (frac=0; frac<=1; frac = frac + step) + { + pos = spline_calculate_position(s, frac); + if (frac == 0) + { + pos2 = pos; + continue; + } + + fakeline(pos2, pos, color); + pos2 = pos; + } + + //fakeline(pos2, s->pos[s->numpoints-1]); + +// draw points + for (i=0; inumpoints; i++) + { + color = '0.25 0.25 0.25'; + if (i == s->selected_point) + color = '1 1 1'; + draw_point(s->pos[i], color); + } + // draw position + pos = spline_calculate_position(s, s->position); + draw_point(pos, '1 0 0'); }; +void(float attime) spline_overrides = +{ +}; /*called to move the view to some simulation time*/ +/* void(float attime) spline_overrides = { local cam_t *cam; @@ -145,15 +317,15 @@ void(float attime) spline_overrides = local vector src, dst; local float frac; - /*find correct spline based upon time*/ - for (i = 0; i < camdata->numcams; i++) + //find correct spline based upon time + for (i = 0; i < camdata->count; i++) { cam = &camdata->cam[i]; if (cam->starttime <= attime && cam->endtime > attime) break; } - /*give up if no matches*/ - if (i == camdata->numcams) + //give up if no matches + if (i == camdata->count) return; frac = (attime - cam->starttime) / (cam->endtime - cam->starttime); @@ -164,6 +336,7 @@ void(float attime) spline_overrides = setviewprop(VF_ANGLES, vectoangles(dst - src)); setviewprop(VF_AFOV, (cam->startfov * (1-frac)) + (cam->endfov * frac)); }; +*/ void() editor_spline_add = { @@ -173,25 +346,224 @@ void() editor_spline_add = spline_init(); /*add visible splines to the scene*/ - for (i = 0; i < camdata->numcams; i+=1i) - spline_draw(&camdata->cam[i].view); + for (i = 0; i < camdata->position_count; i+=1i) + spline_draw(&camdata->position[i].spline); /*sort out the overrides*/ // spline_overrides(simtime); }; void(vector curmousepos) editor_spline_overlay = { + local vector pos, v; + local spline_t *s; + if (splinefile < 0) spline_init(); /*draw menu*/ /*dunno if the light editor has any convienient code*/ - drawrawstring('0 32 0', sprintf("spline %i / %i", camdata->selcam, camdata->numcams), '8 8 0', '1 1 1', 1); + pos_y = 40; + drawrawstring(pos, sprintf("cam : %i / %i", camdata->position_selected + 1i , camdata->position_count), '8 8 0', '1 1 1', 1); + if (camdata->position_count) + { + pos_y += 8; + s = &camdata->position[camdata->position_selected].spline; + v = s->pos[s->selected_point]; + drawrawstring(pos, sprintf(" point: %i / %i -- %f %f %f", s->selected_point + 1i, s->numpoints, v_x, v_y, v_z), '8 8 0', '1 1 1', 1); + } + pos_y += 8; + drawrawstring(pos, sprintf("view : %i / %i", camdata->view_selected + 1i, camdata->view_count), '8 8 0', '1 1 1', 1); }; + +int (spline_t *s, vector point) spline_add_point = +{ + if (s->numpoints + 1 >= 64) + { + return 1; + } + + s->pos[s->numpoints] = point; + s->selected_point = s->numpoints; + s->numpoints = s->numpoints + 1i; //oldskool! + return 0; +}; + +void (spline_t *s, vector point) spline_set_point = +{ + s->pos[s->selected_point] = point; +}; + +void (int type) position_add_point = +{ + vector v; + int i; + local cam_t *c; + local spline_t *s; + + i = camdata->position_selected; + c = &camdata->position[i]; + s = &c->spline; + v = getviewprop(VF_ORIGIN); + if (type == 0) + { + if (spline_add_point(s, v)) + { +// print(sprintf("point could not be added.\n")); + } + else + { +// print(sprintf("added point: %i %f %f %f\n", s->selected_point, v_x, v_y, v_z)); + } + } +} + +void(void) position_add +{ + print(sprintf("test\n")); + if (camdata->position_count >= 64) + { + return; + } + + print(sprintf("adding a position\n")); + + camdata->position_selected = camdata->position_count; + camdata->position_count = camdata->position_count + 1i; //oldskool +} + +void(void) view_add +{ + if (camdata->view_count >= 64) + { + return; + } + camdata->view_selected = camdata->view_count; + camdata->view_count = camdata->view_count + 1i; +} + +void(spline_t *s, vector change) spline_change_point_position +{ + if (s->numpoints == 0) + return; + s->pos[s->selected_point] = s->pos[s->selected_point] + change; +}; + +void (spline_t *s, float change) spline_change_position +{ + s->position = s->position + change; + if (s->position < 0) + s->position = 0; + else if (s->position > 1) + s->position = 1; +} + +void(spline_t *s, int change) spline_change_selected_point +{ + if (s->numpoints == 0) + return; + + if (change + s->selected_point < 0) + s->selected_point = s->numpoints -1; + else if (change + s->selected_point >= s->numpoints) + s->selected_point = 0; + else + s->selected_point = s->selected_point + change; +}; + float(float keycode, float unicode, vector curmousepos) editor_spline_key { - /*print/figure out the codes yourself :P */ + if (keycode == 'j') + { + if (camdata->position_count == 0) + position_add(); + position_add_point(0); + } + + if (keycode == 'k') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_selected_point(&camdata->position[camdata->position_selected].spline, -1); + return TRUE; + } + + if (keycode == 'l') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_selected_point(&camdata->position[camdata->position_selected].spline, 1); + return TRUE; + } + + if (keycode == '9') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_point_position(&camdata->position[camdata->position_selected].spline, '0 0 -1'); + return TRUE; + } + + if (keycode == '0') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_point_position(&camdata->position[camdata->position_selected].spline, '0 0 1'); + return TRUE; + } + + if (keycode == '7') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_point_position(&camdata->position[camdata->position_selected].spline, '0 -1 0'); + return TRUE; + } + + if (keycode == '8') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_point_position(&camdata->position[camdata->position_selected].spline, '0 1 0'); + return TRUE; + } + + if (keycode == '5') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_point_position(&camdata->position[camdata->position_selected].spline, '-1 0 0'); + return TRUE; + } + + if (keycode == '6') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_point_position(&camdata->position[camdata->position_selected].spline, '1 0 0'); + return TRUE; + } + + if (keycode == '-') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_position(&camdata->position[camdata->position_selected].spline, -0.01); + return TRUE; + } + + if (keycode == '=') + { + if (camdata->position_count == 0) + return FALSE; + spline_change_position(&camdata->position[camdata->position_selected].spline, 0.01); + return TRUE; + } + + + + + return FALSE; };