如何从等矩形投影绘制正投影

2024-10-01 11:25:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这张图片:https://raw.githubusercontent.com/Mihara/RasterPropMonitor/master/GameData/JSI/RasterPropMonitor/Library/Components/NavBall/NavBall000.png

我不知道它到底是什么类型的投影,我猜是等角的还是墨卡托的。它是an attitude indicatorb的纹理。在

我想画一个orthographic projectionb或者一个General Perspective projection(哪一个看起来更好)根据一个由两个角度(航向和俯仰)定义的方向向量。这个方向定义了球体上的一个点,这个点应该是投影的中心。在

我想让它从飞行员的角度看,所以只能画出球体的一半。在

我使用python,我还没有选择图形库,但我可能会使用pygame。在

我发现了一些相关的东西:http://www.pygame.org/project-Off-Center+Map+Projections-2881-.html但是它使用OpenGL,我没有使用它的经验,但是如果需要,我可以尝试一下。在

我该怎么做?我可能可以通过计算公式计算每个像素来手动绘制它,但我认为有一些库工具可以有效地实现这一点(可能是硬件加速?)。在


Tags: an类型定义图片方向pygameindicatorgeneral
2条回答

对于全Python解决方案(使用numpy/scipy array ops,这将比任何显式的每像素循环都快),它:

#!/usr/bin/env python

import math
import numpy as np
import scipy
import scipy.misc
import scipy.ndimage.interpolation
import subprocess

src=scipy.misc.imread("ji80w.png")

size=256
frames=50

for frame in xrange(0,frames):

    # Image pixel co-ordinates
    px=np.arange(-1.0,1.0,2.0/size)+1.0/size
    py=np.arange(-1.0,1.0,2.0/size)+1.0/size
    hx,hy=scipy.meshgrid(px,py)

    # Compute z of sphere hit position, if pixel's ray hits
    r2=hx*hx+hy*hy
    hit=(r2<=1.0)
    hz=np.where(
        hit,
        -np.sqrt(1.0-np.where(hit,r2,0.0)),
        np.NaN
        )

    # Some spin and tilt to make things interesting
    spin=2.0*np.pi*(frame+0.5)/frames
    cs=math.cos(spin)
    ss=math.sin(spin)
    ms=np.array([[cs,0.0,ss],[0.0,1.0,0.0],[-ss,0.0,cs]])

    tilt=0.125*np.pi*math.sin(2.0*spin)
    ct=math.cos(tilt)
    st=math.sin(tilt)
    mt=np.array([[1.0,0.0,0.0],[0.0,ct,st],[0.0,-st,ct]])

    # Rotate the hit points
    xyz=np.dstack([hx,hy,hz])
    xyz=np.tensordot(xyz,mt,axes=([2],[1]))
    xyz=np.tensordot(xyz,ms,axes=([2],[1]))
    x=xyz[:,:,0]
    y=xyz[:,:,1]
    z=xyz[:,:,2]

    # Compute map position of hit
    latitude =np.where(hit,(0.5+np.arcsin(y)/np.pi)*src.shape[0],0.0)
    longitude=np.where(hit,(1.0+np.arctan2(z,x)/np.pi)*0.5*src.shape[1],0.0)
    latlong=np.array([latitude,longitude])

    # Resample, and zap non-hit pixels
    dst=np.zeros((size,size,3))
    for channel in [0,1,2]:
        dst[:,:,channel]=np.where(
            hit,
            scipy.ndimage.interpolation.map_coordinates(
                src[:,:,channel],
                latlong,
                order=1
                ),
            0.0
            )

    # Save to f0000.png, f0001.png, ... 
    scipy.misc.imsave('f{:04}.png'.format(frame),dst)

# Use imagemagick to make an animated gif
subprocess.call('convert -delay 10 f????.png anim.gif',shell=True)

会得到你的

animated thing。在

尽管如此,OpenGL确实是一个进行这种像素争夺的地方,尤其是对于任何交互式的东西。在

我看了看你链接的“偏心地图投影”里的代码。。。在

作为一个起点,我想说这是非常好的,特别是如果你想在PyGame中以任何形式的效率实现这一点,因为将任何类型的每像素操作卸载到OpenGL上都将比Python中的速度快得多。在

显然,要进一步了解OpenGL,投影是用main.py的GLSL代码(字符串中传递给mod_program.ShaderFragment)的GLSL代码实现的,如果您阅读过等矩形投影,那么atan和asin就不应该感到惊讶了。在

但是,要达到你想要的效果,你必须弄清楚如何渲染球体而不是视口填充四边形(在主.py在glBegin(GL_QUADS);)。或者,继续使用屏幕填充四边形并在着色器代码中执行光线球体交集(这实际上就是我另一个答案中的python代码所做的)。在

相关问题 更多 >