以下是一个基于C语言和OpenGL ES的示例程序,可以加载4张图片并使用提供的顶点数据、坐标数据进行像素混合操作(alpha blending):
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <EGL/egl.h>
#include <stdio.h>
// 顶点着色器代码
const char* vertexShaderSource =
"attribute vec4 position;\n"
"attribute vec2 texCoord;\n"
"varying vec2 v_texCoord;\n"
"void main() {\n"
" gl_Position = position;\n"
" v_texCoord = texCoord;\n"
"}\n";
// 片段着色器代码
const char* fragmentShaderSource =
"precision mediump float;\n"
"varying vec2 v_texCoord;\n"
"uniform sampler2D texture1;\n"
"uniform sampler2D texture2;\n"
"void main() {\n"
" vec4 color1 = texture2D(texture1, v_texCoord);\n"
" vec4 color2 = texture2D(texture2, v_texCoord);\n"
" vec4 blendedColor = mix(color1, color2, color2.a);\n"
" gl_FragColor = blendedColor;\n"
"}\n";
// 顶点和纹理坐标数据
GLfloat vertices[] = {
// 顶点坐标 纹理坐标
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
// 两个坐标数据
GLfloat pixelCoords1[2];
GLfloat pixelCoords2[2];
// 加载纹理
GLuint loadTexture(const char* imagePath) {
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// 读取图片文件
FILE* file = fopen(imagePath, "rb");
if (file == NULL) {
printf("Failed to open image file\n");
return 0;
}
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
rewind(file);
unsigned char* imageData = (unsigned char*)malloc(fileSize);
if (imageData == NULL) {
fclose(file);
printf("Failed to allocate memory for image data\n");
return 0;
}
if (fread(imageData, 1, fileSize, file) != fileSize) {
fclose(file);
free(imageData);
printf("Failed to read image file\n");
return 0;
}
fclose(file);
// 将图片数据加载到纹理中
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
free(imageData);
return texture;
}
// 初始化 OpenGL ES 环境
int initializeEGL() {
EGLDisplay display;
EGLSurface surface;
EGLContext context;
EGLConfig config;
EGLint numConfigs;
EGLint majorVersion;
EGLint minorVersion;
// 获取 EGL 显示连接
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (display == EGL_NO_DISPLAY) {
printf("Failed to get EGL display\n");
return -1;
}
// 初始化 EGL
if (!eglInitialize(display, &majorVersion, &minorVersion)) {
printf("Failed to initialize EGL\n");
return -1;
}
// 获取 EGL 配置
EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};
if (!eglChooseConfig(display, attribs, &config, 1, &numConfigs)) {
printf("Failed to choose EGL config\n");
return -1;
}
// 创建窗口表面
surface = eglCreatePbufferSurface(display, config, NULL);
if (surface == EGL_NO_SURFACE) {
printf("Failed to create EGL surface\n");
return -1;
}
// 创建 OpenGL ES 上下文
EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
if (context == EGL_NO_CONTEXT) {
printf("Failed to create EGL context\n");
return -1;
}
// 绑定 OpenGL ES 上下文和窗口表面
if (!eglMakeCurrent(display, surface, surface, context)) {
printf("Failed to make EGL context current\n");
return -1;
}
return 0;
}
int main() {
// 初始化 OpenGL ES 环境
if (initializeEGL() != 0) {
return -1;
}
// 创建顶点着色器
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// 创建片段着色器
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// 创建 OpenGL ES 程序对象
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glBindAttribLocation(program, 0, "position");
glBindAttribLocation(program, 1, "texCoord");
// 连接 OpenGL ES 程序对象
glLinkProgram(program);
glUseProgram(program);
// 加载纹理
GLuint texture1 = loadTexture("/path/to/image1.png");
GLuint texture2 = loadTexture("/path/to/image2.png");
// 获取顶点和纹理坐标属性的位置
GLuint positionAttrib = glGetAttribLocation(program, "position");
GLuint texCoordAttrib = glGetAttribLocation(program, "texCoord");
// 创建顶点缓冲对象
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// 启用顶点和纹理坐标属性
glEnableVertexAttribArray(positionAttrib);
glEnableVertexAttribArray(texCoordAttrib);
glVertexAttribPointer(positionAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
glVertexAttribPointer(texCoordAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (const void*)(2 * sizeof(GLfloat)));
// 获取两个坐标数据
FILE* file = fopen("/path/to/coordinates.txt", "r");
if (file == NULL) {
printf("Failed to open coordinates file\n");
return -1;
}
fscanf(file, "%f %f", &pixelCoords1[0], &pixelCoords1[1]);
fscanf(file, "%f %f", &pixelCoords2[0], &pixelCoords2[1]);
fclose(file);
// 设置片段着色器中的纹理采样器
GLint texture1Location = glGetUniformLocation(program, "texture1");
glUniform1i(texture1Location, 0);
GLint texture2Location = glGetUniformLocation(program, "texture2");
glUniform1i(texture2Location, 1);
// 渲染
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
// 交换显示缓冲区
eglSwapBuffers(eglGetCurrentDisplay(), eglGetCurrentSurface(EGL_DRAW));
// 清理资源
glDisableVertexAttribArray(positionAttrib);
glDisableVertexAttribArray(texCoordAttrib);
glDeleteBuffers(1, &vbo);
glDeleteProgram(program);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteTextures(1, &texture1);
glDeleteTextures(1, &texture2);
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(eglGetCurrentDisplay(), eglGetCurrentContext());
return 0;
}
上述代码中,你需要替换文件路径/path/to/image1.png、/path/to/image2.png和/path/to/coordinates.txt为你实际的图片路径和坐标文件路径。
这个示例程序使用了OpenGL ES 2.0,需要在编译时链接相应的库文件,并保证你的设备支持OpenGL ES 2.0。