How to fix weird object stretching while rotating in OpenGL in x or y axis?

GoldSpark :

When I am using rotate matrix to rotate my square in openGL and when I want to rotate it around X or Y axis the whole object dissapears and stretches.Z axis is working fine. What could the problem be?

I have tried loading uniformMatrix4f before DrawArrays and it is not working.Still the same problem. Translation is working fine and scaling.

Render method

public void render(Player model, Cube cube_model, StaticShader shader) {

        GL30.glBindVertexArray(model.vaoID);
        GL20.glEnableVertexAttribArray(0);
        GL20.glEnableVertexAttribArray(1);

        shader.loadModelMatrix(Maths.createModelMatrix(new Vector3f(move_X,-Display.getHeight(),0f), 0f,0f,tilt_Y,1f));

        GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);

        GL20.glDisableVertexAttribArray(1);
        GL20.glDisableVertexAttribArray(0);

        GL30.glBindVertexArray(0);

        GL30.glBindVertexArray(cube_model.vaoID);
        GL20.glEnableVertexAttribArray(0);
        GL20.glEnableVertexAttribArray(1);
        shader.loadModelMatrix(Maths.createModelMatrix(new Vector3f(0f,Display.getHeight() - 200f,0f), 0f,increaseRotation,0f,0.5f));

        GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 6);

        increaseRotation += 1f;
        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL30.glBindVertexArray(0);
    }

Matrix math:

public static Matrix4f createModelMatrix(Vector3f t, float rx, float ry, float rz, float s) {
        Matrix4f modelMatrix = new Matrix4f();
        modelMatrix.setIdentity();

        Matrix4f.translate(t, modelMatrix, modelMatrix);
        Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1,0,0), modelMatrix, modelMatrix);
        Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0,1,0), modelMatrix, modelMatrix);
        Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0,0,1), modelMatrix, modelMatrix);
        Matrix4f.scale(new Vector3f(s,s,s), modelMatrix, modelMatrix);

        return modelMatrix;
    }

Projection matrix setup:

public StaticShader(String vertexShaderFileName, String fragmentShaderFileName) {
        super(vertexShaderFileName, fragmentShaderFileName);

        enableProgram();
        loadProjectionMatrix(Maths.createOrthoMatrix(-Display.getWidth(), Display.getWidth(), -Display.getHeight(), Display.getHeight(), 0.1f, 1000));
        disableProgram();
    }

Cube vertices:

private static final float vertices[] = {
        -Display.getWidth() / 5, 0.0f, 0.0f,                        1.0f,1.0f,1.0f,1.0f,
        Display.getWidth() / 5, 0.0f, 0.0f,                         0.0f,1.0f,1.0f,1.0f,
        -Display.getWidth() / 5, Display.getHeight() / 5, 0.0f,     1.0f,1.0f,1.0f,1.0f,

        Display.getWidth() / 5, 0.0f, 0.0f,                         1.0f,1.0f,1.0f,1.0f,
        Display.getWidth() / 5, Display.getHeight() /5, 0.0f,       0.0f,1.0f,1.0f,1.0f,
        -Display.getWidth() / 5, Display.getHeight() / 5, 0.0f,     1.0f,1.0f,1.0f,1.0f
};

I am expecting the cube to rotate in place in Y axis normally but it is actually rotating and dissapears then a line of it appears stretched in middle and dissapears.

Rabbid76 :

I want to rotate it around X or Y axis the whole object disapears and stretches.Z axis is working fine.

Of course.

Your object is a 2 dimensional object. It is just a plane with in the 2 dimensions x and y. The x and y axis are parallel to the viewport. The z axis points out of the viewport.
If you do a rotation around the z axis (in the xy plane), the object keeps its size and rotates.
But if you do a rotation around the x axis, then object seems to be stretched along y and disappears at an angle of 90° respectively 270° degrees. if you do a rotation around the y axis, then object seems to be stretched along x.

The rotation around the x and y axis looks "flat", because of the Orthographic projection. Actually you've no projection matrix, so the scene is projected in parallel (orthographic).
If you want to "see" a 3 dimensional rotation, then you've to use a Perspective projection

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140973&siteId=1