DGL
https://delphigl.com/forum/

using shaders with 2d vector graphics for gradient fills
https://delphigl.com/forum/viewtopic.php?f=20&t=8330
Seite 1 von 1

Autor:  noeska [ Mo Apr 06, 2009 11:14 ]
Betreff des Beitrags:  using shaders with 2d vector graphics for gradient fills

Hello,

My shader knowledge is bit rusty to say at least. So i could use some help.

I have an triangulated 2d shape. I can apply (angled) gradient fills on that as long as it is evenly triangulated. If not thing go look strange.
E.g. take a look at the circle in my glvg project: http://thuis.vanderhoning.net/svn_glvg/trunk/
So either i tesselate that in a better way (giving way more tringles). Still circular gradients will be hard (maybe even impossible) to apply.

Or:

Could i use shaders to apply gradient fills. Vertex shaders would not do as the same as above aplies to them i think.
So it should be fragment shaders. (most graphics card should handle these by now i hope).

1) What kind of shader should i use glsl or some low level arb fp1.0 shader?
But more importantly how does color calculation work in a fragment shader do i have x,y and are these relative to the screen? How would i relate that the 2d shape being rendered?
2) Also how do i pass the both colors to the fragment shader?

Thanks for your answers in advance.

Autor:  Lord Horazont [ Mo Apr 06, 2009 11:56 ]
Betreff des Beitrags: 

Since you are able to understand written german, I would at first recommend to take a look at Tutorial_glsl. There you find a (nearly) complete reference about the commands and syntax of a glsl shader.

Now... To the coordinates: In the fragment shader you can get both Viewport-relative and "3d-space" coordinates. The first ones are always available via gl_FragCoord (see wiki article).
For "3d-space" coords you have to declare a variant and fill it in the vertex shader:
Code:
  1.  
  2. varying vec4 pos;
  3. void main(void)
  4. {
  5.   // tell opengl the transformed position of the vertex
  6.   // gl_Vertex is the vector you set using glVertexXx
  7.   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  8.   // hand the position over to the fragment shader via a varying variable
  9.   pos = gl_Vertex;
  10.   // dont know about this line, I think this declares the color value you
  11.   // get in the fragment shader, just as if you handed it over to it
  12.   // via a varying variable
  13.   // gl_Color is the color you set using glColorXx
  14.   gl_FrontColor = gl_Color;
  15. }
  16.  

And you can use this now in the fragment shader like this:
Code:
  1.  
  2. varying vec4 pos;
  3. void main(void)
  4. {
  5.   // since varyings are interpolated for the fragments based on
  6.   // what you assigned to them in the vertex shader, you get the
  7.   // 3d-space fragment position here in pos.
  8.   // now you could use this for anything you want...
  9. }
  10.  

If you need the translated vertex position (i.e. with matrix operations applied to it, you just have to write a multiplication with gl_ModelViewProjectionMatrix or just gl_ModelViewMatrix (depending on what you need) in front of the gl_Vertex when assigning the value to pos in the vertex shader.

I hope this helped, if not, just ask again :)

greetings
Lord Horazont

Autor:  Coolcat [ Mo Apr 06, 2009 11:59 ]
Betreff des Beitrags: 

Zitat:
1) What kind of shader should i use glsl or some low level arb fp1.0 shader?
But more importantly how does color calculation work in a fragment shader do i have x,y and are these relative to the screen? How would i relate that the 2d shape being rendered?

You should use GLSL. Even my old ATI Radeon 9800Pro was able to handle that.

I think the GLSL tutorial is a good start. The fragmentshader is executed for each fragment (pixel) you render. Colors work simply be assigning a color to gl_FragColor.
In fragment shader you can access window coords of the current fragment using gl_FragCoord.xy. In case you need the window size, you will need to use an uniform variable for that. If you require world coordinates for your computation, you can use a varying from the vertexshader.

Zitat:
2) Also how do i pass the both colors to the fragment shader?

You could either use vertex attributes (simply store colors in texture coords) or use uniform variables. The third way would be a texture. You can do arbitrary texture accesses in shaders, so you can use an texture as array.

Edit:
@Lord Horazont: variant => varying ??

Autor:  Lord Horazont [ Mo Apr 06, 2009 12:04 ]
Betreff des Beitrags: 

@Coolcat: Oh ja, das hab ich wohl falsch in erinnerung gehabt, ich habs mal schnell korrigiert. Danke für den Hinweis.

Gruß Lord Horazont

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/