发布覆盖绘制曲线的c ++类

时间:2015-10-03 08:28:04

标签: c++ opengl subclass glut curves

我正在尝试实现一个程序,当用户点击屏幕时,将从一个点到另一个点绘制线条。我有一个Polyline类,它是Freeform类的子类,它是Curve类的子类。 Curve超类的draw方法通常调用getPoint,它将获得该特定点的曲线的加权点。但是,对于Polyline的draw方法,我试图覆盖曲线类以获得用户点击的点(正如您所看到的,Polyline类的draw方法从不调用getPoint)。但是,当我调试代码时,我发现当我尝试绘制折线时仍然会调用getPoint。有什么建议吗?

class Curve {

public:
    virtual float2 getPoint(float t)=0;

    void draw(){

        glBegin(GL_LINE_STRIP);
        for (float i = 0; i < 1; i+=.01) {
            float2 point = getPoint(i);
            float x = point.x;
            float y = point.y;
            glVertex2d(x, y);

        }
        glEnd();
        }; 
    };

class Freeform : public Curve
{
protected:
    std::vector<float2> controlPoints;

public:
    virtual float2 getPoint(float t)=0;
    virtual void addControlPoint(float2 p)
    {
        controlPoints.push_back(p);
    }
    void drawControlPoints(){
        glBegin(GL_POINTS);
        for (float i = 0; i < controlPoints.size(); i++) {
            float2 point = controlPoints.at(i);
            float x = point.x;
            float y = point.y;
            glVertex2d(x, y);

        }
        glEnd();// draw points at control points
    }
};

class Polyline : public Freeform {
public:
    float2 getPoint(float t) {
        return float2(0.0, 0.0);
    }
    //we add a control point (in this case, control point is where mouse is clicked)
    void addControlPoint(float2 p)
    {
        controlPoints.push_back(p);
    }
    //trying to override Curve draw method
    void draw(){
        glBegin(GL_LINE_STRIP);
            for (float i = 0; i < controlPoints.size(); i++) {
            float2 point = controlPoints.at(i);
            float x = point.x;
            float y = point.y;
            glVertex2d(x, y);
        }
        glEnd();
    };
};

1 个答案:

答案 0 :(得分:4)

//trying to override Curve draw method

上面的评论几乎都说明了一切;您&#34;尝试&#34; 覆盖void Curve::draw (),但由于先前未声明成员函数,因此无法执行此操作{{1} }。

解决方案

  • virtual声明为void Curve::draw ()
  • 如果或更高版本,请查看(并使用)关键字override