如何在OpenGL中渲染径向场?

如何在OpenGL中渲染径向场?,opengl,Opengl,如何在OpenGL中渲染二维径向场?我知道我可以逐像素渲染,但我想知道是否有更有效的解决方案?我不介意它是否需要OpenGL3+功能 您对着色器的熟悉程度如何?因为我认为一个简单的答案是渲染一个四边形,然后编写一个片段着色器,根据每个像素离中心的距离为四边形着色 伪代码: 顶点着色器: vec2 center = vec2((x1+x2)/2,(y1+y2)/2); //pass this to the fragment shader 片段着色器: float dist = distance(

如何在OpenGL中渲染二维径向场?我知道我可以逐像素渲染,但我想知道是否有更有效的解决方案?我不介意它是否需要OpenGL3+功能

您对着色器的熟悉程度如何?因为我认为一个简单的答案是渲染一个四边形,然后编写一个片段着色器,根据每个像素离中心的距离为四边形着色

伪代码:

顶点着色器:

vec2 center = vec2((x1+x2)/2,(y1+y2)/2); //pass this to the fragment shader
片段着色器:

float dist = distance(pos,center); //"pos" is the interpolated position of the fragment. Its passed in from the vertex shader
//Now that we have the distance between each fragment and the center, we can do all kinds of stuff:
gl_fragcolor = vec4(1,1,1,dist) //Assuming you're drawing a unit square, this will make each pixel's transparency smoothly vary from 1 (right next to the center) to 0 (on the edce of the square)
gl_fragcolor = vec4(dist, dist, dist, 1.0) //Vary each pixel's color from white to black
//etc, etc

如果您需要更详细的信息,请告诉我

您是否使用直线或曲线(我真的不知道什么是径向场…)我猜增加的效率将基于GPU中正在处理的数据?是的。这是你想在着色器中做的事情。GPU将并行执行所有着色,因此即使我们逐像素渲染径向场,我们仍然可以获得巨大的速度提升。