1 前言
本文主要介绍使用 Mesh 绘制立方体,读者如果对 Mesh 不太熟悉,请回顾以下内容:
- 使用Mesh绘制三角形
- 使用Mesh绘制矩形
- 使用Mesh绘制圆形
在绘制立方体的过程中,主要用到了 MVP (Model View Projection)矩阵变换。
- Model:模型变换,施加在模型上的空间变换,包含平移变换(translateM)、旋转变换(rotateM)、对称变换(transposeM)、缩放变换(scaleM);
- View:观察变换,施加在观察点上的变换,用于调整观察点位置、观察朝向、观察正方向;
- Projection:透视变换,施加在视觉上的变换,用于调整模型的透视效果(如:矩形的透视效果是梯形)。
上述变换依次叠加,得到一个总的变换矩阵,即 MVP 变换矩阵,mvpMatrix = projectionMatrix * viewMatrix * modelMatrix,MVP 变换作用到模型的原始坐标矩阵上,得到的最终坐标矩阵即为用户观测到的模型状态。
对于立体图形的绘制,绘制前需要清除深度缓存,并开启深度测试,如下。
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT);
Gdx.gl.glEnable(GL30.GL_DEPTH_TEST);
2 绘制立方体
本节将使用 Mesh、ShaderProgram、Shader 绘制立方体,OpenGL ES 的实现见博客 → 绘制立方体。
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("Cube");new Lwjgl3Application(new Cube(), config);}
}
Cube.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.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;public class Cube extends ApplicationAdapter {private PerspectiveCamera mCamera;private ShaderProgram mShaderProgram;private Mesh mMesh;private Vector3 mRotateAxis; // 旋转轴private int mRotateAgree = 0; // 旋转角度Matrix4 mModelMatrix; // 模型变换矩阵@Overridepublic void create() {initCamera();initShader();initMesh();mRotateAxis = new Vector3(0.3f, 0.5f, 0.7f);mModelMatrix = new Matrix4();}@Overridepublic 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();transform();mMesh.render(mShaderProgram, GL30.GL_TRIANGLES);}@Overridepublic void dispose() {mShaderProgram.dispose();mMesh.dispose();}private void initCamera() { // 初始化相机mCamera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());mCamera.near = 0.3f;mCamera.far = 1000f;mCamera.position.set(0f, 0f, 2.5f);mCamera.lookAt(0, 0, 0);mCamera.update();}private void initShader() { // 初始化着色器程序String vertex = Gdx.files.internal("shaders/cube_vertex.glsl").readString();String fragment = Gdx.files.internal("shaders/cube_fragment.glsl").readString();mShaderProgram = new ShaderProgram(vertex, fragment);}private void initMesh() { // 初始化网格float[] vertices = getVertices(0.5f, 1.0f);short[] indices = getIndices();VertexAttribute vertexPosition = new VertexAttribute(Usage.Position, 3, "a_position");VertexAttribute colorPosition = new VertexAttribute(Usage.ColorUnpacked, 4, "a_color");mMesh = new Mesh(true, vertices.length / 7, indices.length, vertexPosition, colorPosition);mMesh.setVertices(vertices);mMesh.setIndices(indices);}private void transform() { // MVP矩阵变换mRotateAgree = (mRotateAgree + 2) % 360;mModelMatrix.idt(); // 模型变换矩阵单位化mModelMatrix.rotate(mRotateAxis, mRotateAgree);Matrix4 mvpMatrix = mModelMatrix.mulLeft(mCamera.combined);mShaderProgram.setUniformMatrix("u_mvpTrans", mvpMatrix);}private float[] getVertices(float r, float c) { // 获取顶点数据float[] vertex = new float[] {r, r, r, c, c, c, 1, //0-r, r, r, 0, c, c, 1, //1-r, -r, r, 0, 0, c, 1, //2r, -r, r, c, 0, c, 1, //3r, r, -r, c, c, 0, 1, //4-r, r, -r, 0, c, 0, 1, //5-r, -r, -r, 0, 0, 0, 1, //6r, -r, -r, c, 0, 0, 1 //7};return vertex;}private short[] getIndices() { // 获取三角形顶点索引序列short[] indices = new short[] {0, 1, 2, 0, 2, 3, //前面0, 5, 1, 0, 4, 5, //上面0, 3, 7, 0, 7, 4, //右面6, 5, 4, 6, 4, 7, //后面6, 3, 2, 6, 7, 3, //下面6, 2, 1, 6, 1, 5 //左面};return indices;}
}
cube_vertex.glsl
#version 300 esin 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;
}
cube_fragment.glsl
#version 300 es
precision mediump float; // 声明float型变量的精度为mediumpin vec4 v_color;out vec4 fragColor;void main() {fragColor = v_color;
}
运行效果如下。