Blending.html (24491B)
1 <div id="content"> 2 <h1 id="content-title">Blending</h1> 3 <h1 id="content-url" style='display:none;'>Advanced-OpenGL/Blending</h1> 4 <p> 5 <def>Blending</def> in OpenGL is commonly known as the technique to implement <def>transparency</def> within objects. Transparency is all about objects (or parts of them) not having a solid color, but having a combination of colors from the object itself and any other object behind it with varying intensity. A colored glass window is a transparent object; the glass has a color of its own, but the resulting color contains the colors of all the objects behind the glass as well. This is also where the name blending comes from, since we <def>blend</def> several pixel colors (from different objects) to a single color. Transparency thus allows us to see through objects. 6 </p> 7 8 <img src="/img/advanced/blending_transparency.png" class="clean" alt="Image of full transparent window and partially transparent window"/> 9 10 <p> 11 Transparent objects can be completely transparent (letting all colors through) or partially transparent (letting colors through, but also some of its own colors). The amount of transparency of an object is defined by its color's <def>alpha</def> value. The alpha color value is the 4th component of a color vector that you've probably seen quite often now. Up until this chapter, we've always kept this 4th component at a value of <code>1.0</code> giving the object <code>0.0</code> transparency. An alpha value of <code>0.0</code> would result in the object having complete transparency. An alpha value of <code>0.5</code> tells us the object's color consist of 50% of its own color and 50% of the colors behind the object. 12 </p> 13 14 <p> 15 The textures we've used so far all consisted of <code>3</code> color components: red, green and blue, but some textures also have an embedded alpha channel that contains an <def>alpha</def> value per texel. This alpha value tells us exactly which parts of the texture have transparency and by how much. For example, the following <a href="/img/advanced/blending_transparent_window.png" target="_blank">window texture</a> has an alpha value of <code>0.25</code> at its glass part and an alpha value of <code>0.0</code> at its corners. The glass part would normally be completely red, but since it has 75% transparency it largely shows the page's background through it, making it seem a lot less red: 16 </p> 17 18 <img src="/img/advanced/blending_transparent_window.png" class="clean" alt="Texture image of window with transparency"/> 19 20 <p> 21 We'll soon be adding this windowed texture to the scene from the depth testing chapter, but first we'll discuss an easier technique to implement transparency for pixels that are either fully transparent or fully opaque. 22 </p> 23 24 <h2>Discarding fragments</h2> 25 <p> 26 Some effects do not care about partial transparency, but either want to show something or nothing at all based on the color value of a texture. Think of grass; to create something like grass with little effort you generally paste a grass texture onto a 2D quad and place that quad into your scene. However, grass isn't exactly shaped like a 2D square so you only want to display some parts of the grass texture and ignore the others. 27 </p> 28 29 <p> 30 The following <a href="/img/textures/grass.png" target="_blank">texture</a> is exactly such a texture where it either is full opaque (an alpha value of <code>1.0</code>) or it is fully transparent (an alpha value of <code>0.0</code>) and nothing in between. You can see that wherever there is no grass, the image shows the page's background color instead of its own. 31 </p> 32 33 <img src="/img/textures/grass.png" class="clean" style="width:384px; height:384px;" alt="Texture image of grass with transparency"/> 34 35 <p> 36 So when adding vegetation to a scene we don't want to see a square image of grass, but rather only show the actual grass and see through the rest of the image. We want to <def>discard</def> the fragments that show the transparent parts of the texture, not storing that fragment into the color buffer. 37 </p> 38 39 <p> 40 Before we get into that we first need to learn how to load a transparent texture. To load textures with alpha values there's not much we need to change. <code>stb_image</code> automatically loads an image's alpha channel if it's available, but we do need to tell OpenGL our texture now uses an alpha channel in the texture generation procedure: 41 </p> 42 43 <pre class="cpp"><code> 44 <function id='52'>glTexImage2D</function>(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 45 </code></pre> 46 47 <p> 48 Also make sure that you retrieve all <code>4</code> color components of the texture in the fragment shader, not just the RGB components: 49 </p> 50 51 <pre><code> 52 void main() 53 { 54 // FragColor = vec4(vec3(texture(texture1, TexCoords)), 1.0); 55 FragColor = texture(texture1, TexCoords); 56 } 57 </code></pre> 58 59 <p> 60 Now that we know how to load transparent textures it's time to put it to the test by adding several of these leaves of grass throughout the basic scene introduced in the <a href="https://learnopengl.com/Advanced-OpenGL/Depth-testing" target="_blank">depth testing</a> chapter. 61 </p> 62 63 <p> 64 We create a small <code>vector</code> array where we add several <code>glm::vec3</code> vectors to represent the location of the grass leaves: 65 </p> 66 67 <pre><code> 68 vector<glm::vec3> vegetation; 69 vegetation.push_back(glm::vec3(-1.5f, 0.0f, -0.48f)); 70 vegetation.push_back(glm::vec3( 1.5f, 0.0f, 0.51f)); 71 vegetation.push_back(glm::vec3( 0.0f, 0.0f, 0.7f)); 72 vegetation.push_back(glm::vec3(-0.3f, 0.0f, -2.3f)); 73 vegetation.push_back(glm::vec3( 0.5f, 0.0f, -0.6f)); 74 </code></pre> 75 76 <p> 77 Each of the grass objects is rendered as a single quad with the grass texture attached to it. It's not a perfect 3D representation of grass, but it's a lot more efficient than loading and rendering a large number of complex models. With a few tricks like adding randomized rotations and scales you can get pretty convincing results with quads. 78 </p> 79 80 <p> 81 Because the grass texture is going to be displayed on a quad object we'll need to create another VAO again, fill the VBO, and set the appropriate vertex attribute pointers. Then after we've rendered the floor and the two cubes we're going to render the grass leaves: 82 </p> 83 84 <pre><code> 85 <function id='27'>glBindVertexArray</function>(vegetationVAO); 86 <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, grassTexture); 87 for(unsigned int i = 0; i < vegetation.size(); i++) 88 { 89 model = glm::mat4(1.0f); 90 model = <function id='55'>glm::translate</function>(model, vegetation[i]); 91 shader.setMat4("model", model); 92 <function id='1'>glDrawArrays</function>(GL_TRIANGLES, 0, 6); 93 } 94 </code></pre> 95 96 <p> 97 Running the application will now look a bit like this: 98 </p> 99 100 <img src="/img/advanced/blending_no_discard.png" class="clean" alt="Not discarding transparent parts of texture results in weird artifacts in OpenGL"/> 101 102 <p> 103 This happens because OpenGL by default does not know what to do with alpha values, nor when to discard them. We have to manually do this ourselves. Luckily this is quite easy thanks to the use of shaders. GLSL gives us the <code>discard</code> command that (once called) ensures the fragment will not be further processed and thus not end up into the color buffer. Thanks to this command we can check whether a fragment has an alpha value below a certain threshold and if so, discard the fragment as if it had never been processed: 104 </p> 105 106 <pre><code> 107 #version 330 core 108 out vec4 FragColor; 109 110 in vec2 TexCoords; 111 112 uniform sampler2D texture1; 113 114 void main() 115 { 116 vec4 texColor = texture(texture1, TexCoords); 117 if(texColor.a < 0.1) 118 discard; 119 FragColor = texColor; 120 } 121 </code></pre> 122 123 124 <p> 125 Here we check if the sampled texture color contains an alpha value lower than a threshold of <code>0.1</code> and if so, discard the fragment. This fragment shader ensures us that it only renders fragments that are not (almost) completely transparent. Now it'll look like it should: 126 </p> 127 128 129 <img src="/img/advanced/blending_discard.png" class="clean" alt="Image of grass leaves rendered with fragment discarding in OpenGL"/> 130 131 <note> 132 Note that when sampling textures at their borders, OpenGL interpolates the border values with the next repeated value of the texture (because we set its wrapping parameters to <var>GL_REPEAT</var> by default). This is usually okay, but since we're using transparent values, the top of the texture image gets its transparent value interpolated with the bottom border's solid color value. The result is then a slightly semi-transparent colored border you may see wrapped around your textured quad. To prevent this, set the texture wrapping method to <var>GL_CLAMP_TO_EDGE</var> whenever you use alpha textures that you don't want to repeat: 133 134 <pre><code> 135 <function id='15'>glTexParameter</function>i( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 136 <function id='15'>glTexParameter</function>i( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 137 </code></pre> 138 </note> 139 140 <p> 141 You can find the source code <a href="/code_viewer_gh.php?code=src/4.advanced_opengl/3.1.blending_discard/blending_discard.cpp" target="_blank">here</a>. 142 </p> 143 144 145 <h2>Blending</h2> 146 <p> 147 While discarding fragments is great and all, it doesn't give us the flexibility to render semi-transparent images; we either render the fragment or completely discard it. To render images with different levels of transparency we have to enable <def>blending</def>. Like most of OpenGL's functionality we can enable blending by enabling <var>GL_BLEND</var>: 148 </p> 149 150 <pre><code> 151 <function id='60'>glEnable</function>(GL_BLEND); 152 </code></pre> 153 154 <p> 155 Now that we've enabled blending we need to tell OpenGL <strong>how</strong> it should actually blend. 156 </p> 157 158 <p> 159 Blending in OpenGL happens with the following equation: 160 </p> 161 162 \begin{equation}\bar{C}_{result} = \bar{\color{green}C}_{source} * \color{green}F_{source} + \bar{\color{red}C}_{destination} * \color{red}F_{destination}\end{equation} 163 164 <ul> 165 <li>\(\bar{\color{green}C}_{source}\): the source color vector. This is the color output of the fragment shader.</li> 166 <li>\(\bar{\color{red}C}_{destination}\): the destination color vector. This is the color vector that is currently stored in the color buffer.</li> 167 <li>\(\color{green}F_{source}\): the source factor value. Sets the impact of the alpha value on the source color.</li> 168 <li>\(\color{red}F_{destination}\): the destination factor value. Sets the impact of the alpha value on the destination color.</li> 169 </ul> 170 171 <p> 172 After the fragment shader has run and all the tests have passed, this <def>blend equation</def> is let loose on the fragment's color output and with whatever is currently in the color buffer. The source and destination colors will automatically be set by OpenGL, but the source and destination factor can be set to a value of our choosing. Let's start with a simple example: 173 </p> 174 175 <img src="/img/advanced/blending_equation.png" class="clean" alt="Two squares where one has alpha value lower than 1"/> 176 177 <p> 178 We have two squares where we want to draw the semi-transparent green square on top of the red square. The red square will be the destination color (and thus should be first in the color buffer) and we are now going to draw the green square over the red square. 179 </p> 180 181 <p> 182 The question then arises: what do we set the factor values to? Well, we at least want to multiply the green square with its alpha value so we want to set the \(F_{src}\) equal to the alpha value of the source color vector which is <code>0.6</code>. Then it makes sense to let the destination square have a contribution equal to the remainder of the alpha value. If the green square contributes 60% to the final color we want the red square to contribute 40% of the final color e.g. <code>1.0 - 0.6</code>. So we set \(F_{destination}\) equal to one minus the alpha value of the source color vector. The equation thus becomes: 183 </p> 184 185 \begin{equation}\bar{C}_{result} = \begin{pmatrix} \color{red}{0.0} \\ \color{green}{1.0} \\ \color{blue}{0.0} \\ \color{purple}{0.6} \end{pmatrix} * \color{green}{0.6} + \begin{pmatrix} \color{red}{1.0} \\ \color{green}{0.0} \\ \color{blue}{0.0} \\ \color{purple}{1.0} \end{pmatrix} * (\color{red}{1 - 0.6}) \end{equation} 186 187 <p> 188 The result is that the combined square fragments contain a color that is 60% green and 40% red: 189 </p> 190 191 <img src="/img/advanced/blending_equation_mixed.png" class="clean" alt="Two containers where one has alpha value lower than 1"/> 192 193 <p> 194 The resulting color is then stored in the color buffer, replacing the previous color. 195 </p> 196 197 <p> 198 So this is great and all, but how do we actually tell OpenGL to use factors like that? Well it just so happens that there is a function for this called <fun><function id='70'>glBlendFunc</function></fun>. 199 </p> 200 201 <p> 202 The <fun><function id='70'>glBlendFunc</function>(GLenum sfactor, GLenum dfactor)</fun> function expects two parameters that set the option for the <def>source</def> and <def>destination factor</def>. OpenGL defined quite a few options for us to set of which we'll list the most common options below. Note that the constant color vector \(\bar{\color{blue}C}_{constant}\) can be separately set via the <fun><function id='73'>glBlendColor</function></fun> function. 203 </p> 204 205 <table> 206 <tr> 207 <th>Option</th> 208 <th>Value</th> 209 </tr> 210 <tr> 211 <td><code>GL_ZERO</code></td> 212 <td>Factor is equal to \(0\).</td> 213 </tr> 214 <tr> 215 <td><code>GL_ONE</code></td> 216 <td>Factor is equal to \(1\).</td> 217 </tr> 218 <tr> 219 <td><code>GL_SRC_COLOR</code></td> 220 <td>Factor is equal to the source color vector \(\bar{\color{green}C}_{source}\).</td> 221 </tr> 222 <tr> 223 <td><code>GL_ONE_MINUS_SRC_COLOR</code></td> 224 <td>Factor is equal to \(1\) minus the source color vector: \(1 - \bar{\color{green}C}_{source}\). </td> 225 </tr><tr> 226 <td><code>GL_DST_COLOR</code></td> 227 <td>Factor is equal to the destination color vector \(\bar{\color{red}C}_{destination}\)</td> 228 </tr> 229 <tr> 230 <td><code>GL_ONE_MINUS_DST_COLOR</code></td> 231 <td>Factor is equal to \(1\) minus the destination color vector: \(1 - \bar{\color{red}C}_{destination}\).</td> 232 </tr> 233 <tr> 234 <td><code>GL_SRC_ALPHA</code></td> 235 <td>Factor is equal to the \(alpha\) component of the source color vector \(\bar{\color{green}C}_{source}\). </td> 236 </tr> 237 <tr> 238 <td><code>GL_ONE_MINUS_SRC_ALPHA</code></td> 239 <td>Factor is equal to \(1 - alpha\) of the source color vector \(\bar{\color{green}C}_{source}\).</td> 240 </tr> 241 <tr> 242 <td><code>GL_DST_ALPHA</code></td> 243 <td>Factor is equal to the \(alpha\) component of the destination color vector \(\bar{\color{red}C}_{destination}\). </td> 244 </tr> 245 <tr> 246 <td><code>GL_ONE_MINUS_DST_ALPHA</code></td> 247 <td>Factor is equal to \(1 - alpha\) of the destination color vector \(\bar{\color{red}C}_{destination}\).</td> 248 </tr> 249 <tr> 250 <td><code>GL_CONSTANT_COLOR</code></td> 251 <td>Factor is equal to the constant color vector \(\bar{\color{blue}C}_{constant}\). </td> 252 </tr> 253 <tr> 254 <td><code>GL_ONE_MINUS_CONSTANT_COLOR</code></td> 255 <td>Factor is equal to \(1\) - the constant color vector \(\bar{\color{blue}C}_{constant}\).</td> 256 </tr> 257 <tr> 258 <td><code>GL_CONSTANT_ALPHA</code></td> 259 <td>Factor is equal to the \(alpha\) component of the constant color vector \(\bar{\color{blue}C}_{constant}\). </td> 260 </tr> 261 <tr> 262 <td><code>GL_ONE_MINUS_CONSTANT_ALPHA</code></td> 263 <td>Factor is equal to \(1 - alpha\) of the constant color vector \(\bar{\color{blue}C}_{constant}\).</td> 264 </tr> 265 </table> 266 267 <p> 268 To get the blending result of our little two square example, we want to take the \(alpha\) of the source color vector for the source factor and \(1 - alpha\) of the same color vector for the destination factor. This translates to <fun><function id='70'>glBlendFunc</function></fun> as follows: 269 </p> 270 271 <pre><code> 272 <function id='70'>glBlendFunc</function>(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 273 </code></pre> 274 275 <p> 276 It is also possible to set different options for the RGB and alpha channel individually using <fun><function id='71'><function id='70'>glBlendFunc</function>Separate</function></fun>: 277 </p> 278 279 <pre><code> 280 <function id='71'><function id='70'>glBlendFunc</function>Separate</function>(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO); 281 </code></pre> 282 283 <p> 284 This function sets the RGB components as we've set them previously, but only lets the resulting alpha component be influenced by the source's alpha value. 285 </p> 286 287 <p> 288 OpenGL gives us even more flexibility by allowing us to change the operator between the source and destination part of the equation. Right now, the source and destination components are added together, but we could also subtract them if we want. <fun><function id='72'>glBlendEquation</function>(GLenum mode)</fun> allows us to set this operation and has 5 possible options: 289 </p> 290 291 <ul> 292 <li><code>GL_FUNC_ADD</code>: the default, adds both colors to each other: \(\bar{C}_{result} = \color{green}{Src} + \color{red}{Dst}\).</li> 293 <li><code>GL_FUNC_SUBTRACT</code>: subtracts both colors from each other: \(\bar{C}_{result} = \color{green}{Src} - \color{red}{Dst}\).</li> 294 <li><code>GL_FUNC_REVERSE_SUBTRACT</code>: subtracts both colors, but reverses order: \(\bar{C}_{result} = \color{red}{Dst} - \color{green}{Src}\).</li> 295 <li><code>GL_MIN</code>: takes the component-wise minimum of both colors: \(\bar{C}_{result} = min(\color{red}{Dst}, \color{green}{Src})\).</li> 296 <li><code>GL_MAX</code>: takes the component-wise maximum of both colors: \(\bar{C}_{result} = max(\color{red}{Dst}, \color{green}{Src})\).</li> 297 </ul> 298 299 <p> 300 Usually we can simply omit a call to <fun><function id='72'>glBlendEquation</function></fun> because <var>GL_FUNC_ADD</var> is the preferred blending equation for most operations, but if you're really trying your best to break the mainstream circuit any of the other equations could suit your needs. 301 </p> 302 303 <h2>Rendering semi-transparent textures</h2> 304 <p> 305 Now that we know how OpenGL works with regards to blending it's time to put our knowledge to the test by adding several semi-transparent windows. We'll be using the same scene as in the start of this chapter, but instead of rendering a grass texture we're now going to use the <a href="/img/advanced/blending_transparent_window.png" target="_blank">transparent window</a> texture from the start of this chapter. 306 </p> 307 308 <p> 309 First, during initialization we enable blending and set the appropriate blending function: 310 </p> 311 312 <pre><code> 313 <function id='60'>glEnable</function>(GL_BLEND); 314 <function id='70'>glBlendFunc</function>(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 315 </code></pre> 316 317 <p> 318 Since we enabled blending there is no need to discard fragments so we'll reset the fragment shader to its original version: 319 </p> 320 321 <pre><code> 322 #version 330 core 323 out vec4 FragColor; 324 325 in vec2 TexCoords; 326 327 uniform sampler2D texture1; 328 329 void main() 330 { 331 FragColor = texture(texture1, TexCoords); 332 } 333 </code></pre> 334 335 <p> 336 This time (whenever OpenGL renders a fragment) it combines the current fragment's color with the fragment color currently in the color buffer based on the alpha value of <var>FragColor</var>. Since the glass part of the window texture is semi-transparent we should be able to see the rest of the scene by looking through this window. 337 </p> 338 339 340 <img src="/img/advanced/blending_incorrect_order.png" class="clean" alt="A blended scene in OpenGL where order is incorrect."/> 341 342 <p> 343 If you take a closer look however, you may notice something is off. The transparent parts of the front window are occluding the windows in the background. Why is this happening? 344 </p> 345 346 <p> 347 The reason for this is that depth testing works a bit tricky combined with blending. When writing to the depth buffer, the depth test does not care if the fragment has transparency or not, so the transparent parts are written to the depth buffer as any other value. The result is that the background windows are tested on depth as any other opaque object would be, ignoring transparency. Even though the transparent part should show the windows behind it, the depth test discards them. 348 </p> 349 350 <p> 351 So we cannot simply render the windows however we want and expect the depth buffer to solve all our issues for us; this is also where blending gets a little nasty. To make sure the windows show the windows behind them, we have to draw the windows in the background first. This means we have to manually sort the windows from furthest to nearest and draw them accordingly ourselves. 352 </p> 353 354 <note> 355 Note that with fully transparent objects like the grass leaves we have the option to discard the transparent fragments instead of blending them, saving us a few of these headaches (no depth issues). 356 </note> 357 358 <h2>Don't break the order</h2> 359 <p> 360 To make blending work for multiple objects we have to draw the most distant object first and the closest object last. The normal non-blended objects can still be drawn as normal using the depth buffer so they don't have to be sorted. We do have to make sure they are drawn first before drawing the (sorted) transparent objects. When drawing a scene with non-transparent and transparent objects the general outline is usually as follows: 361 </p> 362 363 <ol> 364 <li>Draw all opaque objects first.</li> 365 <li>Sort all the transparent objects.</li> 366 <li>Draw all the transparent objects in sorted order.</li> 367 </ol> 368 369 <p> 370 One way of sorting the transparent objects is to retrieve the distance of an object from the viewer's perspective. This can be achieved by taking the distance between the camera's position vector and the object's position vector. We then store this distance together with the corresponding position vector in a <fun>map</fun> data structure from the STL library. A <fun>map</fun> automatically sorts its values based on its keys, so once we've added all positions with their distance as the key they're automatically sorted on their distance value: 371 </p> 372 373 <pre><code> 374 std::map<float, glm::vec3> sorted; 375 for (unsigned int i = 0; i < windows.size(); i++) 376 { 377 float distance = glm::length(camera.Position - windows[i]); 378 sorted[distance] = windows[i]; 379 } 380 </code></pre> 381 382 <p> 383 The result is a sorted container object that stores each of the window positions based on their <var>distance</var> key value from lowest to highest distance. 384 </p> 385 386 <p> 387 Then, this time when rendering, we take each of the map's values in reverse order (from farthest to nearest) and then draw the corresponding windows in correct order: 388 </p> 389 390 <pre><code> 391 for(std::map<float,glm::vec3>::reverse_iterator it = sorted.rbegin(); it != sorted.rend(); ++it) 392 { 393 model = glm::mat4(1.0f); 394 model = <function id='55'>glm::translate</function>(model, it->second); 395 shader.setMat4("model", model); 396 <function id='1'>glDrawArrays</function>(GL_TRIANGLES, 0, 6); 397 } 398 </code></pre> 399 400 <p> 401 We take a reverse iterator from the <fun>map</fun> to iterate through each of the items in reverse order and then translate each window quad to the corresponding window position. This relatively simple approach to sorting transparent objects fixes the previous problem and now the scene looks like this: 402 </p> 403 404 <img src="/img/advanced/blending_sorted.png" class="clean" alt="Image of an OpenGL scene with blending enabled, objects are sorted from far to near"/> 405 406 <p> 407 You can find the complete source code with sorting <a href="/code_viewer_gh.php?code=src/4.advanced_opengl/3.2.blending_sort/blending_sorted.cpp" target="_blank">here</a>. 408 </p> 409 410 <p> 411 While this approach of sorting the objects by their distance works well for this specific scenario, it doesn't take rotations, scaling or any other transformation into account and weirdly shaped objects need a different metric than simply a position vector. 412 </p> 413 414 <p> 415 Sorting objects in your scene is a difficult feat that depends greatly on the type of scene you have, let alone the extra processing power it costs. Completely rendering a scene with solid and transparent objects isn't all that easy. There are more advanced techniques like <def>order independent transparency</def> but these are out of the scope of this chapter. For now you'll have to live with normally blending your objects, but if you're careful and know the limitations you can get pretty decent blending implementations. 416 </p> 417 418 419 </div> 420