我正在做一些OpenGL编程,绘制2D矩形。我使用的是OpenGL ES 2.0,所以我不能使用glTranslate/glRotate等等,不得不推出我自己的矩阵。我希望能够为它们设置旋转和缩放值,缩放或旋转的起点取决于'halignment'和'valignment'。这段代码对于旋转正常工作(但是x,y坐标似乎还是有点偏离),但是我怎样才能使缩放工作?它似乎仍然希望使用左下角作为原点。矩阵 - 在底部左侧旋转一个矩形,原点位于中心
vid.xratio/vid.yratio是当前屏幕与本地坐标系中x,y,宽度和高度传入的比率。例如,如果本地坐标基于640x480的网格,但你的屏幕当前是1600x1200,坐标(238,185)将是(595,462.5)。
const byte picVerts[] = {0, 0, 1, 0, 1, 1, 0, 1};
//
// DrawPictureSize
//
// Draws a picture on the 2D screen, width and height specified.
//
void DrawPictureSize(float x, float y, float width, float height, float scalex, float scaley, Vector::vector_t *upVec, byte alpha, Texture::texture_t *texture, int halignment, int valignment)
{
if (!texture)
return;
float matrix[16];
Matrix::LoadIdentity(matrix);
width *= scalex * vid.xratio;
height *= scaley * vid.yratio;
float angle = U_Rad2Deg(atan2(upVec->y, upVec->x)) - 90;
float xalign = 0;
float yalign = 0;
// Move into position
if (halignment == Font::HALIGN_CENTER)
xalign = width/2;
else if (halignment == Font::HALIGN_RIGHT)
xalign = width;
if (valignment == Font::VALIGN_CENTER)
yalign = height/2;
else if (valignment == Font::VALIGN_TOP)
yalign = height;
// Move into position
Matrix::Translate(matrix, x * vid.xratio, y * vid.yratio, 0.0f);
// Translate to the origin before doing scaling/rotation
Matrix::Translate(matrix, xalign, yalign, 0);
Matrix::Rotate(matrix, angle, 0, 0, 1);
Matrix::Translate(matrix, -xalign, -yalign, 0);
// Expand square to image size
Matrix::Scale(matrix, width, height, 1.0f);