forked from mortennobel/SimpleRenderEngine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstencil_test.cpp
More file actions
163 lines (139 loc) · 5.26 KB
/
Copy pathstencil_test.cpp
File metadata and controls
163 lines (139 loc) · 5.26 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
155
156
157
158
159
160
161
162
#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/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <sre/SDLRenderer.hpp>
#include <sre/impl/GL.hpp>
#include <sre/Inspector.hpp>
#include <sre/ModelImporter.hpp>
using namespace sre;
class StencilExample{
public:
StencilExample(){
r.init();
std::vector<std::shared_ptr<Material>> materials_unused;
mesh = sre::ModelImporter::importObj("test_data", "suzanne.obj", materials_unused);
plane = Mesh::create().withQuad(1).build();
camera.setPerspectiveProjection(45,0.1,10);
camera.lookAt({0,0,3.5f},{0,0,0},{0,1,0});
worldLights.setAmbientLight(glm::vec3{0.05f});
worldLights.addLight(Light::create().withPointLight({0.5,2,0.5}).build());
matStencilWrite = Shader::create()
.withSourceResource("unlit_vert.glsl", ShaderType::Vertex)
.withSourceResource("unlit_frag.glsl", ShaderType::Fragment)
.withDepthWrite(false)
.withColorWrite({false,false,false,false})
.withStencil(Stencil{
StencilFunc ::Always,
1,
1,
StencilOp::Replace,
StencilOp::Replace,
StencilOp::Replace,
})
.withName("StencilWrite").build()->createMaterial();
matStencilTest = Shader::create()
.withSourceResource("unlit_vert.glsl", ShaderType::Vertex)
.withSourceResource("unlit_frag.glsl", ShaderType::Fragment)
.withStencil(Stencil{
StencilFunc ::Equal,
1,
1
})
.withName("StencilClippedShadow").build()->createMaterial();
matStencilTest->setColor({0.3f,0.3f,0.3f});
mat1 = Shader::getStandardPhong()->createMaterial();
shadow = Shader::getUnlit()->createMaterial();
shadow->setColor({0.3f,0.3f,0.3f});
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.mouseEvent = [&](SDL_Event& event){
if (event.type == SDL_MOUSEMOTION){
float mouseSpeed = 1/50.0f;
rotateY = event.motion.x*mouseSpeed;
rotateX = event.motion.y*mouseSpeed;
}
if (event.button.button==SDL_BUTTON_RIGHT){
showInspector = true;
}
};
r.startEventLoop();
}
void render(){
auto rp = RenderPass::create()
.withCamera(camera)
.withClearColor(true,{0,0,0,1})
.withClearStencil(true)
.withWorldLights(&worldLights)
.build();
auto pos = worldLights.getLight(0)->position;
float y = pos.y-0.01f-shadowPlane;
glm::mat4 shadow = glm::transpose(glm::mat4(
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,1/-y,0,0
));
glm::mat4 projectedShadow = glm::translate(pos) * shadow * glm::translate(-pos);
auto modelRotation = (glm::rotate(rotateX, glm::vec3(1,0,0))*glm::rotate(rotateY, glm::vec3(0,1,0)));
if (drawShadow) {
if (useStencil){
rp.draw(plane, glm::translate(glm::vec3{0,shadowPlane,0})*glm::rotate(-glm::half_pi<float>(),glm::vec3{1,0,0}), matStencilWrite);
rp.draw(mesh, projectedShadow * modelRotation, matStencilTest);
} else {
rp.draw(mesh, projectedShadow * modelRotation, this->shadow);
}
}
rp.draw(mesh, modelRotation, mat1);
if (drawPlane){
rp.draw(plane, glm::translate(glm::vec3{0,shadowPlane,0})*glm::rotate(-glm::half_pi<float>(),glm::vec3{1,0,0}), mat1);
}
ImGui::SetNextWindowPos(ImVec2(0,0));
ImGui::SetNextWindowContentSize(ImVec2(300,120));
ImGui::Begin("Shadow");
ImGui::DragFloat("Ground height ",&shadowPlane,0.1f);
ImGui::Checkbox("Draw plane",&drawPlane);
ImGui::Checkbox("Draw shadow",&drawShadow);
ImGui::Checkbox("Use stencil",&useStencil);
ImGui::DragFloat3("Light pos",&worldLights.getLight(0)->position.x,.1f);
ImGui::End();
static Inspector inspector;
inspector.update();
if (showInspector){
inspector.gui();
}
}
private:
float time;
SDLRenderer r;
Camera camera;
float shadowPlane = -1;
std::shared_ptr<Mesh> mesh;
std::shared_ptr<Mesh> plane;
std::shared_ptr<Material> mat1;
std::shared_ptr<Material> matStencilWrite;
std::shared_ptr<Material> matStencilTest;
std::shared_ptr<Material> shadow;
WorldLights worldLights;
float rotateX = 0;
float rotateY = 0;
bool drawPlane = true;
bool drawShadow = true;
bool useStencil = false;
bool showInspector = false;
};
int main() {
std::make_unique<StencilExample>();
return 0;
}