Trace.h
上传用户:lhwx1029
上传日期:2013-03-07
资源大小:1173k
文件大小:11k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. #if defined(_CI_)
  2.  #error Cannot use _CI_ for ray tracing.
  3. #endif
  4. #if !defined(_TRACE_H_)
  5. #define _TRACE_H_
  6. /** 3DGPL *************************************************
  7.  * (RGB hardware)                                         *
  8.  * Header for the 3D ray tracing engine.                  *
  9.  *                                                        *
  10.  * Ifdefs:                                                *
  11.  *  _RGB_                   Colour/Intensity model.       *
  12.  *                                                        *
  13.  * Files:                                                 *
  14.  *  trace.c                 Ray tracing engine.           *
  15.  *                                                        *
  16.  * (c) 1995-98 Sergei Savchenko, (savs@cs.mcgill.ca)      *
  17. **********************************************************/
  18. #include "Vector.h"               /* V_LNG_VECTOR */
  19. /**********************************************************
  20.  * Material. Contains reflection coeficients.             *
  21.  *                                                        *
  22.  *  +--------------------+                                *
  23.  *  |             +---+---+---+                           *
  24.  *  | tr_ambient  | R | G | B |                           *
  25.  *  |             +---+---+---+                           *
  26.  *  |                    |                                *
  27.  *  |             +---+---+---+                           *
  28.  *  | tr_diffuse  | R | G | B |                           *
  29.  *  |             +---+---+---+                           *
  30.  *  |                    |                                *
  31.  *  | tr_specular        |                                *
  32.  *  | tr_exponent        |                                *
  33.  *  | tr_reflect         |                                *
  34.  *  +--------------------+                                *
  35.  *                                                        *
  36. **********************************************************/
  37. struct TR_matter
  38. {
  39. //环境反射,RGB矢量
  40.  float tr_ambient[V_LNG_VECTOR];            /* coefs of ambient reflection */
  41. //漫反射,RGB矢量
  42.  float tr_diffuse[V_LNG_VECTOR];            /* coefs of diffuse reflection */
  43. //镜面反射
  44.  float tr_specular;                         /* coef of specular reflection */
  45. //镜面反射系数
  46.  float tr_exponent;                         /* specular exponent */
  47. //递归光线
  48.  float tr_reflect;                          /* recursive ray */
  49. };
  50. /**********************************************************
  51.  * Light source. Represents a point light equal into all  *
  52.  * directions.                                            *
  53.  *                                                        *
  54.  *  +--------------------+                                *
  55.  *  |              +---+---+---+                          *
  56.  *  | tr_centre    | X | Y | Z |                          *
  57.  *  |              +---+---+---+                          *
  58.  *  |                    |                                *
  59.  *  |              +---+---+---+                          *
  60.  *  | tr_intensity | R | G | B |                          *
  61.  *  |              +---+---+---+                          *
  62.  *  +--------------------+                                *
  63.  *                                                        *
  64. **********************************************************/
  65. struct TR_point_light
  66. {
  67.  float tr_centre[V_LNG_VECTOR];             /* point light source */
  68.  float tr_intensity[V_LNG_VECTOR];
  69. };
  70. /**********************************************************
  71.  * Ray. Represented through origine and direction vector. *
  72.  *                                                        *
  73.  *  +--------------------+                                *
  74.  *  |               +---+---+---+                         *
  75.  *  | tr_start      | X | Y | Z |                         *
  76.  *  |               +---+---+---+                         *
  77.  *  |                    |                                *
  78.  *  |               +---+---+---+                         *
  79.  *  | tr_codirected | X | Y | Z |                         *
  80.  *  |               +---+---+---+                         *
  81.  *  +--------------------+                                *
  82.  *                                                        *
  83. **********************************************************/
  84. struct TR_ray
  85. {
  86.  float tr_start[V_LNG_VECTOR];              /* origin of the ray */
  87.  float tr_codirected[V_LNG_VECTOR];         /* a co-directed vector */
  88. };
  89. /**********************************************************
  90.  * Sphere. Represented through centre and radius.         *
  91.  *                                                        *
  92.  *  +--------------------+                                *
  93.  *  | tr_type            |            TR_SPHERE           *
  94.  *  |             +-------------+                         *
  95.  *  | tr_material | TR_material |                         *
  96.  *  |             +-------------+                         *
  97.  *  |                    |                                *
  98.  *  |               +---+---+---+                         *
  99.  *  | tr_centre     | X | Y | Z |                         *
  100.  *  |               +---+---+---+                         *
  101.  *  | tr_radius          |                                *
  102.  *  +--------------------+                                *
  103.  *                                                        *
  104. **********************************************************/
  105. #define TR_SPHERE 0x1
  106. struct TR_generic_object                    /* for storage in the world */
  107. {
  108.  int tr_type;
  109.  struct TR_matter tr_material;              /* material it is made of */
  110. };
  111. struct TR_sphere
  112. {
  113.  int tr_type;                               /* TR_SPHERE */
  114.  struct TR_matter tr_material;              /* material it is made of */
  115.  float tr_centre[V_LNG_VECTOR];             /* sphere's centre */
  116.  float tr_radius;
  117. };
  118. void TR_sphere_init(struct TR_sphere *s);
  119. float TR_sphere_intersect(struct TR_ray *r,struct TR_sphere *s);
  120. float *TR_sphere_normal(float *normal,float *where,
  121.                         struct TR_sphere *s
  122.                        );
  123. /**********************************************************
  124.  * Polygon. Represented through its vertex set.           *
  125.  *                                                        *
  126.  *  +--------------------+                                *
  127.  *  | tr_type            |                TR_POLYGON      *
  128.  *  |             +-------------+                         *
  129.  *  | tr_material | TR_material |                         *
  130.  *  |             +-------------+                         *
  131.  *  |                    |                                *
  132.  *  |             +---+---+---+                           *
  133.  *  | tr_normal   | X | Y | Z |                           *
  134.  *  |             +---+---+---+                           *
  135.  *  |                    |                                *
  136.  *  |                    |         +---+---+---+---+      *
  137.  *  | tr_edges ------------------->| A | B | C | D |      *
  138.  *  |                    |         +---+---+---+---+      *
  139.  *  |                    |         |      ...      |      *
  140.  *  |                    |         +---------------+      *
  141.  *  |                    |                                *
  142.  *  | tr_no_vertices     |         +---+---+---+          *
  143.  *  | tr_vertices ---------------->| X | Y | Z |          *
  144.  *  |                    |         +---+---+---+          *
  145.  *  |                    |         |    ...    |          *
  146.  *  +--------------------+         +-----------+          *
  147.  *                                                        *
  148. **********************************************************/
  149. #define TR_POLYGON 0x2
  150. struct TR_polygon
  151. {
  152.  int tr_type;
  153.  //多边形的材质
  154.  struct TR_matter tr_material;              /* material it is made of */
  155.  float tr_normal[V_LNG_VECTOR];
  156.  //多边形的边链表
  157.  float *tr_edges;                           /* for intersection calcs */
  158.  //起始点和中止点重合,多边形的节点总数
  159.  int tr_no_vertices;                        /* first vertex equals last */
  160.  //多边形的节点链表
  161.  float *tr_vertices;
  162. };
  163. void TR_polygon_init(struct TR_polygon *p);
  164. float TR_polygon_intersect(struct TR_ray *r,struct TR_polygon *p);
  165. float *TR_polygon_normal(float *normal,float *where,
  166.                          struct TR_polygon *p
  167.                         );
  168. /**********************************************************
  169.  * World. A set of objects, a set of lights and a viewer. *
  170.  * The latter always views along Z axis.                  *
  171.  *                                                        *
  172.  *  +--------------------+                                *
  173.  *  |               +---+---+---+                         *
  174.  *  | tr_viewer     | X | Y | Z |                         *
  175.  *  |               +---+---+---+                         *
  176.  *  |                    |                                *
  177.  *  |               +---+---+---+                         *
  178.  *  | tr_ambient    | R | G | B |                         *
  179.  *  |               +---+---+---+                         *
  180.  *  | tr_no_point_lights |                                *
  181.  *  |                    |     +---+---+-- ----+          *
  182.  *  | tr_point_lights -------->| O | O |  ...  |          *
  183.  *  |                    |     +-|-+-|-+- -----+          *
  184.  *  |                    |       V   V                    *
  185.  *  |            +----------------+ +----------------+    *
  186.  *  |            | TR_point_light | | TR_point_light |    *
  187.  *  |            +----------------+ +----------------+    *
  188.  *  | tr_no_objects      |                                *
  189.  *  |                    |     +---+---+-- ----+          *
  190.  *  | tr_object -------------->| O | O |  ...  |          *
  191.  *  |                    |     +-|-+-|-+- -----+          *
  192.  *  +--------------------+       V   V                    *
  193.  *                    +-----------+ +-----------+         *
  194.  *                    | TR_object | | TR-object |         *
  195.  *                    +-----------+ +-----------+         *
  196. **********************************************************/
  197. #define TR_MAX_POINT_LIGHTS 10
  198. #define TR_MAX_SPHERES      10
  199. struct TR_world
  200. {
  201.  float tr_ambient[V_LNG_VECTOR];            /* illumination of the world */
  202.  int tr_no_point_lights;
  203.  struct TR_point_light **tr_point_lights;
  204.  int tr_no_objects;                         /* number of objects */
  205.  struct TR_generic_object **tr_objects;
  206. };
  207. #define TR_AMBIENT  0x1                     /* only ambient illumination */
  208. #define TR_DIFFUSE  0x2                     /* only diffuse illumination */
  209. #define TR_SPECULAR 0x4                     /* plus all of the above */
  210. #define TR_SHADOW   0x8                     /* shoot shadow rays */
  211. #define TR_REFLECT  0x10                    /* shoot reflection rays */
  212. void TR_init_rendering(int type);
  213. void TR_set_camera(float viewer_x,float viewer_y,float viewer_z,
  214.                    float screen_x,float screen_y,float screen_z,
  215.                    float screen_ux,float screen_uy,float screen_uz,
  216.                    float screen_vx,float screen_vy,float screen_vz
  217.                   );
  218. void TR_init_world(struct TR_world *w);
  219. void TR_trace_world(struct TR_world *w,int depth);
  220. /**********************************************************/
  221. #endif