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;
}
????????運行效果如下。