/* plane.c */ #include "ray.h" /**/ /* Determine if a vector projected from location *base */ /* in direction *dir hits the plane.. If so the return */ /* value is the distance from the base to the point of */ /* contact. */ double plane_hits( object_t *obj, vec_t base, /* ray base */ vec_t dir) /* unit direction vector */ { plane_t *pln; assert(obj->cookie == OBJ_COOKIE); /* extract pln pointer from object structure */ ------------------- /* We will use the following notation in the comments below */ /* Q = point on the plane (plane attribute ) */ /* N = normal to the plane (plane attribute ) */ /* B = base of ray (input parameter ) */ /* D = direction of ray */ /* ndotd = N dot D */ /* ndotb = N dot B */ /* ndotq = N dot Q --- which was precomputed in plane_init */ /* dist = signed distance from base to hitpoint location */ /* Compute ndotd and if 0 return(-1.0); /* ------------------ /* Compute ndotb */ -------------------- /* Compute dist = (ndotq - ndotb) / ndotd; */ -------------------- /* If dist <= 0.0 return(-1.0); */ -------------------- /* Compute location of hit point by scaling dir by dist and */ /* adding to base */ -------------------- /* if hitpoint_location[Z] is postive return(-1). */ -------------------- /* Copy hitpoint_location to obj->last_hit */ /* and pln->normal to obj->last_normal */ return(dist); } void plane_print( object_t *obj, FILE *out) { plane_t *pln; ------------- } int plane_load_attributes( FILE *in, plane_t *pln) { -------------- } /**/ /* Create a new plane object and initialize it */ void plane_init( FILE *in, model_t *model, int attrmax) { plane_t *pln; object_t *obj; int count; /* Call the object_init()function to create the object_t */ /* and process the "material" attribute */ ---------------- /* Use list_get_entity() to make obj point to the newly */ /* created object_t structure.Your list_add() function */ /* MUST set current to the last element in the list for */ /* this to work correctly. */ ----------------- /* malloc a plane_t structure and set the priv pointer */ /* in the object_t structure to point to the plane_t */ ----------------- /* Store the word "plane" in the object_type field of */ /* the object_t structure. Use the strcpy() function */ ----------------- /* Ask plane_load_attributes to load the attributes */ /* Attributes are normal and point */ count = plane_load_attributes(in, pln); assert(count == 2); /* Setup polymorphic function pointers in object_t */ ---------------- /* pre-compute ndotq */ ----------------- }