tailoring

The previous two sections covered deep testing and front and back culling, respectively. Both of these skills are commonly used in OpenGL to improve rendering performance. Cutting is also a way to enhance sex. OpenGL allows you to refresh only the parts of the screen that have changed. You can specify a clipping box in the window that you want to render. In this clipping box to render the image you need.

The basic principle is to limit the drawing area at render time. This technique allows you to specify a rectangular area on the screen (frame buffer). The clipping test is then enabled, and the slices that are not in the rectangular region are discarded, and only the slices that are in the rectangular region may enter the frame buffer. So what you’re actually doing is opening up a small window on the screen where you can draw the specified content.

Cutting test

The clipping test takes place in the chip shader, where rasterization transforms 3D graphics into 2D graphics. The clipping test determines whether (Xw,Yw) is within the range of the part of the clipping rectangle determined by the current OpenGL context. If the segment is outside the clipping region, it is discarded.

Here we need to understand and distinguish the window, viewport and clipping area probability.

Windows, viewports, and cropping areas

  • Window: is to display the interface
  • Viewport: A rectangular area of a window used to display graphics. It can be as large as, larger than or smaller than the window. Only the graph drawn in the viewport area can be displayed, and if part of the graph is outside the viewport area, that part is not visible. Set via the glViewport() function.
  • Clipping area (parallel projection): refers to the left,right and bottom,top coordinates of the rectangular area of the viewport, instead of the minimum, maximum X and y coordinates of the window. Set with the glOrtho() function, which also specifies the nearest and farthest Z-coordinates, forming a stereoscopic clipping region.

The relevant API

  • Enable clipping Tests: Like most features of OpenGL, we can enable clipping tests by enabling GL_SCISSOR_TEST: glEnable (GL_SCISSOR_TEST).
  • Turn off clipping tests: glDisable (GL_SCISSOR_TEST)
  •  Specify clipping window:GlScissor (Glint x,Glint Y,GLSize width,GLSize height);X,y specify the lower left corner of the clipping box, weight and height specify the clipping size

hybrid

In OpenGL, Blending is usually a technique to achieve Transparency of objects. Transparency means that an object (or part of it) is not Solid Color, but its Color is a different intensity combination of the Color of the object itself and the Color of other objects behind it. A colored glass window is a transparent object. The glass has its own color, but its final color also contains the color of all objects behind the glass. That’s where the name “Blend” comes from. We Blend (different objects) multiple colors into one color. So transparency allows us to see through objects.

Color combination

To render an image with multiple levels of transparency, we need to enable Blending; GlEnable (GL_BLEND). With blending enabled, we need to tell OpenGL how to blend it. When blending is enabled, the combination of source and destination colors is controlled by blending equation Cf = (Cs * S) + (Cd * D). Where Cf represents the final calculated color; Cs represents the source color; Cd represents the target color. S indicates that the source blending factor specifies the effect of the alpha value on the source color; D indicates that the target blending factor specifies the effect of the alpha value on the target color.

After the slice shader has run and all the tests have passed, the Blend Equation is applied to the slice color output and the value in the current color buffer (the color of the previous fragment stored before the current fragment). The source and target colors will be set automatically by OpenGL, but the values of the source and target factors can be determined by us. Let’s start with a simple example:

                        

Suppose you have two squares, and now you want to draw the translucent green square on top of the red square. The red square will be the target color (so it should be in the color buffer first) and we will draw the green square on top of the red square. So here’s the question. How to set the factor value? If you at least want to multiply the green square by its alpha value, you will set the source blending factor S to the alpha value of the source color vector, which is 0.6. It should then be clear that the contribution of the target square should be the remaining alpha value. If the green square contributed 60% to the final color, then the red square should contribute 40% to the final color, which is 1.0-0.6. Cf = [0 1 0 0.6] * 0.6 + [1 0 0 1] * 0.4. The result is a piece of overlapping squares containing a dirty color that is 60% green and 40% red.

                                             

The final color will be stored in the color buffer, replacing the previous color. So how do you set the mixing factor in OpenGL.

Set the mixing factor

To set the mixing factor, use the glBlendFun function.

glBlendFunc(GLenum sfactor, GLenum dfactor) 

Two parameters represent the source factor and the target factor respectively. OpenGL defines a number of options for us, most of which are listed below:    

In the table, R, G, B and A represent red, green, blue and alpha respectively. The subscripts S and D represent the source and target respectively. C stands for constant color (black by default). Note that the C constant color can be set separately using the glBlendColor function.

For example, in the example above we use alpha of the source color vector as the source factor and 1-alpha as the target factor. That corresponds to glBlendFun.

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);Copy the code

You can also use glBlendFuncSeparate to set different options for RGB and alpha channels. Such as:

glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);Copy the code

This function sets the RGB component as we did earlier, but only so that the final alpha component is affected by the alpha value of the source color vector.

OpenGL gives us even more flexibility, allowing us to change the operators in the source and target parts of the equation. The current source and target are added, but we can subtract them if we want. GlBlendEquation (GLenum mode) allows us to set the operator, which provides three options:

  • GL_FUNC_ADD: Default option to add two components
  • GL_FUNC_SUBTRACT: Subtract two components
  • GL_FUNC_REVERSE_SUBTRACT: Subtract two components in reverse order

In general we can omit the glBlendEquation call because GL_FUNC_ADD is the hybrid equation we want for most operations, but if you really want to break the mainstream, other equations may work for you.

Related articles:
  1. OpenGL first lesson — Name explanation
  2. OpenGL is introduced in lesson 2 – The common fixed storage shader

  3. OpenGL introduction third lesson — matrix transformation and coordinate system

  4. OpenGL entry lesson 4 — Depth

  5. OpenGL entry lesson 5 — front and back elimination

  6. OpenGL entry lesson 6 — Clipping and Blending

  7. OpenGL entry lesson 7 — Texture

  8. OpenGL is in class 8 — a supplementary case

  9. OpenGL ES que

  10. The application of GLKit

  11. GLSL met

  12. How to use custom shaders written in GLSL in iOS

  13. Precision qualifier in GLSL

  14. Use OpenGL to write a rotating stereo album for your girlfriend