-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.hpp
176 lines (152 loc) · 5.48 KB
/
shader.hpp
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
#pragma once
#include <cmath>
#include <epoxy/gl.h>
#include <oglplus/config/basic.hpp>
#include <oglplus/shader.hpp>
#include <oglplus/buffer.hpp>
#include <oglplus/context.hpp>
#include <oglplus/math/matrix.hpp>
#include <oglplus/math/vector.hpp>
#include <oglplus/program.hpp>
#include <oglplus/uniform.hpp>
#include <oglplus/vertex_array.hpp>
#include <oglplus/vertex_attrib.hpp>
#include <string>
#include <vector>
//#include <eigen3/unsupported/Eigen/OpenGLSupport>
using namespace std::string_view_literals;
struct Shader {
oglplus::Program program;
oglplus::VertexArrayAttrib v_pos;
oglplus::VertexArrayAttrib v_color;
oglplus::Uniform<oglplus::Mat4f> v_matrix;
struct Vertex {
oglplus::Vec2f pos;
oglplus::Vec3f color;
};
Shader()
: program{[] {
try {
oglplus::Program ret;
oglplus::VertexShader vs(R"(
#version 330
in vec2 pos;
in vec3 color;
uniform mat4 matrix;
out vec4 f_color;
void main() {
gl_Position = matrix*vec4(pos, 0.0, 1.0);
f_color=vec4(color, 1.0);
}
)");
oglplus::FragmentShader fs(R"(
#version 330
in vec4 f_color;
out vec4 outputColor;
void main() {
outputColor = f_color;
}
)");
vs.Compile();
fs.Compile();
ret.AttachShader(vs);
ret.AttachShader(fs);
ret.Link();
return ret;
} catch (oglplus::ProgramBuildError& ex) {
std::cerr << ex.Log() << std::endl;
throw;
}
}()},
v_pos(program, "pos"),
v_color(program, "color"),
v_matrix(program, "matrix")
{
}
void setMatrix(const oglplus::Mat4f& mat)
{
program.Use();
v_matrix.Set(mat);
}
struct VertexArray {
Shader* shader;
oglplus::VertexArray vao;
oglplus::Buffer buf;
oglplus::PrimitiveType primitive_type;
int len;
int primitive_size;
VertexArray(Shader& shader, oglplus::PrimitiveType primitive_type = oglplus::PrimitiveType::LineStrip, int primitive_size = 5) : shader(&shader), primitive_type(primitive_type), primitive_size(primitive_size)
{
vao.Bind();
buf.Bind(oglplus::BufferTarget::Array);
shader.v_pos.Pointer(3, oglplus::DataType::Float, false, sizeof(Vertex), (void*)offsetof(Vertex, pos));
shader.v_pos.Enable();
shader.v_color.Pointer(3, oglplus::DataType::Float, false, sizeof(Vertex), (void*)offsetof(Vertex, color));
shader.v_color.Enable();
}
VertexArray(Shader& shader, Vertex* start, int len,
oglplus::PrimitiveType primitive_type = oglplus::PrimitiveType::LineStrip,
oglplus::BufferUsage usage = oglplus::BufferUsage::StaticDraw,
int primitive_size = 5) : VertexArray(shader, primitive_type, primitive_size)
{
set(start, len, usage);
}
void set(Vertex* start, int len, oglplus::BufferUsage usage = oglplus::BufferUsage::StaticDraw)
{
buf.Bind(oglplus::BufferTarget::Array);
oglplus::Buffer::Data(oglplus::BufferTarget::Array, sizeof(Vertex) * len, start, usage);
this->len = len;
}
void setPartial(int start_index, Vertex* start, int len)
{
buf.Bind(oglplus::BufferTarget::Array);
oglplus::Buffer::SubData(oglplus::BufferTarget::Array, sizeof(Vertex) * start_index, sizeof(Vertex) * len, start);
}
void invalidate()
{
buf.InvalidateData();
}
void invalidatePartial(int start_index, int len)
{
buf.InvalidateSubData(sizeof(Vertex) * start_index, sizeof(Vertex) * len);
}
void draw()
{
draw(0, len);
}
void draw(int start_index, int len)
{
shader->draw(*this, start_index, len);
}
};
VertexArray createVertexArray(oglplus::PrimitiveType primitive_type = oglplus::PrimitiveType::LineStrip, int primitive_size = 5)
{
return VertexArray(*this, primitive_type, primitive_size);
}
VertexArray createVertexArray(Vertex* start, int len,
oglplus::PrimitiveType primitive_type = oglplus::PrimitiveType::LineStrip,
oglplus::BufferUsage usage = oglplus::BufferUsage::StaticDraw,
int primitive_size = 5)
{
return VertexArray(*this, start, len, primitive_type, usage, primitive_size);
}
void draw(const VertexArray& vertex_array, int start_index, int len)
{
program.Use();
if (vertex_array.primitive_type == oglplus::PrimitiveType::Points) {
oglplus::Context::PointSize(vertex_array.primitive_size);
} else if (vertex_array.primitive_type == oglplus::PrimitiveType::LineLoop
|| vertex_array.primitive_type == oglplus::PrimitiveType::Lines
|| vertex_array.primitive_type == oglplus::PrimitiveType::LinesAdjacency
|| vertex_array.primitive_type == oglplus::PrimitiveType::LineStrip
|| vertex_array.primitive_type == oglplus::PrimitiveType::LineStripAdjacency) {
oglplus::Context::LineWidth(vertex_array.primitive_size);
}
vertex_array.vao.Bind();
oglplus::Context::DrawArrays(vertex_array.primitive_type, start_index, len);
}
void draw(const VertexArray& vertex_array)
{
draw(vertex_array, 0, vertex_array.len);
}
};