我目前正在尝试使用 OpenGL ES 进行图像处理。我正在尝试做一些基本的图像效果,比如模糊切换色彩空间等等。 我想构建最简单的程序来执行以下操作:
- 图片加载
- 图像处理(使用着色器)
- 图片保存
我已经成功构建了以下设置:
- OpenGL 环境
- 我想要使用 DevIL 加载的图像效果。
- 两个着色器(一个顶点着色器和一个片段着色器)
我现在坚持使用我加载的图像将数据发送到片段着色器。我想要做的是将图像作为 sampler2D 发送到片段着色器并对其进行处理。
我有多个问题,例如:
- 如果我只想处理纯 2D 图像,是否需要顶点着色器?
- 如果我这样做,应该在这个顶点着色器中做什么,因为我根本没有顶点。我应该创建四边形顶点(如 (0,0) (1, 0) (0, 1) (1, 1)) 吗?如果是,为什么?
- 我是否需要使用 VBO(似乎与顶点着色器相关)、FBO 或其他类似的东西?
- 我不能只将我的图像加载到纹理中并等待片段着色器在此纹理上做我想做的一切吗?
- 有人可以提供一些简单的“干净”代码来帮助我理解(没有任何使理解变得如此复杂的花哨类)吗?
这是我的片段着色器进行简单颜色交换时的样子:
uniform int width;
uniform int height;
uniform sampler2D texture;
void main() {
vec2 texcoord = vec2(gl_FragCoord.x/width, gl_FragCoord.y/height);
vec4 texture_value = texture2D(texture, texcoord);
gl_FragColor = texture_value.bgra;
}
和我的 main.cpp :
int main(int argc, char** argv)
{
if (argc != 4) {
std::cerr << "Usage: " << argv[0] << " <vertex shader path> <fragment shader path> <image path>" << std::endl;
return EXIT_FAILURE;
}
// Get an EGL valid display
EGLDisplay display;
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (display == EGL_NO_DISPLAY) {
std::cerr << "Failed to get EGL Display" << std::endl
<< "Error: " << eglGetError() << std::endl;
return EXIT_FAILURE;
}
else {
std::cerr << "Successfully get EGL Display." << std::endl;
}
// Create a connection to the display
int minor, major;
if (eglInitialize(display, &minor, &major) == EGL_FALSE) {
std::cerr << "Failed to initialize EGL Display" << std::endl
<< "Error: " << eglGetError() << std::endl;
eglTerminate(display);
return EXIT_FAILURE;
}
else {
std::cerr << "Successfully intialized display (OpenGL ES version " << minor << "." << major << ")." << std::endl;
}
// OpenGL ES Config are used to specify things like multi sampling, channel size, stencil buffer usage, & more
// See the doc: https://www.khronos.org/registry/EGL/sdk/docs/man/html/eglChooseConfig.xhtml for more informations
EGLConfig config;
EGLint num_configs;
if (!eglChooseConfig(display, configAttribs, &config, 1, &num_configs)) {
std::cerr << "Failed to choose EGL Config" << std::endl
<< "Error: " << eglGetError() << std::endl;
eglTerminate(display);
return EXIT_FAILURE;
}
else {
std::cerr << "Successfully choose OpenGL ES Config ("<< num_configs << ")." << std::endl;
}
// Creating an OpenGL Render Surface with surface attributes defined above.
EGLSurface surface = eglCreatePbufferSurface(display, config, pbufferAttribs);
if (surface == EGL_NO_SURFACE) {
std::cerr << "Failed to create EGL Surface." << std::endl
<< "Error: " << eglGetError() << std::endl;
}
else {
std::cerr << "Successfully created OpenGL ES Surface." << std::endl;
}
eglBindAPI(EGL_OPENGL_API);
EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
if (context == EGL_NO_CONTEXT) {
std::cerr << "Failed to create EGL Context." << std::endl
<< "Error: " << eglGetError() << std::endl;
}
else {
std::cerr << "Successfully created OpenGL ES Context." << std::endl;
}
//Bind context to surface
eglMakeCurrent(display, surface, surface, context);
// Create viewport and check if it has been created correctly
glViewport(0, 0, WIDTH, HEIGHT);
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
if (viewport[2] != WIDTH || viewport[3] != HEIGHT) {
std::cerr << "Failed to create the viewport. Size does not match (glViewport/glGetIntegerv not working)." << std::endl
<< "OpenGL ES might be faulty!" << std::endl
<< "If you are on Raspberry Pi, you should not updated EGL as it will install fake EGL." << std::endl;
eglTerminate(display);
return EXIT_FAILURE;
}
// Clear buffer and get ready to draw some things
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Create a shader program
GLuint program = load_shaders(std::string(argv[1]), std::string(argv[2]));
if (program == -1)
{
std::cerr << "Failed to create a shader program. See above for more details." << std::endl;
eglTerminate(display);
return EXIT_FAILURE;
}
/* Initialization of DevIL */
if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION) {
std::cerr << "Failed to use DevIL: Wrong version." << std::endl;
return EXIT_FAILURE;
}
ilInit();
ILuint image = load_image(argv[3]);
GLuint texId;
glGenTextures(1, &texId); /* Texture name generation */
glBindTexture(GL_TEXTURE_2D, texId); /* Binding of texture name */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear interpolation for magnification filter */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* We will use linear interpolation for minifying filter */
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),
0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData()); /* Texture specification */
谢谢。
最佳答案
Do I need a vertex shader as all I want to do is pure 2D image processing ?
在 OpenGL ES 2 中必须使用顶点和片段着色器。
If I do, what should be done in this vertex shader as I have no vertices at all. Should I create quad vertices (like (0,0) (1, 0) (0, 1) (1, 1)) ? If so, why ?
是的。因为这就是 OpenGL ES 2 的工作原理。否则,您将需要使用诸如计算机着色器(在 OpenGL ES 3.1+ 中支持)或 OpenCL 之类的东西。
Do I need to use things like VBO (which seems to be related to the vertex shader), FBO or other thing like that ?
使用 VBO/IBO 实际上对您没有任何影响,因为您只有 4 个顶点和 2 个基元。根据您的需要,您可能想要渲染到纹理。
Can't I just load my image into the texture and wait for the fragment shader to do everything I want on this texture ?
没有。
关于c++ - OpenGL (ES) 图像处理 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50326310/