#include "Triangle.hpp"
#include "rasterizer.hpp"
#include <eigen3/Eigen/Eigen>
#include <iostream>
#include <opencv2/opencv.hpp>
constexpr double MY_PI = 3.1415926;
Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos)
{
Eigen::Matrix4f view = Eigen::Matrix4f::Identity();
Eigen::Matrix4f translate;
translate << 1, 0, 0, -eye_pos[0], 0, 1, 0, -eye_pos[1], 0, 0, 1,
-eye_pos[2], 0, 0, 0, 1;
view = translate * view;
return view;
}
Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
// TODO: Implement this function
// Create the model matrix for rotating the triangle around the Z axis.
// Then return it.
float rAngleRad = rotation_angle / 180 * MY_PI;
model <<
cos(rAngleRad), -sin(rAngleRad), 0, 0,
sin(rAngleRad), cos(rAngleRad), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
return model;
}
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar)
{
// Students will implement this function
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
// TODO: Implement this function
// Create the projection matrix for the given parameters.
// Then return it.
float fov_rad = eye_fov / 180 * MY_PI;
float n = -zNear, f = -zFar;
float t = abs(n) * tan(fov_rad / 2);
float b = -t;
float r = t * aspect_ratio;
float l = -r;
Eigen::Matrix4f persp2ortho = Eigen::Matrix4f::Identity();
Eigen::Matrix4f ortho1 = Eigen::Matrix4f::Identity();
Eigen::Matrix4f ortho2 = Eigen::Matrix4f::Identity();
persp2ortho <<
n, 0, 0, 0,
0, n, 0, 0,
0, 0, n + f, -n * f,
0, 0, 1, 0;
ortho1 <<
1, 0, 0, -(r + l) / 2,
0, 1, 0, -(t + b) / 2,
0, 0, 1, -(n + f) / 2,
0, 0, 0, 1;
ortho2 <<
2 / (r - l), 0, 0, 0,
0, 2 / (t - b), 0, 0,
0, 0, 2 / (n - f), 0,
0, 0, 0, 1;
projection = ortho1 * ortho2 * persp2ortho;
return projection;
}
Eigen::Matrix4f get_rotation(Eigen::Vector3f axis, float angle)
{
//float angle_rad = angle / 180 * MY_PI;
//Eigen::Vector3f n;
//Eigen::Matrix3f I, N, Rodrigues;
//Eigen::Matrix4f rotation = Eigen::Matrix4f::Identity();
//n = axis.normalized();//归一化旋转轴
//I <<
// 1, 0, 0,
// 0, 1, 0,
// 0, 0, 1;
//N <<
// 0, -axis.z(), axis.y(),
// axis.z(), 0, -axis.x(),
// -axis.y(), axis.x(), 0;
//Rodrigues = cos(angle_rad) * I + (1 - cos(angle_rad)) * n * n.transpose() + sin(angle_rad) * N;
//rotation.block<3, 3>(0, 0) = Rodrigues;
//rotation(3, 3) = 1;
//return rotation;
angle = angle / 180 * MY_PI;
Eigen::Matrix4f any_rotation = Eigen::Matrix4f::Zero();
any_rotation(3, 3) = 1;
Eigen::Vector3f normal_axis = axis.normalized();
Eigen::Matrix3f mult_factor;
mult_factor << 0, -normal_axis.z(), normal_axis.y(),
normal_axis.z(), 0, -normal_axis.x(),
-normal_axis.y(), normal_axis.x(), 0;
mult_factor = cos(angle) * Eigen::Matrix3f::Identity()
+ (1 - cos(angle)) * normal_axis * normal_axis.transpose()
+ sin(angle) * mult_factor;
any_rotation.block(0, 0, 2, 2) = mult_factor.block(0, 0, 2, 2);
return any_rotation;
}
int main(int argc, const char** argv)
{
float angle = 0;
bool command_line = false;
std::string filename = "output.png";
if (argc >= 3) {
command_line = true;
angle = std::stof(argv[2]); // -r by default
if (argc == 4) {
filename = std::string(argv[3]);
}
}
rst::rasterizer r(700, 700);
Eigen::Vector3f eye_pos = {0, 0, 5};
std::vector<Eigen::Vector3f> pos{{2, 0, -2}, {0, 2, -2}, {-2, 0, -2}};
std::vector<Eigen::Vector3i> ind{{0, 1, 2}};
auto pos_id = r.load_positions(pos);//读入上述点的位置信息,并返回三角形的索引数
auto ind_id = r.load_indices(ind);//读入上述索引信息,并返回三角形的索引数
int key = 0;
int frame_count = 0;
if (command_line) {
r.clear(rst::Buffers::Color | rst::Buffers::Depth);//清除frame_buf和depth_buf
//MVP变换:
r.set_model(get_model_matrix(angle));
r.set_view(get_view_matrix(eye_pos));
r.set_projection(get_projection_matrix(45, 1, 0.1, 50));
r.draw(pos_id, ind_id, rst::Primitive::Triangle);//顶点id: pos_id、索引id: ind_id、图元绘制方法三角形
//把一个点向量转化为一张图片的。首先用Mat构造一个image,Mat(nrows, ncols, type, fillValue)
// //r.frame_buffer().data() 返回指针,指向vector的第一个元素的地址 C++11
// 然后转换一下类型,从32位3通道Float类型转换为8位3通道unsigned类型
cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());//取出frame_buffer内容生成图片
image.convertTo(image, CV_8UC3, 1.0f);//
cv::imwrite(filename, image);
return 0;
}
while (key != 27) {//AD左右旋转的输入
r.clear(rst::Buffers::Color | rst::Buffers::Depth);
// Eigen::Vector3f axis(0, 1, 0);//原本旋转的另一种表示方式
// r.set_model(get_rotation(axis, angle));
//r.set_model(get_model_matrix(angle));//原本的旋转
r.set_model(get_rotation(Eigen::Vector3f(-1, 1, 0), angle));//绕45度轴斜着旋转
r.set_view(get_view_matrix(eye_pos));
r.set_projection(get_projection_matrix(45, 1, 0.1, 50));
r.draw(pos_id, ind_id, rst::Primitive::Triangle);
cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());
image.convertTo(image, CV_8UC3, 1.0f);
cv::imshow("image", image);
key = cv::waitKey(10);
std::cout << "frame count: " << frame_count++ << '\n';
if (key == 'a') {
angle += 10;
}
else if (key == 'd') {
angle -= 10;
}
}
return 0;
}
//
// Created by goksu on 4/6/19.
//
#include <algorithm>
#include "rasterizer.hpp"
#include <opencv2/opencv.hpp>
#include <math.h>
#include <stdexcept>
rst::pos_buf_id rst::rasterizer::load_positions(const std::vector<Eigen::Vector3f> &positions)
{
auto id = get_next_id();
pos_buf.emplace(id, positions);
return {id};
}
rst::ind_buf_id rst::rasterizer::load_indices(const std::vector<Eigen::Vector3i> &indices)
{
auto id = get_next_id();
ind_buf.emplace(id, indices);
return {id};
}
// Bresenham's line drawing algorithm
// Code taken from a stack overflow answer: https://stackoverflow.com/a/16405254
void rst::rasterizer::draw_line(Eigen::Vector3f begin, Eigen::Vector3f end)
{
auto x1 = begin.x();
auto y1 = begin.y();
auto x2 = end.x();
auto y2 = end.y();
Eigen::Vector3f line_color = {255, 255, 255};
int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i;
dx=x2-x1;
dy=y2-y1;
dx1=fabs(dx);
dy1=fabs(dy);
px=2*dy1-dx1;
py=2*dx1-dy1;
if(dy1<=dx1)
{
if(dx>=0)
{
x=x1;
y=y1;
xe=x2;
}
else
{
x=x2;
y=y2;
xe=x1;
}
Eigen::Vector3f point = Eigen::Vector3f(x, y, 1.0f);
set_pixel(point,line_color);//把每个顶点的x,y 输入,转换为其在frame_buf中的下标位置也就是图像中的像素点位置,并且涂上色。
for(i=0;x<xe;i++)
{
x=x+1;
if(px<0)
{
px=px+2*dy1;
}
else
{
if((dx<0 && dy<0) || (dx>0 && dy>0))
{
y=y+1;
}
else
{
y=y-1;
}
px=px+2*(dy1-dx1);
}
// delay(0);
Eigen::Vector3f point = Eigen::Vector3f(x, y, 1.0f);
set_pixel(point,line_color);
}
}
else
{
if(dy>=0)
{
x=x1;
y=y1;
ye=y2;
}
else
{
x=x2;
y=y2;
ye=y1;
}
Eigen::Vector3f point = Eigen::Vector3f(x, y, 1.0f);
set_pixel(point,line_color);
for(i=0;y<ye;i++)
{
y=y+1;
if(py<=0)
{
py=py+2*dx1;
}
else
{
if((dx<0 && dy<0) || (dx>0 && dy>0))
{
x=x+1;
}
else
{
x=x-1;
}
py=py+2*(dx1-dy1);
}
// delay(0);
Eigen::Vector3f point = Eigen::Vector3f(x, y, 1.0f);
set_pixel(point,line_color);
}
}
}
auto to_vec4(const Eigen::Vector3f& v3, float w = 1.0f)
{
return Vector4f(v3.x(), v3.y(), v3.z(), w);
}
void rst::rasterizer::draw(rst::pos_buf_id pos_buffer, rst::ind_buf_id ind_buffer, rst::Primitive type)
{
if (type != rst::Primitive::Triangle)
{
throw std::runtime_error("Drawing primitives other than triangle is not implemented yet!");
}
auto& buf = pos_buf[pos_buffer.pos_id];//从pos_buf和ind_buf中取出顶点坐标和index
auto& ind = ind_buf[ind_buffer.ind_id];
float f1 = (100 - 0.1) / 2.0;
float f2 = (100 + 0.1) / 2.0;
Eigen::Matrix4f mvp = projection * view * model;
for (auto& i : ind)
{
Triangle t;
Eigen::Vector4f v[] = {
mvp * to_vec4(buf[i[0]], 1.0f),
mvp * to_vec4(buf[i[1]], 1.0f),
mvp * to_vec4(buf[i[2]], 1.0f)
};
for (auto& vec : v) {
vec /= vec.w();
}
for (auto & vert : v)
{
vert.x() = 0.5*width*(vert.x()+1.0);
vert.y() = 0.5*height*(vert.y()+1.0);
//vert.z() = vert.z() * (100 - 0.1) / 2.0 + (100 + 0.1) / 2.0;近平面为0.1,远平面为50,把[-1,1]区间的z值映射到近平面和原平面之间:
vert.z() = vert.z() * f1 + f2;
}
for (int i = 0; i < 3; ++i)
{
//然后把三个顶点赋予给三角形,按照index的指定顺序:
t.setVertex(i, v[i].head<3>());
t.setVertex(i, v[i].head<3>());
t.setVertex(i, v[i].head<3>());
}
t.setColor(0, 255.0, 0.0, 0.0);
t.setColor(1, 0.0 ,255.0, 0.0);
t.setColor(2, 0.0 , 0.0,255.0);
rasterize_wireframe(t);//执行线框绘制
}
}
void rst::rasterizer::rasterize_wireframe(const Triangle& t)
{
draw_line(t.c(), t.a());//绘制线段顺序为2-0,2-1,1-0
draw_line(t.c(), t.b());
draw_line(t.b(), t.a());
}
void rst::rasterizer::set_model(const Eigen::Matrix4f& m)
{
model = m;
}
void rst::rasterizer::set_view(const Eigen::Matrix4f& v)
{
view = v;
}
void rst::rasterizer::set_projection(const Eigen::Matrix4f& p)
{
projection = p;
}
void rst::rasterizer::clear(rst::Buffers buff)
{
if ((buff & rst::Buffers::Color) == rst::Buffers::Color)
{
std::fill(frame_buf.begin(), frame_buf.end(), Eigen::Vector3f{0, 0, 0});//初始化为0
}
if ((buff & rst::Buffers::Depth) == rst::Buffers::Depth)
{
std::fill(depth_buf.begin(), depth_buf.end(), std::numeric_limits<float>::infinity());//初始化为无穷大
}
}
rst::rasterizer::rasterizer(int w, int h) : width(w), height(h)
{
frame_buf.resize(w * h);
depth_buf.resize(w * h);
}
int rst::rasterizer::get_index(int x, int y)
{
return (height-y)*width + x;
}
void rst::rasterizer::set_pixel(const Eigen::Vector3f& point, const Eigen::Vector3f& color)
{
//old index: auto ind = point.y() + point.x() * width;
if (point.x() < 0 || point.x() >= width ||
point.y() < 0 || point.y() >= height) return;
auto ind = (height-1-point.y())*width + point.x();
frame_buf[ind] = color;
}