文章目录

  • 前言
  • 一、纹理贴图
    • 1. 纹理贴图介绍
    • 2. 使用opengles 实现纹理贴图的主要步骤
  • 二、代码实例
    • 1. egl_wayland_texture.c
    • 2. xdg-shell-client-protocol.h 和 xdg-shell-protocol.c
    • 3. 编译
    • 4. 运行
  • 总结

前言

本文主要介绍如何在一个wayland client 里面使用 egl + opengles 实现一个最简单的纹理贴图功能,在阅读本篇文章之前,建议先读一下之前的文章 《wayland(xdg_wm_base) + egl + opengles 最简实例》
软硬件环境
硬件:PC
软件:ubuntu22.04 weston9.0 opengles2.0 egl1.4


一、纹理贴图

1. 纹理贴图介绍

纹理贴图(Texture Mapping)是计算机图形学中的一种技术,用于将图像或纹理应用到模型的表面上,以增强模型的外观和细节;纹理贴图可以使模型表面呈现出更加真实、具有细节和复杂度的外观,而不仅仅是简单的几何形状。它通过将图像或纹理映射到模型的各个顶点或多边形上来实现。

2. 使用opengles 实现纹理贴图的主要步骤

1.创建纹理对象并绑定到当前环境中
使用 glGenTextures 函数生成一个纹理对象的标识符,并绑定到当前环境中;

GLuint textureID;glGenTextures(1, &textureID);glBindTexture(GL_TEXTURE_2D, textureID);
  1. 设置纹理参数
    通过 glTexParameteri 函数设置纹理的过滤方式和纹理坐标的环绕方式,常见的过滤方式有线性过滤和最近点采样,常见的环绕方式有重复和镜像
// 设置纹理缩小时的过滤方式(缩小的情况下,将多个纹素映射到一个片元上)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);// 设置纹理放大时的过滤方式(放大的情况下,将一个纹素映射到一个片元上)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);// 设置纹理坐标的环绕方式glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  1. 加载纹理图像数据
    使用 glTexImage2D 函数加载纹理图像数据到纹理对象中。可以从图像文件中加载纹理,也可以通过代码生成纹理数据
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  1. 使用纹理
    将纹理坐标传递给顶点着色器,并在片段着色器中进行纹理采样
// 在顶点着色器中传递纹理坐标const char* const vertexShaderSource ="attribute vec4 vPosition; \n""attribute vec2 aTexCoord; \n""varying vec2 vTexCoord; \n""void main()\n""{\n"" gl_Position = vPosition;\n""vTexCoord = aTexCoord; \n""}\n";
// 在片段着色器中进行纹理采样const char* const fragmentShaderSource = "precision mediump float;\n""varying vec2 vTexCoord; \n""uniform sampler2D uTexture; \n""void main()\n""{\n"" gl_FragColor = texture2D(uTexture, vTexCoord);\n""}\n";
  1. 绑定纹理对象并传递给片段着色器
glBindTexture(GL_TEXTURE_2D, texture);glUniform1i(u_texture, 0);
  1. 绘制结束,清除纹理
glDeleteTextures(1, &texture);

二、代码实例

1. egl_wayland_texture.c

代码如下(示例):

#include #include #include #include #include #include #include #include #include "xdg-shell-client-protocol.h"#define WIDTH 640#define HEIGHT 480struct wl_display *display = NULL;struct wl_compositor *compositor = NULL;struct xdg_wm_base *wm_base = NULL;struct wl_registry *registry = NULL;struct window { struct wl_surface *surface;struct xdg_surface *xdg_surface;struct xdg_toplevel *xdg_toplevel;struct wl_egl_window *egl_window;};// Index to bind the attributes to vertex shadersconst unsigned int VertexArray = 0;static voidxdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial){ xdg_wm_base_pong(shell, serial);}/*for xdg_wm_base listener*/static const struct xdg_wm_base_listener wm_base_listener = { xdg_wm_base_ping,};/*for registry listener*/static void registry_add_object(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { if (!strcmp(interface, "wl_compositor")) { compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);} else if (strcmp(interface, "xdg_wm_base") == 0) { wm_base = wl_registry_bind(registry, name,</