bugfix: color shadow

This commit is contained in:
Shuo Feng 2024-02-27 23:36:12 -05:00
parent 43a8204032
commit a8a00d386e
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
5 changed files with 19 additions and 9 deletions

View file

@ -2,6 +2,10 @@
#include <Eigen/Dense>
Vector3f Geometry::diffuse() const { return cd; }
Vector3f Geometry::specular() const { return cs; }
Vector3f Geometry::ambient() const { return ca; }
void Geometry::setTransform(const Matrix4f &transform) {
this->transform = transform;
}

View file

@ -29,6 +29,9 @@ protected:
Matrix4f transform = Matrix4f::Identity();
public:
Vector3f diffuse() const;
Vector3f specular() const;
Vector3f ambient() const;
void setTransform(const Matrix4f &);
};

View file

@ -10,7 +10,7 @@ void Output::write() {
for (unsigned int x = 0; x < width; ++x)
fout << (char)(255.0f * red[y * width + x])
<< (char)(255.0f * green[y * width + x])
<< (char)(255.0f * blue[y * height + x]);
<< (char)(255.0f * blue[y * width + x]);
fout.close();
}

View file

@ -12,9 +12,10 @@ using std::vector;
class Output {
public:
Output(const Vector3f &bgc, string path, int w, int h)
: red(vector<float>(w * h, bgc.x())),
green(vector<float>(w * h, bgc.y())),
blue(vector<float>(w * h, bgc.z())), path(path), width(w), height(h) {}
: red(vector<float>(w * h + 1, bgc.x())),
green(vector<float>(w * h + 1, bgc.y())),
blue(vector<float>(w * h + 1, bgc.z())), path(path), width(w),
height(h) {}
void write();

View file

@ -47,14 +47,16 @@ void RayTracer::render(Scene *scene) {
for (int x = 0; x < width; ++x) {
Ray ray = getRay(x, y, cameraPos, pxUpperLeft, du, dv);
for (auto geometry : geometries)
if (geometry->intersect(ray)) {
buffer->r(y * width + x, 1);
buffer->g(y * width + x, 1);
buffer->b(y * width + x, 1);
for (auto g : geometries)
if (g->intersect(ray)) {
Vector3f diffuse = g->diffuse();
buffer->r(y * width + x, diffuse.x());
buffer->g(y * width + x, diffuse.y());
buffer->b(y * width + x, diffuse.z());
break;
}
}
outputs.push_back(buffer);
}