计算机图形学作业:三阶贝塞尔曲面

题目要求

编写程序,设计一个三阶的 Bezier 曲面,在给定控制点的情况下,计算出 Bezier 曲面上的点(设 u,v 方向的曲面精度可通过参数设置),然后将这些点保存到数组中。如果该 Bezier 曲面以三角面为基本图元进行存储,计算出三角形顶点的索引,并保存到数组中。

解答

//三阶(次)Bezier曲面
float R[4][4][3];//控制点三维数组,每一维分别为u,v方向,xyz坐标
typedef struct indexs {
    int m, n;
}Indexs;
Indexs indices[N];  //索引
void Bezier(float uc,float vc) //uc,vc,控制曲面精度的增量
{
//数组最大长度101,精度最大0.01
    float points_u[101][4][3] = { 0.0 };
    for (int j = 0; j < 4; j++) {
        int t = 0;
        for (double u = 0.0; u <= 1.0; u += uc)
        {
            float B0u = (1 - u) * (1 - u) * (1 - u);
            float B1u = 3 * (1 - u) * (1 - u) * u;
            float B2u = 3 * (1 - u) * u * u;
            float B3u = u * u * u;
            points_u[t][j][0] = R[0][j][0] * B0u + R[1][j][0] * B1u + R[2][j][0] * B2u + R[3][j][0] * B3u;
            points_u[t][j][1] = R[0][j][1] * B0u + R[1][j][1] * B1u + R[2][j][1] * B2u + R[3][j][1] * B3u;
            points_u[t][j][2] = R[0][j][2] * B0u + R[1][j][2] * B1u + R[2][j][2] * B2u + R[3][j][2] * B3u;
            t = t + 1;
        }
    }
 float points_uv[101][101][3] = { 0.0 };//存Bezier 曲面上的点
    for (int j = 0; j < 1.0/uc+1; j++) {
        int i = 0;
        for (double t = 0.0; t <= 1.0; t += vc)
        {
            float a1 = (1 - t) * (1 - t) * (1 - t);
            float a2 = 3 * (1 - t) * (1 - t) * t;
            float a3 = 3 * t * t * (1 - t);
            float a4 = t * t * t;
            points_uv[j][i][0] = a1 * points_u[j][0][0] + a2 * points_u[j][1][0] + a3 * points_u[j][2][0] + a4 * points_u[j][3][0];
            points_uv[j][i][1] = a1 * points_u[j][0][1] + a2 * points_u[j][1][1] + a3 * points_u[j][2][1] + a4 * points_u[j][3][1];
            points_uv[j][i][2] = a1 * points_u[j][0][2] + a2 * points_u[j][1][2] + a3 * points_u[j][2][2] + a4 * points_u[j][3][2];
            i = i + 1;
        }
}

int k = 0; //保存索引
    for (int i = 0; i < 1.0/vc+1; i++)
    {
        for (int j = 0; j < 1.0/uc+1; j++)
        {
            indices[k].m = j;
            indices[k].n = i;
            k++;
            indices[k].m = j;
            indices[k].n = i+1;
            k++;
            indices[k].m = j+1;
            indices[k].n = i;
            k++;
            indices[k].m = j + 1;
            indices[k].n = i;
            k++;
            indices[k].m = j ;
            indices[k].n = i+ 1;
            k++;
            indices[k].m = j + 1;
            indices[k].n = i + 1;
            k++;
        }
    }
}

参考:双三次Bezier曲面-CSDN博客

你可能感兴趣的:(计算机图形学实验/作业,c++)