-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomFrame.h
194 lines (187 loc) · 5.58 KB
/
CustomFrame.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#pragma once
#include <variant>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <vector>
#include <memory>
#include "memory.hpp"
#include "constants.h"
#include "memory.hpp"
using colorraw_t = glm::tvec3<uint8_t>;
template <typename T>
using value_ref_t = std::variant<T, _std::observer_ptr<T>>;
template <typename T>
inline T& get_valueref(value_ref_t<T>& val)
{
if (val.index()==0)
return std::get<T>(val);
else if (val.index()==1)
return *std::get<_std::observer_ptr<T>>(val);
else
throw std::runtime_error("value_ref_t not inited.");
}
template <typename T>
inline const T& get_valueref(const value_ref_t<T>& val)
{
if (val.index()==0)
return std::get<T>(val);
else if (val.index()==1)
return *std::get<_std::observer_ptr<T>>(val);
else
throw std::runtime_error("value_ref_t not inited.");
}
struct VertexBrut
{
glm::ivec2 pos;
colorraw_t color;
};
struct Vertex
{
Vertex(){}
Vertex(glm::vec4 _pos, glm::vec3 _color) : pos(_pos), color(_color) {}
virtual glm::vec4 getPosition() const {return pos;};
glm::vec4 pos;
glm::vec3 color;
glm::vec2 uv;
float w=1.f;
};
template <typename VecT, typename T>
inline VecT interp(VecT first, VecT second, T current, T total, float aW, float bW)
{
if (total==0)
return first;
return ((first*(total-current)/total)/aW + (second*(current)/total)/bW)/(((total-current)/total)/aW+(current/total)/bW);
}
template <typename VecT, typename T>
inline VecT interp(VecT first, VecT second, T current, T total)
{
if (total==0)
return first;
return first*(total-current)/total+second*(current)/total;
}
template <typename T>
inline Vertex interp(Vertex first, Vertex second, T current, T total)
{
if (total==0)
return first;
Vertex vres;
// interpolation par w deja fait
vres.pos = interp(first.pos, second.pos, static_cast<float>(current), static_cast<float>(total));
if (first.w == 1.f && second.w == 1.f)
{
vres.color = interp(first.color, second.color, static_cast<float>(current), static_cast<float>(total));
vres.uv = interp(first.uv, second.uv, static_cast<float>(current), static_cast<float>(total));
}
else
{
vres.color = interp(first.color, second.color, static_cast<float>(current), static_cast<float>(total), first.w, second.w);
vres.uv = interp(first.uv, second.uv, static_cast<float>(current), static_cast<float>(total), first.w, second.w);
vres.w = 1.f;
}
return vres;
}
template <typename InputType, typename OutputType>
class Shader
{
public:
virtual OutputType get(InputType) const {return OutputType();};
};
class VertexShader : public Shader<Vertex, Vertex>
{
public:
virtual Vertex get(Vertex) const override;
value_ref_t<glm::mat4> m_modelmat=glm::mat4(1.f);
value_ref_t<glm::mat4> m_viewmat=glm::mat4(1.f);
value_ref_t<glm::mat4> m_projmat=glm::mat4(1.f);
};
class FragmentShader : public Shader<Vertex, glm::vec3>
{
public:
virtual glm::vec3 get(Vertex) const override ;
};
struct VertexBuffer
{
enum class Type{
Triangles,
TriangleStrip,
TriangleFan,
Lines,
LineFan,
LineStrip
};
Type type;
std::vector<Vertex> verts;
};
struct Command
{
enum class Type{
Clear,
DrawBuffer,
};
Command(Type _type) : type(_type){}
const Type type;
};
struct ClearCommand : public Command
{
using observer=_std::observer_ptr<ClearCommand>;
ClearCommand(glm::vec3 _color=glm::vec3(0.f)) : Command(Command::Type::Clear), color(_color){}
glm::vec3 color;
};
struct DrawCommand : public Command
{
using observer=_std::observer_ptr<DrawCommand>;
DrawCommand() : Command(Command::Type::DrawBuffer){}
_std::observer_ptr<VertexBuffer> vbo;
_std::observer_ptr<VertexShader> vertShader;
_std::observer_ptr<FragmentShader> fragShader;
};
struct DrawInternalBufferCommand : public DrawCommand
{
using observer=_std::observer_ptr<DrawCommand>;
DrawInternalBufferCommand() : DrawCommand(){}
std::unique_ptr<VertexBuffer> m_internatBuffer;
};
class CommandBuffer
{
public:
void reserve(size_t size);
ClearCommand::observer clear_image(glm::vec3 color);
DrawCommand::observer draw_line(glm::vec2 pos1, glm::vec2 pos2, glm::vec3 color);
DrawCommand::observer draw_triangle(glm::vec2 pos1, glm::vec2 pos2, glm::vec2 pos3, glm::vec3 color);
DrawCommand::observer draw_buffer(_std::observer_ptr<VertexBuffer>);
void clear_buffer();
const std::vector<std::unique_ptr<Command>>& get_buffer() const
{return m_cmdBuffer;}
private:
std::vector<std::unique_ptr<Command>> m_cmdBuffer;
};
struct Pixels
{
colorraw_t* colors=nullptr;
float* zbuffer=nullptr;
};
class CustomFrame
{
public:
CustomFrame(glm::uvec2 = glm::uvec2(Constants::TextureWidth, Constants::TextureHeight));
~CustomFrame();
void setImageSize(glm::uvec2);
glm::uvec2 getImageSize();
void blit(GLuint fboDestination, glm::ivec2 pos1, glm::ivec2 pos2);
void draw_command_buffer(const CommandBuffer& cmdBuffer);
void apply();
private:
void clear_image(Pixels pixs, const glm::vec3& cmd);
void draw_line(Pixels pixs, const DrawCommand& cmd);
void draw_triangle(Pixels pixs, const DrawCommand& cmd);
void draw_horizontal(Pixels pixs, int y, std::pair<int, int> xs, std::pair<Vertex, Vertex> verts, const FragmentShader& fshad);
glm::ivec2 toScreenSpace(glm::vec2 p)
{
return (p+1.f)*0.5f*glm::vec2(m_size);
}
glm::uvec2 m_size;
std::vector<float> m_zbuffer;
GLuint PBO;
GLuint FBO;
GLuint texture;
};