【问题标题】:Shape manipulation with openFrameworks使用 openFrameworks 进行形状操作
【发布时间】:2011-02-25 17:08:40
【问题描述】:

我是一个 openFrameworks 新手。我正在学习基本的 2d 绘图,到目前为止一切都很好。我画了一个圆圈:

ofSetColor(0x333333);
ofFill;
ofCircle(100,650,50);

我的问题是如何给圆圈起一个变量名,以便我可以在 mousepressed 方法中进行操作?我尝试在 ofCircle 之前添加一个名称

theball.ofSetColor(0x333333);
theball.ofFill;
theball.ofCircle(100,650,50);

但是得到我'theball'没有在这个范围错误中声明。

【问题讨论】:

    标签: c++ openframeworks


    【解决方案1】:

    正如 razong 指出的那样,OF 不是这样工作的。 OF(据我所知)为许多 OpenGL 的东西提供了一个方便的包装器。因此,您应该使用 OF 调用来影响当前的绘图上下文(而不是考虑带有精灵对象或其他东西的画布)。我通常将这种东西整合到我的对象中。所以假设你有这样的课程......

    class TheBall {
    
    protected:
    
        ofColor col;
        ofPoint pos;
    
    public:
    
        // Pass a color and position when we create ball
        TheBall(ofColor ballColor, ofPoint ballPosition) {
            col = ballColor;
            pos = ballPosition;
        }
    
        // Destructor
        ~TheBall();
    
       // Make our ball move across the screen a little when we call update
       void update() { 
           pos.x++;
           pos.y++; 
       }
    
       // Draw stuff
       void draw(float alpha) {
           ofEnableAlphaBlending();     // We activate the OpenGL blending with the OF call
           ofFill();                    // 
           ofSetColor(col, alpha);      // Set color to the balls color field
           ofCircle(pos.x, pos.y, 5);   // Draw command
           ofDisableAlphaBlending();    // Disable the blending again
       }
    
    
    };
    

    好吧,我希望这是有道理的。现在使用这种结构,您可以执行以下操作

    testApp::setup() {
    
        ofColor color;
        ofPoint pos;
    
        color.set(255, 0, 255); // A bright gross purple
        pos.x, pos.y = 50;
    
        aBall = new TheBall(color, pos);
    
    }
    
    testApp::update() {
        aBall->update() 
    }
    
    testApp::draw() {
        float alpha = sin(ofGetElapsedTime())*255; // This will be a fun flashing effect
        aBall->draw(alpha)
    }
    

    快乐编程。 设计愉快。

    【讨论】:

      【解决方案2】:

      你不能那样做。 ofCircle 是一个全局绘制方法,只绘制一个圆。

      您可以声明一个变量(或者对于 rgb 最好是三个 int - 因为您不能使用 ofColor 作为 ofSetColor 的参数)来存储圆的颜色并在 mousepressed 方法中对其进行修改。

      在绘制圆之前,在 draw 方法中使用你的变量 ofSetColor。

      【讨论】:

      • 实际上这是这种情况下的“最佳实践”,但如果他真的想要,他可以做 rykardo 在另一个答案中写的,并创建与 OF 函数具有相同名称的方法。
      猜你喜欢
      • 2014-10-29
      • 2018-02-27
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多