home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / lights.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  4KB  |  184 lines

  1. // new LIGHTING support routines for VR-386
  2. // 29/12/93 by Dave Stampe
  3.  
  4.  
  5.  
  6. /*
  7.  This code is part of the VR-386 project, created by Dave Stampe.
  8.  VR-386 is a desendent of REND386, created by Dave Stampe and
  9.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  10.  Stampre for VR-386.
  11.  
  12.  Copyright (c) 1994 by Dave Stampe:
  13.  May be freely used to write software for release into the public domain
  14.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  15.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  16.  this software or source code into their products!  Usually there is no
  17.  charge for under 50-100 items for low-cost or shareware products, and terms
  18.  are reasonable.  Any royalties are used for development, so equipment is
  19.  often acceptable payment.
  20.  
  21.  ATTRIBUTION:  If you use any part of this source code or the libraries
  22.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  23.  and any other authors in your documentation, source code, and at startup
  24.  of your program.  Let's keep the freeware ball rolling!
  25.  
  26.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  27.  REND386, improving programmer access by rewriting the code and supplying
  28.  a standard API.  If you write improvements, add new functions rather
  29.  than rewriting current functions.  This will make it possible to
  30.  include you improved code in the next API release.  YOU can help advance
  31.  VR-386.  Comments on the API are welcome.
  32.  
  33.  CONTACT: dstampe@psych.toronto.edu
  34. */
  35.  
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>  /* for atol(), only for debugging! */
  39. #include <dos.h>
  40. #include <alloc.h>
  41.  
  42. #include "vr_api.h"
  43. #include "segment.h"
  44. #include "intmath.h"
  45.  
  46.  
  47.     // create, initialize a light and its segment
  48. LIGHT *create_light(OBJECT *parent, WORD type, WORD intensity)
  49. {
  50.   SEGMENT *p = object2segment(parent);
  51.   SEGMENT *s;
  52.   LIGHT *l = calloc(sizeof(LIGHT),1);
  53.   if(!l) return NULL;
  54.   s = new_seg(p);
  55.   if(!s)
  56.     {
  57.       free(l);
  58.       return NULL;
  59.     }
  60.   register_system_segment(s);
  61.   l->seg = s;
  62.   l->type = type;
  63.   l->intensity = intensity;
  64.   return l;
  65. }
  66.  
  67.     // destroy a light and its segment
  68. void destroy_light(LIGHT *l)
  69. {
  70.   detach_segment(l->seg,0);
  71.   destroy_segment(l->seg);
  72.   free(l);
  73. }
  74.  
  75.     // returns light type
  76. WORD get_light_type(LIGHT *l)
  77. {
  78.   return l->type;
  79. }
  80.  
  81.     // returns light intensity
  82. WORD get_light_intensity(LIGHT *l)
  83. {
  84.   return l->intensity;
  85. }
  86.  
  87.     // sets intensity of light
  88. void set_light_intensity(LIGHT *l, WORD i)
  89. {
  90.   l->intensity = i;
  91. }
  92.  
  93.     // sets light type
  94. void set_light_type(LIGHT *l, WORD t)
  95. {
  96.   l->type = t;
  97. }
  98.  
  99.     // attaches light to segment
  100. BOOL attach_light(LIGHT *l, OBJECT *s, BOOL preserve)
  101. {
  102.   attach_object(l->seg, s, preserve);
  103.   return TRUE;
  104. }
  105.  
  106.     // detaches light from segment
  107. BOOL detach_light(LIGHT *l, BOOL preserve)
  108. {
  109.   detach_segment(l->seg, preserve);
  110.   return TRUE;
  111. }
  112.  
  113.     // change angle of a spotlight
  114. BOOL rotate_spotlight(LIGHT *l, POSE *p)
  115. {
  116.   seg_setpose(l->seg, p);
  117.   full_update_segment(l->seg);
  118.   l->type = SPOT_LIGHT;
  119.   return TRUE;
  120. }
  121.  
  122.     // change position of point source
  123. BOOL position_pointlight(LIGHT *l, POSE *p)
  124. {
  125.   seg_setpose(l->seg, p);
  126.   full_update_segment(l->seg);
  127.   l->type = POINT_LIGHT;
  128.   return TRUE;
  129. }
  130.  
  131.     // change pos/rot of light source
  132. BOOL set_light_pose(LIGHT *l, POSE *p)
  133. {
  134.   seg_setpose(l->seg, p);
  135.   full_update_segment(l->seg);
  136.   return TRUE;
  137. }
  138.  
  139.     // get local position/angle of light
  140. void get_light_localpose(LIGHT *l, POSE *p)
  141. {
  142.   seg_getpose(l->seg, p);
  143. }
  144.  
  145.     // get world position/angle of light
  146. void get_light_worldpose(LIGHT *l, POSE *p)
  147. {
  148.   seg_getworldpose(l->seg, p);
  149. }
  150.  
  151.     //  fetch light data for computing
  152. WORD get_light_data(LIGHT *l, COORD *xp, COORD *yp, COORD *zp, WORD *intensity)
  153. {
  154.   *intensity = l->intensity;
  155.   if(l->type==POINT_LIGHT)
  156.     {
  157.       seg_getposxyz(l->seg, xp, yp, zp);
  158.     }
  159.   else if(l->type==SPOT_LIGHT)
  160.     {
  161.       MATRIX *m = get_seg_pmatrix(l->seg);
  162.       if(xp) *xp = (*m)[0][2]; // get the Z column as the vector
  163.       if(yp) *yp = (*m)[1][2];
  164.       if(zp) *zp = (*m)[2][2];
  165.     }
  166.   return l->type;
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.