Skip to content

Commit 1524672

Browse files
author
Thayer J Andrews
committed
CCEffectHue - Address Scott's review comments regarding matrix ordering
Create transposed versions of the matrices and reverse the order of all operations using them. The effect is the same but we decided that this would be easier for others to understand. Change the comments around a bit to explain what's going on.
1 parent 3bd53d2 commit 1524672

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

cocos2d/CCEffectHue.m

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ -(void)buildFragmentFunctions
108108
// The non-color matrix shader is based on the hue filter in GPUImage - https://github.com/BradLarson/GPUImage
109109
#if CCEFFECTHUE_USES_COLOR_MATRIX
110110
NSString* effectBody = CC_GLSL(
111-
// Since GL matrices are in column major order, u_hueRotationMtx is transposed relative to
112-
// the implementations referenced below. Do color * mtx (as opposed to mtx * color) to
113-
// account for this.
114-
return inputValue * u_hueRotationMtx;
111+
return u_hueRotationMtx * inputValue;
115112
);
116113
#else
117114
NSString* effectBody = CC_GLSL(
@@ -200,22 +197,29 @@ GLKMatrix4 matrixWithHue(float hue)
200197
// And here:
201198
// http://en.wikipedia.org/wiki/YIQ
202199
//
203-
// Note that GL matrices are column major so our matrices are transposed relative to the
204-
// reference implementations when loaded as they are below. I'm leaving them like this to
205-
// improve readability for comparison purposes and I handle the transpose by doing color * mtx
206-
// in the shader instead of mtx * color. The order that the matrices are composed below is
207-
// also reversed to account for the left multiply of the color value.
200+
// Note that GL matrices are column major so we have to transpose them when loading them in
201+
// the order specified here.
208202

209-
GLKMatrix4 rgbToYiq = GLKMatrix4Make(0.299, 0.587, 0.114, 0.0,
210-
0.595716, -0.274453, -0.321263, 0.0,
211-
0.211456, -0.522591, 0.31135, 0.0,
212-
0.0, 0.0, 0.0, 1.0);
213-
GLKMatrix4 yiqToRgb = GLKMatrix4Make(1.0, 0.9563, 0.6210, 0.0,
214-
1.0, -0.2721, -0.6474, 0.0,
215-
1.0, -1.1070, 1.7046, 0.0,
216-
0.0, 0.0, 0.0, 1.0);
217-
GLKMatrix4 rotation = GLKMatrix4MakeRotation(hue, 1.0f, 0.0f, 0.0f);
218-
GLKMatrix4 composed = GLKMatrix4Multiply(rgbToYiq, GLKMatrix4Multiply(rotation, yiqToRgb));
203+
GLKMatrix4 rgbToYiq = GLKMatrix4MakeAndTranspose(0.299, 0.587, 0.114, 0.0,
204+
0.595716, -0.274453, -0.321263, 0.0,
205+
0.211456, -0.522591, 0.31135, 0.0,
206+
0.0, 0.0, 0.0, 1.0);
207+
GLKMatrix4 yiqToRgb = GLKMatrix4MakeAndTranspose(1.0, 0.9563, 0.6210, 0.0,
208+
1.0, -0.2721, -0.6474, 0.0,
209+
1.0, -1.1070, 1.7046, 0.0,
210+
0.0, 0.0, 0.0, 1.0);
211+
212+
// Positive rotation in YIQ is the opposite of positive rotation in HSV so negate the
213+
// rotation value. See this:
214+
// http://upload.wikimedia.org/wikipedia/commons/8/82/YIQ_IQ_plane.svg
215+
// And this:
216+
// http://upload.wikimedia.org/wikipedia/commons/5/52/HSL-HSV_hue_and_chroma.svg
217+
// To visualize the difference between the two color spaces.
218+
//
219+
GLKMatrix4 rotation = GLKMatrix4MakeRotation(-hue, 1.0f, 0.0f, 0.0f);
220+
221+
// Put everything together into one color matrix.
222+
GLKMatrix4 composed = GLKMatrix4Multiply(yiqToRgb, GLKMatrix4Multiply(rotation, rgbToYiq));
219223
return composed;
220224
}
221225
#endif

0 commit comments

Comments
 (0)