在正交投影中居中缩放效果的此着色器有什么问题? [英] What's wrong with this shader for a centered zooming effect in Orthographic projection?

查看:84
本文介绍了在正交投影中居中缩放效果的此着色器有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个基本的正交着色器,用于显示纹理的精灵.效果很好.

I've created a basic orthographic shader that displays sprites from textures. It works great.

我已经在其中添加了缩放"因子,以使精灵可以缩放或变大或变小.假设纹理的原点锚定在左下",则它朝该原点缩小,或从原点向右上方扩展.我真正想要的是缩小或扩展原位"以保持居中.

I've added a "zoom" factor to it to allow the sprite to scale to become larger or smaller. Assuming that the texture is anchored with its origin in the "lower left", what it does is shrink towards that origin point, or expand from it towards the upper right. What I actually want is to shrink or expand "in place" to stay centered.

因此,一种实现方法是找出要缩小或扩展并补偿的像素数量.我不太确定该怎么做,我也知道,这不是最好的方法.我以翻译和缩放的顺序为准,认为可以先缩放然后放置,但结果却很糟糕.我无法解决问题.

So, one way of achieving that would be to figure out how many pixels I'll shrink or expand, and compensate. I'm not quite sure how I'd do that, and I also know that's not the best way. I fooled with order of my translates and scales, thinking I can scale first and then place, but I just get various bad results. I can't wrap my head around a way to solve the issue.

这是我的着色器:


// Set up orthographic projection (960 x 640)
mat4 projectionMatrix = mat4( 2.0/960.0, 0.0, 0.0, -1.0,
                             0.0, 2.0/640.0, 0.0, -1.0,
                             0.0, 0.0, -1.0, 0.0,
                             0.0, 0.0, 0.0, 1.0);                        

void main()
{
    // Set position
    gl_Position = a_position;

    // Translate by the uniforms for offsetting
    gl_Position.x += translateX;
    gl_Position.y += translateY;

    // Apply our (pre-computed) zoom factor to the X and Y of our matrix
    projectionMatrix[0][0] *= zoomFactorX;
    projectionMatrix[1][1] *= zoomFactorY;

    // Translate
    gl_Position *= projectionMatrix;

    // Pass right along to the frag shader
    v_texCoord = a_texCoord;
}

推荐答案

mat4 projectionMatrix =

GLSL中的矩阵是按列构造的.对于mat4,前4个值是第一列,然后下4个值是第二列,依此类推.

Matrices in GLSL are constructed column-wise. For a mat4, the first 4 values are the first column, then the next 4 values are the second column and so on.

您转置了矩阵.

另外,那些-1代表什么?

Also, what are those -1's for?

对于其余的问题,缩放不是 projection 矩阵应处理的事情.您所说的不是那种扩展.在将比例尺与投影矩阵相乘之前,应将比例尺应用于位置.就像3D对象一样.

For the rest of your question, scaling is not something the projection matrix should be dealing with. Not the kind of scaling you're talking about. Scales should be applied to the positions before you multiply them with the projection matrix. Just like for 3D objects.

您没有发布精灵的顶点数据是什么,因此无法确定.但是应该的工作方式是,精灵的顶点位置应居中于精灵的中心(无论您将其定义在何处).

You didn't post what your sprite's vertex data is, so there's no way to know for sure. But the way it ought to work is that the vertex positions for the sprite should be centered at the sprite's center (which is wherever you define it to be).

因此,如果您有一个16x24的精灵,并且希望精灵的中心向右偏移8个像素,向上偏移8个像素,则您的精灵矩形应为(-8,-8)到(8,16)(从左下角的坐标系开始.

So if you have a 16x24 sprite, and you want the center of the sprite to be offset 8 pixels right and 8 pixels up, then your sprite rectangle should be (-8, -8) to (8, 16) (from a bottom-left coordinate system).

然后,如果缩放它,它将围绕精灵的坐标系的中心缩放.

Then, if you scale it, it will scale around the center of the sprite's coordinate system.

这篇关于在正交投影中居中缩放效果的此着色器有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