Jeroen
Jeroen

Reputation: 16865

How to render a radial field in OpenGL?

How would I render a 2D radial field in OpenGL? I know I can render it pixel by pixel but I'm wondering if there are more efficient solutions? I don't mind if it requires OpenGL3+ functionality.

Upvotes: 0

Views: 175

Answers (1)

redsoxfantom
redsoxfantom

Reputation: 956

How familiar are you with shaders? Because I'm thinking an easy-ish answer would be to render a quad and then write a fragment shader to color the quad based off of how far each pixel is from the center.

Pseudocode:

vertex shader:

vec2 center = vec2((x1+x2)/2,(y1+y2)/2); //pass this to the fragment shader

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

Let me know if you need more detail

Upvotes: 1

Related Questions