aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/plush/CLIP.C
blob: 5cbd06febbf41d99f76afd405395650b5a86ed7a (plain) (blame)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/******************************************************************************
Plush Version 1.2
clip.c
3D Frustum Clipping
Copyright (c) 1996-2000, Justin Frankel
******************************************************************************/

#include "plush.h"

#define NUM_CLIP_PLANES 5

typedef struct
{
  pl_Vertex newVertices[8];
  double Shades[8];
  double MappingU[8];
  double MappingV[8];
  double eMappingU[8];
  double eMappingV[8];
} _clipInfo;


static _clipInfo m_cl[2];


static double m_clipPlanes[NUM_CLIP_PLANES][4];
static pl_Cam *m_cam;
static pl_sInt32 m_cx, m_cy;
static double m_fov;
static double m_adj_asp;

static void _FindNormal(double x2, double x3,
                        double y2, double y3,
                        double zv,
                        double *res);

 /* Returns: 0 if nothing gets in,  1 or 2 if pout1 & pout2 get in */
static pl_uInt _ClipToPlane(pl_uInt numVerts, double *plane);

void plClipSetFrustum(pl_Cam *cam) {
  m_adj_asp = 1.0 / cam->AspectRatio;
  m_fov = plMin(plMax(cam->Fov,1.0),179.0);
  m_fov = (1.0/tan(m_fov*(PL_PI/360.0)))*(double) (cam->ClipRight-cam->ClipLeft);
  m_cx = cam->CenterX<<20;
  m_cy = cam->CenterY<<20;
  m_cam = cam;
  memset(m_clipPlanes,0,sizeof(m_clipPlanes));

  /* Back */
  m_clipPlanes[0][2] = -1.0;
  m_clipPlanes[0][3] = -cam->ClipBack;

  /* Left */
  m_clipPlanes[1][3] = 0.00000001;
  if (cam->ClipLeft == cam->CenterX) {
    m_clipPlanes[1][0] = 1.0;
  }
  else _FindNormal(-100,-100,
                100, -100,
                m_fov*-100.0/(cam->ClipLeft-cam->CenterX),
                m_clipPlanes[1]);
  if (cam->ClipLeft > cam->CenterX) {
    m_clipPlanes[1][0] = -m_clipPlanes[1][0];
    m_clipPlanes[1][1] = -m_clipPlanes[1][1];
    m_clipPlanes[1][2] = -m_clipPlanes[1][2];
  }

  /* Right */
  m_clipPlanes[2][3] = 0.00000001;
  if (cam->ClipRight == cam->CenterX) {
    m_clipPlanes[2][0] = -1.0;
  }
  else _FindNormal(100,100,
                -100, 100,
                m_fov*100.0/(cam->ClipRight-cam->CenterX),
                m_clipPlanes[2]);
  if (cam->ClipRight < cam->CenterX) {
    m_clipPlanes[2][0] = -m_clipPlanes[2][0];
    m_clipPlanes[2][1] = -m_clipPlanes[2][1];
    m_clipPlanes[2][2] = -m_clipPlanes[2][2];
  }
  /* Top */
  m_clipPlanes[3][3] = 0.00000001;
  if (cam->ClipTop == cam->CenterY) {
    m_clipPlanes[3][1] = -1.0;
  } else _FindNormal(100, -100,
                100, 100,
                m_fov*m_adj_asp*100.0/(cam->CenterY-cam->ClipTop),
                m_clipPlanes[3]);
  if (cam->ClipTop > cam->CenterY) {
    m_clipPlanes[3][0] = -m_clipPlanes[3][0];
    m_clipPlanes[3][1] = -m_clipPlanes[3][1];
    m_clipPlanes[3][2] = -m_clipPlanes[3][2];
  }

  /* Bottom */
  m_clipPlanes[4][3] = 0.00000001;
  if (cam->ClipBottom == cam->CenterY) {
    m_clipPlanes[4][1] = 1.0;
  } else _FindNormal(-100, 100,
                -100, -100,
                m_fov*m_adj_asp*-100.0/(cam->CenterY-cam->ClipBottom),
                m_clipPlanes[4]);
  if (cam->ClipBottom < cam->CenterY) {
    m_clipPlanes[4][0] = -m_clipPlanes[4][0];
    m_clipPlanes[4][1] = -m_clipPlanes[4][1];
    m_clipPlanes[4][2] = -m_clipPlanes[4][2];
  }
}


