
OpenGL动态绘制Brezier曲线|OpenGL
发布日期:2021-05-07 06:46:20
浏览次数:21
分类:精选文章
本文共 3617 字,大约阅读时间需要 12 分钟。

#include#include #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")int SCREEN_HEIGHT = 600;int NUMPOINTS = 0;class Point{ public: float x, y; void setxy(float x2, float y2) { x = x2; y = y2; }};Point abc[4];Point bef[3];Point mid[2];Point link[1000];void myInit(){ glClearColor(0.0, 0.0, 0.0, 0.0); glColor3f(1.0f, 0.0, 0.0); glPointSize(4.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 640, 0.0, 480.0);}void drawDot(Point pt){ glBegin(GL_POINTS); glVertex2f(pt.x, pt.y); glEnd(); glFlush(); } void drawLine(Point p1, Point p2) { glBegin(GL_LINES); glVertex2f(p1.x, p1.y); glVertex2f(p2.x, p2.y); glEnd(); glFlush();}//四个控制点的贝塞尔曲线 即三次Bezier曲线 void drawBezier(Point A, Point B, Point C, Point D){ for (double t = 0.0; t <= 1.0; t += 0.001) { bef[0].setxy((1 - t)*A.x + t*B.x, (1 - t)*A.y + t*B.y); bef[1].setxy((1 - t)*B.x + t*C.x, (1 - t)*B.y + t*C.y); bef[2].setxy((1 - t)*C.x + t*D.x, (1 - t)*C.y + t*D.y); mid[0].setxy((1 - t)*bef[0].x + t*bef[1].x, (1 - t)*bef[0].y + t*bef[1].y); mid[1].setxy((1 - t)*bef[1].x + t*bef[2].x, (1 - t)*bef[1].y + t*bef[2].y); link[0].setxy((1 - t)*mid[0].x + t*mid[1].x, (1 - t)*mid[0].y + t*mid[1].y); glColor3f(1.0f, 1.0f, 0.0f); drawLine(bef[0], bef[1]); glColor3f(1.0f, 1.0f, 0.0f); drawLine(bef[1], bef[2]); glColor3f(1.0f, 1.0f, 0.0f); drawLine(mid[0], mid[1]); glColor3f(1.0f, 0.0f, 0.0f); drawDot(link[0]); glColor3f(0.0f, 0.0f, 0.0f); drawLine(bef[0], bef[1]); glColor3f(0.0f, 0.0f, 0.0f); drawLine(bef[1], bef[2]); glColor3f(0.0f, 0.0f, 0.0f); drawLine(mid[0], mid[1]); glColor3f(0.0, 1.0, 0.0); drawLine(abc[0], abc[1]); glColor3f(0.0, 1.0, 0.0); drawLine(abc[1], abc[2]); glColor3f(0.0, 1.0, 0.0); drawLine(abc[2], abc[3]); }}void myDisplay(){ glClear(GL_COLOR_BUFFER_BIT); glFlush();}void ChangeSize(GLsizei width, GLsizei height){ GLfloat aspect_init, aspect; glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); aspect_init = 4.0 / 3.0; aspect = 1.0*width / height; glMatrixMode(GL_PROJECTION); glLoadIdentity(); //Keep Aspect of Current Window if (aspect <= aspect_init) glViewport(0, (height - width / aspect) / 2, width, width / aspect_init); else glViewport((width - height*aspect_init) / 2, 0, height*aspect_init, height); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glOrtho(-2, 2, -2, 2, -2, 2);}void myMouse(int button, int state, int x, int y){ if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){ abc[NUMPOINTS].setxy((float)x, (float)(SCREEN_HEIGHT - y)); NUMPOINTS++; if (NUMPOINTS == 4) { glColor3f(1.0, 0.0, 1.0); drawDot(abc[0]); glColor3f(1.0, 0.0, 1.0); drawDot(abc[1]); glColor3f(1.0, 0.0, 1.0); drawDot(abc[2]); glColor3f(1.0, 0.0, 1.0); drawDot(abc[3]); glColor3f(0.0, 1.0, 0.0); drawLine(abc[0], abc[1]); glColor3f(0.0, 1.0, 0.0); drawLine(abc[1], abc[2]); glColor3f(0.0, 1.0, 0.0); drawLine(abc[2], abc[3]); //Point POld = abc[0]; drawBezier(abc[0], abc[1], abc[2], abc[3]); glColor3f(1.0, 0.0, 1.0); drawDot(abc[0]); glColor3f(0.0, 1.0, 0.0); drawLine(abc[0], abc[1]); glColor3f(0.0, 1.0, 0.0); drawLine(abc[1], abc[2]); glColor3f(0.0, 1.0, 0.0); drawLine(abc[2], abc[3]); glColor3f(1.0, 0.0, 0.0); NUMPOINTS = 0; } }}void main(int argc, char **argv){ glutInit(&argc, argv); // 在这里设置双缓冲区。 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(0, 0); glutInitWindowSize(800, 600); glutCreateWindow("bezier"); glutMouseFunc(myMouse); myInit(); //glutIdleFunc(RenderScene); glutDisplayFunc(myDisplay); glutMainLoop();}
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月09日 04时05分30秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
410. Split Array Largest Sum
2019-03-06
开源项目在闲鱼、b 站上被倒卖?这是什么骚操作?
2019-03-06
Vue3发布半年我不学,摸鱼爽歪歪,哎~就是玩儿
2019-03-06
《实战java高并发程序设计》源码整理及读书笔记
2019-03-06
Java开源博客My-Blog之docker容器组件化修改
2019-03-06
Java开源博客My-Blog(SpringBoot+Docker)系列文章
2019-03-06
程序员视角:鹿晗公布恋情是如何把微博搞炸的?
2019-03-06
《大型网站技术架构:核心原理与案例分析》读书笔记系列
2019-03-06
设置git同时推送github和gitee远程仓库
2019-03-06
【JavaScript】动态原型模式创建对象 ||为何不能用字面量创建原型对象?
2019-03-06
Linux应用-线程操作
2019-03-06
多态体验,和探索爷爷类指针的多态性
2019-03-06
操作系统知识点
2019-03-06
系统编程-进程间通信-无名管道
2019-03-06
记2020年初对SimpleGUI源码的阅读成果
2019-03-06
C语言实现面向对象方法学的GLib、GObject-初体验
2019-03-06
系统编程-进程-ps命令、进程调度、优先级翻转、进程状态
2019-03-06