summaryrefslogtreecommitdiffhomepage
path: root/nGenEx/Water.cpp
blob: 41fa2f3e08f75ca5a4f96ecc33fb60f3bea4701e (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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*  Project nGenEx
    Destroyer Studios LLC
    Copyright © 1997-2006. All Rights Reserved.

    SUBSYSTEM:    nGen.lib
    FILE:         Water.cpp
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Water surface effect w/ reflection and caustics
*/

#include "MemDebug.h"
#include "Water.h"
#include "Random.h"

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

struct WATER_REFRACT
{
    // Vrefract = (V + refract * N) * norm
    float refract;
    float refractNorm; 
    DWORD diffuse;
};

struct WATER_SURFACE
{
    float height;
    Vec3  normal;
};

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

#if defined(_X86) && !defined(_WIN64)
inline int f2i(float flt) 
{
	volatile int n; 

	__asm 
	{
		fld flt
		fistp n
	}

	return n;
}
#else
inline int f2i(float flt) 
{
	return (int) flt;
}
#endif


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

static WATER_REFRACT RefractionTable[512];
static bool refractInit = false;

static const int     WAVE_SIZE = 256;
static const DWORD   WAVE_MASK = 0xff;

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

Water::Water()
   :  size(0), depth(0), scaleTex(1), avgHeight(0),
      nVertices(0), surface(0), waves(0)
{
}

Water::~Water()
{
   delete [] surface;
   delete [] waves;
}

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

void
Water::Init(int n, float s, float d)
{
   size     = s;
   depth    = d;
   scaleTex = 1/size;

   // Calculate number of vertices
   nVertices = n;

   // Create refraction table
   if (!refractInit) {
      WATER_REFRACT* refract = &RefractionTable[256];

      for (UINT u = 0; u < 256; u++) {        
         float fCos0 = (float) u / (float) 256.0f;
         float f0    = acosf(fCos0);
         float fSin0 = sinf(f0);

         float fSin1 = fSin0 / 1.333f; // water
         float f1    = asinf(fSin1);
         float fCos1 = cosf(f1);

         refract[u].refract = fSin0 / fSin1 * fCos1 - fCos0;
         refract[u].refractNorm = - fSin1 / fSin0;
         refract[u].diffuse = ((((0xff - u)*(0xff - u)*(0xff - u)) << 8) & 0xff000000);

         RefractionTable[u] = RefractionTable[256];
      }

      refractInit = true;
   }

   // Create maps
   if (surface)
      delete [] surface;

   surface = new(__FILE__,__LINE__) WATER_SURFACE[n*n];
   ZeroMemory(surface, n*n * sizeof(WATER_SURFACE));

   if (waves)
      delete [] waves;

   waves = new(__FILE__,__LINE__) float[WAVE_SIZE*4];

   double f = 1.0 / (double) WAVE_SIZE;
   for (int i = 0; i < WAVE_SIZE; i++) {
      double s0 = sin(2*PI*i*f);
      double s1 = sin(4*PI*i*f);
      double s2 = sin(6*PI*i*f);
      double s3 = sin(8*PI*i*f);

      waves[0*WAVE_SIZE + i] = (float) (1.8 * s0*s0 - 0.9);
      waves[1*WAVE_SIZE + i] = (float) (1.6 * s1*s1 - 0.8);
      waves[2*WAVE_SIZE + i] = (float) (0.4 * s2);
      waves[3*WAVE_SIZE + i] = (float) (0.8 * s3*s3 - 0.4);
   }

   for (i = 0; i < 4; i++) {
      offsets[i]   = (float) Random(0, WAVE_SIZE);
   }

   offsets[4] = 12.45f;
   offsets[5] = 14.23f;
   offsets[6] = 16.72f;
   offsets[7] = 20.31f;
}

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

void
Water::CalcWaves(double seconds)
{
   int  i, n[4];
   UINT SIZE = nVertices;
   UINT STEP = WAVE_SIZE / (SIZE-1);
   UINT STEP2 = STEP/2;
   UINT AREA = SIZE * SIZE;
   UINT x, y;

   for (i = 0; i < 4; i++) {
      n[i] = (int) offsets[i];
   }

   WATER_SURFACE* pSurf = surface;

   // compute heights
   for (y = 0; y < SIZE; y++) {
      for (x = 0; x < SIZE; x++) {
         float h = 0;
         h += waves[ ((n[0] + x*STEP
                            - y*STEP2) & WAVE_MASK) + 0*WAVE_SIZE ];
         h += waves[ ((n[1] + x*STEP2
                            + y*STEP)  & WAVE_MASK) + 1*WAVE_SIZE ];
         h += waves[ ((n[2] + x*STEP)  & WAVE_MASK) + 2*WAVE_SIZE ];
         h += waves[ ((n[3] + y*STEP)  & WAVE_MASK) + 3*WAVE_SIZE ];

         pSurf->height = h * depth;
         pSurf++;
      }
   }

   // compute normals
   UINT uXN, uX0, uXP;
   UINT uYN, uY0, uYP;

   uYP = AREA - SIZE;
   uY0 = 0;
   uYN = SIZE;

   for (y = 0; y < SIZE; y++) {
      uXP = SIZE - 1;
      uX0 = 0;
      uXN = 1;

      for (x = 0; x < SIZE; x++) {
         Vec3  vecN;
         float f;

         f = surface[uXN + uYN].height - surface[uXP + uYP].height; vecN.x = vecN.z = f;           
         f = surface[uX0 + uYN].height - surface[uX0 + uYP].height; vecN.z += f;
         f = surface[uXP + uYN].height - surface[uXN + uYP].height; vecN.x -= f; vecN.z += f;
         f = surface[uXN + uY0].height - surface[uXP + uY0].height; vecN.x += f;

         vecN.y = -15.0f * depth;
         vecN.Normalize();

         surface[uX0 + uY0].normal = vecN * -1.0f;

         uXP = uX0;
         uX0 = uXN;
         uXN = (uXN + 1) % SIZE;
      }

      uYP = uY0;
      uY0 = uYN;
      uYN = (uYN + SIZE) % AREA;
   }

   // update offsets
   for (i = 0; i < 4; i++) {
      offsets[i] += (float) (offsets[i+4] * seconds);

      if (offsets[i] > WAVE_SIZE)
         offsets[i] -= WAVE_SIZE;
   }

}

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

void
Water::UpdateSurface(Vec3& eyePos, VertexSet* vset)
{
   UINT SIZE = nVertices;
   UINT AREA = SIZE * SIZE;
   UINT x, y;
   
   WATER_SURFACE* pSurf = surface;
   Vec3*          pLoc  = vset->loc;
   Vec3*          pNorm = vset->nrm;
   DWORD*         pDiff = vset->diffuse;
   float*         pTu   = vset->tu;
   float*         pTv   = vset->tv;

   float          fInc  = 1.0f / (float) (SIZE-1);
   float          fx    = 0.0f;
   float          fz    = 0.0f;

   for (y = 0; y < SIZE; y++) {
      for (x = 0; x < SIZE; x++) {
         // update vertex height and normal
         pLoc->y += pSurf->height;
         *pNorm  =  pSurf->normal;

         /*
         // Update texture coords and diffuse based upon refraction
         Vec3 vec = eyePos - *pLoc;
         vec.Normalize();

         WATER_REFRACT *pRefract;
         pRefract = RefractionTable + 256 + f2i(vec.dot(*pNorm) * 255.0f);

         *pDiff = pRefract->diffuse;

         // compute apparent displacement
         Vec3 vecD = (pSurf->normal * pRefract->refract + vec) * pRefract->refractNorm;
         Vec3 vecP = *pLoc;
         vecP.y -= depth;

         // perturb texture coords
         float fB = vecD * vecP * 2.0f;
         float fD = fB * fB - depth;
         float fScale = (-fB + sqrtf(fD)) * 0.5f;

         *pTu = vecD.x * fScale + fx;
         *pTv = vecD.z * fScale + fz;
         */

         fx += fInc;
         pSurf++;
         pLoc++;
         pNorm++;
         pDiff++;
         pTu++;
         pTv++;
      }

      fx =  0.0f;
      fz += fInc;
   }
}