1 前言
纹理贴图的本质是将图片的纹理坐标与模型的顶点坐标建立一一映射关系。纹理坐标的 x、y 轴正方向分别朝右和朝下,如下。
2 纹理贴图
本节将使用 Mesh、ShaderProgram、Shader 实现纹理贴图,OpenGL ES 的实现见博客 → 纹理贴图,本节完整代码资源见 → libGDX Mesh纹理贴图。
package com.zhyan8.game;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.zhyan8.game.Chartlet;
public class DesktopLauncher {
public static void main (String[] arg) {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setForegroundFPS(60);
config.setTitle("Chartlet");
new Lwjgl3Application(new Chartlet(), config);
}
}
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.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
public class Chartlet extends ApplicationAdapter {
private ShaderProgram mShaderProgram;
private Mesh mMesh;
private Texture mTexture;
@Override
public void create() {
initShader();
initMesh();
mTexture = new Texture(Gdx.files.internal("textures/girl.jpg"));
}
@Override
public void render() {
Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
mShaderProgram.bind();
// mShaderProgram.setUniformi("u_texture", 0); // 设置纹理单元
mTexture.bind(0);
mMesh.render(mShaderProgram, GL30.GL_TRIANGLE_FAN);
}
@Override
public void dispose() {
mShaderProgram.dispose();
mMesh.dispose();
}
private void initShader() { // 初始化着色器程序
String vertex = Gdx.files.internal("shaders/chartlet_vertex.glsl").readString();
String fragment = Gdx.files.internal("shaders/chartlet_fragment.glsl").readString();
mShaderProgram = new ShaderProgram(vertex, fragment);
}
private void initMesh() { // 初始化网格
float[] vertices = {
-1f, -1f, 0f, 0f, 1f, // 左下
1f, -1f, 0f, 1f, 1f, // 右下
1f, 1f, 0f, 1f, 0f, // 右上
-1f, 1f, 0f, 0f, 0f // 左上
};
short[] indices = {0, 1, 2, 3};
VertexAttribute vertexPosition = new VertexAttribute(Usage.Position, 3, "a_position");
VertexAttribute texCoords = new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0");
mMesh = new Mesh(true, vertices.length / 5, indices.length, vertexPosition, texCoords);
mMesh.setVertices(vertices);
mMesh.setIndices(indices);
}
}
#version 300 es
in vec3 a_position;
in vec2 a_texCoord0;
out vec2 v_texCoord0;
void main() {
gl_Position = vec4(a_position, 1.0);
v_texCoord0 = a_texCoord0;
}
#version 300 es
precision mediump float; // 声明float型变量的精度为mediump
in vec2 v_texCoord0;
uniform sampler2D u_texture;
out vec4 fragColor;
void main() {
fragColor = texture(u_texture, v_texCoord0);
}
原文地址:https://blog.csdn.net/m0_37602827/article/details/134606031
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_22776.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。