[r287]: branches / Axiom_Upgrade_0.8.0 / mvsm / OceanPage.cs  Maximize  Restore  History

Download this file

423 lines (347 with data), 17.3 kB

  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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using Axiom.Core;
using Axiom.Graphics;
using Axiom.Math;
namespace Axiom.SceneManagers.Multiverse
{
public class OceanPage : SimpleRenderable
{
private AxisAlignedBox waveBounds;
private new VertexData vertexData = null;
private new IndexData indexData;
private int innerWaveDistance = 150;
private int outerWaveDistance = 256;
private int outerViewDistance = 1280;
private int innerMetersPerSample = 4;
private int outerMetersPerSample = 32;
private bool useShaders;
protected OceanConfig config;
#region Buffer Management Methods
public OceanPage(OceanConfig config)
{
this.config = config;
config.ConfigChange += new ConfigChangeHandler(ConfigChanged);
material = (Material)MaterialManager.Instance.GetByName("MVSMOcean");
if (material == null)
{
config.ShowOcean = false;
return;
}
material.Load();
// if the shader technique is supported, then use shaders
useShaders = material.GetTechnique(0).IsSupported;
// build the vertex buffer
BuildVertexBuffer();
// build the index buffer
BuildIndexBuffer();
// force update of shader variables in material
UpdateMaterial();
UpdateBounds();
CastShadows = false;
}
private float WaveAmpScale(int x, int z)
{
float ret = 1;
x = System.Math.Abs(x);
z = System.Math.Abs(z);
int max = System.Math.Max(x, z);
if ((max > innerWaveDistance) && (max < outerWaveDistance))
{
ret = ((float)(outerWaveDistance - max)) / (float)((outerWaveDistance - innerWaveDistance));
}
if (max >= outerWaveDistance)
{
ret = 0;
}
return ret;
}
private unsafe void outerVertex(float* pData, int x, int z)
{
*pData++ = x * TerrainManager.oneMeter;
if (useShaders)
{
*pData++ = 0;
}
else
{
// if we are not using a shader, then set y coord to sea level
*pData = config.SeaLevel * TerrainManager.oneMeter;
}
*pData++ = z * TerrainManager.oneMeter;
*pData++ = x;
*pData++ = z;
}
private int numVerticesForPlane(int x1, int x2, int y1, int y2, int metersPerSample)
{
int x = x2 - x1;
int y = y2 - y1;
return ((x / metersPerSample) + 1) * ((y / metersPerSample) + 1);
}
private int numIndicesForPlane(int x1, int x2, int y1, int y2, int metersPerSample)
{
int x = x2 - x1;
int y = y2 - y1;
return ((x / metersPerSample)) * ((y / metersPerSample)) * 6;
}
private unsafe float* fillVertexPlane(float* pData, int x1, int x2, int y1, int y2, int metersPerSample)
{
for (int y = y1; y <= y2; y += metersPerSample)
{
for (int x = x1; x <= x2; x += metersPerSample)
{
// position
*pData++ = x * TerrainManager.oneMeter;
if (useShaders)
{
*pData++ = WaveAmpScale(x, y);
}
else
{
*pData++ = config.SeaLevel * TerrainManager.oneMeter;
}
*pData++ = y * TerrainManager.oneMeter;
// texture coords
*pData++ = x;
*pData++ = y; // 1 - y ??
}
}
return pData;
}
private unsafe short* fillIndexPlane(short* pIndex, short startIndex, int x1, int x2, int y1, int y2, int metersPerSample)
{
// make tris in a zigzag pattern (strip compatible)
int width = (short)((x2 - x1) / metersPerSample);
int height = (short)((y2 - y1) / metersPerSample);
int stride = width + 1;
short v1, v2, v3;
for (int v = 0; v < height; v++)
{
for (int u = 0; u < width; u++)
{
// First Tri in cell
// -----------------
v1 = (short)(((v + 1) * stride) + u);
v2 = (short)(((v + 1) * stride) + (u + 1));
v3 = (short)((v * stride) + u);
// Output indexes
*pIndex++ = (short)(v1 + startIndex);
*pIndex++ = (short)(v2 + startIndex);
*pIndex++ = (short)(v3 + startIndex);
// Second Tri in cell
// ------------------
v1 = (short)(((v + 1) * stride) + (u + 1));
v2 = (short)((v * stride) + (u + 1));
v3 = (short)((v * stride) + u);
// Output indexes
*pIndex++ = (short)(v1 + startIndex);
*pIndex++ = (short)(v2 + startIndex);
*pIndex++ = (short)(v3 + startIndex);
}
}
return pIndex;
}
private void BuildVertexBuffer()
{
if (vertexData != null)
{
// if we already have a buffer, free the hardware buffer
vertexData.vertexBufferBinding.GetBuffer(0).Dispose();
}
//
// create vertexData object
//
vertexData = new VertexData();
//
// Set up the vertex declaration
//
VertexDeclaration vertexDecl = vertexData.vertexDeclaration;
int currOffset = 0;
// add position data
vertexDecl.AddElement(0, currOffset, VertexElementType.Float3, VertexElementSemantic.Position);
currOffset += VertexElement.GetTypeSize(VertexElementType.Float3);
// add texture coords
vertexDecl.AddElement(0, currOffset, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0);
currOffset += VertexElement.GetTypeSize(VertexElementType.Float2);
//
// create hardware vertex buffer
//
int innerVertexCount = numVerticesForPlane(-outerWaveDistance, outerWaveDistance, -outerWaveDistance, outerWaveDistance, innerMetersPerSample);
int outerVertexCount =
numVerticesForPlane(-outerViewDistance, outerViewDistance, -outerViewDistance, -outerWaveDistance, outerMetersPerSample) +
numVerticesForPlane(-outerViewDistance, -outerWaveDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample) +
numVerticesForPlane(outerWaveDistance, outerViewDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample) +
numVerticesForPlane(-outerViewDistance, outerViewDistance, outerWaveDistance, outerViewDistance, outerMetersPerSample);
vertexData.vertexCount = innerVertexCount + outerVertexCount;
// create a new vertex buffer (based on current API)
HardwareVertexBuffer vbuf =
HardwareBufferManager.Instance.CreateVertexBuffer(vertexDecl, vertexData.vertexCount, BufferUsage.StaticWriteOnly, false);
// HardwareBufferManager.Instance.CreateVertexBuffer(vertexDecl.GetVertexSize(0), vertexData.vertexCount, BufferUsage.StaticWriteOnly, false);
// get a reference to the vertex buffer binding
VertexBufferBinding binding = vertexData.vertexBufferBinding;
// bind the first vertex buffer
binding.SetBinding(0, vbuf);
// generate vertex data
unsafe
{
// lock the vertex buffer
IntPtr data = (IntPtr)vbuf.Lock(BufferLocking.Discard).Ptr;
float* pData = (float*)data.ToPointer();
pData = fillVertexPlane(pData, -outerWaveDistance, outerWaveDistance, -outerWaveDistance, outerWaveDistance, innerMetersPerSample);
pData = fillVertexPlane(pData, -outerViewDistance, outerViewDistance, -outerViewDistance, -outerWaveDistance, outerMetersPerSample);
pData = fillVertexPlane(pData, -outerViewDistance, -outerWaveDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample);
pData = fillVertexPlane(pData, outerWaveDistance, outerViewDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample);
pData = fillVertexPlane(pData, -outerViewDistance, outerViewDistance, outerWaveDistance, outerViewDistance, outerMetersPerSample);
// unlock the buffer
vbuf.Unlock();
} // unsafe
}
private void BuildIndexBuffer()
{
//
// create vertex and index data objects
//
indexData = new IndexData();
//
// create hardware index buffer
//
int innerIndexCount = numIndicesForPlane(-outerWaveDistance, outerWaveDistance, -outerWaveDistance, outerWaveDistance, innerMetersPerSample);
int outerIndexCount =
numIndicesForPlane(-outerViewDistance, outerViewDistance, -outerViewDistance, -outerWaveDistance, outerMetersPerSample) +
numIndicesForPlane(-outerViewDistance, -outerWaveDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample) +
numIndicesForPlane(outerWaveDistance, outerViewDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample) +
numIndicesForPlane(-outerViewDistance, outerViewDistance, outerWaveDistance, outerViewDistance, outerMetersPerSample);
indexData.indexCount = innerIndexCount + outerIndexCount;
// create the index buffer using the current API
indexData.indexBuffer =
HardwareBufferManager.Instance.CreateIndexBuffer(IndexType.Size16, indexData.indexCount, BufferUsage.StaticWriteOnly, false);
// short v1, v2, v3; (unused)
// grab a reference for easy access
HardwareIndexBuffer idxBuffer = indexData.indexBuffer;
// lock the whole index buffer
IntPtr data = (IntPtr)idxBuffer.Lock(BufferLocking.Discard).Ptr;
int startIndex = 0;
unsafe
{
short* pIndex = (short*)data.ToPointer();
// make tris in a zigzag pattern (strip compatible)
pIndex = fillIndexPlane(pIndex, (short)startIndex, -outerWaveDistance, outerWaveDistance, -outerWaveDistance, outerWaveDistance, innerMetersPerSample);
startIndex += numVerticesForPlane(-outerWaveDistance, outerWaveDistance, -outerWaveDistance, outerWaveDistance, innerMetersPerSample);
pIndex = fillIndexPlane(pIndex, (short)startIndex, -outerViewDistance, outerViewDistance, -outerViewDistance, -outerWaveDistance, outerMetersPerSample);
startIndex += numVerticesForPlane(-outerViewDistance, outerViewDistance, -outerViewDistance, -outerWaveDistance, outerMetersPerSample);
pIndex = fillIndexPlane(pIndex, (short)startIndex, -outerViewDistance, -outerWaveDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample);
startIndex += numVerticesForPlane(-outerViewDistance, -outerWaveDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample);
pIndex = fillIndexPlane(pIndex, (short)startIndex, outerWaveDistance, outerViewDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample);
startIndex += numVerticesForPlane(outerWaveDistance, outerViewDistance, -outerWaveDistance, outerWaveDistance, outerMetersPerSample);
pIndex = fillIndexPlane(pIndex, (short)startIndex, -outerViewDistance, outerViewDistance, outerWaveDistance, outerViewDistance, outerMetersPerSample);
}
// unlock the buffer
idxBuffer.Unlock();
}
private unsafe void outerQuad(short* pIndex, short innerVertexCount, short nw, short ne, short se, short sw)
{
// First Tri in cell
// -----------------
// Output indexes
*pIndex++ = (short)(innerVertexCount + sw);
*pIndex++ = (short)(innerVertexCount + nw);
*pIndex++ = (short)(innerVertexCount + se);
// Second Tri in cell
// Output indexes
*pIndex++ = (short)(innerVertexCount + se);
*pIndex++ = (short)(innerVertexCount + nw);
*pIndex++ = (short)(innerVertexCount + ne);
}
private void UpdateBounds()
{
float radius = outerViewDistance * TerrainManager.oneMeter;
// set bounding box
waveBounds = new AxisAlignedBox(new Vector3(-radius, config.SeaLevel - config.WaveHeight, -radius),
new Vector3(radius, config.SeaLevel + config.WaveHeight, radius));
this.box = waveBounds;
}
#endregion Buffer Management Methods
protected void UpdateMaterial()
{
if (useShaders && config.UseParams)
{
GpuProgramParameters vertexParams = material.GetTechnique(0).GetPass(0).VertexProgramParameters;
vertexParams.SetNamedConstant("seaLevel", new Vector3(config.SeaLevel, 0, 0));
vertexParams.SetNamedConstant("waveAmp", new Vector3(config.WaveHeight, 0, 0));
vertexParams.SetNamedConstant("BumpScale", new Vector3(config.BumpScale, 0, 0));
vertexParams.SetNamedConstant("bumpSpeed", new Vector3(config.BumpSpeedX, config.BumpSpeedZ, 0));
vertexParams.SetNamedConstant("textureScale", new Vector3(config.TextureScaleX, config.TextureScaleZ, 0));
GpuProgramParameters fragmentParams = material.GetTechnique(0).GetPass(0).FragmentProgramParameters;
fragmentParams.SetNamedConstant("deepColor", config.DeepColor);
fragmentParams.SetNamedConstant("shallowColor", config.ShallowColor);
}
}
protected void ConfigChanged(object sender, EventArgs e)
{
UpdateMaterial();
}
#region Properties
public float WaveTime
{
set
{
// set the gpu parameter for the current time
if (useShaders)
{
material.GetTechnique(0).GetPass(0).VertexProgramParameters.SetNamedConstant("time", new Vector3(value, 0, 0));
}
}
}
#endregion Properties
public override AxisAlignedBox BoundingBox
{
get
{
return (AxisAlignedBox)waveBounds.Clone();
}
}
public override Sphere GetWorldBoundingSphere(bool derive)
{
if (derive)
{
worldBoundingSphere.Radius = this.BoundingRadius;
worldBoundingSphere.Center = parentNode.DerivedPosition;
}
else
{
worldBoundingSphere.Radius = waveBounds.Maximum.Length;
worldBoundingSphere.Center = waveBounds.Center;
}
return worldBoundingSphere;
}
public void GetRenderOperation(RenderOperation op)
{
op.useIndices = true;
op.operationType = OperationType.TriangleList;
op.vertexData = vertexData;
op.indexData = indexData;
}
public override float GetSquaredViewDepth(Camera camera)
{
// Use squared length to avoid square root
return (this.ParentNode.DerivedPosition - camera.DerivedPosition).LengthSquared;
}
public override float BoundingRadius
{
get
{
return 0f;
}
}
/// <summary>
///
/// </summary>
/// <param name="queue"></param>
public override void UpdateRenderQueue(RenderQueue queue)
{
// add ourself to the render queue
// we render late, since most of the ocean will be rendered over by terrain, and
// we will benefit from early out in the pixel shader due to z-test
queue.AddRenderable(this, 1, RenderQueueGroupID.Nine);
}
}
}