forked from mortennobel/SimpleRenderEngine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom-mesh-layout-default-values.cpp
More file actions
107 lines (86 loc) · 2.76 KB
/
Copy pathcustom-mesh-layout-default-values.cpp
File metadata and controls
107 lines (86 loc) · 2.76 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
#include <iostream>
#include "sre/Texture.hpp"
#include "sre/Renderer.hpp"
#include "sre/Material.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <sre/SDLRenderer.hpp>
#include <sre/Resource.hpp>
#include <sre/impl/GL.hpp>
#include <sre/Inspector.hpp>
using namespace sre;
class CustomMeshLayoutExample{
public:
CustomMeshLayoutExample(){
r.init();
std::vector<glm::vec2> positions({
{0, 1},
{0, 0},
{1, 0}
});
std::vector<glm::vec4> colors({
{1, 0,0,1},
{0, 1,0,1},
{0, 0,1,1},
});
mesh = Mesh::create()
.withAttribute("posxyzw",positions)
.withAttribute("vertex_color",colors)
.build();
std::string vertexShaderSource = R"(#version 330
in vec4 posxyzw; // should automatically cast vec2 -> vec4 by appending (z = 0.0, w = 1.0)
in vec4 vertex_color;
out vec4 vColor;
#pragma include "global_uniforms_incl.glsl"
void main(void) {
gl_Position = g_projection * g_view * g_model * posxyzw;
vColor = vertex_color;
}
)";
std::string fragmentShaderSource = R"(#version 330
out vec4 fragColor;
in vec4 vColor;
void main(void)
{
fragColor = vColor;
}
)";
Resource::set("custom-mesh-vert.glsl", vertexShaderSource);
Resource::set("custom-mesh-frag.glsl", fragmentShaderSource);
mat1 = Shader::create()
.withSourceResource("custom-mesh-vert.glsl",ShaderType::Vertex)
.withSourceResource("custom-mesh-frag.glsl", ShaderType::Fragment)
.build()->createMaterial();
std::string info;
if (!mat1->getShader()->validateMesh(mesh.get(), info)){
std::cout << info <<std::endl;
} else {
std::cout << "Mesh ok" << std::endl;
}
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);
static Inspector inspector;
inspector.update();
inspector.gui();
}
private:
float time;
SDLRenderer r;
Camera camera;
std::shared_ptr<Mesh> mesh;
std::shared_ptr<Material> mat1;
};
int main() {
std::make_unique<CustomMeshLayoutExample>();
return 0;
}