OpenGL 4 Shading Language Cookbook, Third Edition by David Wolff

OpenGL 4 Shading Language Cookbook, Third Edition by David Wolff

Author:David Wolff
Language: eng
Format: mobi, epub
Tags: COM012040 - COMPUTERS / Programming / Games, COM012000 - COMPUTERS / Computer Graphics, COM051070 - COMPUTERS / Programming Languages / C++
Publisher: Packt
Published: 2018-09-27T08:50:36+00:00


How to do it...

In the fragment shader, we apply the Blinn-Phong reflection model in the first pass. In the second pass, we compute the vertical sum. In the third, we compute the horizontal sum:

in vec3 Position; // Vertex position in vec3 Normal; // Vertex normal

uniform int Pass; // Pass number

layout(binding=0) uniform sampler2D Texture0; // Light/material uniforms ....

layout( location = 0 ) out vec4 FragColor; uniform int PixOffset[5] = int[](0,1,2,3,4); uniform float Weight[5]; vec3 blinnPhong( vec3 pos, vec3 norm ) { // ... } vec4 pass1() { return vec4(blinnPhong( Position, normalize(Normal) ),1.0); } vec4 pass2() { ivec2 pix = ivec2(gl_FragCoord.xy); vec4 sum = texelFetch(Texture0, pix, 0) * Weight[0]; for( int i = 1; i < 5; i++ ) { sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,PixOffset[i])) * Weight[i]; sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,-PixOffset[i])) * Weight[i]; } return sum; } vec4 pass3() { ivec2 pix = ivec2(gl_FragCoord.xy); vec4 sum = texelFetch(Texture0, pix, 0) * Weight[0]; for( int i = 1; i < 5; i++ ) { sum += texelFetchOffset( Texture0, pix, 0, ivec2(PixOffset[i],0)) * Weight[i]; sum += texelFetchOffset( Texture0, pix, 0, ivec2(-PixOffset[i],0)) * Weight[i]; } return sum; } void main() { if( Pass == 1 ) FragColor = pass1();

else if( Pass == 2 ) FragColor = pass2();

else if( Pass == 3 ) FragColor = pass3(); }

In the OpenGL application, compute the Gaussian weights for the offsets found in the uniform variable PixOffset, and store the results in the array Weight. You could use the following code to do so:

char uniName[20]; float weights[5], sum, sigma2 = 4.0f; // Compute and sum the weights weights[0] = gauss(0,sigma2); // The 1-D Gaussian function sum = weights[0]; for( int i = 1; i < 5; i++ ) { weights[i] = gauss(i, sigma2); sum += 2 * weights[i]; } // Normalize the weights and set the uniform for( int i = 0; i < 5; i++ ) { snprintf(uniName, 20, "Weight[%d]", i); prog.setUniform(uniName, weights[i] / sum); }

In the main render function, implement the following steps for pass #1:

Select the render framebuffer, enable the depth test, and clear the color/depth buffers

Set Pass to 1

Draw the scene



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(56781)
What's Done in Darkness by Kayla Perrin(26802)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(20670)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20446)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20444)
The Fifty Shades Trilogy & Grey by E L James(19305)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19259)
Shot Through the Heart by Mercy Celeste(19128)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17292)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17227)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(17073)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16997)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16615)
The Subtle Art of Not Giving a F*ck by Mark Manson(14616)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14309)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13967)
Scorched Earth by Nick Kyme(12963)
Drei Generationen auf dem Jakobsweg by Stein Pia(11146)
Suna by Ziefle Pia(11071)
Scythe by Neal Shusterman(10556)