c - OpenGL ES 2.0 着色器创建失败

标签 c linux opengl-es-2.0 x11

我在编写与 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);

...其中 surfaceeglCreateWindowSurface (XWindow) eglCreatePBufferSurface(或多或少是 OpenGL 帧缓冲区)之一创建,或 eglCreatePixmapSurface (XPixmap)。

关于c - OpenGL ES 2.0 着色器创建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56124930/

相关文章:

c - 为什么在结构初始化期间使用点运算符?

ios - GLKit 不绘制 GL_POINTS 或 GL_LINES

c - 为什么我在用于指定数组大小的 const int 上得到不同的编译器行为?

c - 错误: Lvalue required. 需要解释

python - 从字符串中解析时间,从输入文件中读取(纯文本)

regex - 正则表达式 - 搜索直到出现特定字符串

objective-c - iPad(第一代)的着色器文件警告

iPhone OpenGLES 2.0 文本纹理带有奇怪的边框(不是描边)问题

c++ - 正则表达式。查找所有非拉丁字符的单词

c++ - 如何用 `R CMD INSTALL` 和 `Makevars` 覆盖 `--configure-args` 的 `--configure-vars` 编译标志?