Problems With Lighting

Hi,

I’m new to OpenGL development. I have other programming experience as well as 3d experience.

I’m having trouble getting my shader to work properly and hoping I can get some help.

I made a class that reads OBJ format and renders them on Android. The objects are made in 3DS Max and exported as a .OBJ file to be imported by my class.

These are my results (3DS Max on the left vs final OpenGL on Android on the right):

copy and paste -> i.imgur.com/YA2vAsN.jpg
copy and paste -> i.imgur.com/AgQvPQS.jpg

I’m unable to upload the files (probably because I’m new here), but I can provide any information necessary to help solve this.

The code basically goes through each line.
“v” is a vertex
“vn” is a vertex normal
“f” is face. The face format is 1/2/3 4/5/6 7/8/9, where 1, 4, 7 refer to the three vertices of the face, 2,5,8 are texture info (not used in my case), 3,6,9 refer to which normals to use. The values for the face are NOT X,Y,Z, they are indices (starting at 1, not 0) of the values specified previously in the file.

uColor is { 0.6f, 0.7f, 0.2f }
uLightPos is { 7, 0, 0 }

Vertex Shader:

uniform mat4 uMVPMatrix;
uniform mat4 uMVMatrix;

uniform vec3 uColor;

attribute vec4 aPosition;
attribute vec3 aNormal;

varying vec3 vColor;
varying vec3 vPosition;
varying vec3 vNormal;

void main() {
	vColor = uColor;
	vPosition = vec3( uMVMatrix * aPosition );
	vNormal = vec3( uMVMatrix * vec4( aNormal, 0.0 ) ); 
	gl_Position = uMVPMatrix * aPosition;
}

Fragment Shader:

precision mediump float;

uniform vec3 uLightPos;

varying vec3 vColor;
varying vec3 vPosition;
varying vec3 vNormal;

void main() {
	float distance = length( uLightPos - vPosition );
	vec3 lightVector = normalize( uLightPos - vPosition );
	float diffuse = max( dot( vNormal, lightVector ), 0.1 );
	diffuse = diffuse * (1000.0 / (1.0 + (0.25 * distance * distance)));
	gl_FragColor = vec4( vColor, 1 ) * diffuse;
}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.