如何在调整大小后让此程序正确绘制贝塞尔曲线?

时间:2010-11-01 08:11:21

标签: opengl glut bezier

当窗口以原始大小(640 x 480)使用时,此代码会正确绘制贝塞尔曲线,但在调整大小时会错误地呈现控制点。

单击鼠标功能

   if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
   // Store where the user clicked, note Y is backwards.
    abc[NUMPOINTS].setxy((float)x,(float)(SCREEN_HEIGHT - y));
    NUMPOINTS++;

    // Draw the red  dot.
    drawDot(x, SCREEN_HEIGHT - y);

我不明白如何处理坐标系。

为什么使用SCREEN_HEIGHT转换适用于640x480但不适用于其他窗口尺寸?如何实现普及?

在我自己的应用程序中,我一直在没有glutOrtho2d工作,只是使用每个X,Y的计算,但我已经读过这种方法更好。我很感激对它的详细解释。

修改后的代码:

/* bezier.cpp by detour@metalshell.com
 *
 * Create a bezier curve by defining the three points
 * with your mouse.
 *
 * http://www.metalshell.com/
 *
 */

#include <windows.h>
#include <math.h>
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <gl/glut.h>

// CREEN_WIDTH = 640, SCREEN_HEIGHT=480;

int SCREEN_WIDTH=640;

int SCREEN_HEIGHT = 480;
// Keep track of times clicked, on 3 clicks draw.
int NUMPOINTS = 0;

// Point class to keep it a little cleaner.
class Point {
public:
    float x, y;
    void setxy(float x2, float y2) { x = x2; y = y2; }
    const 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.0,0.0,0.0);
    glPointSize(4.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,(float)SCREEN_WIDTH,0.0,(float)SCREEN_HEIGHT);

}

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 next bezier point.
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 left button was clicked
  if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
      // Store where the user clicked, note Y is backwards.
      //abc[NUMPOINTS].setxy((float)x,(float)(y));
    abc[NUMPOINTS].setxy((float)x,(float)(SCREEN_HEIGHT - y));

    // abc[NUMPOINTS] es un arreglo de objetos Point, su contraparte en Casteljau es 


    NUMPOINTS++;

    // Draw the red  dot.
    drawDot(x, SCREEN_HEIGHT - y);
    //drawDot(x, SCREEN_HEIGHT);

    // If 3 points are drawn do the curve.
    if(NUMPOINTS == 3) {
        glColor3f(1.0,1.0,1.0);
        // Draw two legs of the triangle
        drawLine(abc[0], abc[1]);
        drawLine(abc[1], abc[2]);
        Point POld = 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.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 *argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(SCREEN_WIDTH,SCREEN_HEIGHT);
    glutInitWindowPosition(100,150);
    glutCreateWindow("Bezier Curve");

    glutMouseFunc(myMouse);
    glutDisplayFunc(myDisplay);

    myInit();
    glutMainLoop();

    return 0;
}

原始代码:

 /* bezier.cpp by detour@metalshell.com
     *
     * Create a bezier curve by defining the three points
     * with your mouse.
     *
     * http://www.metalshell.com/
     *
     */

    #include <windows.h>
    #include <math.h>
    #include <gl/Gl.h>
    #include <gl/Glu.h>
    #include <gl/glut.h>

    int SCREEN_HEIGHT = 480;
    // Keep track of times clicked, on 3 clicks draw.
    int NUMPOINTS = 0;

    // Point class to keep it a little cleaner.
    class Point {
    public:
    float x, y;
    void setxy(float x2, float y2) { x = x2; y = y2; }
    const 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.0,0.0,0.0);
    glPointSize(4.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,640.0,0.0,480.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 next bezier point.
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 left button was clicked
  if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
      // Store where the user clicked, note Y is backwards.
    abc[NUMPOINTS].setxy((float)x,(float)(SCREEN_HEIGHT - y));
    NUMPOINTS++;

    // Draw the red  dot.
    drawDot(x, SCREEN_HEIGHT - y);

    // If 3 points are drawn do the curve.
    if(NUMPOINTS == 3) {
        glColor3f(1.0,1.0,1.0);
        // Draw two legs of the triangle
        drawLine(abc[0], abc[1]);
        drawLine(abc[1], abc[2]);
        Point POld = 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.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 *argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutCreateWindow("Bezier Curve");

    glutMouseFunc(myMouse);
    glutDisplayFunc(myDisplay);

    myInit();
    glutMainLoop();

    return 0;
}

2 个答案:

答案 0 :(得分:2)

在使用640或480的代码中随处更改以使用SCREEN_WIDTH和SCREEN_HEIGHT。

gluOrtho2D(0.0,640.0,0.0,480.0);

=&GT;

gluOrtho2D( 0.0,(float)SCREEN_WIDTH,0.0,(float)SCREEN_HEIGHT );

&安培;

glutInitWindowSize(640,480);

=&GT;

glutInitWindowSize( SCREEN_WDITH, SCREEN_HEIGHT );

这样,当您更改SCREEN_WIDTH或SCREEN_HEIGHT时,您不依赖于其他地方设置的某些值。我怀疑你的问题是因为你没有修改gluOrtho2D电话......

答案 1 :(得分:0)

您的代码中存在许多问题:

  1. 看看glutReshapeFunc是什么 确实
  2. myDisplay函数应呈现 图像和交换缓冲区(如果你 正在做双缓冲)
  3. 所有其他功能都应该调用 glutPostRedisplay