【libGDX】立方体手动旋转

1 前言

​ 本文主要介绍使用 libGDX 绘制立方体,并实现手动触摸事件控制立方体旋转。

​ 为方便控制触摸旋转,并提高渲染性能,我们通过改变相机的位置和姿态实现立方体旋转效果。

​ 读者如果对 libGDX 不太熟悉,请回顾以下内容。

2 立方体手动旋转

​ 本节将使用 Mesh、ShaderProgram、Shader、InputAdapter 实现立方体手动旋转,OpenGL ES 的实现见博客 → 立方体手动旋转,本节完整代码资源见 → libGDX立方体手动旋转

​ DesktopLauncher.java

package com.zhyan8.game;

import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;

public class DesktopLauncher {
	public static void main (String[] arg) {
		Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
		config.setForegroundFPS(60);
		config.setTitle("RotateCube");
		new Lwjgl3Application(new RotateCube(), config);
	}
}

​ RotateCube.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;

public class RotateCube extends ApplicationAdapter {
	private ShaderProgram mShaderProgram;
	private Mesh mMesh;
	private CameraController mCameraController;

	@Override
	public void create() {
		initShader();
		initMesh();
		mCameraController = new CameraController();
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT);
		Gdx.gl.glEnable(GL30.GL_DEPTH_TEST);
		mShaderProgram.bind();
		mShaderProgram.setUniformMatrix("u_mvpTrans", mCameraController.combined());
		renderCube();
	}

	@Override
	public void dispose() {
		mShaderProgram.dispose();
		mMesh.dispose();
	}

	private void renderCube() { // 渲染立方体
		for (int i = 0; i < 6; i++) {
			mMesh.render(mShaderProgram, GL30.GL_TRIANGLE_FAN, i * 4, 4);
		}
	}

	private void initShader() { // 初始化着色器程序
		String vertex = Gdx.files.internal("shaders/square_vertex.glsl").readString();
		String fragment = Gdx.files.internal("shaders/square_fragment.glsl").readString();
		mShaderProgram = new ShaderProgram(vertex, fragment);
	}

	private void initMesh() { // 初始化网格
		float[] vertices = Model.vertices;
		short[] indices = Model.indices;
		VertexAttribute vertexPosition = new VertexAttribute(Usage.Position, 3, "a_position");
		VertexAttribute vertexColor = new VertexAttribute(Usage.ColorUnpacked, 4, "a_color");
		mMesh = new Mesh(true, vertices.length / 7, indices.length, vertexPosition, vertexColor);
		mMesh.setVertices(vertices);
		mMesh.setIndices(indices);
	}
}

​ CameraController.java

package com.zhyan8.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;

public class CameraController {
    private PerspectiveCamera mCamera;

    public CameraController() {
        mCamera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        mCamera.near = 0.3f;
        mCamera.far = 1000f;
        mCamera.position.set(0f, 0f, 4f);
        mCamera.lookAt(0, 0, 0);
        mCamera.update();
        Gdx.input.setInputProcessor(mInputAdapter);
    }

    public Matrix4 combined() {
        return mCamera.combined;
    }

    private void updateCamera(float deltaX, float deltaY) {
        Vector3 right = mCamera.up.cpy();
        right.crs(mCamera.direction);
        Vector3 vec1 = new Vector3(right.x * deltaX, right.y * deltaX, right.z * deltaX);
        Vector3 vec2 = new Vector3(mCamera.up.x * deltaY, mCamera.up.y * deltaY, mCamera.up.z * deltaY);
        vec1.add(vec2).crs(mCamera.direction);
        float angle = (float) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
        mCamera.rotateAround(Vector3.Zero, vec1, angle);
        //mCamera.lookAt(Vector3.Zero);
        mCamera.update();
    }

    private InputAdapter mInputAdapter = new InputAdapter() {
        private float mPreX = 0;
        private float mPreY = 0;

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            mPreX = screenX;
            mPreY = screenY;
            return true;
        }

        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            float offsetX = screenX - mPreX;
            float offsetY = screenY - mPreY;
            updateCamera(offsetX * 0.3f, offsetY * 0.3f);
            mPreX = screenX;
            mPreY = screenY;
            return true;
        }
    };
}

​ Model.java

package com.zhyan8.game;

public class Model {
    private static float r = 1.0f;

    public static float[] vertices = new float[] {
            // 前面
            r, r, r, 0.92f, 0.30f, 0.29f, 1.0f, // 0
            -r, r, r, 0.92f, 0.30f, 0.29f, 1.0f, // 1
            -r, -r, r, 0.92f, 0.30f, 0.29f, 1.0f, // 2
            r, -r, r, 0.92f, 0.30f, 0.29f, 1.0f, // 3
            // 后面
            r, r, -r, 0.93f, 0.57f, 0.17f, 1.0f, // 4
            -r, r, -r, 0.93f, 0.57f, 0.17f, 1.0f, // 5
            -r, -r, -r, 0.93f, 0.57f, 0.17f, 1.0f, // 6
            r, -r, -r, 0.93f, 0.57f, 0.17f, 1.0f, // 7
            // 上面
            r, r, r, 1.0f, 1.0f, 1.0f, 1.0f, // 8
            r, r, -r, 1.0f, 1.0f, 1.0f, 1.0f, // 9
            -r, r, -r, 1.0f, 1.0f, 1.0f, 1.0f, // 10
            -r, r, r, 1.0f, 1.0f, 1.0f, 1.0f, // 11
            // 下面
            r, -r, r, 0.99f, 0.95f, 0.0f, 1.0f, // 12
            r, -r, -r, 0.99f, 0.95f, 0.0f, 1.0f, // 13
            -r, -r, -r, 0.99f, 0.95f, 0.0f, 1.0f, // 14
            -r, -r, r, 0.99f, 0.95f, 0.0f, 1.0f, // 15
            // 右面
            r, r, r, 0.11f, 0.61f, 0.98f, 1.0f, // 16
            r, r, -r, 0.11f, 0.61f, 0.98f, 1.0f, // 17
            r, -r, -r, 0.11f, 0.61f, 0.98f, 1.0f, // 18
            r, -r, r, 0.11f, 0.61f, 0.98f, 1.0f, // 19
            // 左面
            -r, r, r, 0.15f, 0.87f, 0.50f, 1.0f, // 20
            -r, r, -r, 0.15f, 0.87f, 0.50f, 1.0f, // 21
            -r, -r, -r, 0.15f, 0.87f, 0.50f, 1.0f, // 22
            -r, -r, r, 0.15f, 0.87f, 0.50f, 1.0f // 23
    };

    public static short[] indices = new short[] {
            0, 1, 2, 3, // 前面
            4, 5, 6, 7, // 上面
            8, 9, 10, 11, // 右面
            12, 13, 14, 15, // 后面
            16, 17, 18, 19, // 下面
            20, 21, 22, 23 // 左面
    };
}

​ square_vertex.glsl

#version 300 es

in vec3 a_position;
in vec4 a_color;

uniform mat4 u_mvpTrans; // MVP矩阵变换

out vec4 v_color;

void main() {
    gl_Position = u_mvpTrans * vec4(a_position, 1.0);
    v_color = a_color;
}

​ square_fragment.glsl

#version 300 es
precision mediump float; // 声明float型变量的精度为mediump

in vec4 v_color;

out vec4 fragColor;

void main() {
    fragColor = v_color;
}

​ 运行效果如下。

​ 声明:本文转自【libGDX】立方体手动旋转

热门相关:萌妻太甜:总裁大人,别傲娇   总裁大人,又又又吻我了   娇妻如云   玉堂金闺   北宋闲王