summaryrefslogtreecommitdiffhomepage
path: root/nGenEx/Shadow.cpp
blob: 1e7eb96949ceaf366d7c91634f6afac3ce943aa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*  Project nGenEx
    Destroyer Studios LLC
    Copyright © 1997-2004. All Rights Reserved.

    SUBSYSTEM:    nGenEx.lib
    FILE:         Shadow.cpp
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Dynamic Stencil Shadow Volumes
*/

#include "MemDebug.h"
#include "Shadow.h"
#include "Light.h"
#include "Solid.h"
#include "Scene.h"
#include "Video.h"

static bool visible_shadow_volumes = false;

// +--------------------------------------------------------------------+

Shadow::Shadow(Solid* s)
   : verts(0), nverts(0), max_verts(0), edges(0), num_edges(0), enabled(true)
{
   solid = s;

   if (solid && solid->GetModel()) {
      Model*   model       = solid->GetModel();
      int      npolys      = model->NumPolys();

      max_verts = model->NumVerts() * 4;
      verts     = new(__FILE__,__LINE__) Vec3[max_verts];
      edges     = new(__FILE__,__LINE__) WORD[npolys * 6];
   }
}

// +--------------------------------------------------------------------+

Shadow::~Shadow()
{
   if (verts)  delete [] verts;
   if (edges)  delete [] edges;
}

// +--------------------------------------------------------------------+

void
Shadow::Reset()
{
   num_edges   = 0;
   nverts      = 0;
}

// +--------------------------------------------------------------------+

void
Shadow::Render(Video* video)
{
   if (enabled)
      video->DrawShadow(solid, nverts, verts, visible_shadow_volumes);
}

// +--------------------------------------------------------------------+

void
Shadow::Update(Light* light)
{
   Reset();

   if (!light || !solid || !solid->GetModel() || !edges) return;

   Vec3     lpos        = light->Location();
   bool     directional = light->Type() == Light::LIGHT_DIRECTIONAL;
   Model*   model       = solid->GetModel();

   ListIter<Surface> iter = model->GetSurfaces();
   while (++iter) {
      Surface* s = iter.value();

      // transform light location into surface object space
      Matrix xform(solid->Orientation()); // XXX should be: (s->GetOrientation());

      Vec3 tmp = light->Location();

      if (!directional)
         tmp -= (solid->Location() + s->GetOffset());

      lpos.x = tmp * Vec3(xform(0,0), xform(0,1), xform(0,2));
      lpos.y = tmp * Vec3(xform(1,0), xform(1,1), xform(1,2));
      lpos.z = tmp * Vec3(xform(2,0), xform(2,1), xform(2,2));

      // compute the silohuette for the mesh with respect to the light:
      
      for (int i = 0; i < s->NumPolys(); i++) {
         Poly* p = s->GetPolys() + i;

         // skip polys with non-shadowing materials:
         if (p->material && !p->material->shadow)
            continue;

         // if this poly faces the light:
         if (p->plane.normal * lpos > 0) {
            for (int n = 0; n < p->nverts; n++) {
               if (n < p->nverts-1)
                  AddEdge(p->vlocs[n], p->vlocs[n+1]);
               else
                  AddEdge(p->vlocs[n], p->vlocs[0]);
            }
         }
      }

      // extrude the silohuette away from the light source
      // to create the shadow volume:

      Vec3 extent = lpos * -1;
      extent.Normalize();
      extent *= 50.0e3f; //solid->Radius() * 2.1f;

      for (int i = 0; i < (int) num_edges; i++) {
         if (nverts+6 <= max_verts) {
            Vec3 v1 = s->GetVLoc()[edges[2*i+0]];
            Vec3 v2 = s->GetVLoc()[edges[2*i+1]];
            Vec3 v3 = v1 + extent;
            Vec3 v4 = v2 + extent;

            verts[nverts++] = v1;
            verts[nverts++] = v2;
            verts[nverts++] = v3;

            verts[nverts++] = v2;
            verts[nverts++] = v4;
            verts[nverts++] = v3;
         }
      }
   }
}

void
Shadow::AddEdge(WORD v1, WORD v2)
{
   // Remove interior edges (which appear in the list twice)
   for (DWORD i = 0; i < num_edges; i++) {
      if ((edges[2*i+0] == v1 && edges[2*i+1] == v2) ||
          (edges[2*i+0] == v2 && edges[2*i+1] == v1))
      {
         if (num_edges > 1) {
            edges[2*i+0] = edges[2*(num_edges-1)+0];
            edges[2*i+1] = edges[2*(num_edges-1)+1];
         }

         num_edges--;
         return;
      }
   }

   edges[2*num_edges+0] = v1;
   edges[2*num_edges+1] = v2;

   num_edges++;
}

bool
Shadow::GetVisibleShadowVolumes()
{
   return visible_shadow_volumes;
}

void
Shadow::SetVisibleShadowVolumes(bool vis)
{
   visible_shadow_volumes = vis;
}