Question

I'm pretty new to modern OpenGL (3.0+) and right now I'm trying to create a basic white triangle on a black background. I'm on Mac OS X Mavericks 10.9.2, using GLFW 3.0.4 to create window, GLEW 1.10.0 to load extensions. You can find the full source code here.

My problem is, that my fragment shader doesn't want to be attached to my program. I checked both the program (glIsProgram()) and the shader (glIsShader) and they both passed. The shaders are compiling, and the program is linking..

However my code compiles, runs and doesn't throw any error, I only got a running window with a black background but without the white triangle. Here is the part, I think that is broken somehow (or at least this is where I can find the error):

const char* opengl_get_error(void)
{
    switch (glGetError())
    {
        case 0x0500: return "GL_INVALID_ENUM​";
        case 0x0501: return "GL_INVALID_VALUE​";
        case 0x0502: return "GL_INVALID_OPERATION​";
        case 0x0503: return "GL_STACK_OVERFLOW​";
        case 0x0504: return "GL_STACK_UNDERFLOW​";
        case 0x0505: return "GL_OUT_OF_MEMORY​";
        case 0x0506: return "GL_INVALID_FRAMEBUFFER_OPERATION​";
        case 0x8031: return "GL_TABLE_TOO_LARGE​";
            default: return "GL_NO_ERROR";
    }
}

void check_attached_shaders(GLuint program)
{
    GLsizei count = 0;
    GLuint shaders[] = {0, 0, 0, 0};
    glGetAttachedShaders(program, 4, &count, shaders);
    printf(">>> ERROR: %s\n", opengl_get_error());
    printf("\tnumber of shaders: %d\n", count);
    for (int i=0; i < 4; i++) printf("\tshader_id: %d\n", shaders[i]);
}

...

    // Get program ID
    GLuint programID = glCreateProgram();

    // Attach shaders
/* CHECKING: */ check_attached_shaders(programID);
    glAttachShader(programID, vert_shader);
/* CHECKING: */ check_attached_shaders(programID);
    glAttachShader(programID, frag_shader);
/* CHECKING: */ check_attached_shaders(programID);

    // Link shader program
    glLinkProgram(programID);

...

And I have this output:

/*
>>> ERROR: GL_NO_ERROR
    number of shaders: 0
    shader_id: 0
    shader_id: 0
    shader_id: 0
    shader_id: 0
>>> ERROR: GL_NO_ERROR
    number of shaders: 1
    shader_id: 1
    shader_id: 0
    shader_id: 0
    shader_id: 0
>>> ERROR: GL_INVALID_OPERATION
    number of shaders: 1
    shader_id: 1
    shader_id: 0
    shader_id: 0
    shader_id: 0
*/

This is what my vertex shader looks like:

// OpenGL Shading Language Version 1.5.0
#version 150 core

in vec2 input_position;

void main()
{
    gl_Position = vec4(input_position, 0.f, 1);
}

This is how my fragment shader looks like:

// OpenGL Shading Language Version 1.5.0
#version 150 core

out vec4 output_fragment;

void main()
{
    output_fragment = vec4(1.f, 1.f, 1.f, 1.f);
}

Any feedback is welcome,

Thanks in advance!

Was it helpful?

Solution

As noted in comments, the issue was in the function GLuint create_shader_from_file(GLchar *path, GLenum shader_type) which compiled a shader from a source file and returned 0 or 1 to indicate failure or success.

The result was then used as a shader ID.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top