<p>I am learning about fragment shaders and have seen how by setting the colours of each vertex a triangle can be coloured via interpolation.</p>

<p><a href="https://i.sstatic.net/sxTXz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/sxTXz.png" alt="enter image description here"></a></p>

<p>However what if we want to use our own function to manually specify the colour of each fragment (e.g. for lighting or for creating a procedurally generated texture)?</p>

<p>Is there any way to create a fragment shader that runs a user-defined function to determine the colour of each fragment instead of leaving it up to opengl to interpolate between vertex colours?</p>

<p>As an example - is it possible to create a fragment shader that sets each fragment to a random colour? Or one that creates a striped effect by setting pixels between a certain x range to black and others to white?</p>

CC BY-SA 4.0
2020-03-29T02:15:48.843Z
It's not clear what you're asking about here. A fragment shader *is* a "user-defined function to determine the colour of each fragment". Determining the fragment's color is the *entire point* of a fragment shader. It's just code; you can do what you want with it.
Nicol Bolas
CC BY-SA 4.0
2020-03-29T12:53:25.090Z
@NicolBolas From the wiki: _"The user-defined inputs received by this fragment shader will be interpolated according to the interpolation qualifiers declared on the input variables declared by this fragment shader"_. I cannot find an example of how you would specify a different colour per fragment. All the wiki seems to mention is specifying colours per vertex which then get automatically interpolated for each fragment.
JShorthouse
CC BY-SA 4.0
2020-03-29T13:06:27.997Z
@NicolBolas Nevermind, I think I am getting confused thinking that the fragment shader is the part doing the interpolation, when in the example image the interpolation would actually be done by the vertex shader, or somewhere inbetween the two of them? The fragement shader is just being passed the interpolated value? Is this correct?
JShorthouse
<opengl><glsl>
Specifying Fragment Shader Colors per Fragment (Instead of per Vertex)
JShorthouse
# Specifying Fragment Shader Colors per Fragment (Instead of per Vertex)

(Asked by: username_0 on 2020-03-28)

I am learning about fragment shaders and have seen how by setting the colours of each vertex a triangle can be coloured via interpolation.

[![enter image description here](https://i.sstatic.net/sxTXz.png)](https://i.sstatic.net/sxTXz.png)

However what if we want to use our own function to manually specify the colour of each fragment (e.g. for lighting or for creating a procedurally generated texture)?

Is there any way to create a fragment shader that runs a user-defined function to determine the colour of each fragment instead of leaving it up to opengl to interpolate between vertex colours?

As an example - is it possible to create a fragment shader that sets each fragment to a random colour? Or one that creates a striped effect by setting pixels between a certain x range to black and others to white?

**Tags:** opengl, glsl

> **Comments:**
> **username_1**: It's not clear what you're asking about here. A fragment shader *is* a "user-defined function to determine the colour of each fragment". Determining the fragment's color is the *entire point* of a fragment shader. It's just code; you can do what you want with it.  
> **username_0**: @username_1 From the wiki: _"The user-defined inputs received by this fragment shader will be interpolated according to the interpolation qualifiers declared on the input variables declared by this fragment shader"_. I cannot find an example of how you would specify a different colour per fragment. All the wiki seems to mention is specifying colours per vertex which then get automatically interpolated for each fragment.  
> **username_0**: @username_1 Nevermind, I think I am getting confused thinking that the fragment shader is the part doing the interpolation, when in the example image the interpolation would actually be done by the vertex shader, or somewhere inbetween the two of them? The fragement shader is just being passed the interpolated value? Is this correct?  

---

## 0 answers