void plClipRenderFace(pl_Face *face) {
  pl_uInt k, a, w, numVerts, q;
  double tmp, tmp2;
  pl_Face newface;

  for (a = 0; a < 3; a ++) {
    m_cl[0].newVertices[a] = *(face->Vertices[a]);
    m_cl[0].Shades[a] = face->Shades[a];
    m_cl[0].MappingU[a] = face->MappingU[a];
    m_cl[0].MappingV[a] = face->MappingV[a];
    m_cl[0].eMappingU[a] = face->eMappingU[a];
    m_cl[0].eMappingV[a] = face->eMappingV[a];
  }

  numVerts = 3;
  q = 0;
  a = (m_clipPlanes[0][3] < 0.0 ? 0 : 1);
  while (a < NUM_CLIP_PLANES && numVerts > 2)
  {
    numVerts = _ClipToPlane(numVerts, m_clipPlanes[a]);
    memcpy(&m_cl[0],&m_cl[1],sizeof(m_cl)/2);
    a++;
  }
  if (numVerts > 2) {
    memcpy(&newface,face,sizeof(pl_Face));
    for (k = 2; k < numVerts; k ++) {
      newface.fShade = plMax(0,plMin(face->fShade,1));
      for (a = 0; a < 3; a ++) {
        if (a == 0) w = 0;
        else w = a+(k-2);
        newface.Vertices[a] = m_cl[0].newVertices+w;
        newface.Shades[a] = (pl_Float) m_cl[0].Shades[w];
        newface.MappingU[a] = (pl_sInt32)m_cl[0].MappingU[w];
        newface.MappingV[a] = (pl_sInt32)m_cl[0].MappingV[w];
        newface.eMappingU[a] = (pl_sInt32)m_cl[0].eMappingU[w];
        newface.eMappingV[a] = (pl_sInt32)m_cl[0].eMappingV[w];
        newface.Scrz[a] = 1.0f/newface.Vertices[a]->xformedz;
        tmp2 = m_fov * newface.Scrz[a];
        tmp = tmp2*newface.Vertices[a]->xformedx;
        tmp2 *= newface.Vertices[a]->xformedy;
        newface.Scrx[a] = m_cx + ((pl_sInt32)((tmp*(float) (1<<20))));
        newface.Scry[a] = m_cy - ((pl_sInt32)((tmp2*m_adj_asp*(float) (1<<20))));
      }
      newface.Material->_PutFace(m_cam,&newface);
      plRender_TriStats[3] ++;
    }
    plRender_TriStats[2] ++;
  }
}

pl_sInt plClipNeeded(pl_Face *face) {
  double dr,dl,db,dt;
  double f;
  dr = (m_cam->ClipRight-m_cam->CenterX);
  dl = (m_cam->ClipLeft-m_cam->CenterX);
  db = (m_cam->ClipBottom-m_cam->CenterY);
  dt = (m_cam->ClipTop-m_cam->CenterY);
  f = m_fov*m_adj_asp;
  return ((m_cam->ClipBack <= 0.0 ||
           face->Vertices[0]->xformedz <= m_cam->ClipBack ||
           face->Vertices[1]->xformedz <= m_cam->ClipBack ||
           face->Vertices[2]->xformedz <= m_cam->ClipBack) &&
          (face->Vertices[0]->xformedz >= 0 ||
           face->Vertices[1]->xformedz >= 0 ||
           face->Vertices[2]->xformedz >= 0) &&
          (face->Vertices[0]->xformedx*m_fov<=dr*face->Vertices[0]->xformedz ||
           face->Vertices[1]->xformedx*m_fov<=dr*face->Vertices[1]->xformedz ||
           face->Vertices[2]->xformedx*m_fov<=dr*face->Vertices[2]->xformedz) &&
          (face->Vertices[0]->xformedx*m_fov>=dl*face->Vertices[0]->xformedz ||
           face->Vertices[1]->xformedx*m_fov>=dl*face->Vertices[1]->xformedz ||
           face->Vertices[2]->xformedx*m_fov>=dl*face->Vertices[2]->xformedz) &&
          (face->Vertices[0]->xformedy*f<=db*face->Vertices[0]->xformedz ||
           face->Vertices[1]->xformedy*f<=db*face->Vertices[1]->xformedz ||
           face->Vertices[2]->xformedy*f<=db*face->Vertices[2]->xformedz) &&
          (face->Vertices[0]->xformedy*f>=dt*face->Vertices[0]->xformedz ||
           face->Vertices[1]->xformedy*f>=dt*face->Vertices[1]->xformedz ||
           face->Vertices[2]->xformedy*f>=dt*face->Vertices[2]->xformedz));
}



