1

我正在尝试围绕其中心旋转图片并将其缩放到手机的屏幕分辨率(在 android 中)。

这是我的代码:

// ----------------------------------------
// Scaling:
float scaleWidth = 2/screenWidth;
float scaleHeight = 2/screenHeight;

// ----------------------------------------
// Start position of the picture
int x = 60;
int y = 60;
float startX = x*scaleWidth;
float startY = y*scaleHeight;
// End position of the picture
float endX = (x + bmpWallSize) * scaleWidth;
float endY = (y + bmpWallSize) * scaleHeight;
// Center of the bitmap:
float midX = (x + (bmpWallSize/2)) * scaleWidth;
float midY = (y + (bmpWallSize/2)) * scaleHeight;

// ----------------------------------------
// Rotating:
gl.glTranslatef(midX, midY, 0f);
gl.glRotatef(r,0, 0,-1 );
gl.glTranslatef(-midX, -midY, 0);

r++; if (r > 360) r = 1;

不旋转图片看起来很好,但是一旦旋转它就会改变大小(正方形变成矩形)。我认为这是因为我需要在旋转之后而不是之前缩放图片的顶点,但我不知道如何。我google了很多,但找不到答案。谢谢你的帮助

4

1 回答 1

1

我找到了一个解决方案:

这个函数可以计算立方体的旋转角点:

public Point GetRotatedRecPoint(int degree, int cubeSize, Point centerPoint) {
    degree = 360 - degree;
    float length = (float) Math.sqrt(((cubeSize/2)*(cubeSize/2)) + ((cubeSize/2)*(cubeSize/2)));
    float x = Math.cos(Math.toRadians(degree));
    float y = Math.sin(Math.toRadians(degree))
    return new Point(x*length + centerPoint.x, y*length + centerPoint.y);
}

像这样:

// The degrees of each Corner of the Cube
int topLeftDegree = r+45;
int topRightDegree = r+135;
int botRightDegree = r+225;
int botLeftDegree = r+315;

Point topLeftPoint = new Point(GetRotatedRecPoint(topLeftDegree, cubeSize, centerPoint));
Point topRightPoint = new Point(GetRotatedRecPoint(topRightDegree, cubeSize, centerPoint));
Point botRightPoint = new Point(GetRotatedRecPoint(botRightDegree, cubeSize, centerPoint));
Point botLeftPoint = new Point(GetRotatedRecPoint(botLeftDegree, cubeSize, centerPoint));

之后,我对其进行缩放:

topLeftPoint.x = topLeftPoint.x * widthScale;
topLeftPoint.y = topLeftPoint.y * heightScale;
topRightPoint.x = topRightPoint.x * widthScale;
.....

将它添加到 verticeBuffer 然后绘制它。这可以在不使用 glTranslatef() 或 glRotatef() 的情况下工作。我确信它不是最好的解决方案,但它对我来说很好^^

于 2013-08-22T15:48:39.797 回答