• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps OpenGLES 2.0 GLSL function problem

Hello, as you may or may not know, I've been working on an android gaming engine, and I've finally started implementing lighting (which I'm going ahead and doing, even though I primarily make 2-d games), but my game keeps crashing. After experimenting a little, I found out that the problem lies somewhere in these vec4 functions:

Code:
vec4 calcLight(BaseLight base, vec3 direction, vec3 normal){
	float diffuseFactor = dot(-direction, normal);
	
	vec4 diffuseColor = vec4(0, 0, 0, 0);
	
	if(diffuseFactor > 0){
		diffuseColor = vec4(base.color, 1.0) * base.intensity * diffuseFactor;
	}
	
	return diffuseFactor;
}

vec4 calcDirectionalLight(DirectionalLight directionalLight, vec3 normal){
	vec4 result = calcLight(directionalLight.base, directionalLight.direction, normal);
	return result;
}

Any help as to what I did wrong? I'm still REALLY new to glsl.
 
Hello, as you may or may not know, I've been working on an android gaming engine, and I've finally started implementing lighting (which I'm going ahead and doing, even though I primarily make 2-d games), but my game keeps crashing. After experimenting a little, I found out that the problem lies somewhere in these vec4 functions:

Code:
vec4 calcLight(BaseLight base, vec3 direction, vec3 normal){
	float diffuseFactor = dot(-direction, normal);
	
	vec4 diffuseColor = vec4(0, 0, 0, 0);
	
	if(diffuseFactor > 0){
		diffuseColor = vec4(base.color, 1.0) * base.intensity * diffuseFactor;
	}
	
	return diffuseFactor;
}

vec4 calcDirectionalLight(DirectionalLight directionalLight, vec3 normal){
	vec4 result = calcLight(directionalLight.base, directionalLight.direction, normal);
	return result;
}

Any help as to what I did wrong? I'm still REALLY new to glsl.

Posting the output of GLES20.glGetShaderInfoLog and GLES20.glGetProgramInfoLog after compiling / linking your shaders / program would have been helpful.
Anyway, I would recommend using
Code:
vec4 diffuseColor = vec4(base.color, 1.0) * base.intensity * clamp( diffuseFactor , 0.0 , 1.0);
instead of
Code:
vec4 diffuseColor = vec4(0, 0, 0, 0);
	
if(diffuseFactor > 0){
	diffuseColor = vec4(base.color, 1.0) * base.intensity * diffuseFactor;
}

edit: and as mentioned below by toki78 it makes no sense to return the diffuseFactor. Returning diffuseColor might have been what you actually wanted to do. On some plattforms your glsl shader won't compile if you try to return a float in place of vec4.
 
Back
Top Bottom