Repository navigation
Expand file tree
/
Copy pathShadowMap.cpp
More file actions
47 lines (39 loc) · 1.49 KB
/
Copy pathShadowMap.cpp
File metadata and controls
47 lines (39 loc) · 1.49 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
#include "ShadowMap.h"
#include <stdexcept>
ShadowMap::ShadowMap(int w, int h) : width_(w), height_(h)
{
glGenFramebuffers(1, &FBO);
glGenTextures(1, &depthMap);
glBindTexture(GL_TEXTURE_2D, depthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width_, height_, 0,
GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(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 borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
throw std::runtime_error("ShadowMap FBO incomplete!");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
ShadowMap::~ShadowMap()
{
glDeleteTextures(1, &depthMap);
glDeleteFramebuffers(1, &FBO);
}
void ShadowMap::bindForWriting()
{
glViewport(0, 0, width_, height_);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glClear(GL_DEPTH_BUFFER_BIT);
}
void ShadowMap::bindForReading(GLenum textureUnit)
{
glActiveTexture(textureUnit);
glBindTexture(GL_TEXTURE_2D, depthMap);
}