-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrameBuffer.cpp
85 lines (72 loc) · 2.02 KB
/
FrameBuffer.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
#include "FrameBuffer.h"
FrameBuffer::FrameBuffer(uint32_t w, uint32_t h)
: Buffer(w * h * 4), width(w), height(h), bpp(4)
{
std::cout << "[FRAMEBUFFER] buffer allocate length: " << length << '\n';
}
FrameBuffer::~FrameBuffer()
{
std::cout << "[FRAMEBUFFER] buffer release length: " << length << '\n';
// data½»¸ø¸¸ÀàÎö¹¹
}
const int FrameBuffer::GetWidth() const noexcept
{
return width;
}
const int FrameBuffer::GetHeight() const noexcept
{
return height;
}
bool FrameBuffer::read(uint32_t x, uint32_t y, glm::vec4& color)
{
if (x >= width || y >= height) {
// originally set rgb to 0,
// here set rgb to 1 for shadow map, especially make depth = 1 (b = 1)
// improve the shadow edge
color.r = color.g = color.b = 1;
color.a = 1;
return false;
}
int p = (y * width + x) * bpp;
unsigned char cr = data[p + 0];
unsigned char cg = data[p + 1];
unsigned char cb = data[p + 2];
unsigned char ca = data[p + 3];
color.r = (unsigned int)cr * INV255;
color.g = (unsigned int)cg * INV255;
color.b = (unsigned int)cb * INV255;
color.a = (unsigned int)ca * INV255;
return true;
}
bool FrameBuffer::write(uint32_t x, uint32_t y, const glm::vec4& color)
{
#ifdef _DEBUG
if (x >= width || y >= height) {
return false;
}
#endif
int p = (y * width + x) * bpp;
data[p + 0] = (unsigned int)(color.r * 255.0f);
data[p + 1] = (unsigned int)(color.g * 255.0f);
data[p + 2] = (unsigned int)(color.b * 255.0f);
data[p + 3] = (unsigned int)(color.a * 255.0f);
return true;
}
void FrameBuffer::Clear(const glm::vec4& color)
{
unsigned char r = (unsigned int)(color.r * 255.0f);
unsigned char g = (unsigned int)(color.g * 255.0f);
unsigned char b = (unsigned int)(color.b * 255.0f);
unsigned char a = (unsigned int)(color.a * 255.0f);
//for (int i = 0; i < length; i += bpp) {
// data[i + 0] = r;
// data[i + 1] = g;
// data[i + 2] = b;
// data[i + 3] = a;
//}
int val = a << 24 | b << 16 | g << 8 | r; // endian
std::memset(data, val, length);
//for (int i = 0; i < length; i += bpp) {
// *(int*)(data + i) = val;
//}
}