Skip to content

Commit

Permalink
HW3
Browse files Browse the repository at this point in the history
  • Loading branch information
T7imal committed Jul 17, 2023
1 parent 6a83c16 commit 83c4fba
Show file tree
Hide file tree
Showing 47 changed files with 11,109 additions and 277 deletions.
Binary file not shown.
Binary file modified Homework2/代码框架/build/Rasterizer
Binary file not shown.
14 changes: 4 additions & 10 deletions Homework2/代码框架/rasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,10 @@ void rst::rasterizer::rasterize_triangle(const Triangle& t) {

// TODO : Find out the bounding box of current triangle.
// iterate through the pixel and find if the current pixel is inside the triangle
float xMin = 0;
float yMin = 0;
float xMax = width;
float yMax = width;
for (int i = 0;i < 3;++i) {
xMin = std::min(xMin, v[i][0]);
yMin = std::min(yMin, v[i][1]);
xMax = std::max(xMax, v[i][0]);
yMax = std::max(yMax, v[i][1]);
}
int xMin = std::min(std::min(v[0].x(), v[1].x()), v[2].x());
int yMin = std::min(std::min(v[0].y(), v[1].y()), v[2].y());
int xMax = std::max(std::max(v[0].x(), v[1].x()), v[2].x());
int yMax = std::max(std::max(v[0].y(), v[1].y()), v[2].y());

// If so, use the following code to get the interpolated z value.
//auto[alpha, beta, gamma] = computeBarycentric2D(x, y, t.v);
Expand Down
21 changes: 16 additions & 5 deletions Homework3/Assignment3/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#include "global.hpp"
#include <eigen3/Eigen/Eigen>
#include <opencv2/opencv.hpp>
class Texture{
class Texture {
private:
cv::Mat image_data;

public:
Texture(const std::string& name)
{
Texture(const std::string& name) {
image_data = cv::imread(name);
cv::cvtColor(image_data, image_data, cv::COLOR_RGB2BGR);
width = image_data.cols;
Expand All @@ -22,13 +21,25 @@ class Texture{

int width, height;

Eigen::Vector3f getColor(float u, float v)
{
Eigen::Vector3f getColor(float u, float v) {
auto u_img = u * width;
auto v_img = (1 - v) * height;
auto color = image_data.at<cv::Vec3b>(v_img, u_img);
return Eigen::Vector3f(color[0], color[1], color[2]);
}

Eigen::Vector3f getColorBilinear(float u, float v) {
auto u_img = u * width;
auto v_img = (1 - v) * height;
auto color00 = image_data.at<cv::Vec3b>(v_img, u_img);
auto color01 = image_data.at<cv::Vec3b>(v_img, u_img + 1);
auto color10 = image_data.at<cv::Vec3b>(v_img + 1, u_img);
auto color11 = image_data.at<cv::Vec3b>(v_img + 1, u_img + 1);
auto color0 = color00 * ((float)((int)(u_img + 1)) - u_img) + color01 * (u_img - (float)((int)u_img));
auto color1 = color10 * ((float)((int)(u_img + 1)) - u_img) + color11 * (u_img - (float)((int)u_img));
auto color = color0 * ((float)((int)(v_img + 1)) - v_img) + color1 * (v_img - (float)((int)v_img));
return Eigen::Vector3f(color[0], color[1], color[2]);
}

};
#endif //RASTERIZER_TEXTURE_H
Loading

0 comments on commit 83c4fba

Please sign in to comment.