In programming languages, no matter what type of data you have, there are a variety of limitations that cannot be expressed in real-world situations. For example, int, char will overflow, float will overflow and accuracy is inaccurate.
So we need to pay special attention to these things in development.
In the near future Cocos2dx (2.1.4) engine with shader to do some effects. There are no problems with Windows, but porting to Android can be problematic.
record them. One is to deepen the impression. Second, in order to provide a similar problem to the Bo friends to participate in the test.
Mainly focused on two aspects:
(1) Shader does not support the operation of different types of numbers
Like what
int A;
float B;
Float C = a + B;
This problem is OK, because in Eclipse COCOS2DX will print the compile error log
(2) floating-point overflow problem, this problem will be more difficult to solve, I also slowly try out
First look at some of the minimum range of shader accuracy
Here are some of my shader source code:
<span style= "FONT-SIZE:18PX;" >//varying vec4 v_fragmentcolor;varying vec2 v_texcoord;uniformfloatu_radius;uniform vec2 u_ Touchpos;uniform vec2 u_bgsize;uniform sampler2d cc_texture0;float isincircle () { vec2 pos = u_bgsize * v_ Texcoord; float dis = distance (pos,u_touchpos); if (dis >= U_radius | | u_radius = = 0.0) return 1.0; Elsereturn 0.0;} void Main () { vec4 texcolor = texture2d (cc_texture0, V_texcoord); float isIn = Isincircle (); Gl_fragcolor = Texcolor * ISIN;} </span>
Error concentration in float dis = distance (pos,u_touchpos);
Distance is the distance between two points in the screen. I expect it to be in the form of roughly this
Float distance (vec2 pos1, vec2 pos2) {
VEC3 sub = Pos1-pos2;
return sqrt (sub.x * sub.x + sub.y * sub.y);
}
Because COCOS2DX defaults to the vertex shader using high-precision float, the fragment shader uses a medium-precision float, which is the code of the fragment shader.
So the float range is between 16384 and 16384, and when two hundred numbers multiply it is very likely to cause overflow.
Here's the code for the last change:
<span style= "FONT-SIZE:18PX;" >//varying vec4 v_fragmentcolor;varying vec2 v_texcoord;uniform floatu_radius;uniform highp vec2 u_ Touchpos;uniform vec2u_bgsize;uniform sampler2d cc_texture0;float isincircle () { highp vec2 pos = u_bgSize * v_ Texcoord; float dis = distance (pos,u_touchpos); if (dis >= u_radius) return 1.0; Elsereturn 0.0;} void Main () { vec4 texcolor = texture2d (cc_texture0, V_texcoord); float isIn = Isincircle (); Gl_fragcolor = Texcolor * ISIN;} </span>
COCOS2DX writing shader encountering overflow problem