0

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

Meeresgott
  • 431
  • 3
  • 17
  • I'm not sure the sphere is distorted incorrectly. I think it might just be that you have setup a projection matrix with a very wide field-of-view. frustumM makes setting the field-of-view a bit implicit. You could try reducing the values of left/right/bottom/top or increase the value of near. I suggest you start by doubling 'near' and see if that gets you to a less distorted projection. – Columbo Mar 01 '19 at 17:19
  • Great it works! I have increased near from 1 to 2 and divided left/right/bottom/top by 2. But now I have a different problem. The Grid under the sphere consist of quads with the dimension 1x1 and eyery move takes the sphere quad further like chess. If I go 10 Steps the sphere will be on the grid line. Is the even because the wrong setup of the projection matrix ? – Meeresgott Mar 01 '19 at 17:34

0 Answers0