Openframeworks - 通过OpenGL调用从中心旋转图像

use*_*527 0 c++ opengl openframeworks

我正在使用openframeworks(通过OpenGL渲染),我正在尝试从它的中心旋转图像.

我知道我应该使用ofRotate(),ofTranslate()但我没有设法自己解决.这是我到目前为止所尝试的:

ofPushMatrix();
ofRotate(ofRandom(10),0.0,0.0,1.0);
leafImg.draw(100,100);
ofPopMatrix();
Run Code Online (Sandbox Code Playgroud)

Geo*_*nza 8

在不做太多数学运算的情况下,您可以使用嵌套坐标系统偏移图像,以便在旋转时从中心旋转.总之,你会这样做:

  1. 将坐标系移动到图像的中心
  2. 从那里旋转
  3. 在该坐标系内,做一个更深的水平并将图像大小的一半转换回来,这样你就可以偏移到'0,0'

在代码中,这将是:

ofPushMatrix();
    ofTranslate(leafImg.width/2, leafImg.height/2, 0);//move pivot to centre
    ofRotate(ofGetFrameNum() * .01, 0, 0, 1);//rotate from centre
    ofPushMatrix();
        leafImg.draw(-leafImg.width/2,-leafImg.height/2);//move back by the centre offset
    ofPopMatrix();
ofPopMatrix();
Run Code Online (Sandbox Code Playgroud)

我已经使用缩进来使坐标系嵌套更加明显.

它与以下相同:

ofPushMatrix();
    ofTranslate(leafImg.width/2, leafImg.height/2, 0);//move pivot to centre
    ofRotate(ofGetFrameNum() * .01, 0, 0, 1);//rotate from centre
    ofPushMatrix();
        ofTranslate(-leafImg.width/2,-leafImg.height/2,0);//move back by the centre offset
        leafImg.draw(0,0);
    ofPopMatrix();
ofPopMatrix();
Run Code Online (Sandbox Code Playgroud)

如您所见,旋转到中心相当简单.在业余时间尝试解决如何旋转任意点.

在幕后有一些线性代数正在进行,但推/弹矩阵调用基本上为你处理细节矩阵乘法.您还需要了解并练习使用pushMatrix/popMatrix调用.即使这是用于处理,原理完全相同,语法非常相似,所以我推荐这篇文章:2D转换