0

Is it possible using a GLSL shader to calculate the color of vertices per pixel instead of per vertex? Because it looks a bit like per vertex lighting(kinda ugly).

For example

What I have: enter image description here

What it should look like: enter image description here

user3075425
  • 332
  • 1
  • 6
  • 23

1 Answers1

3

Is it possible using a GLSL shader to calculate the color of vertices per pixel [...]

"Color of vertices per pixel" does not really make sense. What one does calculate is the color of fragments (which are the results of the rasterisation for the pixel raster, but did not yet affect the framebuffer pixels). And that color is always calculated per fragment - in a "per-vertex lighting" setup, that color is just calculated by interpolating the colors from the lighting at each vertex of the primitive (aka. Gouraud Shading), possibly further manipulating it (e.g. by texture mapping).

What you probably mean here is "Is it possible using a GLSL shader to calculate the lighting euqation per fragment (Phong Shading, per-pixel lighting)?" And the answer to that is: yes. That can be done in the fragment shader.

Because it looks a bit like per vertex lighting(kinda ugly).

What looks that way? Fixed-function GL? Yes, it is specified that way. The old fixed-function lighting is done per vertex, and only flat and Gouraud shading were supported back then. Shaders did change that.

derhass
  • 43,833
  • 2
  • 57
  • 78
  • Thanks for the answer. Could you give me a code example? – user3075425 Feb 19 '14 at 06:43
  • @user3075425: You should have a look at the [Illumination Chapter](http://www.arcsynthesis.org/gltut/Illumination/Illumination.html) of the [arcsynthesis tutorial](http://www.arcsynthesis.org/gltut/), especially the sections about [Vertex Point Lights](http://www.arcsynthesis.org/gltut/Illumination/Tutorial%2010.html#d0e9855) and [Fragment Lighting](http://www.arcsynthesis.org/gltut/Illumination/Tut10%20Fragment%20Lighting.html) which come with example shaders for both variants. – derhass Feb 19 '14 at 20:41
  • I still dont quite understand how to do this excactly. Because in per pixel lighting the calculation is in the fragment shader, but in a glColor fragment shader there is no calculation needed. Am I wrong? – user3075425 Feb 22 '14 at 19:19
  • @user3075425: I'm not sure if I get your question right, what is a "glColor shader"?. When you don't do lighting, you don't have to calculate it, that should be obvious. – derhass Feb 22 '14 at 19:23
  • For example if I set gl_FragColor to gl_Color. I think you got my question a bit wrong, I dont have a problem with lighting. The problem is the thing with the vertex color that I set by using glColor3f(...). – user3075425 Feb 23 '14 at 10:36
  • @user3075425: `gl_Color` is a builtin (and hence deprecated) attribute in the vertex language, but `gl_FragCoord` is a builtin (also deprecated) output of the fragment language, hence you cannot set `gl_FragCoord=gl_Color`. What you can do is use a varying to fowrad the color information per vertex to the fragment shader (which will be interpolated per fragemnt across the whole primitve), and can use that to set (or modify) the fragment color. But it is still unclear to me what you are asking... – derhass Feb 23 '14 at 12:58