Shadowrun: Awakened 29 September 2011 - Build 871
Defines | Functions | Variables
triangle.cpp File Reference
#include "machine.h"
#include "types.h"
#include "vector.h"
#include "macros.h"
#include "intersect.h"
#include "util.h"
#include "triangle.h"
Include dependency graph for triangle.cpp:

Go to the source code of this file.

Defines

#define CROSS(dest, v1, v2)
#define DOT(v1, v2)   (v1.x*v2.x+v1.y*v2.y+v1.z*v2.z)
#define SUB(dest, v1, v2)
#define TRIANGLE_PRIVATE

Functions

objectnewstri (void *tex, vector v0, vector v1, vector v2, vector n0, vector n1, vector n2)
objectnewtri (void *tex, vector v0, vector v1, vector v2)
static void stri_normal (stri *trn, vector *pnt, ray *incident, vector *N)
static int tri_bbox (void *obj, vector *min, vector *max)
static void tri_intersect (tri *trn, ray *ry)
static void tri_normal (tri *trn, vector *pnt, ray *incident, vector *N)

Variables

static object_methods stri_methods
static object_methods tri_methods

Define Documentation

#define CROSS (   dest,
  v1,
  v2 
)
Value:
dest.x=v1.y*v2.z-v1.z*v2.y; \
          dest.y=v1.z*v2.x-v1.x*v2.z; \
          dest.z=v1.x*v2.y-v1.y*v2.x;

Definition at line 151 of file triangle.cpp.

Referenced by stri_normal(), tri_intersect(), and tri_normal().

#define DOT (   v1,
  v2 
)    (v1.x*v2.x+v1.y*v2.y+v1.z*v2.z)

Definition at line 156 of file triangle.cpp.

Referenced by stri_normal(), and tri_intersect().

#define SUB (   dest,
  v1,
  v2 
)
Value:
dest.x=v1.x-v2.x; \
          dest.y=v1.y-v2.y; \
          dest.z=v1.z-v2.z;

Definition at line 158 of file triangle.cpp.

Referenced by tri_intersect().

Definition at line 71 of file triangle.cpp.


Function Documentation

object* newstri ( void *  tex,
vector  v0,
vector  v1,
vector  v2,
vector  n0,
vector  n1,
vector  n2 
)

Definition at line 118 of file triangle.cpp.

References EPSILON, rt_getmem(), stri_methods, VLength(), and VSub().

Referenced by rt_stri().

                                                            {
  stri * t;
  vector edge1, edge2, edge3;

  VSub(&v1, &v0, &edge1);
  VSub(&v2, &v0, &edge2);
  VSub(&v2, &v1, &edge3);

  /* check to see if this will be a degenerate triangle before creation */
  if ((VLength(&edge1) >= EPSILON) && 
      (VLength(&edge2) >= EPSILON) &&
      (VLength(&edge3) >= EPSILON)) {

    t=(stri *) rt_getmem(sizeof(stri));

    t->nextobj = NULL;
    t->methods = &stri_methods;
 
    t->tex = (texture *)tex;
    t->v0 = v0;
    t->edge1 = edge1;
    t->edge2 = edge2;
    t->n0 = n0;
    t->n1 = n1;
    t->n2 = n2;

    return (object *) t;
  }

  return NULL; /* was a degenerate triangle */
}
object* newtri ( void *  tex,
vector  v0,
vector  v1,
vector  v2 
)

Definition at line 88 of file triangle.cpp.

References EPSILON, rt_getmem(), tri_methods, VLength(), and VSub().

Referenced by rt_tri().

                                                             {
  tri * t;
  vector edge1, edge2, edge3;

  VSub(&v1, &v0, &edge1);
  VSub(&v2, &v0, &edge2);
  VSub(&v2, &v1, &edge3);

  /* check to see if this will be a degenerate triangle before creation */
  if ((VLength(&edge1) >= EPSILON) && 
      (VLength(&edge2) >= EPSILON) && 
      (VLength(&edge3) >= EPSILON)) {

    t=(tri *) rt_getmem(sizeof(tri));

    t->nextobj = NULL;
    t->methods = &tri_methods;

    t->tex = (texture *)tex;
    t->v0 = v0;
    t->edge1 = edge1;
    t->edge2 = edge2;
 
    return (object *) t;
  }
  
  return NULL; /* was a degenerate triangle */
}
static void stri_normal ( stri *  trn,
vector pnt,
ray incident,
vector N 
) [static]

