如何在OpenGL中渲染径向场?

时间:2014-03-31 15:31:09

标签: opengl

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

1 个答案:

答案 0 :(得分:1)

你对着色器有多熟悉?因为我认为一个简单的答案就是渲染一个四边形然后根据每个像素离中心的距离来编写一个片段着色器来为四边形着色。

伪代码:

顶点着色器:

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

如果您需要更多详细信息,请与我们联系