Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Aug 26, 2024
1 parent d79406b commit f81921d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 54 deletions.
91 changes: 43 additions & 48 deletions Frame/Source/Assignment/Assignment3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Eigen::Matrix4f get_view_matrix(const Eigen::Vector3f &eye_pos)
0,0,1,-eye_pos[2],
0,0,0,1;

view = translate*view;
view = translate * view;

return view;
}
Expand Down Expand Up @@ -97,10 +97,8 @@ Eigen::Vector3f vertex_shader(const vertex_shader_payload& payload)

Eigen::Vector3f normal_fragment_shader(const fragment_shader_payload& payload)
{
Eigen::Vector3f return_color = (payload.normal.head<3>().normalized() + Eigen::Vector3f(1.0f, 1.0f, 1.0f)) / 2.f;
Eigen::Vector3f result;
result << return_color.x() * 255, return_color.y() * 255, return_color.z() * 255;
return result;
Eigen::Vector3f color = (payload.normal.head<3>().normalized() + Eigen::Vector3f{ 1.0f, 1.0f, 1.0f }) / 2.0f;
return color * 255.0f;
}

struct light
Expand All @@ -111,24 +109,24 @@ struct light

Eigen::Vector3f phong_fragment_shader(const fragment_shader_payload& payload)
{
static Eigen::Vector3f ka = Eigen::Vector3f(0.005f, 0.005f, 0.005f);
Eigen::Vector3f ka{ 0.005f, 0.005f, 0.005f };
Eigen::Vector3f kd = payload.color;
static Eigen::Vector3f ks = Eigen::Vector3f(0.7937f, 0.7937f, 0.7937f);
Eigen::Vector3f ks{ 0.7937f, 0.7937f, 0.7937f };

static std::vector<light> lights = {
light{{20.0f, 20.0f, 20.0f}, {500.0f, 500.0f, 500.0f}},
light{{-20.0f, 20.0f, 0.0f}, {500.0f, 500.0f, 500.0f}} };
std::vector<light> lights = {
light{{ 20.0f, 20.0f, 20.0f }, { 500.0f, 500.0f, 500.0f }},
light{{ -20.0f, 20.0f, 0.0f }, { 500.0f, 500.0f, 500.0f }} };

static Eigen::Vector3f amb_light_intensity{5.0f, 5.0f, 5.0f };
static Eigen::Vector3f eye_pos{0.0f, 0.0f, 10.0f };
Eigen::Vector3f amb_light_intensity{ 5.0f, 5.0f, 5.0f };
Eigen::Vector3f eye_pos{ 0.0f, 0.0f, 10.0f };

constexpr static float p = 150.0f;
constexpr float p = 150.0f;

Eigen::Vector3f normal = payload.normal.normalized();
Eigen::Vector3f point = payload.view_pos;
Eigen::Vector3f viewDir = (eye_pos - point).normalized();

Eigen::Vector3f color = { 0.0f, 0.0f, 0.0f };
Eigen::Vector3f color{ 0.0f, 0.0f, 0.0f };

for (auto& light : lights)
{
Expand All @@ -152,30 +150,27 @@ Eigen::Vector3f phong_fragment_shader(const fragment_shader_payload& payload)

Eigen::Vector3f texture_fragment_shader(const fragment_shader_payload &payload)
{
// 好像 OpenCV 读取 png 时的返回值范围是 [0, 255],而非 [0, 1]
static constexpr float reciprocal = 1.0f / 255.0f;

static Eigen::Vector3f ka = Eigen::Vector3f(0.005f, 0.005f, 0.005f);
Eigen::Vector3f ka{ 0.005f, 0.005f, 0.005f };
Eigen::Vector3f kd =
payload.texture ?
payload.texture->getColorBilinear(payload.tex_coords.x(), payload.tex_coords.y()) * reciprocal :
payload.texture->getColorBilinear(payload.tex_coords.x(), payload.tex_coords.y()) / 255.0f :
payload.color;
static Eigen::Vector3f ks = Eigen::Vector3f(0.7937f, 0.7937f, 0.7937f);
Eigen::Vector3f ks{ 0.7937f, 0.7937f, 0.7937f };

static std::vector<light> lights = {
light{{20.0f, 20.0f, 20.0f}, {500.0f, 500.0f, 500.0f}},
light{{-20.0f, 20.0f, 0.0f}, {500.0f, 500.0f, 500.0f}} };
std::vector<light> lights = {
light{{ 20.0f, 20.0f, 20.0f}, {500.0f, 500.0f, 500.0f }},
light{{ -20.0f, 20.0f, 0.0f}, {500.0f, 500.0f, 500.0f }} };

static Eigen::Vector3f amb_light_intensity{ 5.0f, 5.0f, 5.0f };
static Eigen::Vector3f eye_pos{ 0.0f, 0.0f, 10.0f };
Eigen::Vector3f amb_light_intensity{ 5.0f, 5.0f, 5.0f };
Eigen::Vector3f eye_pos{ 0.0f, 0.0f, 10.0f };

constexpr static float p = 150.0f;
constexpr float p = 150.0f;

Eigen::Vector3f normal = payload.normal.normalized();
Eigen::Vector3f point = payload.view_pos;
Eigen::Vector3f viewDir = (eye_pos - point).normalized();

Eigen::Vector3f color = { 0.0f, 0.0f, 0.0f };
Eigen::Vector3f color{ 0.0f, 0.0f, 0.0f };

for (auto &light : lights)
{
Expand All @@ -199,8 +194,8 @@ Eigen::Vector3f texture_fragment_shader(const fragment_shader_payload &payload)

Eigen::Vector3f bump_fragment_shader(const fragment_shader_payload &payload)
{
constexpr static float kh = 0.2f;
constexpr static float kn = 0.1f;
constexpr float kh = 0.2f;
constexpr float kn = 0.1f;

Eigen::Vector3f normal = payload.normal.normalized();

Expand Down Expand Up @@ -232,28 +227,28 @@ Eigen::Vector3f bump_fragment_shader(const fragment_shader_payload &payload)
float du = kh * kn * (height_u - height);
float dv = kh * kn * (height_v - height);

Eigen::Vector3f localNormal = Eigen::Vector3f{ -du, -dv, 1.0f };
Eigen::Vector3f localNormal{ -du, -dv, 1.0f };
normal = (TBN * localNormal).normalized();

return normal * 255.f;
return normal * 255.0f;
}

Eigen::Vector3f displacement_fragment_shader(const fragment_shader_payload& payload)
{
static Eigen::Vector3f ka = Eigen::Vector3f(0.005f, 0.005f, 0.005f);
Eigen::Vector3f ka = Eigen::Vector3f(0.005f, 0.005f, 0.005f);
Eigen::Vector3f kd = payload.color;
static Eigen::Vector3f ks = Eigen::Vector3f(0.7937f, 0.7937f, 0.7937f);
Eigen::Vector3f ks = Eigen::Vector3f(0.7937f, 0.7937f, 0.7937f);

static std::vector<light> lights = {
light{{20.0f, 20.0f, 20.0f}, {500.0f, 500.0f, 500.0f}},
light{{-20.0f, 20.0f, 0.0f}, {500.0f, 500.0f, 500.0f}} };
std::vector<light> lights = {
light{{ 20.0f, 20.0f, 20.0f }, { 500.0f, 500.0f, 500.0f }},
light{{ -20.0f, 20.0f, 0.0f }, { 500.0f, 500.0f, 500.0f }} };

static Eigen::Vector3f amb_light_intensity{ 5.0f, 5.0f, 5.0f };
static Eigen::Vector3f eye_pos{ 0.0f, 0.0f, 10.0f };
Eigen::Vector3f amb_light_intensity{ 5.0f, 5.0f, 5.0f };
Eigen::Vector3f eye_pos{ 0.0f, 0.0f, 10.0f };

constexpr static float kh = 0.2f;
constexpr static float kn = 0.1f;
constexpr static float p = 150.0f;
constexpr float kh = 0.2f;
constexpr float kn = 0.1f;
constexpr float p = 150.0f;

Eigen::Vector3f point = payload.view_pos;
Eigen::Vector3f normal = payload.normal.normalized();
Expand Down Expand Up @@ -291,11 +286,11 @@ Eigen::Vector3f displacement_fragment_shader(const fragment_shader_payload& payl
// 将着色点沿着原法线的方向进行位移
point += kn * normal * height;

Eigen::Vector3f localNormal = Eigen::Vector3f{ -du, -dv, 1.0f };
Eigen::Vector3f localNormal{ -du, -dv, 1.0f };
normal = (TBN * localNormal).normalized();
}

Eigen::Vector3f color = { 0.0f, 0.0f, 0.0f };
Eigen::Vector3f color{ 0.0f, 0.0f, 0.0f };

for (auto &light : lights)
{
Expand All @@ -316,7 +311,7 @@ Eigen::Vector3f displacement_fragment_shader(const fragment_shader_payload& payl
color += (ambient + diffuse + specular);
}

return color * 255.f;
return color * 255.0f;
}

int main()
Expand All @@ -326,10 +321,10 @@ int main()
Loader.LoadFile(Utils::PathFromAsset("model/spot/spot_triangulated_good.obj"));
for(auto &mesh : Loader.LoadedMeshes)
{
for(int i = 0; i < mesh.Vertices.size(); i += 3)
for(size_t i = 0; i < mesh.Vertices.size(); i += 3)
{
Triangle* t = new Triangle();
for(int j = 0; j < 3; ++j)
for(size_t j = 0; j < 3; ++j)
{
t->setVertex(j, Vector4f(mesh.Vertices[i + j].Position.X, mesh.Vertices[i + j].Position.Y, mesh.Vertices[i + j].Position.Z, 1.0));
t->setNormal(j, Vector3f(mesh.Vertices[i + j].Normal.X, mesh.Vertices[i + j].Normal.Y, mesh.Vertices[i + j].Normal.Z));
Expand All @@ -342,8 +337,8 @@ int main()
int key = 0;
int frame_count = 0;
float angle = 140.0f;
rst::rasterizer r(700, 700);
Eigen::Vector3f eye_pos = { 0.0f,0.0f,10.0f };
rst::rasterizer r{ 700, 700 };
Eigen::Vector3f eye_pos{ 0.0f,0.0f,10.0f };

// normal_fragment_shader
// phong_fragment_shader
Expand Down
4 changes: 0 additions & 4 deletions Frame/Source/Assignment/Assignment6/BVH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ BVHBuildNode* BVHAccel::recursiveBuild(std::vector<Object*> objects)
#define ENABLE_SAH 1

#if ENABLE_SAH

// 一份比较清晰的 SAH 介绍:https://zhuanlan.zhihu.com/p/50720158

// 划分方式的总数。
Expand Down Expand Up @@ -141,12 +140,9 @@ BVHBuildNode* BVHAccel::recursiveBuild(std::vector<Object*> objects)
}

const auto &target = objects.begin() + (objects.size() * minCostIndex * SlashCountInv);

#else // ENABLE_SAH

// 基本的 BVH 划分方式,按数量从中间一分为二。
const auto &target = objects.begin() + (objects.size() / 2);

#endif // ENABLE_SAH

auto leftObjects = std::vector<Object *>(begin, target);
Expand Down
6 changes: 5 additions & 1 deletion Frame/Source/Assignment/Assignment7/Material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ Vector3f Material::eval(const Vector3f &wi, const Vector3f &wo, const Vector3f &
Vector3f diffuseBRDF = m_albedo * MY_INV_PI;

return KD * diffuseBRDF + specularBRDF;

break;
}
case MaterialType::MIRROR:
Expand All @@ -224,5 +223,10 @@ Vector3f Material::eval(const Vector3f &wi, const Vector3f &wo, const Vector3f &
return Vector3f{ 1.0f , 1.0f , 1.0f };
break;
}
default:
{
return Vector3f{ 0.0f, 0.0f, 0.0f };
break;
}
}
}
2 changes: 1 addition & 1 deletion Frame/Source/Assignment/Assignment7/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Vector3f Scene::castRay(const Ray &ray, uint32_t depth) const
float distanceToLight = (positionLight - position).norm();
float distanceToInter = intersect(Ray{ position, lightDir }).distance;

// 这里有点抽象,EPSILON 大概取 0.00001f 结果就会出现黑条纹,原理未知。
// EPSILON 大概取 0.00001f 结果就会出现黑条纹,原理未知。
if (Utils::FloatEqual(distanceToInter, distanceToLight, 0.0001f))
{
// 着色点与光源之间无阻挡。
Expand Down

0 comments on commit f81921d

Please sign in to comment.