精灵在球体上旋转

时间:2014-05-08 06:28:43

标签: opengl rotation textures sprite

我有一个2D模式,可以显示世界各地的移动精灵。每个精灵都有旋转。

当我试图在3D世界中实现相同的时候,在一个球体上,我遇到了计算精灵旋转的问题,所以看起来它正朝着这个方向移动。我知道精灵只是广告牌而且旋转只是2D,并且不会朝向该方向100%旋转,但至少使它看起来合理。

The 2D Mode that works fine The problem in the 3D Mode

我试图在我的旋转中考虑向北(世界)的向量但是,当我们在球体周围移动相机时,精灵箭头不在运动方向上的情况仍然存在

有人可以指导我寻求解决方案吗?

-------- ADDITION -----------

更多解释:我有2D世界(x,y)。在这个世界上,我有一个向一个方向移动的点(一个角度保存在对象中)。当然,在片段着色器中计算旋转。

在3D世界中,我通过简单的球形公式将此(x,y)转换为(x,y,z)。 我的球体(世界)原点是(0,0,0),半径为1。

角度(保存在移动方向上的点)在2D中用于旋转纹理(如上图第一幅图所示)。问题是3D纹理的旋转。旋转应考虑点方向角和相机。

-------- ADDITION -----------

我的2D片段着色器 - 如果有帮助的话。还有更多的照片和我的愿望

varying vec2 TextureCoord;
varying vec2 TextureSize;
uniform sampler2D sampler;

varying float angle;
uniform vec4 uColor;

void main()
{
    vec2 calcedCoord = gl_PointCoord;
    float c = cos(angle);
    float s = sin(angle);
    vec2 trans = vec2(-0.5, -0.5);
    mat2 rot = mat2(c, s, -s, c);
    calcedCoord = calcedCoord + trans;
    calcedCoord = rot * calcedCoord;
    calcedCoord = calcedCoord - trans;

    vec2 realTexCoord = TextureCoord + (calcedCoord * TextureSize);
    vec4 fragColor = texture2D(sampler, realTexCoord);
    gl_FragColor = fragColor * uColor;
}

enter image description here enter image description here enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

在努力解决这个问题之后,我进入了这个解决方案。

我没有将方向角作为属性附加到每个精灵,而是发送了下一个精灵位置。并在顶点着色器中计算2D角度,如下所示:

varying float angle;

attribute vec3 nextPointAtt;

void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;    
    vec4 nextPnt = gl_ModelViewProjectionMatrix * vec4(nextPointAtt, gl_Vertex.w);

    vec2 ver = gl_Position.xy / gl_Position.w;
    vec2 nextVer = nextPnt.xy / nextPnt.w;

    vec2 d = nextVer - ver;
    angle = atan(d.y, d.x);
}

角度将在片段着色器中使用(查看片段着色器代码的问题)。

相关问题