vs用opengl做bezier曲線,一直都畫不出來,求教大佬們那兒有錯啊
#include <GL/glut.h>
#include <iostream>
#include <iomanip>
using namespace std;
GLint winWidth = 600, winHeight = 600;
//GLfloat x0 = 100.0, y0 = 50.0, z0 = 50.0;
GLfloat xref = 200, yref = 200, zref = 200;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -100.0, ywMin = -100.0, xwMax = 100.0, ywMax = 100.0;
GLfloat dnear = -100.0, dfar = 100.0;
struct data
{
GLfloat x, y, z;
}Point[6];
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0); //設定背景顏色
glMatrixMode(GL_MODELVIEW);
gluLookAt(100, 50, 50, xref, yref, zref, Vx, Vy, Vz);
glMatrixMode(GL_PROJECTION); // 設定投影引數
glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
}
void Bernstain()
{
Point[0].x = 34.4507; Point[0].y = 64.8979; Point[0].z = 92.7423;
Point[1].x = 53.2133; Point[1].y = 62.9103; Point[1].z = 78.7443;
Point[2].x = 72.6174; Point[2].y = 58.5389; Point[2].z = 64.6679;
Point[3].x = 90.4922; Point[3].y = 53.8726; Point[3].z = 51.7908;
Point[4].x = 107.6262; Point[4].y = 50.2527; Point[4].z = 40.0300;
Point[5].x = 124.3115; Point[5].y = 48.5382; Point[5].z = 29.7099;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0); //設定線條顏色
glBegin(GL_LINE_STRIP);
glVertex3f(Point[0].x, Point[0].y, Point[0].z);
glVertex3f(Point[1].x, Point[1].y, Point[1].z);
glVertex3f(Point[2].x, Point[2].y, Point[2].z);
glVertex3f(Point[3].x, Point[3].y, Point[3].z);
glVertex3f(Point[4].x, Point[4].y, Point[4].z);
glVertex3f(Point[5].x, Point[5].y, Point[5].z);
glEnd();
glFlush();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0); //設定線條顏色
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= 100; i++)
{
GLfloat t = i / 100.0;
GLfloat b0 = pow(1 - t, 5.0);
GLfloat b1 = 5.0 * t * pow(1 - t, 4.0);
GLfloat b2 = 10.0 * t * t * pow(1 - t, 3.0);
GLfloat b3 = 10.0 * pow(t, 3.0) * pow(1 - t, 2.0);
GLfloat b4 = 5 * pow(t, 4.0) * (1.0 - t);
GLfloat b5 = pow(t, 5.0);
//運用五次Bezier曲線
GLfloat x = Point[0].x * b0 + Point[1].x * b1 + Point[2].x * b2 + Point[3].x * b3 + Point[4].x * b4 + Point[5].x * b5;
GLfloat y = Point[0].y * b0 + Point[1].y * b1 + Point[2].y * b2 + Point[3].y * b3 + Point[4].y * b4 + Point[5].y * b5;
GLfloat z = Point[0].z * b0 + Point[1].z * b1 + Point[2].z * b2 + Point[3].z * b3 + Point[4].z * b4 + Point[5].z * b5;
glVertex3f(x, y, z);
//cout << "p(" << t << ")" << " " << setw(10) << x << setw(10) << y << setw(10) << z << endl;
}
glEnd();
glFlush();
}
void main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Bezier曲線");
init();
glutDisplayFunc(Bernstain);
glutMainLoop();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98871.html
標籤:C++ 語言
