-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShadowMap.cpp
88 lines (63 loc) · 2.01 KB
/
ShadowMap.cpp
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
#include "ShadowMap.h"
ShadowMap::ShadowMap() :Fbo{ 0 }, sMapTexture{ 0 }, ShadowWidth{ 0 }, ShadowHeight{ 0 }
{}
bool ShadowMap::Init(GLuint Width, GLuint Height)
{
ShadowWidth = Width;
ShadowHeight = Height;
glGenFramebuffers(1, &Fbo);
glGenTextures(1, &sMapTexture);
glBindTexture(GL_TEXTURE_2D, sMapTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,ShadowWidth,ShadowHeight,0,GL_DEPTH_COMPONENT,GL_FLOAT,nullptr);
//GL_Repeat repeats the texture when coordinate outside 0-1 is given
//Texture filtering
//Linear is more clean and fluid , nearest is more blocky and looks like PS1 graphics
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float bColour[] = { 1.f,1.f,1.f,1.f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR,bColour);
glBindFramebuffer(GL_FRAMEBUFFER, Fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, sMapTexture,0);
//We dont actually want to draw and read this buffer . We just want the shadows
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
GLenum FboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (FboStatus != GL_FRAMEBUFFER_COMPLETE)
{
std::cout << "Framebuffer error " << FboStatus << std::endl;
return false;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return true;
}
void ShadowMap::Write()
{
//Writes to Fbo instead of default frame buffers that we use in main.cpp
glBindFramebuffer(GL_FRAMEBUFFER, Fbo);
}
void ShadowMap::Read(GLenum TextureUnit)
{
glActiveTexture(TextureUnit);
glBindTexture(GL_TEXTURE_2D, sMapTexture);
}
GLuint ShadowMap::GetShadowWidth()
{
return ShadowWidth;
}
GLuint ShadowMap::GetShadowHeight()
{
return ShadowHeight;
}
ShadowMap::~ShadowMap()
{
if (Fbo)
{
glDeleteFramebuffers(1, &Fbo);
}
if (sMapTexture)
{
glDeleteTextures(1, &sMapTexture);
}
}