这里我们基于OpenGL ES 3.2和C语言,实现一个简单的加载两张RGBA图片并进行blend(混合)的demo。
我们首先需要加载两张RGBA格式的图片。这里我们仍然使用stb_image库来加载图片:
#include "stb_image.h"
// 读取图片为RGBA格式
unsigned char* loadImage(const char* filepath, int* width, int* height)
{
// 打开文件并读取图片数据
int comp;
unsigned char* data = stbi_load(filepath, width, height, &comp, STBI_rgb_alpha);
return data;
}
unsigned char* image1, *image2;
int width1, height1, width2, height2;
// 加载两张图片
image1 = loadImage("image1.png", &width1, &height1);
image2 = loadImage("image2.png", &width2, &height2);
接着,我们需要创建OpenGL ES上下文,并创建两个纹理对象,用于存储这两张图片:
#include <GLES3/gl32.h>
GLuint texture1, texture2;
// 创建纹理对象并设置纹理参数
void createTexture(GLuint* texture, unsigned char* imageData, int width, int height)
{
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, *texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
// 创建两个纹理对象
createTexture(&texture1, image1, width1, height1);
createTexture(&texture2, image2, width2, height2);
然后,我们可以用glBlendFunc函数来进行混合操作。比如,我们可以将两张图片叠加起来:
// 绑定两个纹理
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
// 设置混合函数
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
// 绘制两个纹理的覆盖
// ...
// 关闭混合
glDisable(GL_BLEND);
在绘制的时候,我们还需要遵循OpenGL的渲染管线,从顶点到片元依次进行处理。
完整的demo代码如下:
#include <stdio.h>
#include "stb_image.h"
#include <GLES3/gl32.h>
unsigned char* loadImage(const char* filepath, int* width, int* height)
{
// ...
}
void createTexture(GLuint* texture, unsigned char* imageData, int width, int height)
{
// ...
}
int main()
{
// 创建OpenGL ES 3.2上下文
// ...
// 加载两张图片
unsigned char* image1, * image2;
int width1, height1, width2, height2;
image1 = loadImage("image1.png", &width1, &height1);
image2 = loadImage("image2.png", &width2, &height2);
// 创建两个纹理对象
GLuint texture1, texture2;
createTexture(&texture1, image1, width1, height1);
createTexture(&texture2, image2, width2, height2);
// 设置混合函数
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
while (1)
{
// 清空缓存
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// 绘制两个纹理
glUseProgram(0);
// 绑定texture1到GL_TEXTURE0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
// 绑定texture2到GL_TEXTURE1
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
// 绘制覆盖后的纹理
GLfloat vertices[] = {
// positions // texture coordinates
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
};
GLuint vbo, vao;
glGenBuffers(1, &vbo);
glGenVertexArrays(1, &vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindVertexArray(vao);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
// 显示渲染结果
// ...
}
// 清理资源
// ...
return 0;
}