GLSL Shader Examples: Blue Color Screen

GLSL TASK:

Create a shader which fills the screen with blue color. For the space occupied by right half of screen, introduce a vertical green bar located in the middle of it, with its width being equal to one-third of it. This should result in right half of screen being divided into three equal parts (blue-green-blue). As for the left half of screen, introduce a pair of vertical red bars, with width of each being equal to one-fourth of it, resulting in left half of screen being divided into four equal parts (blue-red-blue-red).

SOLUTION:

Struggling to find relevant content? Order a custom essay on
GLSL Shader Examples: Blue Color Screen
Let our experts save you the hassle
Order Now

In this sample we are going to create a basic shader in GLSL language. GLSL (OpenGL Shading Language, also referred to as GLslang), is a shading language with C-like syntax, used for programming shader effects. For the sake of demonstration purposes we will use shadertoy.com (the shader code itself is going to be tested at shadertoy.com/new )

First of all, we need to fill the screen with blue color. Shade returns four numerical values, corresponding to colors red/green/blue, as well as alpha (transparency level). So by setting each of colors to 0 we can get a black screen:

1
2
3
4
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    fragColor = vec4(0,0,0,1);
}

glsl-shader-example-1

Now we need to raise numberical value of blue color from lowest (0) to 1. Note that inputting values in-between of thes extremes (for example 0.32) is still possible.

1
2
3
4
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    fragColor = vec4(0,0,1,1);
}

glsl-shader-example-2

We got the blue screen, and now we need to determine its middle point. First of all lets create new variable of vec2 type (for storing the coordinates), and another one of vec4 type (for storing the color information)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    //this is the default blue color
    vec4 varcolor = vec4(0,0,1,1);
    //pixel coordinates obtained
    vec2 varcoord = fragCoord.xy;   
    /* x coordinate divided across screen size,
    so that we could refer to coordinates as a set of values from 0.0 to 1.0 */
    varcoord.x = varcoord.x / iResolution.x;
    //now we can make a rule where starting from exactly the middle of screen
        if(varcoord.x > 0.5) {
                //light blue color is used instead (we increased green color value by 0.5 for this)
            varcolor = vec4(0,0.5,1,1);
        }
        //displaying the result on screen now
        fragColor = varcolor;
}

glsl-shader-example-3

Now we have determined that the varcoord.x values from 0.5 to 1.0 represent the right half of the screen. We can use this knowledge in order to install the vertical green bar in the middle third of this particular screen half:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec4 varcolor = vec4(0,0,1,1);
    vec2 varcoord = fragCoord.xy;   
    varcoord.x = varcoord.x / iResolution.x;
    //this variable holds the length of one third of screen's half
    float thirdOfAHalf = 0.5/3.0;
    if(varcoord.x > 0.5) {
            varcolor = vec4(0,0.5,1,1);
    }
        //adding the green bar
        if(varcoord.x > (0.5 + thirdOfAHalf) && varcoord.x < (1.0 - thirdOfAHalf))
        {
            varcolor = vec4(0,1,0,1);
        }       
        fragColor = varcolor;
}

glsl-shader-example-4

Similarly, we can add a pair of red bars to the left half of the screen:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec4 varcolor = vec4(0,0,1,1);
    vec2 varcoord = fragCoord.xy;   
    varcoord.x = varcoord.x / iResolution.x;
    //this variable holds the length of one third of screen's half
    float thirdOfAHalf = 0.5/3.0;
    //this variable holds the length of one fourth of screen's half
    float fourthOfAHalf = 0.5/4.0;   
    if(varcoord.x > 0.5) {
        varcolor = vec4(0,0.5,1,1);
    }
    //adding the green bar
    if(varcoord.x > (0.5 + thirdOfAHalf) && varcoord.x < (1.0 - thirdOfAHalf)) { varcolor = vec4(0,1,0,1); } //adding first red bar f(varcoord.x > fourthOfAHalf && varcoord.x < 2.0*fourthOfAHalf) { varcolor = vec4(1,0,0,1); } //adding second red bar. We could just write “0.5” instead of “4.0*fourthOfAHalf” if(varcoord.x > 3.0*fourthOfAHalf && varcoord.x < 4.0*fourthOfAHalf)
    {
        varcolor = vec4(1,0,0,1);
    }
    fragColor = varcolor;
}

