gpt4 book ai didi

java - 在 Opengl ES 2.0 中旋转和缩放图片

转载 作者:行者123 更新时间:2023-11-30 03:25:01 27 4
gpt4 key购买 nike

我正在尝试围绕其中心旋转图片并将其缩放到手机的屏幕分辨率(在 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;

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

最佳答案

我找到了一个解决方案:

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

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() 即可工作。我确信这不是最好的解决方案,但对我来说效果很好 ^^

关于java - 在 Opengl ES 2.0 中旋转和缩放图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18374249/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com