I wanted to add shadows to my opentk project, and I did some research and attempted to write some code, but once I finished it, the shadows appeared like this. I have no clue what I did wrong.
code:
public enum LightType
{
Directional,
Point,
Spot
}
internal class LightSource
{
public Vector3 Position { get; set; }
public Vector3 Rotation { get; set; }
public float Intensity { get; set; }
public LightType Type { get; set; }
public LightSource(Vector3 position, Vector3 rotation, float intensity, LightType type)
{
Position = position;
Rotation = rotation;
Intensity = intensity;
Type = type;
}
public Matrix4 CalculateLightSpaceMatrix(Vector3 sceneCenter)
{
Matrix4 lightViewMatrix;
Matrix4 lightProjectionMatrix;
if (Type == LightType.Directional)
{
Vector3 lightDirection = Vector3.Transform(Vector3.UnitZ, Quaternion.FromEulerAngles(Rotation));
float distance = 10.0f;
Position = sceneCenter - lightDirection * distance;
lightViewMatrix = Matrix4.LookAt(Position, sceneCenter, Vector3.UnitY);
float orthoSize = 20.0f;
float nearPlane = 1.0f;
float farPlane = 500.0f;
lightProjectionMatrix = Matrix4.CreateOrthographic(orthoSize, orthoSize, nearPlane, farPlane);
return lightViewMatrix * lightProjectionMatrix;
}
else if (Type == LightType.Point)
{
}
else if (Type == LightType.Spot)
{
}
return Matrix4.Identity;
}
}
protected override void OnRenderFrame(FrameEventArgs args)
{
GL.ClearColor(0.3f, 0.3f, 1f, 1f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Matrix4 model = Matrix4.Identity;
Matrix4 view = c.getViewMatrix();
Matrix4 projection = c.getProjectionMatrix();
int modelLocation = GL.GetUniformLocation(program.ID, "model");
int viewLocation = GL.GetUniformLocation(program.ID, "view");
int projectionLocation = GL.GetUniformLocation(program.ID, "projection");
GL.UniformMatrix4(modelLocation, true, ref model);
GL.UniformMatrix4(viewLocation, true, ref view);
GL.UniformMatrix4(projectionLocation, true, ref projection);
GL.UniformMatrix4(GL.GetUniformLocation(depthMapShader.ID, "lightSpaceMatrix"), true, ref lightSpaceMatrix);
RenderDepthMapPass();
GL.UniformMatrix4(GL.GetUniformLocation(program.ID, "lightSpaceMatrix"), true, ref lightSpaceMatrix);
GL.Uniform1(GL.GetUniformLocation(program.ID, "diffuseTexture"), 0);
GL.Uniform1(GL.GetUniformLocation(program.ID, "shadowMap"), 1);
GL.ActiveTexture(TextureUnit.Texture1);
GL.BindTexture(TextureTarget.Texture2D, depthMapTexture);
chunk.Render(program);
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.DeleteTexture(depthMapTexture);
Context.SwapBuffers();
}
void RenderDepthMapPass()
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, depthMapFBO);
GL.Viewport(0, 0, width, height);
GL.Clear(ClearBufferMask.DepthBufferBit);
Matrix4 chunkMatrix = Matrix4.CreateTranslation(chunk.position);
GL.UniformMatrix4(GL.GetUniformLocation(depthMapShader.ID, "model"), true, ref chunkMatrix);
chunk.Render(depthMapShader);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
void SetupDepthMapFBO(int width, int height)
{
GL.GenFramebuffers(1, out depthMapFBO);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, depthMapFBO);
GL.GenTextures(1, out depthMapTexture);
GL.BindTexture(TextureTarget.Texture2D, depthMapTexture);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent,
width, height, 0, OpenTK.Graphics.OpenGL4.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)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment,
TextureTarget.Texture2D, depthMapTexture, 0);
GL.DrawBuffer(DrawBufferMode.None);
GL.ReadBuffer(ReadBufferMode.None);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
protected override void OnLoad()
{
base.OnLoad();
chunk = new Chunk(new Vector3(0, 0, 0));
program = new ShaderProgram("default.vert", "default.frag");
depthMapShader = new ShaderProgram("LightSource.vert","LightSource.frag");
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
GL.Enable(EnableCap.DepthTest);
c = new camera(width, height, Vector3.Zero);
CursorState = CursorState.Grabbed;
Vector3 lightPosition = new Vector3(20.0f, 8.0f, 20.0f);
Vector3 lightRotation = new Vector3(-45.0f, 0.0f, 0.0f);
float lightIntensity = 1.0f;
LightType lightType = LightType.Directional;
light = new LightSource(lightPosition, lightRotation, lightIntensity, lightType);
Vector3 sceneCenter = new Vector3(0.0f, 0.0f, 0.0f);
lightSpaceMatrix = light.CalculateLightSpaceMatrix(sceneCenter);
}
I tried changing orders of some calculations to see if they were typed wrong, but I couldn't find anything.