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