Opengl es 矩阵-旋转一个原点位于左下角的矩形,就像原点位于中心一样

Opengl es 矩阵-旋转一个原点位于左下角的矩形,就像原点位于中心一样,opengl-es,matrix-multiplication,Opengl Es,Matrix Multiplication,我正在做一些OpenGL编程,画2D矩形。我使用的是OpenGLES2.0,所以我不能使用glTranslate/glRotate等。。。我必须自己制作矩阵。我希望能够为它们设置旋转和缩放值,缩放或旋转的原点根据“halignment”和“valignment”而变化。这段代码对于旋转是正确的(但是x,y坐标看起来仍然有点偏离),但是我如何才能使缩放工作正常呢?它似乎仍然希望使用左下角作为原点 vid.xratio/vid.yratio是当前屏幕与传入的x、y、宽度和高度的本地坐标系的比率。例如

我正在做一些OpenGL编程,画2D矩形。我使用的是OpenGLES2.0,所以我不能使用glTranslate/glRotate等。。。我必须自己制作矩阵。我希望能够为它们设置旋转和缩放值,缩放或旋转的原点根据“halignment”和“valignment”而变化。这段代码对于旋转是正确的(但是x,y坐标看起来仍然有点偏离),但是我如何才能使缩放工作正常呢?它似乎仍然希望使用左下角作为原点

vid.xratio/vid.yratio是当前屏幕与传入的x、y、宽度和高度的本地坐标系的比率。例如,如果本地坐标基于640x480网格,但您的屏幕当前为1600x1200,则坐标(238185)将为(595462.5)


我想出来了。。。下面是正确的代码,以防它帮助其他人

请注意,我删除了对vid.xratio和vid.yratio的引用。。。我所做的是,在对投影矩阵调用Matrix::Ortho之后,我做:

矩阵:比例(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.
//
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);
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);