简体   繁体   中英

How do I rotate object around its center in Z axis in OpenGL?

When I use rotation matrix to rotate my square around Z axis it is actuall amaking small circles around it and not actually rotating in place. I want it to rotate like a wheel.

I have tried putting rotation first and then translation and then vice versa but it is not working. I've read some questions people have asked online but its all in old OpenGL.

My matrix code:

public static Matrix4f createTranslateMatrix(Vector3f t) {
        Matrix4f tM = new Matrix4f();

        Matrix4f.translate(t, tM, tM);

        return tM;

    }

    public static Matrix4f createRotateMatrixZ(float z) {
        Matrix4f zM = new Matrix4f();

        Matrix4f.rotate((float) Math.toRadians(z), new Vector3f(0,0,1), zM, zM);

        return zM;
    }

Render Code:

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



                        Matrix4f translateM = Maths.createTranslateMatrix(new Vector3f(0.0f, Display.getWidth() - 200f, 0.0f));
                    Matrix4f rotateM = Maths.createRotateMatrixZ(increaseRotation);
                    Matrix4f translateM2 = Maths.createTranslateMatrix(new Vector3f(0.0f, 0.0f, 0.0f));


                    Matrix4f modelM = new Matrix4f();
                    modelM.setIdentity();

                    Matrix4f.mul(translateM2, rotateM, modelM);
                    Matrix4f.mul(modelM, translateM, modelM);


                    shader.loadModelMatrix(modelM);



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

                        increaseRotation += 1f;
                        if(increaseRotation == 360.0f) {
                            increaseRotation = 0;
                        }


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

Quad:

    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
    };

Ortho Matrix:

public static Matrix4f createOrthoMatrix(final float l, final float r, final float b, final float t, final float n, final float f ) {
         Matrix4f orthoM = new Matrix4f();

         orthoM.m00 = 2 / (r - l);
         orthoM.m03 = -(r+l)/(r-l);
         orthoM.m11 = 2/(t-b);
         orthoM.m13 = -(t+b)/(t-b);
         orthoM.m22 = -2/(f-n);
         orthoM.m23 = -(f+n)/(f-n);
         orthoM.m33 = 1;


        return orthoM;


    }

Ortho Matrix init:

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

I want the object to rotate IN place not rotate around some of its edges.

Rotations always happen around the origin of the coordinate system. So you first have to translate your geometry to the origin, then rotate and then translate back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM