1 前言

​ 本文通过一个立方体贴图的例子,讲解三维纹理贴图的应用,案例中使用 6 张不同的图片给立方体贴图,图片如下。

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

2 立方体贴图

​ 本节将使用 Mesh、ShaderProgram、Shader 实现立方体贴图,OpenGL ES 的实现见博客 → 立方体贴图(6张图),本节完整代码资源见 → libGDX Mesh立方体贴图(6张图)

​ DesktopLauncher.java

  1. package com.zhyan8.game;
  2. import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
  3. import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
  4. public class DesktopLauncher {
  5. public static void main (String[] arg) {
  6. Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
  7. config.setForegroundFPS(60);
  8. config.setTitle("CubeChartlet");
  9. new Lwjgl3Application(new CubeChartlet(), config);
  10. }
  11. }

​ CubeChartlet.java

  1. package com.zhyan8.game;
  2. import com.badlogic.gdx.ApplicationAdapter;
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.GL30;
  5. import com.badlogic.gdx.graphics.Mesh;
  6. import com.badlogic.gdx.graphics.PerspectiveCamera;
  7. import com.badlogic.gdx.graphics.Texture;
  8. import com.badlogic.gdx.graphics.VertexAttribute;
  9. import com.badlogic.gdx.graphics.VertexAttributes.Usage;
  10. import com.badlogic.gdx.graphics.glutils.ShaderProgram;
  11. import com.badlogic.gdx.math.Matrix4;
  12. import com.badlogic.gdx.math.Vector3;
  13. public class CubeChartlet extends ApplicationAdapter {
  14. private PerspectiveCamera mCamera;
  15. private ShaderProgram mShaderProgram;
  16. private Mesh mMesh;
  17. private Texture[] mTextures;
  18. private Vector3 mRotateAxis; // 旋转轴
  19. private int mRotateAgree = 0; // 旋转角度
  20. Matrix4 mModelMatrix; // 模型变换矩阵
  21. @Override
  22. public void create() {
  23. initCamera();
  24. initShader();
  25. initMesh();
  26. initTextures();
  27. mRotateAxis = new Vector3(0.5f, 1f, 1f);
  28. mModelMatrix = new Matrix4();
  29. }
  30. @Override
  31. public void render() {
  32. Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
  33. Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT);
  34. Gdx.gl.glEnable(GL30.GL_DEPTH_TEST);
  35. mShaderProgram.bind();
  36. transform();
  37. renderCube();
  38. }
  39. @Override
  40. public void dispose() {
  41. mShaderProgram.dispose();
  42. mMesh.dispose();
  43. }
  44. private void renderCube() {
  45. for (int i = 0; i < mTextures.length; i++) { // 给每个面都贴图
  46. // mShaderProgram.setUniformi("u_texture", 0); // 设置纹理单元
  47. mTextures[i].bind(0);
  48. mMesh.render(mShaderProgram, GL30.GL_TRIANGLE_FAN, i * 4, 4);
  49. }
  50. }
  51. private void initCamera() { // 初始化相机
  52. mCamera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  53. mCamera.near = 0.3f;
  54. mCamera.far = 1000f;
  55. mCamera.position.set(0f, 0f, 4f);
  56. mCamera.lookAt(0, 0, 0);
  57. mCamera.update();
  58. }
  59. private void initShader() { // 初始化着色器程序
  60. String vertex = Gdx.files.internal("shaders/square_chartlet_vertex.glsl").readString();
  61. String fragment = Gdx.files.internal("shaders/square_chartlet_fragment.glsl").readString();
  62. mShaderProgram = new ShaderProgram(vertex, fragment);
  63. }
  64. private void initMesh() { // 初始化网格
  65. float[] vertices = Model.vertices;
  66. short[] indices = Model.indices;
  67. VertexAttribute vertexPosition = new VertexAttribute(Usage.Position, 3, "a_position");
  68. VertexAttribute texCoords = new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0");
  69. mMesh = new Mesh(true, vertices.length / 5, indices.length, vertexPosition, texCoords);
  70. mMesh.setVertices(vertices);
  71. mMesh.setIndices(indices);
  72. }
  73. private void initTextures() {
  74. mTextures = new Texture[Model.texturePaths.length];
  75. for (int i = 0; i < mTextures.length; i++) {
  76. mTextures[i] = new Texture(Gdx.files.internal(Model.texturePaths[i]));
  77. }
  78. }
  79. private void transform() { // MVP矩阵变换
  80. mRotateAgree = (mRotateAgree + 2) % 360;
  81. mRotateAxis.x = mRotateAgree / 180f - 1;
  82. mRotateAxis.y = (float) Math.sin(mRotateAgree / 180f * Math.PI * 0.7f);
  83. mRotateAxis.z = (float) Math.cos(mRotateAgree / 180f * Math.PI * 0.5f);
  84. mModelMatrix.idt(); // 模型变换矩阵单位化
  85. mModelMatrix.rotate(mRotateAxis, mRotateAgree);
  86. Matrix4 mvpMatrix = mModelMatrix.mulLeft(mCamera.combined);
  87. mShaderProgram.setUniformMatrix("u_mvpTrans", mvpMatrix);
  88. }
  89. }

