Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
228 lines (194 loc) · 9.29 KB
/
Copy pathmain.cpp
File metadata and controls
228 lines (194 loc) · 9.29 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <iostream>
#include <memory>
#include "TGAImage.h"
#include "Model.h"
#include "Math.h"
#include "Render.h"
#include "Shader/IShader.h"
#include "Shader/PhongShader.h"
#include "Window.h"
#include "Scene.h"
#include "Shader/GBuffer.h"
#include "Camera.h"
#include "random"
#include "Shader/SkyBoxShader.h"
#include "Shader/PBRShader.h"
namespace FilePath {
auto head = "../obj/african_head/african_head.obj";
auto boogie = "../obj/boggie/body.obj";
auto diablo = "../obj/diablo3_pose/diablo3_pose.obj";
auto floor = "../obj/floor.obj";
auto gun = "../obj/gun/Cerberus.obj";
auto helmet = "../obj/helmet/helmet.obj";
auto skybox = "../obj/skybox3/box.obj";
}
constexpr int width = 2000; // output image size
constexpr int height = 2000;
Vec3f LightDir{-1.5, -1.5, -1.5};
Vec3f LightSpotLoc = -LightDir;
Vec3f Eye{0.6, 0, 1.5};
Vec3f Center{0, 0, 0};
Vec3f Up{0, 1, 0};
Camera camera(Eye, Center, Up, Vec2i(width, height), 0.0001, 1000);
std::random_device rd;
std::mt19937 RandomGen(rd());
std::uniform_real_distribution<float> UniformDis01(0.0f, 1.0f);
int main(int argc, char **argv) {
/*Model*/
//std::shared_ptr<Model> model_diablo = std::make_shared<Model>(FilePath::diablo);
//std::shared_ptr<Model> model_floor = std::make_shared<Model>(FilePath::floor);
std::shared_ptr<Model> model_helmet = std::make_shared<Model>(FilePath::helmet);
/*SkyBox*/
std::shared_ptr<Model> model_skybox = std::make_shared<Model>(FilePath::skybox);
model_skybox->loadSkyboxTexture(FilePath::skybox);
/*Scene*/
std::shared_ptr<Scene> scene = std::make_shared<Scene>();
scene->SetLightDir(LightDir);
// scene->Add(model_floor);
// scene->Add(model_diablo);
scene->Add(model_helmet);
scene->SetSkyBox(model_skybox);
/*GBuffer Create*/
auto& ShadowBuffer = *GBuffer::Get().AddBuffer<float>("ShadowBuffer", Vec2i{width, height});
auto& PenumbraBuffer = *GBuffer::Get().AddBuffer<float>("PenumbraBuffer", Vec2i{width, height});
auto& NormalBuffer = *GBuffer::Get().AddBuffer<Vec3f>("NormalBuffer", Vec2i{width, height});
auto& DepthBuffer = *GBuffer::Get().AddBuffer<float>("DepthBuffer", Vec2i{width, height}, std::numeric_limits<float>::max());
auto& ZBuffer = *GBuffer::Get().AddBuffer<float>("ZBuffer", Vec2i{width, height}, std::numeric_limits<float>::max());
/*Render*/
TGAImage image{width, height, TGAImage::RGB};
TGAImage image2{width, height, TGAImage::RGB};
TGAImage image3{width, height, TGAImage::RGB};
// std::shared_ptr<IShader> Shader_SkyBox = std::make_shared<SkyBoxShader>(scene->SkyBox, camera, scene);
// ChillRender::Render(model_skybox, Shader_SkyBox, camera, image2, ZBuffer, ChillRender::EFaceCulling::DisableFacingCulling);
// for (auto &model_WeakPtr: scene->GetAllModels()) {
// auto model = model_WeakPtr.lock();
//
// std::shared_ptr<IShader> Shader_dep = std::make_shared<DepthShder>(model, camera.ProjectionMatrix, lookat(LightSpotLoc, Center, Up), camera.ViewportMatrix);
//
// ChillRender::Render(model, Shader_dep, camera, image, DepthBuffer, ChillRender::EFaceCulling::DisableFacingCulling);
// }
//
// image.flip_vertically();
// image.write_tga_file("lightview_depth.tga");
// for (auto &model_WeakPtr: scene->GetAllModels()) {
// auto model = model_WeakPtr.lock();
// std::shared_ptr<IShader> Shader = std::make_shared<PhongShader>(model, camera, scene);
// ChillRender::Render(model, Shader, camera, image2, ZBuffer, ChillRender::EFaceCulling::BackFacingCulling);
// }
for (auto &model_WeakPtr: scene->GetAllModels()) {
auto model = model_WeakPtr.lock();
std::shared_ptr<IShader> Shader = std::make_shared<PBRShader>(model, camera, scene);
ChillRender::Render(model, Shader, camera, image2, ZBuffer, ChillRender::EFaceCulling::DisableFacingCulling);
}
image2.flip_vertically();//left-bottom is the origin
image2.write_tga_file("output.tga");
//Shadow: PCF
// int sampleHalfSize = 3;
// int sampleSize = 2*sampleHalfSize + 1;
// for(int i = sampleHalfSize; i < width - sampleHalfSize; i++)
// {
// for(int j = sampleHalfSize; j < height - sampleHalfSize; j++)
// {
// // if(!ShadowBuffer[i][j]) continue;
// int SampleShadowNumber = 0;
// for(int dx = -sampleHalfSize; dx <= sampleHalfSize; dx++)
// {
// for(int dy = -sampleHalfSize; dy <= sampleHalfSize; dy++)
// {
// SampleShadowNumber += ShadowBuffer[i + dx][j + dy];
// }
// }
// float SampleShadowRate = 1.0f*SampleShadowNumber / (sampleSize * sampleSize);
// float PCFShadowFactor = 1.0f - 0.7f*SampleShadowRate;
// image2.set(i ,j, image2.get(i,j) * PCFShadowFactor);
// }
// }
/*Shadow: PCSS*/
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int sampleHalfSize = PenumbraBuffer[i][j];
int sampleSize = 2 * sampleHalfSize + 1;
int SampleShadowNumber = 0;
//std::cout<<sampleHalfSize<<"\n";
if (i - sampleHalfSize >= 0 && i + sampleHalfSize < width && j - sampleHalfSize >= 0 &&
j + sampleHalfSize < height) {
for (int dx = -sampleHalfSize; dx <= sampleHalfSize; dx++) {
for (int dy = -sampleHalfSize; dy <= sampleHalfSize; dy++) {
SampleShadowNumber += ShadowBuffer[i + dx][j + dy];
}
}
float SampleShadowRate = 1.0f * SampleShadowNumber / (sampleSize * sampleSize);
float PCFShadowFactor = 1.0f - 0.7f * SampleShadowRate;
image2.set(i, j, image2.get(i, j) * PCFShadowFactor);
}
}
}
/* Post-process : ACES ToneMapping */
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
TGAColor color = image2.get(x, y);
ACESToneMapping({(float) color.raw[0], (float) color.raw[1], (float) color.raw[2]});
}
}
/* Post-process : SSAO */
Mat4x4 WorldSpaceMat2ScreenSpace = camera.ViewportMatrix * camera.ProjectionMatrix * camera.ViewMatrix;
Mat4x4 ScreenSpace2WorldSpaceMat = (camera.ProjectionMatrix * camera.ViewMatrix).Inverse();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (ZBuffer[x][y] > 1e5) continue;
Vec3f Coord_NDC{
(x - 1.0f * width / 2) / (1.0f * width / 2),
(y - 1.0f * height / 2) / (1.0f * height / 2),
ZBuffer[x][y]
};
Mat4x1 ScreenSpaceCoord = Mat4x1::Embed(Coord_NDC);
Mat4x1 WorldCoord = ScreenSpace2WorldSpaceMat * ScreenSpaceCoord;
WorldCoord /= WorldCoord.raw[3][0];
Vec3f n = NormalBuffer[x][y];
if (n.norm() == 0) continue;
int Count = 0;
int Total = 0;
int SampleTimes = 64;
float randR = 0.070f;
for (int i = 0; i < SampleTimes; i++) {
Vec3f SamplePoint = Vec3f{UniformDis01(RandomGen) * 2.0f - 1.0f,
UniformDis01(RandomGen) * 2.0f - 1.0f,
UniformDis01(RandomGen)};
Vec3f RandomVec = Vec3f{UniformDis01(RandomGen) * 2.0f - 1.0f,
UniformDis01(RandomGen) * 2.0f - 1.0f,
UniformDis01(RandomGen) * 2.0f - 1.0f};
//Gramm-Schmidt Process
Vec3f tangent = (RandomVec - NormalBuffer[x][y] * (RandomVec * NormalBuffer[x][y])).normlize();
Vec3f bitangent = NormalBuffer[x][y] ^ tangent;
Mat3x3 TBN{
{tangent.x, bitangent.x, NormalBuffer[x][y].x},
{tangent.y, bitangent.y, NormalBuffer[x][y].y},
{tangent.z, bitangent.z, NormalBuffer[x][y].z}
};
Vec3f SampleVec = SamplePoint;
SampleVec = (TBN * SampleVec.ToMatrix()).ToVec3f().normlize();
float scale = 1.0f * i / SampleTimes;
scale = std::lerp(0.1f, 1.0f, scale * scale);
SampleVec *= scale;
Mat4x1 SampleWorldCoord{
{WorldCoord.raw[0][0] + SampleVec.raw[0]},
{WorldCoord.raw[1][0] + SampleVec.raw[1]},
{WorldCoord.raw[2][0] + SampleVec.raw[2]},
{1.0f}
};
Mat4x1 SamplePointInScreenSpace = WorldSpaceMat2ScreenSpace * SampleWorldCoord;
SamplePointInScreenSpace /= SamplePointInScreenSpace.raw[3][0];
Vec2i ScreenXY(SamplePointInScreenSpace.raw[0][0], SamplePointInScreenSpace.raw[1][0]);
if (ScreenXY.x < width and ScreenXY.x >= 0 and ScreenXY.y >= 0 and ScreenXY.y < height) {
if (ZBuffer[ScreenXY.x][ScreenXY.y]*255.0f/2.0f + 255.0f/2.0f <= SamplePointInScreenSpace.raw[2][0]) Count++;//occu
Total++;
}
}
TGAColor c = TGAColor(255, 255, 255, 255) * (1 - 1.0f * Count / SampleTimes);
image3.set(x, y, c);
}
}
image3.flip_vertically();//left-bottom is the origin
image3.write_tga_file("AO.tga");
return 0;
}