GAMES101-现代计算机图形学学习笔记(5)作业4

前言

上篇作业3

本篇继续更新作业4相关内容

作业4相关链接

games的作业4链接

我的源码

作业4简述

  • 实现贝塞尔曲线
  • (提高)对贝塞尔曲线实现反走样

作业4相关知识笔记

  • 贝塞尔曲线相关

作业4思路

此处我考虑的是迭代。

cv::Point2f recursive_bezier(const std::vector &control_points, float t) 
{
    // TODO: Implement de Casteljau's algorithm
    if(control_points.size()==2) return (1-t) * control_points[0] + t * control_points[1];

    std::vector temp;

    for (int i=0;i
void bezier(const std::vector &control_points, cv::Mat &window) 
{
    // TODO: Iterate through all t = 0 to t = 1 with small steps, and call de Casteljau's 
    // recursive Bezier algorithm.
    
    for (double t= 0.0;t<=1.0;t+=0.001){
        auto p = recursive_bezier (control_points,t);
        window.at(p.y,p.x)[1] =255;
    }
    
}

结果:
GAMES101-现代计算机图形学学习笔记(5)作业4_第1张图片

提高部分:

计算相邻4个边缘点的像素颜色,此处有4种情况,

我们将点所在的像素颜色设为a(不变),那么根据距离再计算相邻3个像素的颜色。

void bezier(const std::vector &control_points, cv::Mat &window) 
{
    // TODO: Iterate through all t = 0 to t = 1 with small steps, and call de Casteljau's 
    // recursive Bezier algorithm.
    //Bilinear
    
    for (double t= 0.0;t<=1.0;t+=0.001){
        auto p = recursive_bezier (control_points,t);
        

        float dx = p.x - std::floor(p.x);
        float dy = p.y - std::floor(p.y);
        cv::Point2f near;
        cv::Point2f p00;
        cv::Point2f p01;
        cv::Point2f p10;
        cv::Point2f p11;

        if(dx<=0.5f && dy<=0.5f){
            p00 = cv::Point2f(std::floor(p.x),std::floor(p.y));
            p01 = cv::Point2f(std::floor(p.x),std::floor(p.y)+1);
            p10 = cv::Point2f(std::floor(p.x)+1,std::floor(p.y));
            p11 = cv::Point2f(std::floor(p.x)+1,std::floor(p.y)+1);
            near = p00;
        }
        if(dx<=0.5f && dy>0.5f){
            p00 = cv::Point2f(std::floor(p.x)-1,std::floor(p.y));
            p01 = cv::Point2f(std::floor(p.x)-1,std::floor(p.y)+1);
            p10 = cv::Point2f(std::floor(p.x),std::floor(p.y));
            p11 = cv::Point2f(std::floor(p.x),std::floor(p.y)+1);
            near = p10;
        }
        if(dx>0.5f && dy<=0.5f){
            p00 = cv::Point2f(std::floor(p.x),std::floor(p.y)-1);
            p01 = cv::Point2f(std::floor(p.x),std::floor(p.y));
            p10 = cv::Point2f(std::floor(p.x)+1,std::floor(p.y)-1);
            p11 = cv::Point2f(std::floor(p.x)+1,std::floor(p.y));
            near = p01;
        }
        if(dx>0.5f && dy>0.5f){
            p00 = cv::Point2f(std::floor(p.x)-1,std::floor(p.y)-1);
            p01 = cv::Point2f(std::floor(p.x)-1,std::floor(p.y));
            p10 = cv::Point2f(std::floor(p.x),std::floor(p.y)-1);
            p11 = cv::Point2f(std::floor(p.x),std::floor(p.y));
            near = p11;
        }

        std::vector vec;
        vec.push_back(p00);
        vec.push_back(p01);
        vec.push_back(p10);
        vec.push_back(p11);

        float distance = sqrt((p-near).x * (p-near).x + (p-near).y * (p-near).y);
        
        for(auto point:vec){
            float d = sqrt((p-point).x * (p-point).x + (p-point).y * (p-point).y);
            float ratio = distance/d;
            window.at(p.y,p.x)[1] =255;            
            cv::Vec3b color = window.at(p.y,p.x);
            window.at(point.y,point.x) = ratio * color;
        }
    }   
}

对比:
GAMES101-现代计算机图形学学习笔记(5)作业4_第2张图片

你可能感兴趣的:(GAMES101-现代计算机图形学学习笔记(5)作业4)