c - OpenGL ES 2.0 着色器创建失败
问题描述
我在编写与 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)之一创建。
推荐阅读
- python-3.x - 带有条件的 Pandas DataFrame 副本
- javascript - 功能组件中的 setState 不保留值
- elasticsearch - 升级弹性客户端和依赖项以连接到较旧的弹性服务器实例
- docker - 在使用 Kubernetes 部署的浏览器上看不到我的应用
- python-3.x - 需要一个类似字节的对象,而不是“str”(不发送到 dataOutput)
- azure-files - 从 Windows 10 PC 连接到 Azure 文件共享时出错
- python - 更新到 3.5.0 棉花糖后的“未知字段”
- python-3.x - Python Cryptography 从密码错误生成密钥
- c# - 逗号分隔的字符串以分隔数组c#中的值
- excel - SSIS:如何替换星号,数据类型DT_R8,excel源码