static void _FindNormal(double x2, double x3,double y2, double y3,
                        double zv, double *res) {
  res[0] = zv*(y2-y3);
  res[1] = zv*(x3-x2);
  res[2] = x2*y3 - y2*x3;
}

 /* Returns: 0 if nothing gets in,  1 or 2 if pout1 & pout2 get in */
static pl_uInt _ClipToPlane(pl_uInt numVerts, double *plane)
{
  pl_uInt i, nextvert, curin, nextin;
  double curdot, nextdot, scale;
  pl_uInt invert, outvert;
  invert = 0;
  outvert = 0;
  curdot = m_cl[0].newVertices[0].xformedx*plane[0] +
           m_cl[0].newVertices[0].xformedy*plane[1] +
           m_cl[0].newVertices[0].xformedz*plane[2];
  curin = (curdot >= plane[3]);

  for (i=0 ; i < numVerts; i++) {
    nextvert = (i + 1) % numVerts;
    if (curin) {
      m_cl[1].Shades[outvert] = m_cl[0].Shades[invert];
      m_cl[1].MappingU[outvert] = m_cl[0].MappingU[invert];
      m_cl[1].MappingV[outvert] = m_cl[0].MappingV[invert];
      m_cl[1].eMappingU[outvert] = m_cl[0].eMappingU[invert];
      m_cl[1].eMappingV[outvert] = m_cl[0].eMappingV[invert];
      m_cl[1].newVertices[outvert++] = m_cl[0].newVertices[invert];
    }
    nextdot = m_cl[0].newVertices[nextvert].xformedx*plane[0] +
              m_cl[0].newVertices[nextvert].xformedy*plane[1] +
              m_cl[0].newVertices[nextvert].xformedz*plane[2];
    nextin = (nextdot >= plane[3]);
    if (curin != nextin) {
      scale = (plane[3] - curdot) / (nextdot - curdot);
      m_cl[1].newVertices[outvert].xformedx = (pl_Float) (m_cl[0].newVertices[invert].xformedx +
           (m_cl[0].newVertices[nextvert].xformedx - m_cl[0].newVertices[invert].xformedx)
             * scale);
      m_cl[1].newVertices[outvert].xformedy = (pl_Float) (m_cl[0].newVertices[invert].xformedy +
           (m_cl[0].newVertices[nextvert].xformedy - m_cl[0].newVertices[invert].xformedy)
             * scale);
      m_cl[1].newVertices[outvert].xformedz = (pl_Float) (m_cl[0].newVertices[invert].xformedz +
           (m_cl[0].newVertices[nextvert].xformedz - m_cl[0].newVertices[invert].xformedz)
             * scale);
      m_cl[1].Shades[outvert] = m_cl[0].Shades[invert] +
                        (m_cl[0].Shades[nextvert] - m_cl[0].Shades[invert]) * scale;
      m_cl[1].MappingU[outvert] = m_cl[0].MappingU[invert] +
           (m_cl[0].MappingU[nextvert] - m_cl[0].MappingU[invert]) * scale;
      m_cl[1].MappingV[outvert] = m_cl[0].MappingV[invert] +
           (m_cl[0].MappingV[nextvert] - m_cl[0].MappingV[invert]) * scale;
      m_cl[1].eMappingU[outvert] = m_cl[0].eMappingU[invert] +
           (m_cl[0].eMappingU[nextvert] - m_cl[0].eMappingU[invert]) * scale;
      m_cl[1].eMappingV[outvert] = m_cl[0].eMappingV[invert] +
           (m_cl[0].eMappingV[nextvert] - m_cl[0].eMappingV[invert]) * scale;
      outvert++;
    }
    curdot = nextdot;
    curin = nextin;
    invert++;
  }
  return outvert;
}