我正在做一些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);
答案 0 :(得分:0)
想出来......这是正确的代码,以防其他人
请注意,我删除了对vid.xratio和vid.yratio的引用...我做的是在投影矩阵上调用Matrix :: Ortho后,我这样做:
Matrix :: Scale(projMatrix,vid.xratio,vid.yratio,1.0f);
const byte picVerts[] = {0, 0, 1, 0, 1, 1, 0, 1};
//
// DrawPictureSize
//
// Draws a picture on the 2D screen, width and height specified.
//
// Note: 'Set2D' must be called before this!
//
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;
height *= scaley;
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 - xalign, y - yalign, 0.0f);
// Translate to the origin before doing rotation
if (angle != 0.0f)
{
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);