-
Notifications
You must be signed in to change notification settings - Fork 8
/
r_shadow.cpp
104 lines (81 loc) · 2.66 KB
/
r_shadow.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "r_shadow.h"
namespace r {
///!shadowMap
shadowMap::shadowMap()
: m_width(0)
, m_height(0)
, m_fbo(0)
, m_shadowMap(0)
{
}
shadowMap::~shadowMap() {
if (m_fbo)
gl::DeleteFramebuffers(1, &m_fbo);
if (m_shadowMap)
gl::DeleteTextures(1, &m_shadowMap);
}
bool shadowMap::init(size_t width, size_t height) {
m_width = width;
m_height = height;
gl::GenFramebuffers(1, &m_fbo);
gl::GenTextures(1, &m_shadowMap);
gl::BindTexture(GL_TEXTURE_2D, m_shadowMap);
gl::TexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_width, m_height, 0,
GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl::BindFramebuffer(GL_FRAMEBUFFER, m_fbo);
gl::FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_TEXTURE_2D, m_shadowMap, 0);
gl::DrawBuffer(GL_NONE);
gl::ReadBuffer(GL_NONE);
const GLenum status = gl::CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
return status == GL_FRAMEBUFFER_COMPLETE;
}
void shadowMap::update(size_t width, size_t height) {
if (m_width == width && m_height == height)
return;
m_width = width;
m_height = height;
gl::BindTexture(GL_TEXTURE_2D, m_shadowMap);
gl::TexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_width, m_height, 0,
GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
}
void shadowMap::bindWriting() {
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
}
GLuint shadowMap::texture() const {
return m_shadowMap;
}
float shadowMap::widthScale(size_t size) const {
return float(size) / float(m_width);
}
float shadowMap::heightScale(size_t size) const {
return float(size) / float(m_height);
}
///! shadowMapMethod
shadowMapMethod::shadowMapMethod()
: m_WVP(nullptr)
{
}
bool shadowMapMethod::init() {
if (!method::init("shadow map"))
return false;
if (!addShader(GL_VERTEX_SHADER, "shaders/shadow.vs"))
return false;
// No fragment shader needed because gl::DrawBuffer(GL_NONE)
if (!finalize({ "position" }))
return false;
m_WVP = getUniform("gWVP", uniform::kMat4);
post();
return true;
}
void shadowMapMethod::setWVP(const m::mat4 &wvp) {
m_WVP->set(wvp);
}
}