home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / RTrace 1.0 / source / text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-16  |  8.2 KB  |  259 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (c) 1988, 1992 Antonio Costa, INESC-Norte.
  3.  * All rights reserved.
  4.  *
  5.  * This code received contributions from the following people:
  6.  *
  7.  *  Roman Kuchkuda      - basic ray tracer
  8.  *  Mark VandeWettering - MTV ray tracer
  9.  *  Augusto Sousa       - overall, shading model
  10.  *  Paulo Almeida       - TEXT3D primitive
  11.  *  Pedro Borges        - TEXT3D primitive
  12.  *
  13.  * Redistribution and use in source and binary forms are permitted
  14.  * provided that the above copyright notice and this paragraph are
  15.  * duplicated in all such forms and that any documentation,
  16.  * advertising materials, and other materials related to such
  17.  * distribution and use acknowledge that the software was developed
  18.  * by Antonio Costa, at INESC-Norte. The name of the author and
  19.  * INESC-Norte may not be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  22.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24.  */
  25. #include "defs.h"
  26. #include "extern.h"
  27. #include "pp_ext.h"
  28.  
  29. /**********************************************************************
  30.  *    RAY TRACING - Text - Version 7.2                                *
  31.  *                                                                    *
  32.  *    MADE BY    : Antonio Costa, INESC-Norte, April 1992             *
  33.  *    MODIFIED BY: Antonio Costa, INESC-Norte, June 1992              *
  34.  **********************************************************************/
  35.  
  36. /***** Text *****/
  37. real
  38. text_intersect(position, vector, object)
  39.   xyz_ptr         position, vector;
  40.   object_ptr      object;
  41. {
  42.   REG real        distance;
  43.   xyz_struct      new_position, new_vector;
  44.   text_ptr        text;
  45.  
  46.   if (object->transf != NULL)
  47.   {
  48.     transform(object->transf, position, &new_position);
  49.     transform_vector(object->transf, position, vector, &new_position,
  50.                      &new_vector);
  51.     NORMALIZE(new_vector);
  52.   } else
  53.   {
  54.     STRUCT_ASSIGN(new_position, *position);
  55.     STRUCT_ASSIGN(new_vector, *vector);
  56.   }
  57.   REALINC(text_tests);
  58.   text = (text_ptr) object->data;
  59.   distance = intersect_pp_obj(&new_position, &new_vector, object->min,
  60.                               object->max, text->data);
  61.   if (distance > threshold_distance)
  62.   {
  63.     if (object->transf != NULL)
  64.     {
  65.       text->position->x = new_position.x + distance * new_vector.x;
  66.       text->position->y = new_position.y + distance * new_vector.y;
  67.       text->position->z = new_position.z + distance * new_vector.z;
  68.       return transform_distance(object->inv_transf, distance,
  69.                                 &new_position, &new_vector, position);
  70.     } else
  71.       return distance;
  72.   }
  73.   return 0.0;
  74. }
  75. void
  76. text_normal(position, object, normal)
  77.   xyz_ptr         position;
  78.   object_ptr      object;
  79.   xyz_ptr         normal;
  80. {
  81.   xyz_struct      old_position, new_normal;
  82.   text_ptr        text;
  83.  
  84.   text = (text_ptr) object->data;
  85.   if (object->transf != NULL)
  86.     STRUCT_ASSIGN(old_position, *(text->position));
  87.   else
  88.     STRUCT_ASSIGN(old_position, *position);
  89.   normal_pp_obj(&old_position, text->data, normal);
  90.   if (object->transf != NULL)
  91.   {
  92.     transform_normal_vector(object->transf, text->position, normal,
  93.                             &new_normal);
  94.     STRUCT_ASSIGN(*normal, new_normal);
  95.   }
  96.   NORMALIZE(*normal);
  97. }
  98. void
  99. text_enclose(object)
  100.   object_ptr      object;
  101. {
  102.   xyz_struct      max, min, temp, vertex;
  103.   text_ptr        text;
  104.  
  105.   if (object->transf != NULL)
  106.   {
  107.     text = (text_ptr) object->data;
  108.     STRUCT_ASSIGN(max, *(object->max));
  109.     STRUCT_ASSIGN(min, *(object->min));
  110.     ALLOCATE(text->position, xyz_struct, 1);
  111.     temp.x = min.x;
  112.     temp.y = min.y;
  113.     temp.z = min.z;
  114.     transform(object->inv_transf, &temp, object->min);
  115.     STRUCT_ASSIGN(*(object->max), *(object->min));
  116.     temp.x = min.x;
  117.     temp.y = min.y;
  118.     temp.z = max.z;
  119.     transform(object->inv_transf, &temp, &vertex);
  120.     if (vertex.x < object->min->x)
  121.       object->min->x = vertex.x;
  122.     if (vertex.x > object->max->x)
  123.       object->max->x = vertex.x;
  124.     if (vertex.y < object->min->y)
  125.       object->min->y = vertex.y;
  126.     if (vertex.y > object->max->y)
  127.       object->max->y = vertex.y;
  128.     if (vertex.z < object->min->z)
  129.       object->min->z = vertex.z;
  130.     if (vertex.z > object->max->z)
  131.       object->max->z = vertex.z;
  132.     temp.x = min.x;
  133.     temp.y = max.y;
  134.     temp.z = min.z;
  135.     transform(object->inv_transf, &temp, &vertex);
  136.     if (vertex.x < object->min->x)
  137.       object->min->x = vertex.x;
  138.     if (vertex.x > object->max->x)
  139.       object->max->x = vertex.x;
  140.     if (vertex.y < object->min->y)
  141.       object->min->y = vertex.y;
  142.     if (vertex.y > object->max->y)
  143.       object->max->y = vertex.y;
  144.     if (vertex.z < object->min->z)
  145.       object->min->z = vertex.z;
  146.     if (vertex.z > object->max->z)
  147.       object->max->z = vertex.z;
  148.     temp.x = min.x;
  149.     temp.y = max.y;
  150.     temp.z = max.z;
  151.     transform(object->inv_transf, &temp, &vertex);
  152.     if (vertex.x < object->min->x)
  153.       object->min->x = vertex.x;
  154.     if (vertex.x > object->max->x)
  155.       object->max->x = vertex.x;
  156.     if (vertex.y < object->min->y)
  157.       object->min->y = vertex.y;
  158.     if (vertex.y > object->max->y)
  159.       object->max->y = vertex.y;
  160.     if (vertex.z < object->min->z)
  161.       object->min->z = vertex.z;
  162.     if (vertex.z > object->max->z)
  163.       object->max->z = vertex.z;
  164.     temp.x = max.x;
  165.     temp.y = min.y;
  166.     temp.z = min.z;
  167.     transform(object->inv_transf, &temp, &vertex);
  168.     if (vertex.x < object->min->x)
  169.       object->min->x = vertex.x;
  170.     if (vertex.x > object->max->x)
  171.       object->max->x = vertex.x;
  172.     if (vertex.y < object->min->y)
  173.       object->min->y = vertex.y;
  174.     if (vertex.y > object->max->y)
  175.       object->max->y = vertex.y;
  176.     if (vertex.z < object->min->z)
  177.       object->min->z = vertex.z;
  178.     if (vertex.z > object->max->z)
  179.       object->max->z = vertex.z;
  180.     temp.x = max.x;
  181.     temp.y = min.y;
  182.     temp.z = max.z;
  183.     transform(object->inv_transf, &temp, &vertex);
  184.     if (vertex.x < object->min->x)
  185.       object->min->x = vertex.x;
  186.     if (vertex.x > object->max->x)
  187.       object->max->x = vertex.x;
  188.     if (vertex.y < object->min->y)
  189.       object->min->y = vertex.y;
  190.     if (vertex.y > object->max->y)
  191.       object->max->y = vertex.y;
  192.     if (vertex.z < object->min->z)
  193.       object->min->z = vertex.z;
  194.     if (vertex.z > object->max->z)
  195.       object->max->z = vertex.z;
  196.     temp.x = max.x;
  197.     temp.y = max.y;
  198.     temp.z = min.z;
  199.     transform(object->inv_transf, &temp, &vertex);
  200.     if (vertex.x < object->min->x)
  201.       object->min->x = vertex.x;
  202.     if (vertex.x > object->max->x)
  203.       object->max->x = vertex.x;
  204.     if (vertex.y < object->min->y)
  205.       object->min->y = vertex.y;
  206.     if (vertex.y > object->max->y)
  207.       object->max->y = vertex.y;
  208.     if (vertex.z < object->min->z)
  209.       object->min->z = vertex.z;
  210.     if (vertex.z > object->max->z)
  211.       object->max->z = vertex.z;
  212.     temp.x = max.x;
  213.     temp.y = max.y;
  214.     temp.z = max.z;
  215.     transform(object->inv_transf, &temp, &vertex);
  216.     if (vertex.x < object->min->x)
  217.       object->min->x = vertex.x;
  218.     if (vertex.x > object->max->x)
  219.       object->max->x = vertex.x;
  220.     if (vertex.y < object->min->y)
  221.       object->min->y = vertex.y;
  222.     if (vertex.y > object->max->y)
  223.       object->max->y = vertex.y;
  224.     if (vertex.z < object->min->z)
  225.       object->min->z = vertex.z;
  226.     if (vertex.z > object->max->z)
  227.       object->max->z = vertex.z;
  228.   }
  229. }
  230.  
  231. /***** Local stuff *****/
  232. short int      pp_surface_id;
  233. real          pp_refraction;
  234.  
  235. void_ptr *
  236. create_pp_obj(min, max)
  237.   xyz_ptr     *min, *max;
  238. {
  239.   text_ptr      text;
  240.  
  241.   POSINC(objects);
  242.   ALLOCATE(object[objects], object_struct, 1);
  243.   object[objects]->id = objects;
  244.   object[objects]->surface_id = pp_surface_id;
  245.   object[objects]->refraction = pp_refraction;
  246.   ALLOCATE(object[objects]->min, xyz_struct, 1);
  247.   ALLOCATE(object[objects]->max, xyz_struct, 1);
  248.   object[objects]->transf = NULL;
  249.   object[objects]->inv_transf = NULL;
  250.   object[objects]->texture = NULL;
  251.   object[objects]->texture_modify_normal = FALSE;
  252.   object[objects]->object_type = TEXT_TYPE;
  253.   ALLOCATE(text, text_struct, 1);
  254.   object[objects]->data = (void_ptr) text;
  255.   *min = object[objects]->min;
  256.   *max = object[objects]->max;
  257.   return &(text->data);
  258. }
  259.