0

I am working with OpenGL and I wanted to invert the image. So I went here, asked a question and finally I had the following code:

    glMatrixMode(GL_PROJECTION);
    glScalef(-1,1,1);
    glTranslatef(-width(),0,0);

From what I understand from this, the position of every pixel gets inverted, so the pixels that were on the right of the image are now on the same absolute position, but are the left of the image, so I have to move the entire thing back exactly as many pixels as its wide: 360 (which is the size of the "canvas", so in the snippents the function width() is being used)! So to undo this process I would invert the image again and then move it back to where it came from:

    glMatrixMode(GL_PROJECTION);
    glScalef(-1,1,1);
    glTranslatef(width(),0,0);

Nope, blackscreen. I have to do exactly the same thing twice to undo the flipping: I have to move with -360 every time I flip the image. Why?

4

1 回答 1

1

正如丹尼尔·菲舍尔在评论中提到的那样。这是该过程的图示。

您必须记住的是,转换是在转换后的坐标系上进行的。

我们从屏幕(绿色)上的图像(灰色)开始:

F1

然后我们缩放图像。所以原点被保留,但 x 轴被镜像。

F2

不,我们必须再次将图像移动到屏幕上。因为 x 轴指向左侧(但我们想将图像向右移动),所以我们必须使用负偏移量进行平移:

F3

如果我们再次翻转图像,会发生以下情况。保留原点并镜像 x 轴:

F4

所以我们必须将图像平移一个负偏移量:

F5

撤消翻转的另一种方法是撤消操作(但顺序相反):

glTranslatef(width, 0, 0);
glScalef(-1,1,1);

数学上的原因是反转反转了顺序。如果我们有 MatrixA = B * C那么A^-1 = (C^-1 * B^-1).

于 2013-07-30T14:39:32.177 回答