Код: Выделить всё
public void SetupShadows()
{
DepthMapFBO = GL.GenFramebuffer();
DepthMap = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, DepthMap);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent,
SHADOW_WIDTH, SHADOW_HEIGHT, 0,
PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToBorder);
float[] borderColor = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor, borderColor);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, DepthMapFBO);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureTarget.Texture2D, DepthMap, 0);
GL.DrawBuffer(DrawBufferMode.None);
GL.ReadBuffer(ReadBufferMode.None);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
float nearPlane = 0.1f, farPlane = 75f;
Matrix4 lightProjection = Matrix4.CreateOrthographicOffCenter(-35f, 35f, -35f, 35f, nearPlane, farPlane);
Matrix4 lightView = Matrix4.LookAt(20*LightPos, Vector3.Zero, Vector3.UnitY);
lightSpaceMatrix = lightProjection * lightView;
Renderer.ShadowShader.UseProgram();
Renderer.ShadowShader.SetUniform("lightSpaceMatrix", lightSpaceMatrix);
GL.BindTexture(TextureTarget.Texture2D, DepthMap);
GL.ActiveTexture(TextureUnit.Texture10);
}
Код: Выделить всё
internal void DrawScene()
{
// Shadows
GL.Enable(EnableCap.DepthTest);
GL.Viewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, DepthMapFBO);
GL.Clear(ClearBufferMask.DepthBufferBit);
// Render DepthMap
GL.CullFace(TriangleFace.Front);
foreach (GameObject gObject in Objects)
{
gObject.RenderObject();
}
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
// Main Shader
GL.Viewport(0, 0, Renderer.Size.X, Renderer.Size.Y);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Renderer.Shader.UseProgram();
Renderer.Shader.SetUniform("view", ActiveCamera.GetViewMatrix());
Renderer.Shader.SetUniform("lightPos", LightPos*1000);
Renderer.Shader.SetUniform("projection", ActiveCamera.GetProjectionMatrix());
Renderer.Shader.SetUniform("lightSpaceMatrix", lightSpaceMatrix);
// Directional Light
if (DirectionalLightEnabled == true)
{
Renderer.Shader.SetUniform("dirLight.ambient", new Vector3(0.1f, 0.1f, 0.1f) * DirectionalLightColor);
Renderer.Shader.SetUniform("dirLight.diffuse", new Vector3(0.5f, 0.5f, 0.5f) * DirectionalLightColor);
Renderer.Shader.SetUniform("dirLight.specular", new Vector3(1.0f, 1.0f, 1.0f));
}else
{
Renderer.Shader.SetUniform("dirLight.ambient", new Vector3(0f, 0f, 0f));
Renderer.Shader.SetUniform("dirLight.diffuse", new Vector3(0f, 0f, 0f));
Renderer.Shader.SetUniform("dirLight.specular", new Vector3(0f, 0f, 0f));
}
Renderer.Shader.SetUniform("viewPos", ActiveCamera.GetWorldLocation());
// SCENE GAME OBJECTS
GL.CullFace(TriangleFace.Back);
foreach (GameObject gObject in Objects)
{
if (Renderer != null)
{
gObject.SceneShader = Renderer.Shader;
}
gObject.RenderObject();
}
// SKYBOX
if (SkyboxEnabled == true)
{
GL.DepthFunc(DepthFunction.Lequal);
GL.DepthMask(false);
// Skybox Shader
Renderer.SkyboxShader.UseProgram();
Renderer.SkyboxShader.SetUniform("view", ActiveCamera.GetViewMatrix());
Renderer.SkyboxShader.SetUniform("projection", ActiveCamera.GetProjectionMatrix());
Renderer.SkyboxShader.SetUniform("texture1", 0);
GL.BindVertexArray(SkyboxVAO);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.TextureCubeMap, SkyboxTextureID);
GL.DrawArrays(PrimitiveType.Triangles, 0, 36);
GL.BindVertexArray(0);
GL.DepthFunc(DepthFunction.Less);
GL.DepthMask(true);
}
}
Код: Выделить всё
internal void RenderMeshPart()
{
Owner.SceneShader.SetUniform("transform", ParentComponent.GetWorldMatrix());
Owner.SceneShader.SetUniform("material.shininess", Material.Shininess);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, Material.TextureID);
Owner.SceneShader.SetUniform("material.diffuse",0);
if(Material.UseSpecular==true)
{
GL.ActiveTexture(TextureUnit.Texture1);
GL.BindTexture(TextureTarget.Texture2D, Material.SpecularID);
Owner.SceneShader.SetUniform("material.specular", 1);
}
Owner.Scene.Renderer.ShadowShader.SetUniform("transform", ParentComponent.GetWorldMatrix());
Owner.Scene.Renderer.ShadowShader.SetUniform("shadowMap", 10);
GL.BindVertexArray(VAO);
GL.DrawElements(PrimitiveType.Triangles, Indices.Count, DrawElementsType.UnsignedInt, 0);
GL.BindVertexArray(0);
}
Код: Выделить всё
#version 330 core
out vec4 FragColor;
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};
struct DirLight {
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
vec3 color;
float intensity;
float constant;
float linear;
float quadratic;
};
#define MAX_POINT_LIGHTS 10
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
in vec4 FragPosLightSpace;
float shadow;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform Material material;
uniform sampler2D shadowMap;
uniform DirLight dirLight;
uniform int numPointLights;
uniform PointLight pointLights[MAX_POINT_LIGHTS];
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(normal, lightDir), 0.0);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 ambient = 0.75 * light.ambient * texture(material.diffuse, TexCoords).rgb;
vec3 diffuse = 1.25 * light.diffuse * diff * texture(material.diffuse, TexCoords).rgb;
vec3 specular = light.specular * spec * texture(material.specular, TexCoords).rgb;
return (ambient + (1.0 - shadow) * (diffuse + specular));
}
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
// --- Blinn-Phong specular ---
vec3 halfDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(normal, halfDir), 0.0), material.shininess);
// diffuse
float diff = max(dot(normal, lightDir), 0.0);
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * distance * distance);
vec3 ambient = 0.025 * light.color * texture(material.diffuse, TexCoords).rgb;
vec3 diffuse = light.intensity * light.color * diff * texture(material.diffuse, TexCoords).rgb;
vec3 specular = light.intensity * light.color * spec * texture(material.specular, TexCoords).rgb;
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return ambient + diffuse + specular;
}
void ShadowCalculation(vec4 fragPosLight)
{
shadow = 0.0f;
vec3 lightCoords = fragPosLight.xyz / fragPosLight.w;
if(lightCoords.z
Подробнее здесь: [url]https://stackoverflow.com/questions/79788153/opengl-opentk-directional-shadow-maps-not-working[/url]
Мобильная версия