I write an application in Android and use OpenGL ES.
In my test scene I have a cube and a sphere. The sphere is able to move. If the Spehre is in the center from the display the sphere renders fine See here. But if I move the sphere to a edge from the screen the spehre is distorted See here. I don't know where it come from.
Here is my ProjectionMatrix:
public void onSurfaceChanged(GL10 unused, int width, int height) {
// ...
float ratio = (float) width / height;
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 1, 100);
// ...
}
My calculation vor the ViewMatrix and the TransformationMatrix:
public static Matrix4f createTransformationMatrix(Vector3f translation,
float rx, float ry, float rz, float scale){
Matrix4f matrix4f = new Matrix4f();
matrix4f.setIdentity();
Matrix4f.translate(translation,matrix4f,matrix4f);
Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1,0,0), matrix4f,matrix4f);
Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0,1,0), matrix4f,matrix4f);
Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0,0,1), matrix4f,matrix4f);
Matrix4f.scale(new Vector3f(scale,scale,scale),matrix4f,matrix4f);
return matrix4f;
}
public static Matrix4f createViewMatrix(Camera camera) {
Matrix4f viewMatrix = new Matrix4f();
viewMatrix.setIdentity();
Matrix4f.rotate((float) Math.toRadians(camera.getPitch()), new Vector3f(1, 0, 0), viewMatrix,viewMatrix);
Matrix4f.rotate((float) Math.toRadians(camera.getYaw()), new Vector3f(0, 1, 0), viewMatrix, viewMatrix);
Matrix4f.rotate((float) Math.toRadians(camera.getRoll()), new Vector3f(0, 0, 1), viewMatrix, viewMatrix);
Vector3f cameraPos = camera.getPosition();
Vector3f negativeCameraPos = new Vector3f(-cameraPos.x,-cameraPos.y,-cameraPos.z);
Matrix4f.translate(negativeCameraPos, viewMatrix, viewMatrix);
return viewMatrix;
}
And the VertexShaderCode:
#version 300 es
//Vertex Shader
in vec3 position;
in vec2 textureCoords;
in vec3 normal;
out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toCameraVector;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;
void main(void){
vec4 worldPosition = transformationMatrix *
vec4(position.x,position.y,position.z,1.0);
gl_Position = projectionMatrix * viewMatrix * worldPosition;
pass_textureCoords = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal,0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
toCameraVector = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz -
worldPosition.xyz;
}
does everyone know where my problem came from and how to solve it ?
please let me know if you have enough information. Here a two moore pictures about the problem. pic3 and pic4
EDIT
Matrix.frustumM(mProjectionMatrix, 0, -ratio/2f, ratio/2f, -1f/2f, 1f/2f, 2, 100);
But know I have this problem: Sphere out of Grid