-
Notifications
You must be signed in to change notification settings - Fork 2
/
Render.cs
576 lines (475 loc) · 15.8 KB
/
Render.cs
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace ModelViewer
{
/// <summary>
/// Class to contain the methods for rendering the scene.
/// </summary>
public class Render
{
private Device device; // Copy of the rendering device.
private Device oldDevice;
private Effect effect; // Copy of the effect class.
private Texture renderedGlow; // Texture to render the emissive portions to.
private Texture renderedScene; // Texture to render the regular scene (used only for blending with glow).
private Texture shadowMap; // Texture to render the shadow map to.
private Texture shadowMap2; // Texture to render the shadow map to.
private Texture shadowMap3; // Texture to render the shadow map to.
private Texture lightTexture;
private List<Model> mesh; // A reference to the list of models.
private RenderToSurface surfRender; // Class to help render emissive passes.
private RenderToSurface shadowHelp; // Call to render to the shadow map.
private EffectHandle prevTechnique; // The previous technique for the shaders.
private int width; // Width of the screen.
private int height; // Height of the screen.
private int shadowMapSize; // Size of the shadow map
private bool showLights;
private GroundPlane groundPlane;
private Material blackMat;
private Material whiteMat;
/// <summary>
/// Constructor to initalize references and build the render targets.
/// </summary>
/// <param name="device">Copy of the rendering device.</param>
/// <param name="effect">Copy of the effect class.</param>
/// <param name="mesh">Reference of the list of models.</param>
/// <param name="width">Width of the rendering panel.</param>
/// <param name="height">Height of the rendering panel.</param>
public Render(ref Device device, ref Effect effect, ref List<Model> mesh, ref GroundPlane groundPlane, int width, int height)
{
this.device = device;
this.effect = effect;
this.mesh = mesh;
this.groundPlane = groundPlane;
this.width = width;
this.height = height;
showLights = false;
shadowMapSize = 512;
blackMat = new Material();
whiteMat = new Material();
blackMat.Ambient = blackMat.Diffuse = blackMat.Emissive = Color.Black;
whiteMat.Ambient = whiteMat.Diffuse = whiteMat.Emissive = Color.White;
// Setup the RenderToSurface class and create the render target textures.
surfRender = new RenderToSurface(device, width, height, Format.X8R8G8B8, true, DepthFormat.D16);
renderedGlow = new Texture(device, width, height, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
renderedScene = new Texture(device, width, height, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
shadowHelp = new RenderToSurface(device, shadowMapSize, shadowMapSize, Format.X8R8G8B8, true, DepthFormat.D16);
shadowMap = new Texture(device, shadowMapSize, shadowMapSize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
shadowMap2 = new Texture(device, shadowMapSize, shadowMapSize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
shadowMap3 = new Texture(device, shadowMapSize, shadowMapSize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
//lightTexture = TextureLoader.FromFile(device, "lightbulb.png");
MemoryStream loMS = new MemoryStream();
global::ModelViewer.Properties.Resources.lightbulb.Save(loMS, System.Drawing.Imaging.ImageFormat.Png);
loMS.Seek(0, 0);
lightTexture = TextureLoader.FromStream(device, loMS);
}
#region Set/Get Methods
/// <summary>
/// Set/Get method for the width.
/// </summary>
public int DeviceWidth
{
get
{
return width;
}
set
{
width = value;
}
}
/// <summary>
/// Set/Get method for the height.
/// </summary>
public int DeviceHeight
{
get
{
return height;
}
set
{
height = value;
}
}
public bool Lights
{
get
{
return showLights;
}
set
{
showLights = value;
}
}
public Device Device
{
set
{
oldDevice = device;
device = value;
RebuildRenderTargets(width, height);
RebuildShadowMaps(shadowMapSize);
}
}
public Effect Effect
{
set
{
effect = value;
}
}
public int ShadowSize
{
get
{
return shadowMapSize;
}
set
{
shadowMapSize = value;
RebuildShadowMaps(value);
}
}
public RenderToSurface SceneRender
{
get
{
return surfRender;
}
}
public RenderToSurface ShadowRender
{
get
{
return shadowHelp;
}
}
#endregion
/// <summary>
/// Rebuilds the render targets after resizing.
/// </summary>
/// <param name="width">The new width of the rendering panel.</param>
/// <param name="height">The new height of the rendering panel.</param>
public void RebuildRenderTargets(int width, int height)
{
surfRender.Dispose();
renderedGlow.Dispose();
renderedScene.Dispose();
surfRender = new RenderToSurface(device, width, height, Format.X8R8G8B8, true, DepthFormat.D16);
renderedGlow = new Texture(device, width, height, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
renderedScene = new Texture(device, width, height, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
}
public void RebuildShadowMaps(int shadowMapSize)
{
shadowHelp.Dispose();
shadowMap.Dispose();
shadowMap2.Dispose();
shadowMap3.Dispose();
shadowHelp = new RenderToSurface(device, shadowMapSize, shadowMapSize, Format.X8R8G8B8, true, DepthFormat.D16);
shadowMap = new Texture(device, shadowMapSize, shadowMapSize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
shadowMap2 = new Texture(device, shadowMapSize, shadowMapSize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
shadowMap3 = new Texture(device, shadowMapSize, shadowMapSize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
}
public void ReleaseRenderTextures()
{
if (!renderedGlow.Disposed)
renderedGlow.Dispose();
if (!renderedScene.Disposed)
renderedScene.Dispose();
if (!shadowMap.Disposed)
shadowMap.Dispose();
if (!shadowMap2.Disposed)
shadowMap2.Dispose();
if (!shadowMap3.Disposed)
shadowMap3.Dispose();
}
/// <summary>
/// Method to prepare the scene for rendering.
/// </summary>
/// <param name="background">Color of the background.</param>
public void BeginRender(Color background)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, background, 1.0f, 0);
device.BeginScene();
}
/// <summary>
/// Method to signal the end of rendering so the frame can be displayed to the screen.
/// </summary>
public void EndRender()
{
device.EndScene();
device.Present();
}
/// <summary>
/// Renders the scene.
/// </summary>
public void RenderScene(Color background, List<LightObj> lights, Camera camera)
{
bool useGlow = false;
// Sets the variables for the shader.
SetVariables(lights, camera);
//effect.Technique = "Test";
for (int i = 0; i < mesh.Count; i++)
{
if (mesh[i].Emissive.Enable && !mesh[i].Emissive.Path.Equals(""))
{
useGlow = true;
break;
}
}
// If/Else statement to control rendering with emissive glow or not.
if (useGlow)
RenderGlow();
else
BeginRender(background);
// If emissive glow is used, the base scene is rendered to a texture.
if (useGlow)
{
surfRender.BeginScene(renderedScene.GetSurfaceLevel(0), device.Viewport);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, background, 1.0f, 0);
}
if (groundPlane.Enabled)
RenderModel((Model)groundPlane, lights[0].Shadows, 0);
// Loops through all the models and renders each.
for (int i = 0; i < mesh.Count; i++)
{
if (mesh[i].Enabled)
RenderModel(mesh[i], lights[0].Shadows, 0);
}
if (showLights)
{
using (Sprite spriteobject = new Sprite(device))
{
foreach (LightObj light in lights)
{
if (!light.Enabled)
continue;
spriteobject.SetWorldViewRH(device.Transform.World, device.Transform.View);
spriteobject.Begin(SpriteFlags.AlphaBlend | SpriteFlags.Billboard | SpriteFlags.SortTexture | SpriteFlags.ObjectSpace);
//spriteobject.Transform = Matrix.Scaling(0.25f, 0.25f, 0.25f);
spriteobject.Draw(lightTexture, Rectangle.Empty, Vector3.Empty, light.Direction, Color.White);
spriteobject.End();
}
}
}
// If emissive glow is used, the textures are set to the shader and rendered to a sprite.
if (useGlow)
{
surfRender.EndScene(Filter.None);
effect.SetValue("renderedScene", renderedScene);
BeginRender(background);
using (Sprite spriteobject = new Sprite(device))
{
prevTechnique = effect.Technique;
effect.Technique = "Bloom";
effect.Begin(FX.None);
spriteobject.Begin(SpriteFlags.None);
effect.BeginPass(0);
spriteobject.Draw(renderedGlow, Rectangle.Empty, new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.White);
effect.EndPass();
spriteobject.End();
effect.End();
effect.Technique = prevTechnique;
}
}
EndRender();
}
/// <summary>
/// Sets the variables within the shader code.
/// </summary>
/// <param name="light">List of lights used.</param>
/// <param name="camera">The camera.</param>
private void SetVariables(List<LightObj> light, Camera camera)
{
// Create an array of the 3 lights intensities.
float[] lightIntensity = new float[3];
int[] enableShadows = new int[3];
int[] enableLights = new int[3];
for (int i = 0; i < 3; i++)
{
lightIntensity[i] = light[i].Intensity;
enableShadows[i] = Convert.ToInt32(light[i].Shadows);
enableLights[i] = Convert.ToInt32(light[i].Enabled);
}
// Create an array of the 3 lights positions.
Vector4[] lightPositions = new Vector4[3];
for (int i = 0; i < 3; i++)
lightPositions[i] = new Vector4(light[i].Direction.X, light[i].Direction.Y, light[i].Direction.Z, 1.0f);
Vector4[] lightAmbient = new Vector4[3];
Vector4[] lightDiffuse = new Vector4[3];
Vector4[] lightSpecular = new Vector4[3];
// Copy over the light colors to different vector4's
for (int i = 0; i < 3; i++)
{
lightAmbient[i] = new Vector4(light[i].Ambient.R / 255.0f, light[i].Ambient.G / 255.0f, light[i].Ambient.B / 255.0f, 1.0f);
lightDiffuse[i] = new Vector4(light[i].Diffuse.R / 255.0f, light[i].Diffuse.G / 255.0f, light[i].Diffuse.B / 255.0f, 1.0f);
lightSpecular[i] = new Vector4(light[i].Specular.R / 255.0f, light[i].Specular.G / 255.0f, light[i].Specular.B / 255.0f, 1.0f);
}
// Set the world, project and view matrixes in the shader
effect.SetValue("projMat", camera.Projection);
effect.SetValue("viewMat", camera.View);
effect.SetValue("worldMat", Matrix.Identity);
// Set the emissive texture
effect.SetValue("glowMap", renderedGlow);
// Sets the light and camera's position in the shader.
effect.SetValue("lightPos", lightPositions);
effect.SetValue("enableLight", enableLights);
effect.SetValue("enableShadows", enableShadows);
effect.SetValue("eyePos", new float[] { camera.Position.X, camera.Position.Y, camera.Position.Z });
// Setup the light information
effect.SetValue("lightIntensity", lightIntensity);
effect.SetValue("lightAmbientColor", lightAmbient);
effect.SetValue("lightDiffuseColor", lightDiffuse);
effect.SetValue("lightSpecularColor", lightSpecular);
}
/// <summary>
/// Method to set the texture values within the effect class and render the model.
/// </summary>
/// <param name="num">The index into the list of models.</param>
private void RenderModel(Model mesh, bool shadows, int num)
{
// Set the technique being used for this particular model.
if (shadows)
{
if (mesh.Normal.Enable && !mesh.Normal.Path.Equals(""))
{
if (Manager.GetDeviceCaps(0, DeviceType.Hardware).PixelShaderVersion > new Version(2, 0))
effect.Technique = "NormalMapShadows";
else
{
MessageBox.Show("Error: Your graphics card does not support pixel shader 2.0b", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
effect.Technique = "PerPixelShadows";
}
}
else
effect.Technique = "PerPixelShadows";
}
else
effect.Technique = mesh.Normal.Technique;
// Set the Ambient Occlusion texture.
effect.SetValue(mesh.Ambient.EffectName, mesh.Ambient.Texture);
effect.SetValue(mesh.Ambient.EffectBool, mesh.Ambient.Enable);
// Set the Diffuse texture.
effect.SetValue(mesh.Diffuse.EffectName, mesh.Diffuse.Texture);
effect.SetValue(mesh.Diffuse.EffectBool, mesh.Diffuse.Enable);
// Set the Normal/Parallax texture.
effect.SetValue(mesh.Normal.EffectName, mesh.Normal.Texture);
effect.SetValue(mesh.Normal.EffectBool, mesh.Normal.Enable);
// Set the Specular texture.
effect.SetValue(mesh.Specular.EffectName, mesh.Specular.Texture);
effect.SetValue(mesh.Specular.EffectBool, mesh.Specular.Enable);
// Begin rendering with the hlsl shader.
int numPasses = effect.Begin(FX.None);
for (int i = 0; i < numPasses; i++)
{
if (shadows)
{
switch (num)
{
case 0:
effect.SetValue("shadowMapLight1", shadowMap);
break;
case 1:
effect.SetValue("shadowMapLight2", shadowMap2);
break;
case 2:
effect.SetValue("shadowMapLight3", shadowMap3);
break;
}
}
effect.BeginPass(i);
// Draw the model.
mesh.Mesh.DrawSubset(0);
// End drawing with the hlsl shader.
effect.EndPass();
}
effect.End();
}
/// <summary>
/// Method to render the emissive pass to a render target.
/// </summary>
public void RenderGlow()
{
surfRender.BeginScene(renderedGlow.GetSurfaceLevel(0), device.Viewport);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
if (groundPlane.Enabled)
{
if (!groundPlane.Emissive.Enable)
device.Material = blackMat;
else
device.Material = whiteMat;
device.SetTexture(0, groundPlane.Emissive.Texture);
groundPlane.Mesh.DrawSubset(0);
}
for (int i = 0; i < mesh.Count; i++)
{
if (!mesh[i].Enabled)
continue;
if(!mesh[i].Emissive.Enable)
device.Material = blackMat;
else
device.Material = whiteMat;
device.SetTexture(0, mesh[i].Emissive.Texture);
mesh[i].Mesh.DrawSubset(0);
}
surfRender.EndScene(Filter.None);
}
/// <summary>
/// Method to build shadow maps.
/// </summary>
public void RenderShadowMap(LightObj light, int num, Matrix projection)
{
Matrix lightMatrix = Matrix.LookAtRH(light.Direction, new Vector3(0, 0, 0), new Vector3(0, 1, 0)) * projection;
switch (num)
{
case 0:
shadowHelp.BeginScene(shadowMap.GetSurfaceLevel(0));//, device.Viewport);
break;
case 1:
shadowHelp.BeginScene(shadowMap2.GetSurfaceLevel(0), device.Viewport);
break;
case 2:
shadowHelp.BeginScene(shadowMap3.GetSurfaceLevel(0), device.Viewport);
break;
}
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
effect.Technique = "ShadowMap";
effect.SetValue("maxDepth", light.Distance * 3);
effect.SetValue("lightWVP", lightMatrix);
effect.Begin(FX.None);
effect.BeginPass(0);
if (groundPlane.Enabled)
groundPlane.Mesh.DrawSubset(0);
for (int i = 0; i < mesh.Count; i++)
{
if (mesh[i].Enabled)
mesh[i].Mesh.DrawSubset(0);
}
effect.EndPass();
effect.End();
shadowHelp.EndScene(Filter.Point);
// shadowMap = shadowTest.
switch (num)
{
case 0:
effect.SetValue("shadowMapLight1", shadowMap);
break;
case 1:
effect.SetValue("shadowMapLight2", shadowMap2);
break;
case 2:
effect.SetValue("shadowMapLight3", shadowMap3);
break;
}
}
}
}