openGL反转图像纹理逻辑

时间:2016-04-04 08:49:46

标签: opengl-es webgl projection

我准备将图像投影到圆柱形全景图中。但首先我需要得到我想要绘制的像素(或像素颜色),然后在具有极坐标的着色器中进行一些数学运算以获得像素的新位置,然后最终绘制像素。

使用这种方式,我将能够将图像的形状从多边形改变为我想要的任何形状。

但是我找不到关于这个方法的任何信息(首先得到像素,然后做数学并获得像素的新位置)。
请问有这样的事吗?

1 个答案:

答案 0 :(得分:0)

OpenGL historically doesn't work that way around; it forward renders — from geometry to pixels — rather than backwards — from pixel to geometry.

The most natural way to achieve what you want to do is to calculate texture coordinates based on geometry, then render as usual. For a cylindrical mapping:

  • establish a mapping from cylindrical coordinates to texture coordinates;
  • with your actual geometry, imagine it placed within the cylinder, then from each vertex proceed along the normal until you intersect the cylinder. Use that location to determine the texture coordinate for the original vertex.

The latter is most easily and conveniently done within your geometry shader; it's a simple ray intersection test, with attributes therefore being only vertex location and vertex normal, and texture location being a varying that is calculated purely from the location and normal.

Extemporaneously, something like:

// get intersection as if ray hits the circular region of the cylinder,
// i.e. where |(position + n*normal).xy| = 1
float planarLengthOfPosition = length(position.xy);
float planarLengthOfNormal = length(normal.xy);
float planarDistanceToPerimeter = 1.0 - planarLengthOfNormal;
vec3 circularIntersection = position + 
                            (planarDistanceToPerimeter/planarLengthOfNormal)*normal;

// get intersection as if ray hits the bottom or top of the cylinder,
// i.e. where |(position + n*normal).z| = 1
float linearLengthOfPosition = abs(position.z);
float linearLengthOfNormal = abs(normal.z);
float linearDistanceToEdge = 1.0 - linearLengthOfPosition;
vec3 endIntersection = position +
                       (linearDistanceToEdge/linearLengthOfNormal)*normal;

// pick whichever of those was lesser
vec3 cylindricalIntersection = mix(circularIntersection,
                                   endIntersection,
                                   step(linearDistanceToEdge, 
                                        planarDistanceToPerimeter));

// ... do something to map cylindrical intersection to texture coordinates ...
textureCoordinateVarying = 
    coordinateFromCylindricalPosition(cylindricalIntersection);

With a common implementation of coordinateFromCylindricalPosition possibly being simply return vec2(atan(cylindricalIntersection.y, cylindricalIntersection.x) / 6.28318530717959, cylindricalIntersection.z * 0.5);.