-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNormalShader.hpp
104 lines (86 loc) · 2.33 KB
/
NormalShader.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
#pragma once
#include "Vertex.h"
#include "SceneContext.h"
#include "VertexShaderMatHelper.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <memory>
class NormalShader
{
public:
using UseDerivative = std::false_type;
struct VSOut {
glm::vec4 proj_pos;
glm::vec4 normal;
glm::vec3 pos; //world position
VSOut& operator+=(const VSOut& rhs)
{
proj_pos += rhs.proj_pos;
normal += rhs.normal;
pos += rhs.pos;
return *this;
}
VSOut operator+(const VSOut& rhs) const
{
return VSOut(*this) += rhs;
}
VSOut& operator*=(float v) {
proj_pos *= v;
normal *= v;
pos *= v;
return *this;
}
VSOut operator*(float rhs) const
{
return VSOut(*this) *= rhs;
}
void Lerp(const VSOut& v0, const VSOut& v1, const VSOut& v2, float a, float b, float c) noexcept {
normal = v0.normal * a + v1.normal * b + v2.normal * c;
pos = v0.pos * a + v1.pos * b + v2.pos * c;
}
};
class VertexShader : public VertexShaderMatHelper {
public:
VSOut operator()(const Vertex& v) const
{
glm::vec4 world = model * glm::vec4(v.position, 1.0f);
return {
proj_view * world,
obj_to_world_normal * glm::vec4(v.normal, 0.0f),
glm::vec3( world )
};
}
};
class PixelShader {
public:
std::shared_ptr<SceneContext> pContext;
glm::vec4 operator()(const VSOut& v, const VSOut& ddx, const VSOut& ddy, int modelId, int meshId) const
{
glm::vec3 color(v.normal.x, v.normal.y, v.normal.z);
color = glm::normalize(color) * 0.5f + glm::vec3(0.5f); //·¨Ïß-1~1 -> 0~1
return glm::vec4(color, 1.0f);
// skybox reflect in world space
//glm::vec3 N = glm::normalize(glm::vec3(v.normal));
//glm::vec3 I = glm::normalize(v.pos - pContext->camera_pos_cache);
//glm::vec3 R = glm::reflect(I, N);
//glm::vec4 color = pContext->skybox->Sample(R.x, R.y, R.z);
//return color;
// skybox refract (only once) in world space
//material ¦Ç
//air 1.00
//water 1.33
//ice 1.309
//glass 1.52
//diamond 2.42
//float eta = 1.00 / 1.52;
//glm::vec3 N = glm::normalize(glm::vec3(v.normal));
//glm::vec3 I = glm::normalize(v.pos - pContext->camera_pos_cache);
//glm::vec3 R = glm::refract(I, N, eta);
//glm::vec4 color = pContext->skybox->Sample(R.x, R.y, R.z);
//return color;
}
};
public:
VertexShader vs;
PixelShader ps;
};