Rev 18 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 18 | Rev 20 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | #include "raycast.h" | 
            1 | #include "raycast.h" | 
          
| 2 | #include "brender.h" | 
            2 | #include "brender/brender.h" | 
          
| 3 | #include "brucetrk.h" | 
            3 | #include "brucetrk.h" | 
          
| 4 | #include "formats.h" | 
            - | |
| 5 | #include "globvars.h" | 
            4 | #include "globvars.h" | 
          
| 6 | #include "harness/trace.h" | 
            5 | #include "harness/trace.h" | 
          
| 7 | #include "shortcut.h" | 
            - | |
| 8 | #include <float.h> | 
            6 | #include <float.h> | 
          
| 9 | #include <math.h> | 
            7 | #include <math.h> | 
          
| 10 | #include <stdlib.h> | 
            8 | #include <stdlib.h> | 
          
| 11 | 9 | ||
| 12 | br_matrix34 gPick_model_to_view__raycast; // suffix added to avoid duplicate symbol  | 
            10 | br_matrix34 gPick_model_to_view__raycast; // suffix added to avoid duplicate symbol  | 
          
| Line 261... | Line 259... | ||
| 261 | } | 
            259 | } | 
          
| 262 | 260 | ||
| 263 | // IDA: int __usercall DRModelPick2D@<EAX>(br_model *model@<EAX>, br_material *material@<EDX>, br_vector3 *ray_pos@<EBX>, br_vector3 *ray_dir@<ECX>, br_scalar t_near, br_scalar t_far, dr_modelpick2d_cbfn *callback, void *arg) | 
            261 | // IDA: int __usercall DRModelPick2D@<EAX>(br_model *model@<EAX>, br_material *material@<EDX>, br_vector3 *ray_pos@<EBX>, br_vector3 *ray_dir@<ECX>, br_scalar t_near, br_scalar t_far, dr_modelpick2d_cbfn *callback, void *arg) | 
          
| 264 | // Suffix added to avoid duplicate symbol | 
            262 | // Suffix added to avoid duplicate symbol | 
          
