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!

GPA Fix: Online Essay Writing Service from Top Tutors
Save More. Score Better
Pages (550 words)
Approximate price: -

Why Hire a Professional Essay Writer from GPA Fix

Quality Urgent Essays

GPA Fix online platform provides the best essay writing service ever received elsewhere, thanks to our professional essay writers. Experience gained in the 10+ years of being in the assignment writing industry is also an added advantage.

Professional and Experienced Academic Writers

A lot is done to maintain us as the best essay writing service provider. Besides owning a degree from a recognized university, a writer must pass the rigorous tests we take them through before they are considered eligible to offer urgent essay help on our website.

Affordable Urgent Assignment Help

Who said a reliable website should not be cheap also? While it is human nature to associate prices with quality, that does not always work. At gpafix.com, we guarantee you cheap and urgent essay help without compromising the quality.

24/7 Available Urgent Essay Writing Service

Whether it is a ‘write my essay for me cheap’ or ‘edit my essay’ order you want to request, always feel free to reach us at any time of the day. Our experts work in shifts to ensure that you access urgent essay writing services at any time of the day.

Plagiarism Free Essays Online

The punitive measures that come with plagiarized content are so harsh, and we understand that. We consequently ensure to write each client’s papers from scratch no matter how urgent the essay is. To acknowledge borrowed content, our quick writer references and cites the work.

Wow Customer Support 24/7

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

So much stress and so little time? Take care of yourself: let us help you with your tasks. We offer all kinds of writing services.

Essays

Custom Essay Writing Services

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission and Business Papers

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Editing

Editing and Proofreading

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Coursework

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.