GL_BYTE normals not interpreted properly in Android shader - android

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);
}

Related

Apply alpha mask to video in OpenGL

I want to apply an alpha mask to a video. Each frame of the video has color information on top and alpha information at the bottom. Like this:
In my case, i want the white pixels in the alpha image to be transparent in the main image. I have an other texture that fills in those transparent pixels.
I was able to generate the only color images or only alpha images, but not able to apply the alpha on to the image. I am using GLES20 in android.
Here is my code which is giving a black screen:
private val vidVertexShaderCode =
"""
precision highp float;
attribute vec3 vertexPosition;
attribute vec4 uvs;
varying vec2 varUvs;
varying vec2 varMaskUvs;
uniform mat4 texMatrix;
uniform mat4 mvp;
void main()
{
varUvs = (texMatrix * vec4(uvs.x, uvs.y, 0, 1.0)).xy;
varMaskUvs = (texMatrix * vec4(uvs.x, uvs.y * 0.5, 0.1, 1.0)).xy;
gl_Position = mvp * vec4(vertexPosition, 1.0);
}
"""
private val vidFragmentShaderCode =
"""
#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 varUvs;
varying vec2 varMaskUvs;
uniform samplerExternalOES texSampler;
void main()
{
vec2 m_uv = vec2(varMaskUvs.x, varMaskUvs.y);
vec4 mask = texture2D(texSampler, m_uv);
vec2 c_uv = vec2(varUvs.x, varUvs.y * 0.5);
vec4 color = texture2D(texSampler, c_uv);
gl_FragColor = vec4(color.rgb, color.a * mask.r)
}
"""
What am i doing wrong?
P.S. I am new to opengl.
Compute the v coordinate of the image (uvs.y * 0.5 + 0.5) and the mask (uvs.y * 0.5) in the vertex shader:
precision highp float;
attribute vec3 vertexPosition;
attribute vec4 uvs;
varying vec2 varUvs;
varying vec2 varMaskUvs;
uniform mat4 texMatrix;
uniform mat4 mvp;
void main()
{
varUvs = (texMatrix * vec4(uvs.x, uvs.y * 0.5 + 0.5, 0, 1.0)).xy;
varMaskUvs = (texMatrix * vec4(uvs.x, uvs.y * 0.5, 0.0, 1.0)).xy;
gl_Position = mvp * vec4(vertexPosition, 1.0);
}
You don't need any further transformation in the fragment shader:
#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 varUvs;
varying vec2 varMaskUvs;
uniform samplerExternalOES texSampler;
void main()
{
vec4 mask = texture2D(texSampler, varMaskUvs);
vec4 color = texture2D(texSampler, varUvs);
gl_FragColor = vec4(color.rgb, color.a * mask.r)
}

openGl glGetUniformLocation returns -1

**it's always returns -1 , here is the code
simple_fragment_shader
precision mediump float;
varying vec4 v_Color;
void main()
{
gl_FragColor = v_Color;
}
simple_vertex_shader
attribute vec4 a_Position;
attribute vec4 a_Color;
varying vec4 v_Color;
void main()
{
v_Color = a_Color;
gl_Position = a_Position;
gl_PointSize = 10.0;
}
why aColorLocation = glGetUniformLocation(program, A_COLOR) retuns -1
where private val A_COLOR = "a_Color"
and program = 3
a_Color is an attribute. Either make it a uniform or use glGetAttribLocation.

type mismatch in arithmetic operation between vec4 and vec3 OpenGL ES

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.

Game runs fine on newer devices but not on older

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);
}
}
}
}

OpenGLES 2.0 Phong shader strange result, makes my object transparent when texture is enabled!

I've been stuck for several days now, trying to make my shader working properly.
The problem is that when I'm not attaching a texture on my object, I multiply the ambient by the light color and I get a dark object when no light, and illuminated properly when a light source is activated.
The problem is that when I attach a texture and multiply it by ambient and light color I get a transparent object that shows up only when a light source is activated, and you can even see through the object while it is illuminated!
I've been trying several codes snippets from the internet but I always get the same result. What I'm doing wrong here? I'm desperate...
The application is developed on Android.
Here is my Vertex Shader:
uniform mat4 uMVPMatrix;
uniform mat4 normalMatrix;
// eye pos
uniform vec3 eyePos;
// position and normal of the vertices
attribute vec4 aPosition;
attribute vec3 aNormal;
// texture variables
uniform float hasTexture;
varying float tex;
attribute vec2 textureCoord;
varying vec2 tCoord;
// lighting
uniform vec4 lightPos;
uniform vec4 lightColor;
// material
uniform vec4 matAmbient;
uniform vec4 matDiffuse;
uniform vec4 matSpecular;
uniform float matShininess;
// normals to pass on
varying vec3 vNormal;
varying vec3 EyespaceNormal;
varying vec3 lightDir, eyeVec;
void main() {
// pass on texture variables
tex = hasTexture;
tCoord = textureCoord;
// normal
EyespaceNormal = vec3(normalMatrix * vec4(aNormal, 1.0));
// the vertex position
vec4 position = uMVPMatrix * aPosition;
// light dir
lightDir = lightPos.xyz - position.xyz;
eyeVec = -position.xyz;
gl_Position = uMVPMatrix * aPosition;
}
And here is my Fragment shader:
precision mediump float;
// texture variables
uniform sampler2D texture1; // color texture
varying float tex;
varying vec2 tCoord;
varying vec3 vNormal;
varying vec3 EyespaceNormal;
// light
uniform vec4 lightPos;
uniform vec4 lightColor;
// material
uniform vec4 matAmbient;
uniform vec4 matDiffuse;
uniform vec4 matSpecular;
uniform float matShininess;
// eye pos
uniform vec3 eyePos;
// from vertex s
varying vec3 lightDir, eyeVec;
void main() {
vec4 b = lightColor;
vec4 c = matAmbient;
vec4 d = matDiffuse;
vec4 e = matSpecular;
vec3 g = eyePos;
float f = matShininess;
vec3 N = normalize(EyespaceNormal);
vec3 E = normalize(eyeVec);
vec3 L = normalize(lightDir);
// Reflect the vector. Use this or reflect(incidentV, N);
vec3 reflectV = reflect(-L, N);
// Get lighting terms
vec4 ambientTerm;
if (tex >= 1.0) {
ambientTerm = texture2D(texture1, tCoord);
}
else
ambientTerm = matAmbient * lightColor;
vec4 diffuseTerm = matDiffuse * max(dot(N, L), 0.0);
vec4 specularTerm = matSpecular * pow(max(dot(reflectV, E), 0.0), matShininess);
gl_FragColor = ambientTerm * diffuseTerm + specularTerm;
}
Thanks in advance.
OK I found it, thanks to JPD002, I was revising the shader again, and I found out that it has to be
vec4 diffuseTerm = matDiffuse * max(dot(N, L), 0.0);
vec4 specularTerm = matSpecular * pow(max(dot(reflectV, E), 1.0), matShininess);
Thanks JDP002, it is always good to have 4 eyes on code rather than 2 =D

Categories

Resources