| 265 | int DRModelPick2D__raycast(br_model* model, br_material* material, br_vector3* ray_pos, br_vector3* ray_dir, br_scalar t_near, br_scalar t_far, dr_modelpick2d_cbfn* callback, void* arg) {  | 
            263 | int DRModelPick2D__raycast(br_model* model, br_material* material, br_vector3* ray_pos, br_vector3* ray_dir, br_scalar t_near, br_scalar t_far, dr_modelpick2d_cbfn* callback, void* arg) {  | 
          
| 266 | 
  | 
            264 | DR_FACE* fp;  | 
          
| 267 | int f;  | 
            265 | int f;  | 
          
| 268 | int axis_m;  | 
            266 | int axis_m;  | 
          
| 269 | int axis_0;  | 
            267 | int axis_0;  | 
          
| 270 | int axis_1;  | 
            268 | int axis_1;  | 
          
| 271 | int group;  | 
            269 | int group;  | 
          
| Line 293... | Line 291... | ||
| 293 | int r;  | 
            291 | int r;  | 
          
| 294 | br_material* this_material;  | 
            292 | br_material* this_material;  | 
          
| 295 |     br_scalar numerator; | 
            293 |     br_scalar numerator; | 
          
| 296 |     //double f_numerator; // Pierre-Marie Baty -- unused variable | 
            294 |     //double f_numerator; // Pierre-Marie Baty -- unused variable | 
          
| 297 | LOG_TRACE("(%p, %p, %p, %p, %f, %f, %p, %p)", model, material, ray_pos, ray_dir, t_near, t_far, callback, arg);  | 
            295 | LOG_TRACE("(%p, %p, %p, %p, %f, %f, %p, %p)", model, material, ray_pos, ray_dir, t_near, t_far, callback, arg);  | 
          
| 298 | - | ||
| 299 | struct v11group* grp_ptr;  | 
            - | |
| 300 | br_vector4* eqn;  | 
            - | |
| 301 | 296 | ||
| 302 | t_near -= 0.001f;  | 
            297 | t_near -= 0.001f;  | 
          
| 303 | t_far += 0.001f;  | 
            298 | t_far += 0.001f;  | 
          
| 304 | for (group = 0; group < V11MODEL(model)->ngroups; group++) {  | 
            299 | for (group = 0; group < V11MODEL(model)->ngroups; group++) {  | 
          
| 305 | grp_ptr = &V11MODEL(model)->groups[group];  | 
            - | |
| 306 | for (f = 0; f < V11MODEL(model)->groups[group].nfaces; f++) {  | 
            300 | for (f = 0; f < V11MODEL(model)->groups[group].nfaces; f++) {  | 
          
| 307 | 
  | 
            301 | fp = &V11MODEL(model)->groups[group].faces[f];  | 
          
| 308 | if (V11MODEL(model)->groups[group].  | 
            302 | if (V11MODEL(model)->groups[group].face_colours_material != NULL) {  | 
          
| 309 | this_material = V11MODEL(model)->groups[group].  | 
            303 | this_material = V11MODEL(model)->groups[group].face_colours_material;  | 
          
| 310 | } else {  | 
            304 | } else {  | 
          
| 311 | this_material = material;  | 
            305 | this_material = material;  | 
          
| 312 |             } | 
            306 |             } | 
          
| 313 | d = BrVector3Dot(eqn, ray_dir);  | 
            307 | d = BrVector3Dot(&fp->eqn, ray_dir);  | 
          
| 314 | if (fabsf(d) >= 0.00000023841858f && ((this_material->flags & (BR_MATF_TWO_SIDED | BR_MATF_ALWAYS_VISIBLE)) != 0 || d <= 0.0)) //  | 
            308 | if (fabsf(d) >= 0.00000023841858f && ((this_material->flags & (BR_MATF_TWO_SIDED | BR_MATF_ALWAYS_VISIBLE)) != 0 || d <= 0.0)) //  | 
          
| 315 |             { | 
            309 |             { | 
          
| 316 | numerator = BrVector3Dot(eqn, ray_pos) -  | 
            310 | numerator = BrVector3Dot(&fp->eqn, ray_pos) - fp->eqn.v[3];  | 
          
| 317 | if (!BadDiv__raycast(numerator, d)) {  | 
            311 | if (!BadDiv__raycast(numerator, d)) {  | 
          
| 318 | t = -(numerator / d);  | 
            312 | t = -(numerator / d);  | 
          
| 319 | if (t >= t_near && t <= t_far) {  | 
            313 | if (t >= t_near && t <= t_far) {  | 
          
| 320 | BrVector3Scale(&p, ray_dir, t);  | 
            314 | BrVector3Scale(&p, ray_dir, t);  | 
          
| 321 | BrVector3Accumulate(&p, ray_pos);  | 
            315 | BrVector3Accumulate(&p, ray_pos);  | 
          
| 322 | axis_m = (fabsf(  | 
            316 | axis_m = (fabsf(fp->eqn.v[1]) > fabsf(fp->eqn.v[0])) ? 1 : 0;  | 
          
| 323 | if (fabsf(  | 
            317 | if (fabsf(fp->eqn.v[2]) > fabsf(fp->eqn.v[axis_m])) {  | 
          
| 324 | axis_m = 2;  | 
            318 | axis_m = 2;  | 
          
| 325 |                         } | 
            319 |                         } | 
          
| 326 | if (axis_m == 0) {  | 
            320 | if (axis_m == 0) {  | 
          
| 327 | axis_0 = 1;  | 
            321 | axis_0 = 1;  | 
          
| 328 | axis_1 = 2;  | 
            322 | axis_1 = 2;  | 
          
| Line 332... | Line 326... | ||
| 332 | } else if (axis_m == 2) {  | 
            326 | } else if (axis_m == 2) {  | 
          
| 333 | axis_0 = 0;  | 
            327 | axis_0 = 0;  | 
          
| 334 | axis_1 = 1;  | 
            328 | axis_1 = 1;  | 
          
| 335 |                         } | 
            329 |                         } | 
          
| 336 | 330 | ||
| 337 | v0 =  | 
            331 | v0 = V11MODEL(model)->groups[group].vertices[fp->vertices[0]].p.v[axis_0];  | 
          
| 338 | u0 =  | 
            332 | u0 = V11MODEL(model)->groups[group].vertices[fp->vertices[0]].p.v[axis_1];  | 
          
| 339 | - | ||
| 340 | v1 =  | 
            333 | v1 = V11MODEL(model)->groups[group].vertices[fp->vertices[1]].p.v[axis_0] - v0;  | 
          
| 341 | u1 =  | 
            334 | u1 = V11MODEL(model)->groups[group].vertices[fp->vertices[1]].p.v[axis_1] - u0;  | 
          
| 342 | v2 =  | 
            335 | v2 = V11MODEL(model)->groups[group].vertices[fp->vertices[2]].p.v[axis_0] - v0;  | 
          
| 343 | u2 =  | 
            336 | u2 = V11MODEL(model)->groups[group].vertices[fp->vertices[2]].p.v[axis_1] - u0;  | 
          
| 344 | 337 | ||
| 345 | v0i1 = p.v[axis_0] - v0;  | 
            338 | v0i1 = p.v[axis_0] - v0;  | 
          
| 346 | v0i2 = p.v[axis_1] - u0;  | 
            339 | v0i2 = p.v[axis_1] - u0;  | 
          
| 347 | if (fabs(v1) > 0.0000002384185791015625) {  | 
            340 | if (fabs(v1) > 0.0000002384185791015625) {  | 
          
| 348 | f_d = v0i2 * v1 - u1 * v0i1;  | 
            341 | f_d = v0i2 * v1 - u1 * v0i1;  | 
          
| Line 358... | Line 351... | ||
| 358 |                         } | 
            351 |                         } | 
          
| 359 | 352 | ||
| 360 | if (alpha >= 0.0 && beta >= 0.0 && beta + alpha <= 1.0) {  | 
            353 | if (alpha >= 0.0 && beta >= 0.0 && beta + alpha <= 1.0) {  | 
          
| 361 | s_alpha = alpha;  | 
            354 | s_alpha = alpha;  | 
          
| 362 | s_beta = beta;  | 
            355 | s_beta = beta;  | 
          
| - | 356 | map.v[0] = V11MODEL(model)->groups[group].vertices[fp->vertices[1]].map.v[0] * s_alpha;  | 
          |
| 363 | 
  | 
            357 | map.v[1] = V11MODEL(model)->groups[group].vertices[fp->vertices[1]].map.v[1] * s_alpha;  | 
          
| 364 |                             DRVector2AccumulateScale__raycast( | 
            358 |                             DRVector2AccumulateScale__raycast( | 
          
| 365 | &map,  | 
            359 | &map,  | 
          
| 366 | &  | 
            360 | &V11MODEL(model)->groups[group].vertices[fp->vertices[2]].map,  | 
          
| 367 | s_beta);  | 
            361 | s_beta);  | 
          
| 368 |                             DRVector2AccumulateScale__raycast( | 
            362 |                             DRVector2AccumulateScale__raycast( | 
          
| 369 | &map,  | 
            363 | &map,  | 
          
| 370 | &  | 
            364 | &V11MODEL(model)->groups[group].vertices[fp->vertices[0]].map,  | 
          
| 371 | 1.0f - (s_alpha + s_beta));  | 
            365 | 1.0f - (s_alpha + s_beta));  | 
          
| 372 | v = 0;  | 
            366 | v = 0;  | 
          
| 373 | e = 1;  | 
            367 | e = 1;  | 
          
| 374 | if (s_alpha <= s_beta) {  | 
            368 | if (s_alpha <= s_beta) {  | 
          
| 375 | if (0.5f - s_beta / 2.0f > s_alpha) {  | 
            369 | if (0.5f - s_beta / 2.0f > s_alpha) {  | 
          
| Line 445... | Line 439... | ||
| 445 | *pNearest_y_below = gHighest_y_below;  | 
            439 | *pNearest_y_below = gHighest_y_below;  | 
          
| 446 | *pNearest_above_model = gAbove_model;  | 
            440 | *pNearest_above_model = gAbove_model;  | 
          
| 447 | *pNearest_below_model = gBelow_model;  | 
            441 | *pNearest_below_model = gBelow_model;  | 
          
| 448 | *pNearest_above_face_index = gAbove_face_index;  | 
            442 | *pNearest_above_face_index = gAbove_face_index;  | 
          
| 449 | *pNearest_below_face_index = gBelow_face_index;  | 
            443 | *pNearest_below_face_index = gBelow_face_index;  | 
          
| - | 444 |     // LOG_DEBUG("FindBestY %f %f '%s' '%s' %d %d", gLowest_y_above, gHighest_y_below, gAbove_model->identifier, gBelow_model->identifier, gAbove_face_index, gBelow_face_index); | 
          |
| 450 | } | 
            445 | } | 
          
| 451 | 446 | ||
| 452 | // IDA: int __cdecl FindYVerticallyBelowPolyCallBack(br_model *pModel, br_material *pMaterial, br_vector3 *pRay_pos, br_vector3 *pRay_dir, br_scalar pT, int pF, int pE, int pV, br_vector3 *pPoint, br_vector2 *pMap, void *pArg) | 
            447 | // IDA: int __cdecl FindYVerticallyBelowPolyCallBack(br_model *pModel, br_material *pMaterial, br_vector3 *pRay_pos, br_vector3 *pRay_dir, br_scalar pT, int pF, int pE, int pV, br_vector3 *pPoint, br_vector2 *pMap, void *pArg) | 
          
| 453 | int FindYVerticallyBelowPolyCallBack(br_model* pModel, br_material* pMaterial, br_vector3* pRay_pos, br_vector3* pRay_dir, br_scalar pT, int pF, int pE, int pV, br_vector3* pPoint, br_vector2* pMap, void* pArg) {  | 
            448 | int FindYVerticallyBelowPolyCallBack(br_model* pModel, br_material* pMaterial, br_vector3* pRay_pos, br_vector3* pRay_dir, br_scalar pT, int pF, int pE, int pV, br_vector3* pPoint, br_vector2* pMap, void* pArg) {  | 
          
| 454 |     br_scalar the_y; | 
            449 |     br_scalar the_y; | 
          
| 455 | LOG_TRACE("(%p, %p, %p, %p, %f, %d, %d, %d, %p, %p, %p)", pModel, pMaterial, pRay_pos, pRay_dir, pT, pF, pE, pV, pPoint, pMap, pArg);  | 
            450 | LOG_TRACE("(%p, %p, %p, %p, %f, %d, %d, %d, %p, %p, %p)", pModel, pMaterial, pRay_pos, pRay_dir, pT, pF, pE, pV, pPoint, pMap, pArg);  | 
          
| 456 | 451 | ||
| 457 | if (pMaterial->identifier == NULL || pMaterial->identifier[0] != '!') {  | 
            452 | if (pMaterial->identifier == NULL || pMaterial->identifier[0] != '!') {  | 
          
| 458 | the_y = pPoint->v[  | 
            453 | the_y = pPoint->v[V_Y];  | 
          
| 459 | if (the_y > gHighest_y_below) {  | 
            454 | if (the_y > gHighest_y_below) {  | 
          
| 460 | gHighest_y_below = the_y;  | 
            455 | gHighest_y_below = the_y;  | 
          
| 461 |         } | 
            456 |         } | 
          
| 462 |     } | 
            457 |     } | 
          
| 463 | return 0;  | 
            458 | return 0;  | 
          
| Line 482... | Line 477... | ||
| 482 |     tU8 z; | 
            477 |     tU8 z; | 
          
| 483 | tTrack_spec* track_spec;  | 
            478 | tTrack_spec* track_spec;  | 
          
| 484 | LOG_TRACE("(%p)", pPosition);  | 
            479 | LOG_TRACE("(%p)", pPosition);  | 
          
| 485 | 480 | ||
| 486 | track_spec = &gProgram_state.track_spec;  | 
            481 | track_spec = &gProgram_state.track_spec;  | 
          
| 487 | XZToColumnXZ(&cx, &cz, pPosition->v[  | 
            482 | XZToColumnXZ(&cx, &cz, pPosition->v[V_X], pPosition->v[V_Z], track_spec);  | 
          
| 488 | gHighest_y_below = BR_SCALAR_MIN;  | 
            483 | gHighest_y_below = BR_SCALAR_MIN;  | 
          
| 489 | BrVector3Copy(&gY_picking_camera->t.t.translate.t, pPosition);  | 
            484 | BrVector3Copy(&gY_picking_camera->t.t.translate.t, pPosition);  | 
          
| 490 | for (x = MAX(cx - 1, 0); x < MIN(cx + 2, track_spec->ncolumns_x); x++) {  | 
            485 | for (x = MAX(cx - 1, 0); x < MIN(cx + 2, track_spec->ncolumns_x); x++) {  | 
          
| 491 | for (z = MAX(cz - 1, 0); z < MIN(cz + 2, track_spec->ncolumns_z); z++) {  | 
            486 | for (z = MAX(cz - 1, 0); z < MIN(cz + 2, track_spec->ncolumns_z); z++) {  | 
          
| 492 | if (track_spec->columns[z][x] != NULL) {  | 
            487 | if (track_spec->columns[z][x] != NULL) {  | 
          
| Line 511... | Line 506... | ||
| 511 | LOG_TRACE("(%p)", pCast_point);  | 
            506 | LOG_TRACE("(%p)", pCast_point);  | 
          
| 512 | 507 | ||
| 513 | BrVector3Copy(&cast_point, pCast_point);  | 
            508 | BrVector3Copy(&cast_point, pCast_point);  | 
          
| 514 | for (number_of_attempts = 0; number_of_attempts <= 10; number_of_attempts++) {  | 
            509 | for (number_of_attempts = 0; number_of_attempts <= 10; number_of_attempts++) {  | 
          
| 515 | result = FindYVerticallyBelow(&cast_point);  | 
            510 | result = FindYVerticallyBelow(&cast_point);  | 
          
| 516 | cast_point.v[  | 
            511 | cast_point.v[V_Y] += .2f;  | 
          
| 517 | if (result >= -100.f) {  | 
            512 | if (result >= -100.f) {  | 
          
| 518 | return result;  | 
            513 | return result;  | 
          
| 519 |         } | 
            514 |         } | 
          
| 520 |     } | 
            515 |     } | 
          
| 521 | return result;  | 
            516 | return result;  |