Bezier曲线使用c++opengl
Bezier curve using c++ opengl
我使用此代码通过单击一个点来绘制Bézier曲线。如果我使用drawBezier
函数中编写的静态公式,它会产生正确的曲线,那么它就会起作用。但如果我用drawBezierGeneralized
写的广义公式,最后一点就有问题了。
我做错了什么?
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
using namespace std;
//Point class for taking the points
class Point {
public:
float x, y;
void setxy(float x2, float y2)
{
x = x2; y = y2;
}
//operator overloading for '=' sign
const Point & operator=(const Point &rPoint)
{
x = rPoint.x;
y = rPoint.y;
return *this;
}
};
int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return n;
}
float binomial_coff(float n,float k)
{
float ans;
ans = factorial(n) / (factorial(k)*factorial(n-k));
return ans;
}
Point abc[20];
int SCREEN_HEIGHT = 500;
int points = 0;
int clicks = 4;
void myInit() {
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0,0.0,0.0);
glPointSize(3);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,500.0);
}
void drawDot(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
}
void drawLine(Point p1, Point p2) {
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd();
glFlush();
}
//Calculate the bezier point
Point drawBezier(Point PT[], double t) {
Point P;
P.x = pow((1 - t), 3) * PT[0].x + 3 * t * pow((1 -t), 2) * PT[1].x + 3 * (1-t) * pow(t, 2)* PT[2].x + pow (t, 3)* PT[3].x;
P.y = pow((1 - t), 3) * PT[0].y + 3 * t * pow((1 -t), 2) * PT[1].y + 3 * (1-t) * pow(t, 2)* PT[2].y + pow (t, 3)* PT[3].y;
return P;
}
//Calculate the bezier point [generalized]
Point drawBezierGeneralized(Point PT[], double t) {
Point P;
P.x = 0;P.y=0;
for(int i=0;i<clicks;i++)
{
P.x = P.x + binomial_coff((float)clicks,(float)i) * pow(t,(double)i) * pow((1-t),(clicks-i)) * PT[i].x;
P.y = P.y + binomial_coff((float)clicks,(float)i) * pow(t,(double)i) * pow((1-t),(clicks-i)) * PT[i].y;
}
//cout<<P.x<<endl<<P.y;
//cout<<endl<<endl;
return P;
}
void myMouse(int button, int state, int x, int y) {
// If left button was clicked
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
// Store where mouse was clicked, Y is backwards.
abc[points].setxy((float)x,(float)(SCREEN_HEIGHT - y));
points++;
// Draw the red dot.
drawDot(x, SCREEN_HEIGHT - y);
// If (click-amout) points are drawn do the curve.
if(points == clicks)
{
glColor3f(0.2,1.0,0.0);
// Drawing the control lines
for(int k=0;k<clicks-1;k++)
drawLine(abc[k], abc[k+1]);
Point p1 = abc[0];
/* Draw each segment of the curve.Make t increment in smaller amounts for a more detailed curve.*/
for(double t = 0.0;t <= 1.0; t += 0.02)
{
Point p2 = drawBezierGeneralized(abc,t);
cout<<p1.x<<" , "<<p1.y<<endl;
cout<<p2.x<<" , "<<p2.y<<endl;
cout<<endl;
drawLine(p1, p2);
p1 = p2;
}
glColor3f(0.0,0.0,0.0);
points = 0;
}
}
}
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,500);
glutInitWindowPosition(100,150);
glutCreateWindow("Bezier Curve");
glutMouseFunc(myMouse);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
我发现了问题所在。希望现在还不算太晚。
此外,感谢您的代码。
Point drawBezierGeneralized(Point PT[], double t) { Point P; P.x = 0; P.y = 0; for (int i = 0; i<clicks; i++) { P.x = P.x + binomial_coff((float)(clicks - 1), (float)i) * pow(t, (double)i) * pow((1 - t), (clicks - 1 - i)) * PT[i].x; P.y = P.y + binomial_coff((float)(clicks - 1), (float)i) * pow(t, (double)i) * pow((1 - t), (clicks - 1 - i)) * PT[i].y; } //cout<<P.x<<endl<<P.y; //cout<<endl<<endl; return P; }
我认为问题出在对原语的调用上。
for(double t = 0.0;t <= 1.0; t += 0.02)
{
//Point p2 = drawBezierGeneralized(abc,t); -> here is the problem, you call the generalized
Point p2 = drawBezier(abc,t);
cout<<p1.x<<" , "<<p1.y<<endl;
cout<<p2.x<<" , "<<p2.y<<endl;
cout<<endl;
drawLine(p1, p2);
p1 = p2;
}
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 了解 GLM- openGL 中的相机转换
- 如何在全屏模式下(在OpenGL中)使背景透明
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- OpenGL大的3D纹理(>2GB)非常慢
- OpenGl glm rotate
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- OpenGL在启用深度测试时不会丢弃我的碎片
- OpenGL相机和相机空间转型的困惑
- OpenGL将纹理四边形渲染为(0,0)
- OpenGL 和 GLM 矩阵无法正确扩展,总是按比例缩小
- 发布旋转矩阵(openGL/glm)
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 无法使用VAO和EBO(openGL)绘制多个对象
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- OpenGL 16 位模板缓冲区?
- 使用 c++ 在 OpenGL 中绘制贝塞尔曲线的切线
- Bezier曲线使用c++opengl
- 在OpenGL中绘制有限细分的贝塞尔曲线
- 如何在OpenGL中使用浮点值用c++绘制贝塞尔曲线