上次的博客中我利用de Casteljau绘制了Bezier曲线,那么为了之后绘制Bezier的三角曲面片,还是先铺垫一下,绘制双三次的Bezier曲面
先上代码
#include<GL/GLUT.H> #include <windows.h> #include <math.h> #include <gl/GL.h> GLfloat ctrlPoints[4][4][3] = { { { -0.8f, -0.7f, 0.2f }, { -0.3f, -0.6f, 0.2f }, { 0.2f, -0.65f, 0.3f }, { 0.7f, -0.7f, 0.2f } }, { { -0.9f, -0.2f, 0.3f }, { -0.3f, 0.2f, 0.5f }, { 0.3f, -0.2f, 0.4f }, { 0.75f, -0.2f, 0.3f }, }, { { -0.9f, 0.3f, 0.3f }, { -0.3f, 0.2f, 0.5f }, { 0.25f, 0.25f, 0.6f }, { 0.8f, 0.3f, 0.3f }, }, { { -0.8f, 0.8f, 0.1f }, { -0.3f, 0.8f, 0.2f }, { 0.2f, 0.85f, 0.1f }, { 0.7f, 0.8f, 0.1f }, } }; void myDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glRotatef(-65.0, 1.0, 0.3, 0.2); /* The following code displays the control points as dots. */ glPointSize(5.0); glColor3f(1.0, 1.0, 0.0); for (int i = 0; i < 4; i++){ glBegin(GL_POINTS); for (int j = 0; j < 4; j++) glVertex3fv(&ctrlPoints[i][j][0]); glEnd(); } glColor3f(0.0, 1.0, 1.0); for (int i = 0; i < 4; i++){ glBegin(GL_LINE_STRIP); for (int j = 0; j < 4; j++) glVertex3fv(&ctrlPoints[i][j][0]); glEnd(); glBegin(GL_LINE_STRIP); for (int j = 0; j < 4; j++) glVertex3fv(&ctrlPoints[j][i][0]); glEnd(); } int n = 4;//number float xarray[4]; float yarray[4]; float zarray[4]; GLfloat ps[11][4][3]; for (int v = 0; v < 4;v++) { int u = 0; for(double t = 0.0;t<=1;t+=0.1) { for (int i = 1; i < n; ++i) { for (int j = 0; j < n - i; ++j) { if (i == 1) // i==1时,第一次迭代,由已知控制点计算 { xarray[j] = ctrlPoints[j][v][0] * (1 - t) + ctrlPoints[j+1][v][0] * t; yarray[j] = ctrlPoints[j][v][1] * (1 - t) + ctrlPoints[j+1][v][1] * t; zarray[j] = ctrlPoints[j][v][2] * (1 - t) + ctrlPoints[j+1][v][2] * t; continue; } // i != 1时,通过上一次迭代的结果计算 xarray[j] = xarray[j] * (1 - t) + xarray[j + 1] * t; yarray[j] = yarray[j] * (1 - t) + yarray[j + 1] * t; zarray[j] = zarray[j] * (1 - t) + zarray[j + 1] * t; } } ps[u][v][0] = xarray[0]; ps[u][v][1] = yarray[0]; ps[u][v][2] = zarray[0]; u++; } } GLfloat ps1[11][11][3]; for (int v = 0; v < 11; v++){ int u = 0; for (double t = 0.0; t <= 1; t += 0.1) { for (int i = 1; i < n; ++i) { for (int j = 0; j < n - i; ++j) { if (i == 1) // i==1时,第一次迭代,由已知控制点计算 { xarray[j] = ps[v][j][0] * (1 - t) + ps[v][j + 1][0] * t; yarray[j] = ps[v][j][1] * (1 - t) + ps[v][j + 1][1] * t; zarray[j] = ps[v][j][2] * (1 - t) + ps[v][j + 1][2] * t; continue; } // i != 1时,通过上一次迭代的结果计算 xarray[j] = xarray[j] * (1 - t) + xarray[j + 1] * t; yarray[j] = yarray[j] * (1 - t) + yarray[j + 1] * t; zarray[j] = zarray[j] * (1 - t) + zarray[j + 1] * t; } } ps1[v][u][0] = xarray[0]; ps1[v][u][1] = yarray[0]; ps1[v][u][2] = zarray[0]; u++; } } glColor3f(1.0, 1.0, 1.0); for (int i = 0; i < 11; i++){ glBegin(GL_LINE_STRIP); for (int j = 0; j < 11; j++) glVertex3fv(&ps1[i][j][0]); glEnd(); glBegin(GL_LINE_STRIP); for (int j = 0; j < 11; j++) glVertex3fv(&ps1[j][i][0]); glEnd(); } glFlush(); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(400, 400); glutCreateWindow("opengl1"); glutDisplayFunc(&myDisplay); glutMainLoop(); return 0; }
其实后来才发现这是我两篇博客http://blog.csdn.net/lafengxiaoyu/article/details/51295464和利用de算法求曲线的合体篇
。。。。
效果是酱的
O了,下一步的工作就要开始绘制三角面片了哦也