不用求值器绘制双二次及双三次Bezier曲面
来源:互联网 发布:mac txt阅读器 编辑:程序博客网 时间:2024/06/08 04:26
之前在探索阶段,我曾经利用求值器获得过双三次Bezier曲面,今天我打算纯粹利用数学的方法计算出这两种曲面
。。。本来以为很容易。。结果一不小心就搞了一上午,主要是变成二维之后感觉各种凌乱,那我索性先把点的数组变成了点的实际情况的排列
实验证明这样会好很多,也就是酱
GLfloat ctrlPoints[3][3][3] ={{{ -0.7f, -0.2f, 0.3f }, { -0.05f, -0.2f, 0.6f }, { 0.5f, -0.2f, 0.4f },},{{ -0.7f, 0.3f, 0.3f }, { -0.1f, 0.2f, 0.7f }, { 0.45f, 0.25f, 0.4f },},{{ -0.7f, 0.8f, 0.1f }, { -0.1f, 0.8f, 0.2f },{ 0.4f, 0.85f, 0.1f },}};这样一看就知道哪个点在哪里,后面就用不着聒噪了。。
具体计算的方法是先从一个方向求三条Bezier曲线,然后再从另一个方向做Bezier曲线
GLfloat ps[11][3][3];for (int j = 0; j < 3; j++){GLint i = 0;for (double t = 0.0; t <= 1.0; t += 0.1){double a1 = pow((1 - t), 2);double a2 = 2*t*(1-t);double a3 = t*t;ps[i][j][0] = a1*ctrlPoints[0][j][0] + a2*ctrlPoints[1][j][0] + a3*ctrlPoints[2][j][0];ps[i][j][1] = a1*ctrlPoints[0][j][1] + a2*ctrlPoints[1][j][1] + a3*ctrlPoints[2][j][1];ps[i][j][2] = a1*ctrlPoints[0][j][2] + a2*ctrlPoints[1][j][2] + a3*ctrlPoints[2][j][2];i = i + 1;}}GLfloat ps1[11][11][3];for (int j = 0; j < 11; j++){GLint i = 0;for (double t = 0.0; t <= 1.0; t += 0.1){double a1 = pow((1 - t), 2);double a2 = 2 * t*(1 - t);double a3 = t*t;ps1[j][i][0] = a1*ps[j][0][0] + a2*ps[j][1][0] + a3*ps[j][2][0];ps1[j][i][1] = a1*ps[j][0][1] + a2*ps[j][1][1] + a3*ps[j][2][1];ps1[j][i][2] = a1*ps[j][0][2] + a2*ps[j][1][2] + a3*ps[j][2][2];i = i + 1;}}上完整代码!
#include <GL/glut.h> #include <stdio.h> #include <Windows.h> #include <stdlib.h> #include <math.h>GLfloat ctrlPoints[3][3][3] ={{{ -0.7f, -0.2f, 0.3f }, { -0.05f, -0.2f, 0.6f }, { 0.5f, -0.2f, 0.4f },},{{ -0.7f, 0.3f, 0.3f }, { -0.1f, 0.2f, 0.7f }, { 0.45f, 0.25f, 0.4f },},{{ -0.7f, 0.8f, 0.1f }, { -0.1f, 0.8f, 0.2f },{ 0.4f, 0.85f, 0.1f },}};void myDisplay(void){glClear(GL_COLOR_BUFFER_BIT);glRotatef(-65.0, 1.0, 0.3, 0.2);GLfloat ps[11][3][3];for (int j = 0; j < 3; j++){GLint i = 0;for (double t = 0.0; t <= 1.0; t += 0.1){double a1 = pow((1 - t), 2);double a2 = 2*t*(1-t);double a3 = t*t;ps[i][j][0] = a1*ctrlPoints[0][j][0] + a2*ctrlPoints[1][j][0] + a3*ctrlPoints[2][j][0];ps[i][j][1] = a1*ctrlPoints[0][j][1] + a2*ctrlPoints[1][j][1] + a3*ctrlPoints[2][j][1];ps[i][j][2] = a1*ctrlPoints[0][j][2] + a2*ctrlPoints[1][j][2] + a3*ctrlPoints[2][j][2];i = i + 1;}}GLfloat ps1[11][11][3];for (int j = 0; j < 11; j++){GLint i = 0;for (double t = 0.0; t <= 1.0; t += 0.1){double a1 = pow((1 - t), 2);double a2 = 2 * t*(1 - t);double a3 = t*t;ps1[j][i][0] = a1*ps[j][0][0] + a2*ps[j][1][0] + a3*ps[j][2][0];ps1[j][i][1] = a1*ps[j][0][1] + a2*ps[j][1][1] + a3*ps[j][2][1];ps1[j][i][2] = a1*ps[j][0][2] + a2*ps[j][1][2] + a3*ps[j][2][2];i = i + 1;}}glColor3f(1.0, 1.0, 0.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();}/* The following code displays the control points as dots. */glPointSize(5.0);glColor3f(1.0, 1.0, 0.0);for (int i = 0; i < 3; i++){glBegin(GL_POINTS);for (int j = 0; j < 3; j++)glVertex3fv(&ctrlPoints[i][j][0]);glEnd();}glColor3f(0.0, 1.0, 1.0);for (int i = 0; i < 3; i++){glBegin(GL_LINE_STRIP);for (int j = 0; j < 3; j++)glVertex3fv(&ctrlPoints[i][j][0]);glEnd();glBegin(GL_LINE_STRIP);for (int j = 0; j < 3; j++)glVertex3fv(&ctrlPoints[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;}
效果如下
如果有了之前的基础那么就会很容易实现,就是换换基函数,然后改改点坐标就好了
直接上完整代码
#include <GL/glut.h> #include <stdio.h> #include <Windows.h> #include <stdlib.h> #include <math.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.6f, 0.2f }, { 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);GLfloat ps[11][4][3];for (int j = 0; j < 4; j++){GLint i = 0;for (double t = 0.0; t <= 1.0; t += 0.1){double a1 = pow((1 - t), 3);double a2 = pow((1 - t), 2) * 3 * t;double a3 = 3 * t*t*(1 - t);double a4 = t*t*t;ps[i][j][0] = a1*ctrlPoints[0][j][0] + a2*ctrlPoints[1][j][0] + a3*ctrlPoints[2][j][0]+ a4*ctrlPoints[3][j][0];ps[i][j][1] = a1*ctrlPoints[0][j][1] + a2*ctrlPoints[1][j][1] + a3*ctrlPoints[2][j][1] + a4*ctrlPoints[3][j][1];ps[i][j][2] = a1*ctrlPoints[0][j][2] + a2*ctrlPoints[1][j][2] + a3*ctrlPoints[2][j][2] + a4*ctrlPoints[3][j][2];i = i + 1;}}GLfloat ps1[11][11][3];for (int j = 0; j < 11; j++){GLint i = 0;for (double t = 0.0; t <= 1.0; t += 0.1){double a1 = pow((1 - t), 3);double a2 = pow((1 - t), 2) * 3 * t;double a3 = 3 * t*t*(1 - t);double a4 = t*t*t;ps1[j][i][0] = a1*ps[j][0][0] + a2*ps[j][1][0] + a3*ps[j][2][0] + a4*ps[j][3][0];ps1[j][i][1] = a1*ps[j][0][1] + a2*ps[j][1][1] + a3*ps[j][2][1] + a4*ps[j][3][1];ps1[j][i][2] = a1*ps[j][0][2] + a2*ps[j][1][2] + a3*ps[j][2][2] + a4*ps[j][3][2];i = i + 1;}}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();}/* 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();}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;}和之前用求值器画出来的对比一下
反正我从肉眼是看不出多大差别来~~嘿嘿
4 0
- 不用求值器绘制双二次及双三次Bezier曲面
- 利用求值器绘制双三次Bezier曲面
- 利用求值器绘制三次Bezier曲线
- 使用基本Bezier求值器绘制曲线和曲面
- 二维求值器绘制曲面
- 双二次,三次有理Bezier曲线表示第一象限1/8球面
- 利用de Casteljau算法绘制Bezier曲面
- 利用de Casteljau算法绘制Bezier曲面
- 一次二次三次bezier曲线的计算方式及demo演示
- OpenGL Bezier曲线及曲面生成
- OpelGL绘制简单多面体及二次三次曲线
- OpenGL: 鼠标动态绘制三次Bezier曲线
- 计算机图形学-三次Bezier曲线的绘制
- 7.5.5编程实例-Bezier曲线曲面绘制
- Bezier曲线的原理 及 二次Bezier曲线的实现 .
- Bezier曲线的原理 及 二次Bezier曲线的实现
- bezier曲线和bezier曲面
- 中点法转化三次Bezier为二次Bezier,从C#到Flash
- 使用pxe方式安装系统
- linux 知识
- 如何查看自己电脑的IP和MAC
- 设计模式(十五)组合模式
- 剑指offer:和为S的连续正数序列
- 不用求值器绘制双二次及双三次Bezier曲面
- leetcode 1-Two Sum
- iOS程序启动内部过程(从点击图标到显示界面)
- HDU-ACM2023
- JAVA技术发展——你不知道的J2SE(三)
- mysql 默认引擎innodb 初探(一)
- Python 文件及文件夹操作记录
- js的Date对象
- HDU-ACM2024