In fact, color mixing is used in many scenarios, such as the synthesis of multiple images, and some image effects in animation games can be achieved through color mixing. The most common mixing method is to achieve the translucent effect between the object and the background. In addition, color mixing during the creation of 2D games can be used to move by creating the mask of the target object. By using masks, objects in the old position do not have to redraw the content of the current whole frame, but only those objects with changes.
For iPhone development considerations. TheCodeOpengl2.1 and OpenGL es1.1 are compatible interfaces.
First, we will introduce the implementation of color mixing between the source object and the target object using OpenGL.
Here, the source object refers to the object you want to draw, and the target object refers to the color already in the frame cache. For example, call glclear (gl_color_buffer_bit); the color that is left in the frame cache.
During calculation, both the source and target are calculated when the source object is drawn. cached pixels outside the source object are not affected.
To facilitate color mixing, we often adopt the rgba color mode. Here, RGB represents the color component, while a represents the blend factor ). A, which is often expressed as alpha in graphics and image processing. It is often used as a transparency factor in image processing.
After we specify the mixing factor of the source and target, OpenGL calculates the final color of the drawn object as follows:
Set: the color of a vertex of the source object to (RS, GS, BS,)
The color of the target object's vertex is (Rd, Gd, BD, AD)
Source mixing factor: (Sr, SG, Sb, SA)
The hybrid factor is: (DR, DG, DB, DA)
The final target color of the vertex is:
(RS * sr <OP> RD * Dr, GS * SG <OP> GD * DG, BS * Sb <OP> BD * dB, as * Sa <OP> Ad * Da)
Where, <OP> can be addition (+), subtraction (-), inverse subtraction, minimum, maximum, or bitwise logical operation, and its priority is smaller than multiplication (*).
Next, we will introduce the related OpenGL interfaces.
The first step is to enable the Hybrid operation. You can use glable (gl_blend.
Then we use glblendequation () to specify a hybrid operation. The parameters can be gl_func_add, gl_func_subtract, gl_func_reverse_subtract, gl_min, and gl_max.
However, it should be noted that OpenGL es1.1 does not have the glblendequation interface, so it can only perform addition operations.
The glblendfunc () interface is used to specify the source and target mixed factors.
For more information about the parameters, see Table 6-1 on the 140th page of the Ruby Chinese version.