0
\$\begingroup\$

I have written a fragment shader, on a shape which is a square made with gl.TRIANGLE_STRIP, and which displays a radar:

enter image description here

To build the radar, I'm using distance from the center of the shape.

Currently, I'm using gl_FragCoord and have to pass the shader the size of the window, the position of the center of the radar, in order to determine where I am in the shape. I'm not really OK with that..

I would like to know if there is a better way to make the shader, to make it independent from display variables.

For example I thought I could use gl_PointCoord whose x and y coordinates goes from 0. to 1., but it requires to use GL_POINTS... I could give it a try by using GL_POINTS, but I'm not sure it's THE best solution.

For example, I could make a 3d scene, with lot of cubes, and display this radar on the faces of that cube. If the shader isn't independent from gl_FragCoord, it feels like it will be a nightmare to make it work.

Is there a way to get the relative position of the pixel we are giving the color, in the shape (triangle) we are rendering? Do you have suggestions / tips?

\$\endgroup\$
  • 1
    \$\begingroup\$ Why not use UV coordinates? You do need to pass them to the shader, but they're probably your best choice \$\endgroup\$ – Bálint Nov 9 '19 at 22:20
  • \$\begingroup\$ Yes it's what I'm looking for. To do this, I think that I need to pass fake textureCoordinates to my shader, and use vTextureCoord.st which should be interpolated inside the fragment shader. Is it like that ? \$\endgroup\$ – Pierre Emmanuel Lallemant Nov 10 '19 at 6:02
  • \$\begingroup\$ Thanks, your sentence helped me to understand that "varying" variables were interpolated for each pixel rendered in the fragment shader, and that I could use it. I sent a "texture coordinate" array to the shader, and now it's rendering correctly :) You can write an answer. \$\endgroup\$ – Pierre Emmanuel Lallemant Nov 10 '19 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.