forked from mortennobel/SimpleRenderEngine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_shader.cpp
More file actions
155 lines (126 loc) · 3.99 KB
/
Copy pathupdate_shader.cpp
File metadata and controls
155 lines (126 loc) · 3.99 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
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
#include <iostream>
#include "sre/Texture.hpp"
#include "sre/Renderer.hpp"
#include "sre/Material.hpp"
#include "sre/Inspector.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <sre/SDLRenderer.hpp>
#include <sre/Resource.hpp>
#include <sre/impl/GL.hpp>
using namespace sre;
class CustomMeshLayoutExample{
public:
CustomMeshLayoutExample(){
r.init();
std::vector<glm::vec3> positions({
{0, 1,0},
{0, 0,0},
{1, 0,0}
});
std::vector<glm::vec4> colors({
{1, 0,0,1},
{0, 1,0,1},
{0, 0,1,1},
});
mesh = Mesh::create()
.withPositions(positions)
.withAttribute("vertex_color",colors)
.build();
std::string vertexShaderSource = R"(#version 330
in vec4 position;
in vec4 vertex_color;
out vec4 vColor;
uniform mat4 customTransform4[2];
uniform float customTransformIndex;
#pragma include "global_uniforms_incl.glsl"
void main(void) {
int id = int(customTransformIndex);
gl_Position = g_projection * g_view * g_model * customTransform4[id]*vec4(position);
vColor = vertex_color;
}
)";
std::string fragmentShaderSource = R"(#version 330
out vec4 fragColor;
in vec4 vColor;
uniform vec4 extra;
void main(void)
{
fragColor = vColor + extra;
}
)";
Resource::set("mat4-vert.glsl", vertexShaderSource);
Resource::set("mat4-frag.glsl", fragmentShaderSource);
mat1 = Shader::create()
.withSourceResource("mat4-vert.glsl",ShaderType::Vertex)
.withSourceResource("mat4-frag.glsl", ShaderType::Fragment)
.build()->createMaterial();
mats4 = std::make_shared<std::vector<glm::mat4>>();
for (int i=0;i<2;i++){
mats4->emplace_back(1);
}
mat1->set("customTransform4", mats4);
mat1->set("customTransformIndex", 1);
mat1->set("extra", glm::vec4(1,0,-1,1));
r.frameRender = [&](){
render();
};
r.startEventLoop();
}
void render(){
auto rp = RenderPass::create()
.withCamera(camera)
.withClearColor(true,{1,0,0,1})
.build();
rp.draw(mesh, glm::mat4(1), mat1);
if (ImGui::Button("Update shader")){
updateShader();
}
static Inspector inspector;
inspector.update();
inspector.gui();
}
void updateShader(){
auto shader = mat1->getShader();
auto shaderBuilder = shader->update();
std::string vertexShaderSource = R"(#version 330
in vec4 position;
in vec4 vertex_color;
out vec4 vColor;
#pragma include "global_uniforms_incl.glsl"
void main(void) {
gl_Position = g_projection * g_view * g_model * vec4(position);
vColor = vertex_color;
}
)";
std::string fragmentShaderSource = R"(#version 330
out vec4 fragColor;
in vec4 vColor;
uniform vec4 extra;
void main(void)
{
fragColor = vColor + extra;
}
)";
Resource::set("mat4-vert.glsl", vertexShaderSource);
Resource::set("mat4-frag.glsl", fragmentShaderSource);
shaderBuilder.withSourceResource("mat4-vert.glsl",ShaderType::Vertex);
shaderBuilder.withSourceResource("mat4-frag.glsl", ShaderType::Fragment);
shaderBuilder.build();
}
private:
SDLRenderer r;
Camera camera;
std::shared_ptr<Mesh> mesh;
std::shared_ptr<Material> mat1;
std::shared_ptr<std::vector<glm::mat4>> mats4;
int id = 0;
glm::vec3 offset[2] = {{0,0,0},{0,0,0}};
float rotate[2] = {0,0};
};
int main() {
std::make_unique<CustomMeshLayoutExample>();
return 0;
}