接“问题五十四”,已经简单学习了bezier曲线曲面,知道了双三次bezier曲面的矩阵表示形式,同时也以此画出了曲面图形。
这一章节主要以对比bezier曲线曲面和b-spline曲线曲面的方式来简单学习b-spline曲线曲面。
56.1 b-spline曲线
56.2 b-spline曲面
56.3 b-spline的C++代码实现
----------------------------------------------parametric_surface.h ------------------------------------------
parametric_surface.h
#ifndef PARAMETRIC_SURFACE_H
#define PARAMETRIC_SURFACE_H
#include <hitable.h>
#include "material.h"
#include "log.h"
#define TYPE 3
/*
TYPE=1: sphere;
TYPE=2: horn;
TYPE=3: bezier3;
*/
#define CURVE 1
/*
CURVE=1: bezier
CURVE=2: b-spline
*/
class parametric_surface : public hitable
{
public:
parametric_surface() {}
#if ((TYPE == 1) || (TYPE ==2))
parametric_surface(vec3 cen, float a, float b, float c, float hy, material *m, float r, float t, float n) : center(cen), intercept_x(a), intercept_y(b), intercept_z(c), height_half_y(hy), ma(m), rho(r), theta(t), num(n) {}
#endif // TYPE
#if TYPE == 3
parametric_surface(material *m, float r, float t, float n, vec3 *cp) : ma(m), rho(r), theta(t), num(n) {
/*相比bicubic Bezier surface,bicubic b-spline surface只需要改动如下黄底部分代码*/
vec3 ctrl_points[4][4];
for (int i=0; i<16; i++) {
ctrl_points[(i/4)][(i%4)] = cp[i];
}
#if CURVE == 1//bezier
float matrix_t[4][4] = {
{ 1, 0, 0, 0},
{-3, 3, 0, 0},
{ 3, -6, 3, 0},
{-1, 3, -3, 1}};
float matrix[4][4] = {
{1, -3, 3, -1},