软件光栅化渲染器(九)

来源:互联网 发布:宁波行知小学总课表 编辑:程序博客网 时间:2024/04/19 10:46

光照计算

暂时只加入了点光源

//点光源class PointLight :public Light{public:    //衰减系数    double kc;    double kl;    double kq;    Color CalculateColor(const Vertex3D &vertex, const Material &material, const Point3D &objectPos)    {        //计算环境光        double r = material.ra.r;        double g = material.ra.g;        double b = material.ra.b;        //计算散射光        Vector3D l = position- vertex.vertex;               double d = CalculateVector3DLength(l);        double dp = VectorDot(vertex.normal, l);        if (dp > 0)        {            double atten = kc + kl * d + kq *d * d;            r += material.rd.r * dp / atten;            g += material.rd.g * dp / atten;            b += material.rd.b * dp / atten;        }        //计算镜面高光        VectorNormalize(l);        double x = l.x - 2 * VectorDot(l, vertex.normal) * vertex.normal.x;        double y = l.y - 2 * VectorDot(l, vertex.normal) * vertex.normal.y;        double z = l.z - 2 * VectorDot(l, vertex.normal) * vertex.normal.z;        Vector3D rVector = { x,y,z,1 };        Vector3D v = objectPos - vertex.vertex;        double is =  pow(VectorDot(rVector, v), material.shininess);        if (is > 0)        {            r += material.rs.r * is;            g += material.rs.g * is;            b += material.rs.b * is;        }        //判断是否溢出        r = r > 1.0 ? 1.0 : r;        g = g > 1.0 ? 1.0 : g;        b = b > 1.0 ? 1.0 : b;        return Color(color.r *r, color.g *g, color.b *b);    }};

这里写图片描述
Github地址

原创粉丝点击