Завъртане и мащабиране на картина в Opengl ES 2.0

Опитвам се да завъртя картина около центъра и да я мащабирам до разделителната способност на екрана на телефона (в 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, но не можах да намеря отговор. Благодаря за всяка помощ


person Ubulum    schedule 22.08.2013    source източник
comment
Глупав въпрос: Опитахте ли да превключите X и Y за мащабиране?   -  person nanofarad    schedule 22.08.2013


Отговори (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(). Сигурен съм, че не е най-доброто решение, но работи добре за мен ^^

person Ubulum    schedule 22.08.2013