glsl-shader-example-5

Now when the additional color bars are in place, we no longer need the light blue color to tell apart different halves of screen, so we can remove it and streamline our code in the following fashion:

1
2
3
4
5
6
7
8
9
10
11
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec4 varcolor = vec4(0,0,1,1);
        vec2 varcoord = fragCoord.xy;   
        varcoord.x = varcoord.x / iResolution.x;
        float thirdOfAHalf = 0.5/3.0;
        float fourthOfAHalf = 0.5/4.0;   
        if(varcoord.x > (0.5 + thirdOfAHalf) && varcoord.x < (1.0 - thirdOfAHalf)) varcolor = vec4(0,1,0,1); if(varcoord.x > fourthOfAHalf && varcoord.x < 2.0*fourthOfAHalf) varcolor = vec4(1,0,0,1); if(varcoord.x > 3.0*fourthOfAHalf && varcoord.x < 0.5)
            varcolor = vec4(1,0,0,1);
    fragColor = varcolor;
}

glsl-shader-example-6

We hope you like our GLSL shader examples. If it is so we would like to offer you our service. GPA Fix is a service that helps students to reach their academic goals. It was established especially for students which need to complete technical tasks. On our website you have the ability to choose the most appropriate expert and cooperate with him or her. We hire only the best experts from different spheres so that you can be confident that you will submit correct assignments. Look through other our sample assignments and you’ll make sure that all of our experts are good in their field. Or, you can just make the order right now and make your final opinion through your own experience!

Calculate the price
Make an order in advance and get the best price
Pages (550 words)
$0.00
*Price with a welcome 15% discount applied.
Pro tip: If you want to save more money and pay the lowest price, you need to set a more extended deadline.
We know how difficult it is to be a student these days. That's why our prices are one of the most affordable on the market, and there are no hidden fees.

Instead, we offer bonuses, discounts, and free services to make your experience outstanding.
How it works
Receive a 100% original paper that will pass Turnitin from a top essay writing service
step 1
Upload your instructions
Fill out the order form and provide paper details. You can even attach screenshots or add additional instructions later. If something is not clear or missing, the writer will contact you for clarification.
Pro service tips
How to get the most out of your experience with GPA Fix
One writer throughout the entire course
If you like the writer, you can hire them again. Just copy & paste their ID on the order form ("Preferred Writer's ID" field). This way, your vocabulary will be uniform, and the writer will be aware of your needs.
The same paper from different writers
You can order essay or any other work from two different writers to choose the best one or give another version to a friend. This can be done through the add-on "Same paper from another writer."
Copy of sources used by the writer
Our college essay writers work with ScienceDirect and other databases. They can send you articles or materials used in PDF or through screenshots. Just tick the "Copy of sources" field on the order form.
Testimonials
See why 20k+ students have chosen us as their sole writing assistance provider
Check out the latest reviews and opinions submitted by real customers worldwide and make an informed decision.
Nursing
Always on time and a life saver for sure!!!!!!!!!!!!
Customer 452453, October 22nd, 2021
Literature
Thank you , this is perfect !
Customer 452795, May 15th, 2022
Other
GREAT
Customer 452813, July 3rd, 2022
public speaking
The work was excelent and very professional.
Customer 452653, November 1st, 2021
ENVIRONMENT SCIENCE
GOOD
Customer 452813, June 19th, 2022
Other
AWESOME
Customer 452813, June 20th, 2022
English 101
thank you
Customer 452483, February 27th, 2021
Nursing
Thanks
Customer 452521, May 13th, 2021
Social Work and Human Services
Excellent! Done earlier than needed and with more sources than needed! Great work!
Customer 452485, August 22nd, 2021
Criminal Justice
Impressive! Will be using the service again.
Customer 452485, December 5th, 2021
Other
AWESOME
Customer 452813, June 30th, 2022
Nursing
I appreciate all the hard work. Thank you!
Customer 452525, August 13th, 2021
11,595
Customer reviews in total
96%
Current satisfaction rate
3 pages
Average paper length
37%
Customers referred by a friend
OUR GIFT TO YOU
15% OFF your first order
Use a coupon FIRST15 and enjoy expert help with any task at the most affordable price.
Claim my 15% OFF Order in Chat