-
Notifications
You must be signed in to change notification settings - Fork 0
/
Water.cs
91 lines (71 loc) · 3.13 KB
/
Water.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using System.Drawing;
using OpenTK.Graphics.OpenGL;
using WaterSimulation.ShaderPrograms;
namespace WaterSimulation
{
public class Water : RenderedEntity
{
private FrameBuffer reflection;
private FrameBuffer refraction;
string normalMap;
public Water(Vector3 position, Color color, Vector2 imgSize, string dudv, Vector3 scale = default(Vector3)) : base(position, new Vector3(-90,0,0), scale, "Quad", "Water", dudv, null)
{
reflection = new FrameBuffer(imgSize, "reflection", false);
refraction = new FrameBuffer(imgSize, "refraction", true);
}
public override void Draw()
{
GL.Enable(EnableCap.ClipDistance0);
float dist = 2 * (EngineCore.gameCamera.position.Y - position.Y);
EngineCore.gameCamera.position.Y -= dist;
EngineCore.gameCamera.rotation.X *= -1;
EngineCore.gameCamera.CalculateMatrices(false);
EngineCore.shaders["Default"].SetClippingPlane(new Vector4(0, 1, 0, -position.Y+0.1f));
reflection.Bind();
EngineCore.RenderWithout(this);
reflection.UnBind();
EngineCore.gameCamera.position.Y += dist;
EngineCore.gameCamera.rotation.X *= -1;
EngineCore.gameCamera.CalculateMatrices(false);
EngineCore.shaders["Default"].SetClippingPlane(new Vector4(0, -1, 0, position.Y+0.1f));
refraction.Bind();
EngineCore.RenderWithout(this);
refraction.UnBind();
EngineCore.shaders["Default"].SetClippingPlane(new Vector4(0, 0, 0, 0));
GL.Disable(EnableCap.ClipDistance0);
GL.UseProgram(GetShader().ProgramID);
GL.BindVertexArray(GetMesh().vaoID);
GetShader().currentObject = this;
GetShader().Start();
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, reflection.textureAttachment);
GL.ActiveTexture(TextureUnit.Texture1);
GL.BindTexture(TextureTarget.Texture2D, refraction.textureAttachment);
GL.ActiveTexture(TextureUnit.Texture2);
GL.BindTexture(TextureTarget.Texture2D, GetTexture().texID); //DuDv Map
GL.ActiveTexture(TextureUnit.Texture3);
GL.BindTexture(TextureTarget.Texture2D, GetNormalMap().texID);
GL.ActiveTexture(TextureUnit.Texture4);
GL.BindTexture(TextureTarget.Texture2D, refraction.depthTextureAttachment);
GL.DrawElements(PrimitiveType.Triangles, GetMesh().indices.Count, DrawElementsType.UnsignedInt, 0);
GetShader().Stop();
GL.BindVertexArray(0);
}
public bool AttachNormalMap(string normal)
{
if (EngineCore.images.ContainsKey(normal))
normalMap = normal;
else
return false;
return true;
}
//Getters
public Texture GetNormalMap() { return EngineCore.images[normalMap]; }
}
}