您好,登錄后才能下訂單哦!
這篇文章主要講解了使用OpenGL繪制Bezier曲線的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
源碼見下
#include <windows.h> #include <math.h> #include <gl/GL.h> #include <gl/glut.h> int SCREEN_HEIGHT = 480; int NUMPOINTS = 0; class Point { public: float x, y; void setxy(float x2, float y2) { x = x2; y = y2; } Point operator&(const Point & rPoint) { x = rPoint.x; y = rPoint.y; return * this; } }; Point abc[3]; 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(); } //三個(gè)控制點(diǎn)的貝塞爾曲線 Point drawBezier(Point A, Point B, Point C, double t) { Point P; P.x = pow((1-t), 2) * A.x + 2*t*(1-t)*B.x + pow(t, 2)*C.x; P.y = pow((1-t), 2) * A.y + 2*t*(1-t)*B.y + pow(t, 2)*C.y; return P; } 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 == 3) { glColor3f(1.0, 0.0, 1.0); drawDot(abc[0]); drawDot(abc[1]); drawDot(abc[2]); glColor3f(1.0, 1.0, 0.0); drawLine(abc[0], abc[1]); drawLine(abc[1], abc[2]); glColor3f(0.0, 1.0, 1.0); Point POld = abc[0]; for (double t = 0.0; t<=1.0;t+=0.1) { Point P = drawBezier(abc[0], abc[1], abc[2], t); drawLine(POld, P); POld = P; } glColor3f(1.0, 0.0, 0.0); NUMPOINTS = 0; } } } void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); glFlush(); } int main(int argc, char * agrv[]) { glutInit(&argc, agrv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow("Bezier Curve"); glutMouseFunc(myMouse); glutDisplayFunc(myDisplay); myInit(); glutMainLoop(); return 0; }
看完上述內(nèi)容,是不是對使用OpenGL繪制Bezier曲線的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。
億速云公眾號
手機(jī)網(wǎng)站二維碼
Copyright ? Yisu Cloud Ltd. All Rights Reserved. 2018 版權(quán)所有
廣州億速云計(jì)算有限公司粵ICP備17096448號-1 粵公網(wǎng)安備 44010402001142號增值電信業(yè)務(wù)經(jīng)營許可證編號:B1-20181529