How to render a radial field in OpenGL?

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.

  • Answer:

    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

Jeroen Bollen at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.