​ Model.java

  1. package com.zhyan8.game;
  2. public class Model {
  3. private static float r = 1.0f;
  4. public static String[] texturePaths = new String[] {
  5. "textures/a1.png", "textures/a2.png", "textures/a3.png",
  6. "textures/a4.png", "textures/a5.png", "textures/a6.png"
  7. };
  8. public static float[] vertices = new float[] {
  9. // 前面
  10. r, r, r, 0f, 0f, // 0
  11. -r, r, r, 1f, 0f, // 1
  12. -r, -r, r, 1f, 1f, // 2
  13. r, -r, r, 0f, 1f, // 3
  14. // 后面
  15. r, r, -r, 0f, 0f, // 4
  16. -r, r, -r, 1f, 0f, // 5
  17. -r, -r, -r, 1f, 1f, // 6
  18. r, -r, -r, 0f, 1f, // 7
  19. // 上面
  20. r, r, r, 0f, 0f, // 8
  21. r, r, -r, 1f, 0f, // 9
  22. -r, r, -r, 1f, 1f, // 10
  23. -r, r, r, 0f, 1f, // 11
  24. // 下面
  25. r, -r, r, 0f, 0f, // 12
  26. r, -r, -r, 1f, 0f, // 13
  27. -r, -r, -r, 1f, 1f, // 14
  28. -r, -r, r, 0f, 1f, // 15
  29. // 右面
  30. r, r, r, 0f, 0f, // 16
  31. r, r, -r, 1f, 0f, // 17
  32. r, -r, -r, 1f, 1f, // 18
  33. r, -r, r, 0f, 1f, // 19
  34. // 左面
  35. -r, r, r, 0f, 0f, // 20
  36. -r, r, -r, 1f, 0f, // 21
  37. -r, -r, -r, 1f, 1f, // 22
  38. -r, -r, r, 0f, 1f // 23
  39. };
  40. public static short[] indices = new short[] {
  41. 0, 1, 2, 3, // 前面
  42. 4, 5, 6, 7, // 上面
  43. 8, 9, 10, 11, // 右面
  44. 12, 13, 14, 15, // 后面
  45. 16, 17, 18, 19, // 下面
  46. 20, 21, 22, 23 // 左面
  47. };
  48. }

​ square_chartlet_vertex.glsl

  1. #version 300 es
  2. in vec3 a_position;
  3. in vec2 a_texCoord0;
  4. uniform mat4 u_mvpTrans; // MVP矩阵变换
  5. out vec2 v_texCoord0;
  6. void main() {
  7. gl_Position = u_mvpTrans * vec4(a_position, 1.0);
  8. v_texCoord0 = a_texCoord0;
  9. }

​ square_chartlet_fragment.glsl

  1. #version 300 es
  2. precision mediump float; // 声明float型变量的精度为mediump
  3. in vec2 v_texCoord0;
  4. uniform sampler2D u_texture;
  5. out vec4 fragColor;
  6. void main() {
  7. fragColor = texture(u_texture, v_texCoord0);
  8. }

​ 运行效果如下。

​ 声明:本文转自【libGDX】Mesh立方体贴图(6张图)

