Reputation: 5657
I trying to compile program (I have previously ported it from Cg
language). Fragment shader is
precision mediump float;
precision mediump int;
uniform float time;
uniform float aspect;
uniform sampler2D sampler_main;
varying vec4 v_texCoord;
void main()
{
vec3 ret;
vec2 uv = v_texCoord.xy;
float rad=sqrt((uv.x-0.5)*(uv.x-0.5)*4.0+(uv.y-0.5)*(uv.y-0.5)*4.0)*.7071067;
float ang=atan(((uv.y-0.5)*2.0),((uv.x-0.5)*2.0));
vec2 uv1 = (uv-0.5)*aspect.xy;
float rad1 = .1/(length(uv1) + .1)) ;
vec2 uv2 = vec2 (ang/3.14, rad1);
uv2.y = uv2.y +0.1*time;
uv2.x = uv2.x +.0*time;
vec2 uv3 = vec2 (ang/3.14, rad1*1.5);
uv3.y = uv3.y + 0.08*time ;
uv3.x = uv3.x + time/32;
vec3 crisp = 2*texture2D(sampler_main, uv2).xyz;
vec3 lay1 = vec3 (0,0,1)*uv.y*pow(1-rad,8);
crisp = 3*crisp * pow(rad,1);
float mask = saturate(1-4*rad);
ret = crisp + lay1*mask + mask * texture2D(sampler_main, uv).xyz;
gl_FragColor.xyz = ret;
gl_FragColor.w = 1.0;
}
I got error on line
uv3.x = uv3.x + time/32;
When I change it to
uv3.x = uv3.x + time/32.0;
Problem is solved, but I don't understand the root of the problem.
The same problem for the line
float mask = saturate(1-4*rad); => float mask = saturate(1.0-4.0*rad);
vec3 crisp = 2*texture2D(sampler_main, uv2).xyz; => vec3 crisp = 2.0*texture2D(sampler_main, uv2).xyz;
vec3 lay1 = vec3 (0,0,1)*uv.y*pow(1-rad,8); => vec3 lay1 = vec3 (0,0,1)*uv.y*pow(1.0-rad,8.0);
crisp = 3*crisp * pow(rad,1); => crisp = 3.0*crisp * pow(rad,1.0);
Could someone explain:
Upvotes: 5
Views: 7204
Reputation: 54572
Since your question has the opengl-es-2.0 tag, the relevant spec is the corresponding OpenGL ES Shading Language spec.
It says in section "5.8 Assignments" (page 46):
The lvalue-expression and rvalue-expression must have the same type. All desired type-conversions must be specified explicitly via a constructor.
and in section "5.9 Expressions" (page 48):
The arithmetic binary operators add (+), subtract (-), multiply (*), and divide (/) operate on integer and floating-point typed expressions (including vectors and matrices). The two operands must be the same type, or one can be a scalar float and the other a float vector or matrix, or one can be a scalar integer and the other an integer vector.
All you need to do is use float constants in float expressions. In your first example, use 32.0
instead of 32
. Subtle detail, if you're used to writing 32.0f
from C/C++: The f
postfix is not supported in the GLSL version that goes with ES 2.0. So writing 32.0f
is an error. It's allowed in ES 3.0.
While I'm sure that some people will violently disagree with my point of view: I think not supporting these automatic type conversions is a good feature. I believe it's useful to always be aware of what type you're operating on, and use the correct types, without relying on automatic conversions. Type safety is valuable, and the loose typing in languages like C and C++ is a frequent source of errors.
Upvotes: 3
Reputation: 2507
Implicit casts are not allowed in early GLSL. So try an explicit cast:
uv3.x = uv3.x + time/float(32);
The GLSL 1.1 Spec says in Chapter 4 (page 16):
The OpenGL Shading Language is type safe. There are no implicit conversions between types
Recent GLSL allows implicit type casts. The GLSL 4.4 Spec says in Chapter 4 (page 25):
The OpenGL Shading Language is type safe. There are some implicit conversions between types. Exactly how and when this can occur is described in section 4.1.10 “Implicit Conversions” and as referenced by other sections in this specification.
And later on starting at page 39 there is a list of possible implicit conversions.
Upvotes: 10