Fixed unproject to work as originally intended. Rather than just being a steaming pile of poo (it was the matrix inversion routine that was broken).
git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@3219 fc73d0e0-1445-4013-8a0c-d673dee63da5
This commit is contained in:
parent
ff4eaa2ea0
commit
bd51fb8e9d
3 changed files with 214 additions and 61 deletions
|
@ -1175,22 +1175,29 @@ static void PF_R_AddEntityMask(progfuncs_t *prinst, struct globalvars_s *pr_glob
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
qboolean csqc_rebuildmatricies;
|
||||
float mvp[12];
|
||||
float mvpi[12];
|
||||
static void buildmatricies(void)
|
||||
float csqc_proj_matrix[16];
|
||||
float csqc_proj_matrix_inverse[16];
|
||||
void buildmatricies(void)
|
||||
{
|
||||
float modelview[16];
|
||||
float proj[16];
|
||||
|
||||
/*build modelview and projection*/
|
||||
Matrix4_ModelViewMatrix(modelview, r_refdef.viewangles, r_refdef.vieworg);
|
||||
Matrix4_Projection2(proj, r_refdef.fov_x, r_refdef.fov_y, 4);
|
||||
Matrix4_Multiply(proj, modelview, mvp);
|
||||
Matrix4_Invert_Simple((matrix4x4_t*)mvpi, (matrix4x4_t*)mvp); //not actually used in this function.
|
||||
|
||||
/*build the project matrix*/
|
||||
Matrix4_Multiply(proj, modelview, csqc_proj_matrix);
|
||||
|
||||
/*build the unproject matrix (inverted project matrix)*/
|
||||
Matrix4_Invert(csqc_proj_matrix, csqc_proj_matrix_inverse);
|
||||
|
||||
csqc_rebuildmatricies = false;
|
||||
}
|
||||
static void PF_cs_project (progfuncs_t *prinst, struct globalvars_s *pr_globals)
|
||||
void PF_cs_project (progfuncs_t *prinst, struct globalvars_s *pr_globals)
|
||||
{
|
||||
if (csqc_rebuildmatricies)
|
||||
buildmatricies();
|
||||
|
@ -1206,45 +1213,52 @@ static void PF_cs_project (progfuncs_t *prinst, struct globalvars_s *pr_globals)
|
|||
v[2] = in[2];
|
||||
v[3] = 1;
|
||||
|
||||
Matrix4_Transform4(mvp, v, tempv);
|
||||
Matrix4_Transform4(csqc_proj_matrix, v, tempv);
|
||||
|
||||
tempv[0] /= tempv[3];
|
||||
tempv[1] /= tempv[3];
|
||||
tempv[2] /= tempv[3];
|
||||
|
||||
out[0] = (1+tempv[0])/2;
|
||||
out[1] = (1+tempv[1])/2;
|
||||
out[2] = (1+tempv[2])/2;
|
||||
out[1] = 1-(1+tempv[1])/2;
|
||||
out[2] = tempv[2];
|
||||
|
||||
out[0] = out[0]*r_refdef.vrect.width + r_refdef.vrect.x;
|
||||
out[1] = out[1]*r_refdef.vrect.height + r_refdef.vrect.y;
|
||||
|
||||
if (tempv[3] < 0)
|
||||
out[2] *= -1;
|
||||
}
|
||||
}
|
||||
static void PF_cs_unproject (progfuncs_t *prinst, struct globalvars_s *pr_globals)
|
||||
void PF_cs_unproject (progfuncs_t *prinst, struct globalvars_s *pr_globals)
|
||||
{
|
||||
if (csqc_rebuildmatricies)
|
||||
buildmatricies();
|
||||
|
||||
|
||||
{
|
||||
float *in = G_VECTOR(OFS_PARM0);
|
||||
float *out = G_VECTOR(OFS_RETURN);
|
||||
float tx, ty;
|
||||
|
||||
float v[4], tempv[4];
|
||||
|
||||
out[0] = (out[0]-r_refdef.vrect.x)/r_refdef.vrect.width;
|
||||
out[1] = (out[1]-r_refdef.vrect.y)/r_refdef.vrect.height;
|
||||
|
||||
v[0] = in[0]*2-1;
|
||||
v[1] = in[1]*2-1;
|
||||
v[2] = in[2]*2-1;
|
||||
tx = ((in[0]-r_refdef.vrect.x)/r_refdef.vrect.width);
|
||||
ty = ((in[1]-r_refdef.vrect.y)/r_refdef.vrect.height);
|
||||
ty = 1-ty;
|
||||
v[0] = tx*2-1;
|
||||
v[1] = ty*2-1;
|
||||
v[2] = in[2];//*2-1;
|
||||
v[3] = 1;
|
||||
|
||||
Matrix4_Transform4(mvpi, v, tempv);
|
||||
//don't use 1, because the far clip plane really is an infinite distance away
|
||||
if (v[2] >= 1)
|
||||
v[2] = 0.999999;
|
||||
|
||||
out[0] = tempv[0];
|
||||
out[1] = tempv[1];
|
||||
out[2] = tempv[2];
|
||||
Matrix4_Transform4(csqc_proj_matrix_inverse, v, tempv);
|
||||
|
||||
out[0] = tempv[0]/tempv[3];
|
||||
out[1] = tempv[1]/tempv[3];
|
||||
out[2] = tempv[2]/tempv[3];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3139,7 +3153,7 @@ static void PF_cl_te_lightning1 (progfuncs_t *prinst, struct globalvars_s *pr_gl
|
|||
{
|
||||
csqcedict_t *ent = (csqcedict_t*)G_EDICT(prinst, OFS_PARM0);
|
||||
float *start = G_VECTOR(OFS_PARM1);
|
||||
float *end = G_VECTOR(OFS_PARM1);
|
||||
float *end = G_VECTOR(OFS_PARM2);
|
||||
|
||||
CL_AddBeam(0, ent->entnum+MAX_EDICTS, start, end);
|
||||
}
|
||||
|
@ -3147,7 +3161,7 @@ static void PF_cl_te_lightning2 (progfuncs_t *prinst, struct globalvars_s *pr_gl
|
|||
{
|
||||
csqcedict_t *ent = (csqcedict_t*)G_EDICT(prinst, OFS_PARM0);
|
||||
float *start = G_VECTOR(OFS_PARM1);
|
||||
float *end = G_VECTOR(OFS_PARM1);
|
||||
float *end = G_VECTOR(OFS_PARM2);
|
||||
|
||||
CL_AddBeam(1, ent->entnum+MAX_EDICTS, start, end);
|
||||
}
|
||||
|
@ -3155,7 +3169,7 @@ static void PF_cl_te_lightning3 (progfuncs_t *prinst, struct globalvars_s *pr_gl
|
|||
{
|
||||
csqcedict_t *ent = (csqcedict_t*)G_EDICT(prinst, OFS_PARM0);
|
||||
float *start = G_VECTOR(OFS_PARM1);
|
||||
float *end = G_VECTOR(OFS_PARM1);
|
||||
float *end = G_VECTOR(OFS_PARM2);
|
||||
|
||||
CL_AddBeam(2, ent->entnum+MAX_EDICTS, start, end);
|
||||
}
|
||||
|
@ -3163,7 +3177,7 @@ static void PF_cl_te_beam (progfuncs_t *prinst, struct globalvars_s *pr_globals)
|
|||
{
|
||||
csqcedict_t *ent = (csqcedict_t*)G_EDICT(prinst, OFS_PARM0);
|
||||
float *start = G_VECTOR(OFS_PARM1);
|
||||
float *end = G_VECTOR(OFS_PARM1);
|
||||
float *end = G_VECTOR(OFS_PARM2);
|
||||
|
||||
CL_AddBeam(5, ent->entnum+MAX_EDICTS, start, end);
|
||||
}
|
||||
|
|
|
@ -1116,44 +1116,183 @@ void Matrix4_Orthographic(float *proj, float xmin, float xmax, float ymax, float
|
|||
proj[15] = 1;
|
||||
}
|
||||
|
||||
void Matrix4_Invert_Simple (matrix4x4_t *out, const matrix4x4_t *in1)
|
||||
/*
|
||||
* Compute inverse of 4x4 transformation matrix.
|
||||
* Code contributed by Jacques Leroy jle@star.be
|
||||
* Return true for success, false for failure (singular matrix)
|
||||
* This came to FTE via mesa's GLU.
|
||||
*/
|
||||
qboolean Matrix4_Invert(const float *m, float *out)
|
||||
{
|
||||
// we only support uniform scaling, so assume the first row is enough
|
||||
// (note the lack of sqrt here, because we're trying to undo the scaling,
|
||||
// this means multiplying by the inverse scale twice - squaring it, which
|
||||
// makes the sqrt a waste of time)
|
||||
#if 1
|
||||
double scale = 1.0 / (in1->m[0][0] * in1->m[0][0] + in1->m[0][1] * in1->m[0][1] + in1->m[0][2] * in1->m[0][2]);
|
||||
#else
|
||||
double scale = 3.0 / sqrt
|
||||
(in1->m[0][0] * in1->m[0][0] + in1->m[0][1] * in1->m[0][1] + in1->m[0][2] * in1->m[0][2]
|
||||
+ in1->m[1][0] * in1->m[1][0] + in1->m[1][1] * in1->m[1][1] + in1->m[1][2] * in1->m[1][2]
|
||||
+ in1->m[2][0] * in1->m[2][0] + in1->m[2][1] * in1->m[2][1] + in1->m[2][2] * in1->m[2][2]);
|
||||
scale *= scale;
|
||||
#endif
|
||||
/* NB. OpenGL Matrices are COLUMN major. */
|
||||
#define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; }
|
||||
#define MAT(m,r,c) (m)[(c)*4+(r)]
|
||||
|
||||
// invert the rotation by transposing and multiplying by the squared
|
||||
// recipricol of the input matrix scale as described above
|
||||
out->m[0][0] = (float)(in1->m[0][0] * scale);
|
||||
out->m[0][1] = (float)(in1->m[1][0] * scale);
|
||||
out->m[0][2] = (float)(in1->m[2][0] * scale);
|
||||
out->m[1][0] = (float)(in1->m[0][1] * scale);
|
||||
out->m[1][1] = (float)(in1->m[1][1] * scale);
|
||||
out->m[1][2] = (float)(in1->m[2][1] * scale);
|
||||
out->m[2][0] = (float)(in1->m[0][2] * scale);
|
||||
out->m[2][1] = (float)(in1->m[1][2] * scale);
|
||||
out->m[2][2] = (float)(in1->m[2][2] * scale);
|
||||
float wtmp[4][8];
|
||||
float m0, m1, m2, m3, s;
|
||||
float *r0, *r1, *r2, *r3;
|
||||
|
||||
// invert the translate
|
||||
out->m[0][3] = -(in1->m[0][3] * out->m[0][0] + in1->m[1][3] * out->m[0][1] + in1->m[2][3] * out->m[0][2]);
|
||||
out->m[1][3] = -(in1->m[0][3] * out->m[1][0] + in1->m[1][3] * out->m[1][1] + in1->m[2][3] * out->m[1][2]);
|
||||
out->m[2][3] = -(in1->m[0][3] * out->m[2][0] + in1->m[1][3] * out->m[2][1] + in1->m[2][3] * out->m[2][2]);
|
||||
r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
|
||||
|
||||
// don't know if there's anything worth doing here
|
||||
out->m[3][0] = 0;
|
||||
out->m[3][1] = 0;
|
||||
out->m[3][2] = 0;
|
||||
out->m[3][3] = 1;
|
||||
r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1),
|
||||
r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3),
|
||||
r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
|
||||
r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1),
|
||||
r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3),
|
||||
r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
|
||||
r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1),
|
||||
r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3),
|
||||
r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
|
||||
r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1),
|
||||
r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3),
|
||||
r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
|
||||
|
||||
/* choose pivot - or die */
|
||||
if (fabs(r3[0]) > fabs(r2[0]))
|
||||
SWAP_ROWS(r3, r2);
|
||||
if (fabs(r2[0]) > fabs(r1[0]))
|
||||
SWAP_ROWS(r2, r1);
|
||||
if (fabs(r1[0]) > fabs(r0[0]))
|
||||
SWAP_ROWS(r1, r0);
|
||||
if (0.0 == r0[0])
|
||||
return false;
|
||||
|
||||
/* eliminate first variable */
|
||||
m1 = r1[0] / r0[0];
|
||||
m2 = r2[0] / r0[0];
|
||||
m3 = r3[0] / r0[0];
|
||||
s = r0[1];
|
||||
r1[1] -= m1 * s;
|
||||
r2[1] -= m2 * s;
|
||||
r3[1] -= m3 * s;
|
||||
s = r0[2];
|
||||
r1[2] -= m1 * s;
|
||||
r2[2] -= m2 * s;
|
||||
r3[2] -= m3 * s;
|
||||
s = r0[3];
|
||||
r1[3] -= m1 * s;
|
||||
r2[3] -= m2 * s;
|
||||
r3[3] -= m3 * s;
|
||||
s = r0[4];
|
||||
if (s != 0.0) {
|
||||
r1[4] -= m1 * s;
|
||||
r2[4] -= m2 * s;
|
||||
r3[4] -= m3 * s;
|
||||
}
|
||||
s = r0[5];
|
||||
if (s != 0.0) {
|
||||
r1[5] -= m1 * s;
|
||||
r2[5] -= m2 * s;
|
||||
r3[5] -= m3 * s;
|
||||
}
|
||||
s = r0[6];
|
||||
if (s != 0.0) {
|
||||
r1[6] -= m1 * s;
|
||||
r2[6] -= m2 * s;
|
||||
r3[6] -= m3 * s;
|
||||
}
|
||||
s = r0[7];
|
||||
if (s != 0.0) {
|
||||
r1[7] -= m1 * s;
|
||||
r2[7] -= m2 * s;
|
||||
r3[7] -= m3 * s;
|
||||
}
|
||||
|
||||
/* choose pivot - or die */
|
||||
if (fabs(r3[1]) > fabs(r2[1]))
|
||||
SWAP_ROWS(r3, r2);
|
||||
if (fabs(r2[1]) > fabs(r1[1]))
|
||||
SWAP_ROWS(r2, r1);
|
||||
if (0.0 == r1[1])
|
||||
return false;
|
||||
|
||||
/* eliminate second variable */
|
||||
m2 = r2[1] / r1[1];
|
||||
m3 = r3[1] / r1[1];
|
||||
r2[2] -= m2 * r1[2];
|
||||
r3[2] -= m3 * r1[2];
|
||||
r2[3] -= m2 * r1[3];
|
||||
r3[3] -= m3 * r1[3];
|
||||
s = r1[4];
|
||||
if (0.0 != s) {
|
||||
r2[4] -= m2 * s;
|
||||
r3[4] -= m3 * s;
|
||||
}
|
||||
s = r1[5];
|
||||
if (0.0 != s) {
|
||||
r2[5] -= m2 * s;
|
||||
r3[5] -= m3 * s;
|
||||
}
|
||||
s = r1[6];
|
||||
if (0.0 != s) {
|
||||
r2[6] -= m2 * s;
|
||||
r3[6] -= m3 * s;
|
||||
}
|
||||
s = r1[7];
|
||||
if (0.0 != s) {
|
||||
r2[7] -= m2 * s;
|
||||
r3[7] -= m3 * s;
|
||||
}
|
||||
|
||||
/* choose pivot - or die */
|
||||
if (fabs(r3[2]) > fabs(r2[2]))
|
||||
SWAP_ROWS(r3, r2);
|
||||
if (0.0 == r2[2])
|
||||
return false;
|
||||
|
||||
/* eliminate third variable */
|
||||
m3 = r3[2] / r2[2];
|
||||
r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
|
||||
r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], r3[7] -= m3 * r2[7];
|
||||
|
||||
/* last check */
|
||||
if (0.0 == r3[3])
|
||||
return false;
|
||||
|
||||
s = 1.0 / r3[3]; /* now back substitute row 3 */
|
||||
r3[4] *= s;
|
||||
r3[5] *= s;
|
||||
r3[6] *= s;
|
||||
r3[7] *= s;
|
||||
|
||||
m2 = r2[3]; /* now back substitute row 2 */
|
||||
s = 1.0 / r2[2];
|
||||
r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
|
||||
r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
|
||||
m1 = r1[3];
|
||||
r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
|
||||
r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
|
||||
m0 = r0[3];
|
||||
r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
|
||||
r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
|
||||
|
||||
m1 = r1[2]; /* now back substitute row 1 */
|
||||
s = 1.0 / r1[1];
|
||||
r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
|
||||
r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
|
||||
m0 = r0[2];
|
||||
r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
|
||||
r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
|
||||
|
||||
m0 = r0[1]; /* now back substitute row 0 */
|
||||
s = 1.0 / r0[0];
|
||||
r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
|
||||
r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
|
||||
|
||||
MAT(out, 0, 0) = r0[4];
|
||||
MAT(out, 0, 1) = r0[5], MAT(out, 0, 2) = r0[6];
|
||||
MAT(out, 0, 3) = r0[7], MAT(out, 1, 0) = r1[4];
|
||||
MAT(out, 1, 1) = r1[5], MAT(out, 1, 2) = r1[6];
|
||||
MAT(out, 1, 3) = r1[7], MAT(out, 2, 0) = r2[4];
|
||||
MAT(out, 2, 1) = r2[5], MAT(out, 2, 2) = r2[6];
|
||||
MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4];
|
||||
MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6];
|
||||
MAT(out, 3, 3) = r3[7];
|
||||
|
||||
return true;
|
||||
|
||||
#undef MAT
|
||||
#undef SWAP_ROWS
|
||||
}
|
||||
|
||||
//screen->3d
|
||||
|
@ -1168,7 +1307,7 @@ void Matrix4_UnProject(vec3_t in, vec3_t out, vec3_t viewangles, vec3_t vieworg,
|
|||
Matrix4_Projection(proj, wdivh, fovy, 4);
|
||||
Matrix4_Multiply(proj, modelview, tempm);
|
||||
|
||||
Matrix4_Invert_Simple((void*)proj, (void*)tempm);
|
||||
Matrix4_Invert(tempm, proj);
|
||||
|
||||
{
|
||||
float v[4], tempv[4];
|
||||
|
|
|
@ -103,7 +103,7 @@ float Q_rsqrt(float number);
|
|||
//used for crosshair stuff.
|
||||
void Matrix3_Multiply (vec3_t *in1, vec3_t *in2, vec3_t *out);
|
||||
void Matrix4_Identity(float *outm);
|
||||
void Matrix4_Invert_Simple (matrix4x4_t *out, const matrix4x4_t *in1);
|
||||
qboolean Matrix4_Invert(const float *m, float *out);
|
||||
void Matrix4x4_CreateTranslate (matrix4x4_t *out, float x, float y, float z);
|
||||
void Matrix4_ModelMatrixFromAxis (float *modelview, vec3_t pn, vec3_t right, vec3_t up, vec3_t vieworg);
|
||||
void Matrix4_ModelViewMatrix (float *modelview, vec3_t viewangles, vec3_t vieworg);
|
||||
|
|
Loading…
Reference in a new issue