-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuz.cs
More file actions
200 lines (178 loc) · 10.5 KB
/
Copy pathLuz.cs
File metadata and controls
200 lines (178 loc) · 10.5 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
using System;
using System.Collections.Generic;
using System.Text;
using TgcViewer.Example;
using TgcViewer;
using Microsoft.DirectX.Direct3D;
using System.Drawing;
using Microsoft.DirectX;
using TgcViewer.Utils.TgcSceneLoader;
using TgcViewer.Utils.TgcGeometry;
using TgcViewer.Utils.Shaders;
using System.Windows.Forms;
namespace AlumnoEjemplos.GODMODE
{
class Luz
{
public Vector3[] posicionesDeLuces = new Vector3[4];
Effect godmodeSpotlightMultiDiffuse = TgcShaders.loadEffect(GuiController.Instance.AlumnoEjemplosDir + "GODMODE\\Media\\Shaders\\SpotlightMultiDifuse.fx");
Effect godmodePointLightMultiDiffuse = TgcShaders.loadEffect(GuiController.Instance.AlumnoEjemplosDir + "GODMODE\\Media\\Shaders\\PointLightMultiDifuse.fx");
Effect godmodeTextureProyection = TgcShaders.loadEffect(GuiController.Instance.AlumnoEjemplosDir + "GODMODE\\Media\\Shaders\\TextureProyection.fx");
ColorValue[] lightColors = new ColorValue[5];
Vector4[] pointLightPositions = new Vector4[5];
float[] pointLightIntensity = new float[5];
float[] pointLightAttenuation = new float[5];
// Shadow map
readonly int SHADOWMAP_SIZE = 1024;
Texture g_pShadowMap; // Texture to which the shadow map is rendered
Texture miTex;
Surface g_pDSShadow; // Depth-stencil buffer for rendering to shadow map
Matrix g_mShadowProj; // Projection matrix for shadow map
Vector3 g_LightPos; // posicion de la luz actual (la que estoy analizando)
Vector3 g_LightDir; // direccion de la luz actual
Matrix g_LightView; // matriz de view del light
float near_plane = 2f;
float far_plane = 1500f;
public Luz()
{
miTex = TextureLoader.FromFile(GuiController.Instance.D3dDevice, GuiController.Instance.AlumnoEjemplosDir + "GODMODE\\Media\\proy.png");
Control panel3d = GuiController.Instance.Panel3d;
float aspectRatio = (float)panel3d.Width / (float)panel3d.Height;
g_mShadowProj = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(80),
aspectRatio, 2, 1500);
GuiController.Instance.D3dDevice.Transform.Projection =
Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f),
aspectRatio, near_plane, far_plane);
}
public void prenderLuz(int tipo, TgcMesh mesh)
{
Effect currentShader;
if (tipo == 0)
{
currentShader = godmodeSpotlightMultiDiffuse;
mesh.Effect = currentShader;
}
else if (tipo == 1)
{
currentShader = godmodeTextureProyection;
mesh.Effect = currentShader;
}
else if (tipo == 2)
{
currentShader = godmodePointLightMultiDiffuse;
mesh.Effect = currentShader;
}
//El Technique depende del tipo RenderType del mesh
mesh.Technique = GuiController.Instance.Shaders.getTgcMeshTechnique(mesh.RenderType);
}
public void apagarLuz(TgcMesh mesh)
{
mesh.Effect = GuiController.Instance.Shaders.TgcMeshShader;
//El Technique depende del tipo RenderType del mesh
mesh.Technique = GuiController.Instance.Shaders.getTgcMeshTechnique(mesh.RenderType);
}
public void renderizarLuz(int tipo, Vector3 posicionCamara, Vector3 direccionDeLuz, TgcMesh mesh, float intensidad,float temblor)
{
g_LightPos = posicionCamara;
//g_LightDir = direccionDeLuz - g_LightPos;
g_LightDir = direccionDeLuz;
g_LightDir.Normalize();
var random = FastMath.Cos(6 * temblor);
//Actualzar posición de la luz
Vector3 lightPos = posicionCamara;
//Normalizar direccion de la luz
Vector3 lightDir = direccionDeLuz;
lightDir.Normalize();
//Cargar variables shader de la luz
if (tipo == 0)
{ lightColors[0] = ColorValue.FromColor(Color.White);
pointLightPositions[0] = TgcParserUtils.vector3ToVector4(lightPos);
pointLightIntensity[0] = intensidad;
pointLightAttenuation[0] = 0.48f;
for (int i = 1; i < 5; i++)
{
lightColors[i] = ColorValue.FromColor(Color.White);
pointLightPositions[i] = TgcParserUtils.vector3ToVector4(posicionesDeLuces[i - 1]);
pointLightIntensity[i] = (float)GuiController.Instance.Modifiers["lightIntensity"];
pointLightAttenuation[i] = (float)GuiController.Instance.Modifiers["lightAttenuation"];
}
mesh.Effect.SetValue("lightColor", lightColors);
mesh.Effect.SetValue("lightPosition", pointLightPositions);
mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(lightPos));
mesh.Effect.SetValue("spotLightDir", TgcParserUtils.vector3ToFloat3Array(lightDir));
mesh.Effect.SetValue("lightIntensity", pointLightIntensity);
mesh.Effect.SetValue("lightAttenuation", pointLightAttenuation);
mesh.Effect.SetValue("spotLightAngleCos", FastMath.ToRad(39f));
mesh.Effect.SetValue("spotLightExponent", 14f);
//Variables de los materiales
mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor(Color.Black));
mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor(Color.White));
mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor(Color.White));
mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor(Color.White));
mesh.Effect.SetValue("materialSpecularExp", 11f);
mesh.render();
}
else if (tipo == 1)
{ // Calculo la matriz de view de la luz
//mesh.Effect.SetValue("g_vLightPos", new Vector4(g_LightPos.X, g_LightPos.Y, g_LightPos.Z, 1));
mesh.Effect.SetValue("g_vLightPos", new Vector4(posicionCamara.X, posicionCamara.Y, posicionCamara.Z, 1));
//mesh.Effect.SetValue("g_vLightDir", new Vector4(g_LightDir.X, g_LightDir.Y, g_LightDir.Z, 1));
mesh.Effect.SetValue("g_vLightDir", new Vector4(g_LightDir.X, g_LightDir.Y, g_LightDir.Z, 1));
mesh.Effect.SetValue("texProy", miTex);
g_LightView = Matrix.LookAtLH(g_LightPos, g_LightPos + g_LightDir, new Vector3(0, 0, 1));
// inicializacion standard:
//mesh.Effect.SetValue("g_mProjLight", g_mShadowProj);
mesh.Effect.SetValue("g_mViewLightProj", g_LightView * g_mShadowProj);
lightColors[0] = ColorValue.FromColor(Color.Yellow);
pointLightPositions[0] = TgcParserUtils.vector3ToVector4(lightPos);
pointLightIntensity[0] = intensidad; //POR 2??
pointLightAttenuation[0] = 0.67f;
for (int i = 1; i < 5; i++)
{
lightColors[i] = ColorValue.FromColor(Color.White);
pointLightPositions[i] = TgcParserUtils.vector3ToVector4(posicionesDeLuces[i - 1]);
pointLightIntensity[i] = (float)GuiController.Instance.Modifiers["lightIntensity"];
pointLightAttenuation[i] = (float)GuiController.Instance.Modifiers["lightAttenuation"];
}
mesh.Effect.SetValue("lightColor", lightColors);
mesh.Effect.SetValue("lightPosition", pointLightPositions);
mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(lightPos));
mesh.Effect.SetValue("lightIntensity", pointLightIntensity);
mesh.Effect.SetValue("lightAttenuation", pointLightAttenuation);
//Cargar variables de shader de Material. El Material en realidad deberia ser propio de cada mesh. Pero en este ejemplo se simplifica con uno comun para todos
mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor(Color.Black));
mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor(Color.Yellow));
mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor(Color.White));
mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor(Color.Yellow));
mesh.Effect.SetValue("materialSpecularExp",33f);
mesh.render();
}
else if (tipo==2)
{
lightColors[0] = ColorValue.FromColor(Color.LightGoldenrodYellow);
pointLightPositions[0] = TgcParserUtils.vector3ToVector4(lightPos);
pointLightIntensity[0] = intensidad + random * 3;
pointLightAttenuation[0] = 0.67f;
for (int i = 1; i < 5; i++)
{
lightColors[i] = ColorValue.FromColor(Color.White);
pointLightPositions[i] = TgcParserUtils.vector3ToVector4(posicionesDeLuces[i - 1]);
pointLightIntensity[i] = (float)GuiController.Instance.Modifiers["lightIntensity"];
pointLightAttenuation[i] = (float)GuiController.Instance.Modifiers["lightAttenuation"];
}
mesh.Effect.SetValue("lightColor", lightColors);
mesh.Effect.SetValue("lightPosition", pointLightPositions);
mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(lightPos));
mesh.Effect.SetValue("lightIntensity", pointLightIntensity);
mesh.Effect.SetValue("lightAttenuation", pointLightAttenuation);
//Cargar variables de shader de Material. El Material en realidad deberia ser propio de cada mesh. Pero en este ejemplo se simplifica con uno comun para todos
mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor(Color.Black));
mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor(Color.White));
mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor(Color.White));
mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor(Color.Orange));
mesh.Effect.SetValue("materialSpecularExp", 23f);
mesh.render();
}
}
}
}