我在编写与 OpenGL ES 2.0 对应的着色器的程序中遇到问题,EGL 无法编译。到目前为止,我已经尝试了很多东西。
我已将内容缩小为一个小示例:
#include <X11/Xlib.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <stdio.h>
#include <stdlib.h>
Display *x11_disp = NULL;
EGLDisplay display = EGL_NO_DISPLAY;
EGLContext context = EGL_NO_CONTEXT;
EGLConfig config = NULL;
static const GLchar * sFragShader_Rect =
"precision mediump float;\n"
"varying vec4 v_color;\n"
"void main() {\n"
" gl_FragColor = v_color;\n"
"}\n";
static const GLchar * sVertShader_Rect =
"uniform mat4 modelviewProjection;\n"
"attribute vec4 pos;\n"
"attribute vec4 color;\n"
"varying vec4 v_color;\n"
"void main() {\n"
" gl_Position = modelviewProjection * pos;\n"
" v_color = color;\n"
"}\n";
static const GLchar * sFragShader_Texture =
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"varying vec2 v_texcoord;\n"
"void main() {\n"
" gl_FragColor = texture2D(tex, v_texcoord);\n"
"}\n";
static const GLchar * sVertShader_Texture =
"uniform mat4 modelviewProjection;\n"
"attribute vec4 pos;\n"
"attribute vec2 texcoord;\n"
"varying vec2 v_texcoord;\n"
"void main() {\n"
" gl_Position = modelviewProjection * pos;\n"
" v_texcoord = texcoord;\n"
"}\n";
uint32_t FragmentShaderRect = 0;
uint32_t VertexShaderRect = 0;
uint32_t FragmentShaderTexture = 0;
uint32_t VertexShaderTexture = 0;
uint32_t ProgramRext = 0;
uint32_t ProgramTexture = 0;
uint32_t createShader(uint32_t shader_type, const char *source)
{
GLenum error;
GLint status = GL_FALSE;
GLuint shader = glCreateShader(shader_type);
error = glGetError();
if (error != GL_NO_ERROR)
{
printf("Error creating shader.");
return 0;
}
glShaderSource(shader, 1, &source, NULL);
error = glGetError();
if (error != GL_NO_ERROR)
{
printf("Error setting shader source.");
return 0;
}
glCompileShader(shader);
error = glGetError();
if (error != GL_NO_ERROR)
{
printf("Error compiling shader.");
return 0;
}
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE)
{
char log[1000] = {};
GLsizei len;
glGetShaderInfoLog(shader, 1000, &len, log);
printf("GL_Error (%d): %s\n", len, log);
}
else
{
printf("Shader compile success.\n");
}
return shader;
}
void initialize()
{
XInitThreads();
x11_disp = XOpenDisplay(NULL);
if (x11_disp == NULL)
{
printf("Error opening display.\n");
exit(-1);
}
display = eglGetDisplay(x11_disp);
if (display == EGL_NO_DISPLAY)
{
printf("Error getting display.\n");
exit(-1);
}
eglInitialize(display, NULL, NULL);
EGLint n_config;
EGLint attributes[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_DEPTH_SIZE, 24,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_NONE
};
if (eglChooseConfig(display, attributes, &config, 1, &n_config) == EGL_FALSE)
{
printf("Error getting config.\n");
exit(-1);
}
if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
{
printf("Could not bind api.\n");
exit(-1);
}
EGLint ctx_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
context = eglCreateContext(display, config, EGL_NO_CONTEXT, ctx_attribs);
if (context == EGL_NO_CONTEXT)
{
printf("Could not create context");
exit(-1);
}
// GLES STUFF
glGetError(); // clear the error
GLenum error;
FragmentShaderRect = createShader(GL_FRAGMENT_SHADER, sFragShader_Rect);
VertexShaderRect = createShader(GL_VERTEX_SHADER, sVertShader_Rect);
FragmentShaderTexture = createShader(GL_FRAGMENT_SHADER, sFragShader_Texture);
VertexShaderTexture = createShader(GL_VERTEX_SHADER, sVertShader_Texture);
}
int main()
{
initialize();
return 0;
}
用gcc -o egltest egltest.c -lEGL -lGLESv2 -lX11
编译
我也没有收到错误消息。
最佳答案
基于 Rabbid76 评论:
eglCreateContext
不会使上下文成为当前上下文,您需要在之后调用 eglMakeCurrent
:
context = eglCreateContext(display, config, EGL_NO_CONTEXT, ctx_attribs);
eglMakeCurrent(display, surface, surface, context);
...其中 surface
由 eglCreateWindowSurface
(XWindow) eglCreatePBufferSurface
(或多或少是 OpenGL 帧缓冲区)之一创建,或 eglCreatePixmapSurface
(XPixmap)。
关于c - OpenGL ES 2.0 着色器创建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56124930/