本文主要介绍如何在一个wayland client 里面使用 egl + opengles 实现一个最简单的纹理贴图功能,在阅读本篇文章之前,建议先读一下之前的文章 《wayland(xdg_wm_base) + egl + opengles 最简实例》
软硬件环境
硬件:PC
软件:ubuntu22.04 weston9.0 opengles2.0 egl1.4
纹理贴图(Texture Mapping)是计算机图形学中的一种技术,用于将图像或纹理应用到模型的表面上,以增强模型的外观和细节;纹理贴图可以使模型表面呈现出更加真实、具有细节和复杂度的外观,而不仅仅是简单的几何形状。它通过将图像或纹理映射到模型的各个顶点或多边形上来实现。
1.创建纹理对象并绑定到当前环境中
使用 glGenTextures 函数生成一个纹理对象的标识符,并绑定到当前环境中;
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// 设置纹理缩小时的过滤方式(缩小的情况下,将多个纹素映射到一个片元上)
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// 在顶点着色器中传递纹理坐标
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";
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(u_texture, 0);
glDeleteTextures(1, &texture);
代码如下(示例):
#include
#include
#include
#include
#include
#include
#include
#include
#include "xdg-shell-client-protocol.h"
#define WIDTH 640
#define HEIGHT 480
struct 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 shaders
const unsigned int VertexArray = 0;
static void
xdg_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,