4次Bezier曲線--計算機圖形學 opengl

張日京發表於2018-04-15

計算機圖形學的第一次作業

參考了網上很多程式碼 但好歹寫出來了


#include "GL\glut.h"

#include <math.h>

 

class Point//一個點類,含有x,y座標

{

public:

       intx, y;

       voidinit(int x1, int y1) {

              x= x1;

              y= y1;

       }

};

 

 

//點的數量

static int PointSum = 0;

 

//四次的bezier曲線共有五個點

static Point points[5];

 

 

void init(void)

{

       glEnable(GL_DEPTH_TEST);

       glMatrixMode(GL_PROJECTION);

       glLoadIdentity();

       gluOrtho2D(0.0,500.0, 0.0, 500.0);

       glMatrixMode(GL_MODELVIEW);

 

}

 

 

//畫點

void setPoint(Point p) {

       glBegin(GL_POINTS);

       glVertex2f(p.x,p.y);

       glEnd();

       glFlush();

}

 

// 畫線

void setline(Point p1, Point p2) {

       glBegin(GL_LINES);

       glVertex2f(p1.x,p1.y);

       glVertex2f(p2.x,p2.y);

       glEnd();

       glFlush();

}

 

// 計算bezier曲線

Point calculateBezier(Point p0, Point p1,Point p2, Point p3, Point p4, double a) {

       Pointp;

       doubleb1 = pow((1 - a), 4);

       doubleb2 = pow((1 - a), 3) * 4 * a;

       doubleb3 = pow((1 - a), 2) * 6 * a * a;

       doubleb4 = (1 - a)*a*a*a * 4;

       doubleb5 = a * a*a*a;

 

       p.x= b1 * p0.x + b2 * p1.x + b3 * p2.x + b4 * p3.x + b5 * p4.x;

       p.y= b1 * p0.y + b2 * p1.y + b3 * p2.y + b4 * p3.y + b5 * p4.y;

       returnp;

}

 

//display函式

void display()

{

       glClearColor(0.88,0.71, 0.71, 0); //設背景為粉色

       glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); //用glClearClolr設定的顏色值清除快取區

       glColor3f(0.0,0.0, 0.0);

       glPointSize(5);

       glFlush();

}

 

 

// 滑鼠事件

void MouseFunction(int button, int state,int x, int y) {

 

      

       if(state == GLUT_DOWN)

       {

              points[PointSum].init(x,500 - y);

              glColor3f(0.86,0.25, 0.0);     // 設定點的顏色,繪製點

              setPoint(points[PointSum]);

              glColor3f(0.12,0.8, 0.25);// 設定線的顏色,繪製線

              if(PointSum > 0) setline(points[PointSum - 1], points[PointSum]);

              //如果達到了4個繪製bezier曲線,並在之後給計數器清零

              if(PointSum == 4) {

 

                     glColor3f(0.0,0.0, 0.0); // 設定bezier曲線為黑色

 

                     Pointp_current = points[0]; //設為起點

                     for(double t = 0.0; t <= 1.0; t += 0.05)

                     {

                            PointP = calculateBezier(points[0], points[1], points[2], points[3], points[4], t);

                            setline(p_current,P);

                            p_current= P;

                     }

 

                     PointSum= 0;

              }

              else{

                     PointSum++;

              }

       }

}

 

int main(int argc, char *argv[])

{

       glutInit(&argc,argv);

       glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB | GLUT_DEPTH);

       glutInitWindowPosition(0,0);

       glutInitWindowSize(500,500); //確定顯示框的大小

       glutCreateWindow("ZZJ的圖形學作業1.0");

       init();

       glutMouseFunc(MouseFunction);// 滑鼠事件

       glutDisplayFunc(display);

       glutMainLoop();

       return0;

}



相關文章