当前位置: 首页 > news >正文

【libGDX】Mesh纹理贴图

1 前言

        纹理贴图的本质是将图片的纹理坐标与模型的顶点坐标建立一一映射关系。纹理坐标的 x、y 轴正方向分别朝右和朝下,如下。

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

        Chartlet.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.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;@Overridepublic void create() {initShader();initMesh();mTexture = new Texture(Gdx.files.internal("textures/girl.jpg"));}@Overridepublic 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);}@Overridepublic 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);}
}

         chartlet_vertex.glsl

#version 300 esin vec3 a_position;
in vec2 a_texCoord0;out vec2 v_texCoord0;void main() {gl_Position = vec4(a_position, 1.0);v_texCoord0 = a_texCoord0;
}

        chartlet_fragment.glsl

#version 300 es
precision mediump float; // 声明float型变量的精度为mediumpin vec2 v_texCoord0;uniform sampler2D u_texture;out vec4 fragColor;void main() {fragColor = texture(u_texture, v_texCoord0);
}

        运行效果。

http://www.lryc.cn/news/242298.html

相关文章:

  • 基线扫描tomcat安全加固-检查是否支持HTTPS等加密协议
  • 基于 STM32F7 和神经网络的实时人脸特征提取与匹配算法实现
  • Android笔记(十四):JetPack Compose中附带效应(一)
  • 【web】Fastapi自动生成接口文档(Swagger、ReDoc )
  • 竞赛选题 题目:基于FP-Growth的新闻挖掘算法系统的设计与实现
  • 188. 股票买卖问题(交易次数为任意正整数)
  • Typescript怎样对URL参数进行编码?
  • AndroidStudio2022.3.1 Patch3使用国内下载源加速
  • Go语言的学习笔记2——Go语言源文件的结构布局
  • python给视频增加字幕
  • 相机设置参数:黑电平(Black Level)详解和示例
  • Mac Ubuntu双系统解决WiFi和WiFi 5G网络不可用问题
  • 数据分析基础之《matplotlib(2)—折线图》
  • Rust语言入门教程(三) - 函数与模块系统
  • ubuntu22.04 arrch64版在线安装java环境
  • 概率论与数理统计中常见的随机变量分布律、数学期望、方差及其介绍
  • 骨传导耳机的优缺点都有哪些?骨传导耳机值得入手吗?
  • 在ASP.NET Core 中使用 .NET Aspire 消息传递组件
  • NLP学习
  • Linux-Ubuntu环境下搭建SVN服务器
  • python tkinter使用(四)
  • 记录ruoyi-plus-vue部署的问题
  • 如何在springboot项目中使用minio上传下载删除文件
  • SSM个性化旅游管理系统开发mysql数据库web结构java编程计算机网页源码eclipse项目
  • 4-Docker命令之docker version
  • Redis高并发缓存架构
  • 谨防利用Redis未授权访问漏洞入侵服务器
  • 关于一些bug的解决1、el-input的输入无效2、搜索之后发现数据不对3、el多选框、单选框点击无用4、
  • 使用 JavaScript 进行 API 测试的综合教程
  • Vue 2.0源码分析-Virtual DOM