【libGDX】Mesh立方体贴图(6张图)的更多相关文章

  1. SQL知识点脑图(一张图总结SQL)

    sql语言的分类DDL:create drop alter DML:insert delete update DCL:rollback grant revoke commit 概要,主外键,视图,索引 ...

  2. OpenGL+OpenCV实现立方体贴图

    我屮艸芔茻,转眼就7月份了. 今天试了一下立方体贴图,比较简单,大概说下和平面贴图的区别. 1. 平面贴图需要的是纹理坐标vec2:立方体贴图需要的是一个方向向量vec3,长度没有关系,重要的是方向, ...

  3. OpenGL 核心技术之立方体贴图

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家.特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...

  4. Unity Shaders and Effects Cookbook (4-1)(4-2)静态立方体贴图的创建与使用

    開始学习第4章 - 着色器的反射 看完了1.2节,来记录一下.反射主要是利用了 Cubemap 立方体贴图. 认识Cubemap 立方体贴图.就如同名字所说.在一个立方体上有6张图.就这样觉得吧. 假 ...

  5. 立方体贴图(Cubemap)

    http://blog.csdn.net/asdjy123/article/details/51190643 点击打开链接 好东西保存方便查看 立方体贴图(Cubemap) 原文 Cubemaps 作 ...

  6. (转)OpenGL学习——立方体贴图

    转自:https://learnopengl-cn.readthedocs.io/zh/latest/04%20Advanced%20OpenGL/06%20Cubemaps/ 我们之前一直使用的是2 ...

  7. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图 代码工程地址: https://github.c ...

  8. threejs立方体贴图产生边缘锯齿问题

    threejs立方体贴图产生边缘锯齿问题 立方体贴图边缘锯齿 解决后 经过试验测试发现, textureGrass.wrapS和 textureGrass.wrapT属性导致的. 解决方法1: 删掉t ...

  9. WebGL 利用FBO完成立方体贴图。

    这篇主要记录WebGL的一些基本要点,顺便也学习下如何使用FBO与环境贴图.先看下效果图(需要支持WebGL,Chrome,火狐,IE11). 主要实现过程如下,先用FBO输出当前环境在立方体纹理中, ...

  10. 一张图告诉你,只会HTML还不够!

    会了HTML和HTML5语法,你就真的会了HTML吗,来看这张图!是这本<超实用的HTML代码段>入门实例书的导览!熊孩子们,赶紧学习去吧! 如果一半以上的你都不会,必须看这本书,阿里一线 ...

随机推荐

  1. ebpf 单行程序学习

    ebpf 单行程序学习 背景 公司方神借给我一本: <BPF之巅:洞悉linux系统和应用性能>纸质书 拿回家晚上在沙发上看了几天. 感觉书很厚看的不是很系统. 仅能凭自己的感觉总结一下这 ...

  2. Vue基础系列文章11---router基本使用

    1.系统中引入路由js文件,加两个连接,分别到用户管理和用户注册页面 <router-link to="/user">用户列表</router-link> ...

  3. C# await和Result对比

    1.Result 上图是微软官网的截图,由图可知在使用GetXXXX的方法的时候,会阻塞调用其他线程,直到当前异步操作完成,相当于调用wait方法.但是使用异步编程应该避免使用TASK.WAIT或TA ...

  4. Mysql索引失效场景

    Mysql索引失效场景   序言   众所周知在Mysql中,通过使用索引的方式可以加快查询速度,从而避免全文搜索:而索引本身就像图书馆中所有书籍目录,通过查询关键字就能快速找到目标书籍在几列几行,这 ...

  5. 【Java】先return还是先finally

    之前调试只发现有的方法执行完return语句后再执行finally,但是没有细究 最近debug代码的时候发现,不同返回类型的方法,return和finally执行顺序竟然不一样 先看返回类型为voi ...

  6. MedicalGPT:基于LLaMA-13B的中英医疗问答模型(LoRA)

    MedicalGPT:基于LLaMA-13B的中英医疗问答模型(LoRA).实现包括二次预训练.有监督微调.奖励建模.强化学习训练[LLM:含Ziya-LLaMA]. **** 训练医疗大模型,实现包 ...

  7. 推荐系统[四]:精排-详解排序算法LTR (Learning to Rank)_ poitwise, pairwise, listwise相关评价指标,超详细知识指南。

    0.前言召回排序流程策略算法简介 推荐可分为以下四个流程,分别是召回.粗排.精排以及重排: 召回是源头,在某种意义上决定着整个推荐的天花板: 粗排是初筛,一般不会上复杂模型: 精排是整个推荐环节的重中 ...

  8. 【3】opencv_contrib 4.3.0库配置+opencv安装

    相关文章: [1]windows下安装OpenCV(4.3)+VS2017安装+opencv_contrib4.3.0配置 [2]Visual Studio 2017同时配置OpenCV2.4 以及O ...

  9. C++ Qt开发:数据库与TableView多组件联动

    Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍TableVi ...

  10. C/C++ 使用CRC检测磁盘文件完整性

    当软件被开发出来时,为了增加软件的安全性,防止被破解,通常情况下都会对自身内存或磁盘文件进行完整性检查,以防止解密者修改程序,我们可以将exe与dll文件同时做校验,来达到相互认证的目的,解密者想要破 ...