I have a monochrome.vs which is
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_textCoord0;
varying vec4 v_color;
varying vec2 v_textCoords;
uniform mat4 u_projTrans;
void main(){
v_color = a_color;
v_textCoords = a_textCoord0;
gl_Position = u_projTrans * a_position;
}
I have a fragment shader monochrome.fs
#ifdef GL_ES
precision mediump float
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform float u_amount;
void main(){
vec4 color = v_color* texture(u_texture,v_texCoords);
float greyScale = dot(color.rgb,vec3(0.222,0.707,0.071));
color.rgb = mix(color.rgb,vec3(greyScale));
gl_FragColor =color;
}
I am running this through libGDX on an android device.
I am using the code
if(!shaderProgram.isCompiled()){
String msg = "shader not compiled ->"+shaderProgram.getLog();
throw new GdxRuntimeException(msg);
}
which is producing the following output
10-23 11:21:49.546 4979-5018/? E/AndroidRuntime: com.badlogic.gdx.utils.GdxRuntimeException: shader not compiled ->Fragment shader compilation failed.
10-23 11:21:49.546 4979-5018/? E/AndroidRuntime: ERROR: 0:5: 'varying' : Syntax error: syntax error
10-23 11:21:49.546 4979-5018/? E/AndroidRuntime: ERROR: 1 compilation errors. No code generated.
I really cant see what I am doing wrong, nor can I find an answers on SO, any help?
The problem was the semicolon, so I changed
#ifdef GL_ES
precision mediump float
#endif
to
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform float u_amount;
void main(){
vec4 color = v_color* texture2D(u_texture,v_texCoords);
float greyScale = dot(color.rgb,vec3(0.222,0.707,0.071));
color.rgb = mix(color.rgb,vec3(greyScale),u_amount);
gl_FragColor =color;
}
and it now works
Related
in opengl es program i input two textures and use fragment shader:
varying highp vec2 textureCoordinate;
varying vec2 textureCoordinate2;
uniform sampler2D inputImageTexture;
uniform sampler2D inputImageTexture2;
uniform lowp float intensity;
void main() {
highp vec4 newColor = texture2D(inputImageTexture2,textureCoordinate2);
highp vec4 vColor = texture2D(inputImageTexture, textureCoordinate);
newColor.r = newColor.r + vColor.r * (1.0 - newColor.a);
newColor.g = newColor.g + vColor.g * (1.0 - newColor.a);
newColor.b = newColor.b + vColor.b * (1.0 - newColor.a);
gl_FragColor = vec4(mix(vColor.rgb, newColor.rgb, 0.86), newColor.a);
};
but the result have some "error color" here, how to fix this?
I found the answer myself, this is because I write and read from a same FBO texture, and now I change it and work fine!
I create a shader its run ok on pc but not on my android phone
vertex
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
uniform vec3 u_distort;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * vec4(a_position+u_distort,1.0);
}
fragment
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform mat4 u_projTrans;
void main() {
vec4 color = texture2D(u_texture, v_texCoords) * v_color;
gl_FragColor = color;
}
I have add precision highp float; to the 2 files
enter code here
for what I find out so far is most probably an OpenGL ES incompatibility problem I must find a diferent way to write ne variables vec4 and vec3, vec3 u_distortvariable i'm update the values on the render method.
My games runs fine on newer devices (eg..droid turbo, galaxy note 3) but on my motorola xoom there are problems, such as :
1) My shaderProgram causes the stage that is drawn inside of it to not be rendered
2) Touch inputs don't work every time using InputProcessor (only senses 90% of the time)
These are the problems I currently notice. Could this be because it is an older OS version (4.1.2)? Not that old. Are known bugs with libgdx's ShaderProgram? Even then, the touch input not sensing every click is very strange. Thanks for your time and help! My rendering code for the shaderProgram is all correct, so no need to show that. And for touchInput, I am just using touchDown method from InputProcessor.
EDIT :
Error produced by shaderProgram
"(22) : error C1101: ambiguous overloaded function reference "smoothstep(mediump float, float, lowp float)"
Vertex Shader
#version 100
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_worldView;
uniform vec2 u_lightPos;
uniform vec3 u_lightColor;
uniform vec4 u_spaceColor;
varying vec4 v_color;
varying vec2 v_texCoords;
varying vec2 v_lightPos;
varying vec3 v_lightColor;
varying vec2 v_position;
varying vec4 v_spaceColor;
void main() {
v_color = a_color;
v_lightColor = u_lightColor;
v_lightPos = u_lightPos;
v_position.xy = a_position.xy;
v_texCoords = a_texCoord0;
gl_Position = u_worldView * a_position;
v_spaceColor = u_spaceColor;
}
Fragment Shader
#version 100
#ifdef GL_ES
precision lowp float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
varying vec2 v_lightPos;
varying vec3 v_lightColor;
varying vec2 v_position;
varying vec4 v_spaceColor;
uniform sampler2D u_texture;
void main() {
for(int row = 0; row < 2; row++) {
for(int col = 0; col < 2; col++) {
float dist = distance(v_position, vec2(-1 + col, 1 - row));
float delta = 0.1;
float alpha = smoothstep(100.0-delta, 100.0, dist);
if(dist > 23.0){
gl_FragColor = mix(v_spaceColor, v_color, alpha);
}
else{
gl_FragColor = v_color * texture2D(u_texture, v_texCoords);
}
}
}
}
My shader works GREAT on iOS and Win32. The problem is Android. All devices really botch the normals if I send them as GL_BYTE. If I convert them to GL_FLOAT, everything's peachy.
glVertexAttribPointer(program->slots[Shaders::A_NORMAL_SLOT], 3, GL_FLOAT, GL_FALSE, 0, frame->normals);
glVertexAttribPointer(program->slots[Shaders::A_NORMAL_SLOT], 3, GL_BYTE, GL_TRUE, 0, frame->normals);
and YES, 'frame->normals' is of type float* in the first example, and of type char* in the second example. Like I said, this exact same code works great on iOS/Win32.
What's going on here? Is this some kind of Android bug that it won't accept byte normals?
My shader, Vertex:
uniform mat3 normal_matrix;
uniform mat4 mvp_matrix;
uniform vec3 u_lightpos;
uniform vec3 u_eyepos;
uniform vec4 u_color;
attribute vec4 a_vertex;
attribute vec3 a_normal;
attribute vec2 a_texcoord0;
attribute vec4 a_color;
varying vec2 v_texcoord0;
varying vec3 v_viewDir;
varying vec3 v_lightDir;
varying vec3 v_normal;
varying vec4 v_color;
void main( void )
{
vec4 fvObjectPosition = mvp_matrix * (a_vertex);
v_viewDir = u_eyepos;
v_lightDir = u_lightpos;
v_normal = normalize(normal_matrix * a_normal);
v_color = u_color * a_color;
v_texcoord0 = a_texcoord0.xy;
gl_Position = fvObjectPosition;
}
Fragment:
#ifdef GL_ES
precision mediump float;
#endif
uniform vec4 u_specular;
uniform vec4 u_diffuse;
uniform float u_specularpower;
uniform sampler2D t_texture0;
varying vec2 v_texcoord0;
varying vec3 v_viewDir;
varying vec3 v_lightDir;
varying vec3 v_normal;
varying vec4 v_color;
void main( void )
{
float fNDotL = dot( v_normal, v_lightDir );
vec3 fvReflection = normalize( ( ( 2.0 * v_normal ) * fNDotL ) - v_lightDir );
vec4 fvBaseColor = v_color * texture2D( t_texture0, v_texcoord0 );
vec4 fvTotalDiffuse = vec4((u_diffuse * fNDotL).xyz, 1.0) * fvBaseColor;
vec4 fvTotalSpecular = u_specular * ( pow( max( 0.0, dot( fvReflection, -v_viewDir ) ), u_specularpower ) );
gl_FragColor = vec4(( fvBaseColor + fvTotalDiffuse + fvTotalSpecular ).xyz, v_color.w);
}
When I try to compile the simplified vertex shader below on Galaxy S (PowerVR SGX540, Android), it does not compile and no error is given. Just "Compile failed." is in the log.
#ifdef GL_ES
precision mediump float;
precision lowp int;
#endif
uniform mat4 u_m; //model
uniform mat4 u_mvp; //model view projection
uniform vec3 u_lightPos[1];
uniform int u_lightCount;
attribute vec3 a_position;
varying vec3 v_lightDir[1];
void pointLight(int i, vec3 vertPos){
v_lightDir[i] = u_lightPos[i] - vertPos;
}
void main( void )
{
vec3 vertPos = (u_m * vec4(a_position, 1.0)).xyz;
if(u_lightCount > 0){
pointLight(0, vertPos);
}
gl_Position = u_mvp * vec4(a_position, 1.0);
}
However, when I move the function block into the main function, it compiles just fine:
#ifdef GL_ES
precision mediump float;
precision lowp int;
#endif
uniform mat4 u_m; //model
uniform mat4 u_mvp; //model view projection
uniform vec3 u_lightPos[1];
uniform int u_lightCount;
attribute vec3 a_position;
varying vec3 v_lightDir[1];
void main( void )
{
vec3 vertPos = (u_m * vec4(a_position, 1.0)).xyz;
if(u_lightCount > 0){
v_lightDir[0] = u_lightPos[0] - vertPos;
}
gl_Position = u_mvp * vec4(a_position, 1.0);
}
On desktop, both compiles.
I can't figure out, why it behaves that way. Could anyone explain it or is it a bug inside the system?
It seems that your OpenGL ES implementation does not support array indexing by variable (v_lightDir[i] = ...)