OpenGL 4 Shading Language Cookbook by David Wolff
Author:David Wolff
Language: eng
Format: epub
Tags: COM051070 - COMPUTERS / Programming Languages / C++, COM012040 - COMPUTERS / Programming / Games, COM012000 - COMPUTERS / Computer Graphics
Publisher: Packt Publishing
Published: 2018-09-27T08:45:54+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
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.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7406)
Drawing Shortcuts: Developing Quick Drawing Skills Using Today's Technology by Leggitt Jim(2532)
Hands-On Unity 2022 Game Development by Nicolas Alejandro Borromeo(2493)
The 46 Rules of Genius: An Innovator's Guide to Creativity (Voices That Matter) by Marty Neumeier(2312)
Autodesk Civil 3D 2024 from Start to Finish by Stephen Walz Tony Sabat(2292)
Mathematics for Game Programming and Computer Graphics by Penny de Byl(2197)
Rapid Viz: A New Method for the Rapid Visualization of Ideas by Kurt Hanks & Larry Belliston(2195)
Learn Qt 5: Build modern, responsive cross-platform desktop applications with Qt, C++, and QML by Nicholas Sherriff(2176)
Fusion 360 for Makers by Lydia Sloan Cline(1987)
Hands-On Neural Networks with Keras by Niloy Purkait(1883)
Taking Blender to the Next Level by Ruan Lotter(1871)
Express Your Creativity with Adobe Express by Rosie Sue(1849)
Mastering GUI Programming with Python by Alan D. Moore(1762)
Game Physics Cookbook by Gabor Szauer(1576)
Creative Character Design by Bryan Tillman(1558)
Unreal Engine 4 Game Development Quick Start Guide by Rachel Cordone(1438)
Hands-On GUI Application Development in Go by Andrew Williams(1428)
Qt 5 and OpenCV 4 Computer Vision Projects by Zhuo Qingliang(1409)
Mastering Graphics Programming with Vulkan by Marco Castorina | Gabriel Sassone(1391)