Definition at line 232 of file triangle.cpp.

References CROSS, DOT, V, VNorm(), VSUB, vector::x, vector::y, and vector::z.

                                                                               {
  flt U, V, W, lensqr;
  vector P, tmp, norm;
  
  CROSS(norm, trn->edge1, trn->edge2);
  lensqr = DOT(norm, norm); 

  VSUB((*pnt), trn->v0, P);

  CROSS(tmp, P, trn->edge2);
  U = DOT(tmp, norm) / lensqr;   

  CROSS(tmp, trn->edge1, P);
  V = DOT(tmp, norm) / lensqr;   

  W = 1.0 - (U + V);

  N->x = W*trn->n0.x + U*trn->n1.x + V*trn->n2.x;
  N->y = W*trn->n0.y + U*trn->n1.y + V*trn->n2.y;
  N->z = W*trn->n0.z + U*trn->n1.z + V*trn->n2.z;

  VNorm(N);
}
static int tri_bbox ( void *  obj,
vector min,
vector max 
) [static]

Definition at line 163 of file triangle.cpp.

References MYMAX, MYMIN, VAdd(), vector::x, vector::y, and vector::z.

                                                            {
  tri * t = (tri *) obj;
  vector v1, v2;

  VAdd(&t->v0, &t->edge1, &v1); 
  VAdd(&t->v0, &t->edge2, &v2); 

  min->x = MYMIN( t->v0.x , MYMIN( v1.x , v2.x ));
  min->y = MYMIN( t->v0.y , MYMIN( v1.y , v2.y ));
  min->z = MYMIN( t->v0.z , MYMIN( v1.z , v2.z ));

  max->x = MYMAX( t->v0.x , MYMAX( v1.x , v2.x ));
  max->y = MYMAX( t->v0.y , MYMAX( v1.y , v2.y ));
  max->z = MYMAX( t->v0.z , MYMAX( v1.z , v2.z ));

  return 1;
}
static void tri_intersect ( tri *  trn,
ray ry 
) [static]

Definition at line 181 of file triangle.cpp.

References add_intersection(), CROSS, ray::d, DOT, EPSILON, ray::o, and SUB.

                                               {
  vector tvec, pvec, qvec;
  flt det, inv_det, t, u, v;

  /* begin calculating determinant - also used to calculate U parameter */
  CROSS(pvec, ry->d, trn->edge2);

  /* if determinant is near zero, ray lies in plane of triangle */
  det = DOT(trn->edge1, pvec);

   if (det > -EPSILON && det < EPSILON)
     return;

   inv_det = 1.0 / det;

   /* calculate distance from vert0 to ray origin */
   SUB(tvec, ry->o, trn->v0);

   /* calculate U parameter and test bounds */
   u = DOT(tvec, pvec) * inv_det;
   if (u < 0.0 || u > 1.0)
     return;

   /* prepare to test V parameter */
   CROSS(qvec, tvec, trn->edge1);

   /* calculate V parameter and test bounds */
   v = DOT(ry->d, qvec) * inv_det;
   if (v < 0.0 || u + v > 1.0)
     return;

   /* calculate t, ray intersects triangle */
   t = DOT(trn->edge2, qvec) * inv_det;

  add_intersection(t,(object *) trn, ry);
}
static void tri_normal ( tri *  trn,
vector pnt,
ray incident,
vector N 
) [static]

Definition at line 219 of file triangle.cpp.

References CROSS, ray::d, VDot(), VNorm(), vector::x, vector::y, and vector::z.

                                                                             {

  CROSS((*N), trn->edge1, trn->edge2);

  VNorm(N);

  if (VDot(N, &(incident->d)) > 0.0)  {
    N->x=-N->x;
    N->y=-N->y;
    N->z=-N->z;
  }
}

Variable Documentation

Initial value:
 {
  (void (*)(void *, void *))(tri_intersect),
  (void (*)(void *, void *, void *, void *))(stri_normal),
  tri_bbox, 
  free 
}

Definition at line 81 of file triangle.cpp.

Referenced by newstri().

Initial value:
 {
  (void (*)(void *, void *))(tri_intersect),
  (void (*)(void *, void *, void *, void *))(tri_normal),
  tri_bbox, 
  free 
}

Definition at line 74 of file triangle.cpp.

Referenced by newtri().


Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net