-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomFrame.cpp
159 lines (146 loc) · 6.01 KB
/
CustomFrame.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "CustomFrame.h"
#include "constants.h"
#include <iostream>
std::unique_ptr<VertexShader> DefaultVertexShader=std::make_unique<VertexShader>();
std::unique_ptr<FragmentShader> DefaultFragmentShader=std::make_unique<FragmentShader>();;
CustomFrame::CustomFrame(glm::uvec2 size)
{
glGenBuffers(1, &PBO);
glGenFramebuffers(1, &FBO);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);
if (auto status = glCheckFramebufferStatus(FBO))
std::cout << "FrameBuffer error " << status << std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
setImageSize(size);
}
CustomFrame::~CustomFrame()
{
glDeleteBuffers(1, &PBO);
glDeleteTextures(1, &texture);
glDeleteFramebuffers(1, &FBO);
}
void CustomFrame::blit(GLuint fboDestination, glm::ivec2 pos1, glm::ivec2 pos2)
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, FBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboDestination);
glBlitFramebuffer(0, 0, m_size.x, m_size.y, pos1.x, pos1.y, pos2.x, pos2.y, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
}
void CustomFrame::apply()
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, PBO);
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_size.x, m_size.y, GL_RGB, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}
void CustomFrame::draw_command_buffer(const CommandBuffer& cmdBuffer)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, PBO);
Pixels pixs;
pixs.colors = static_cast<colorraw_t*>(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY));
pixs.zbuffer = m_zbuffer.data();
const auto& cmdData = cmdBuffer.get_buffer();
for (auto& cmd : cmdData)
{
switch(cmd->type)
{
case Command::Type::Clear:
clear_image(pixs, reinterpret_cast<ClearCommand*>(cmd.get())->color);
break;
case Command::Type::DrawBuffer:
{
auto drawcmd = reinterpret_cast<DrawCommand*>(cmd.get());
if (drawcmd->vbo->type>=VertexBuffer::Type::Lines)
draw_line(pixs, *drawcmd);
else
draw_triangle(pixs, *drawcmd);
}
break;
default:
break;
}
}
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
apply();
}
void CommandBuffer::reserve(size_t size)
{
m_cmdBuffer.reserve(size);
}
_std::observer_ptr<ClearCommand> CommandBuffer::clear_image(glm::vec3 color)
{
std::unique_ptr<ClearCommand> cmd(new ClearCommand(color));
_std::observer_ptr<ClearCommand> res(cmd.get());
m_cmdBuffer.push_back(std::move(cmd));
return res;
}
_std::observer_ptr<DrawCommand> CommandBuffer::draw_line(glm::vec2 pos1, glm::vec2 pos2, glm::vec3 color)
{
std::unique_ptr<DrawInternalBufferCommand> cmd(new DrawInternalBufferCommand);
cmd->m_internatBuffer=std::unique_ptr<VertexBuffer>(new VertexBuffer);
cmd->m_internatBuffer->verts.resize(2);
cmd->m_internatBuffer->verts[0]=Vertex{glm::vec4(pos1,0.f,1.f), color};
cmd->m_internatBuffer->verts[1]=Vertex{glm::vec4(pos2,0.f,1.f), color};
cmd->vbo = util::makeObserver(cmd->m_internatBuffer.get());
cmd->vertShader.reset(DefaultVertexShader.get());
cmd->fragShader.reset(DefaultFragmentShader.get());
m_cmdBuffer.push_back(std::move(cmd));
_std::observer_ptr<DrawCommand> res(cmd.get());
m_cmdBuffer.push_back(std::move(cmd));
return res;
}
DrawCommand::observer CommandBuffer::draw_triangle(glm::vec2 pos1, glm::vec2 pos2, glm::vec2 pos3, glm::vec3 color)
{
std::unique_ptr<DrawInternalBufferCommand> cmd(new DrawInternalBufferCommand);
cmd->m_internatBuffer=std::unique_ptr<VertexBuffer>(new VertexBuffer);
cmd->m_internatBuffer->verts.resize(3);
cmd->m_internatBuffer->verts[0]=Vertex{glm::vec4(pos1,0.f,1.f), color};
cmd->m_internatBuffer->verts[1]=Vertex{glm::vec4(pos2,0.f,1.f), color};
cmd->m_internatBuffer->verts[2]=Vertex{glm::vec4(pos3,0.f,1.f), color};
cmd->vbo = util::makeObserver(cmd->m_internatBuffer.get());
cmd->vertShader.reset(DefaultVertexShader.get());
cmd->fragShader.reset(DefaultFragmentShader.get());
DrawCommand::observer res(cmd.get());
m_cmdBuffer.push_back(std::move(cmd));
return res;
}
DrawCommand::observer CommandBuffer::draw_buffer(_std::observer_ptr<VertexBuffer> vbo)
{
std::unique_ptr<DrawCommand> cmd(new DrawCommand);
std::swap(vbo, cmd->vbo);
DrawCommand::observer res(cmd.get());
cmd->vertShader.reset(DefaultVertexShader.get());
cmd->fragShader.reset(DefaultFragmentShader.get());
m_cmdBuffer.push_back(std::move(cmd));
return res;
}
void CustomFrame::setImageSize(glm::uvec2 size)
{
size+=size%glm::uvec2(2);
m_size=size;
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, PBO);
glBufferData(GL_PIXEL_UNPACK_BUFFER, size.x*size.y*3, nullptr, GL_STREAM_DRAW);
#ifndef NDEBUG
{
uint8_t* pixs = static_cast<uint8_t*>(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY));
size_t totalSize=size.x*size.y*3;
for(int i=0;i<totalSize;i++)
pixs[i] = rand()%256;
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
}
#endif
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
m_zbuffer.resize(size.x*size.y);
}