Jump to content
  • Advertisement
Sign in to follow this  
TheStudent111

Problem with OpenGL error

This topic is 1133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just wanted to know what would cause this problem. Whenever I run my application, I get the following error in the command prompts

 

an internal OpenGL call failed in RenderTarget (61): GL_INVALID_OPERATION

 

The application can run and the window appears with the triangle, but the fragment shader is not working, as the triangle is white. It is suppose to be red. I wrote some debug code to detect whether or not an error has occurred but it does not run, so I'm guessing its not the shaders that is the problem.

 

During debug however, the error occurs after the command

Window.clear()  // This is an SFML call.

Here is the some code

    static const GLchar * vertex_shader_source[] =
    {
        "#version 400 core                                                     \n",
        "                                                                                  \n",
        "layout (location = 0) in vec3 position;                        \n",
        "                                                                                  \n",
        "layout (location = 1) in vec4 color;                             \n",
        "out vec4 FS_color;                                                     \n",
        "                                                                                  \n",
        "void main(void)                                                           \n",
        "{                                                                                 \n",
        "                                                                                   \n",
        "  gl_Position = vec4(position.x, position.y, position.z, 1.0f);    \n",
        "  FS_color = color;                                                \n",
        "                                                                                        \n",
        "}                                                                                 \n",
        "                                                                       \n"

    };
 
    static const GLchar * fragment_shader_source[] =
    {
        "#version 400 core               \n",
        "                                \n",
        "in vec4 FS_color;               \n",
        "out vec4 color_output;          \n",
        "                                \n",
        "void main(void)                 \n",
        "{                               \n",
        "  color_output = FS_color;      \n",
        "                                \n",
        "                                \n",
        "}                               \n"
        
    };
 

MAIN FUNCTIONS:

    const GLfloat vertices[] =
    {
         0.00f,    0.00f,    0.00f,
        -0.50f,   -0.50f,    0.00f,
         0.50f,   -0.50f,    0.00f
    };

    const GLfloat redColor[] =
    {
        1.0f, 0.0f, 0.0f, 1.0f
    };
    
    GLuint Program = compiler_shader();
    

    GLuint VAO_1;
    glGenVertexArrays(1, &VAO_1);
    glBindVertexArray(VAO_1);
        
        
        GLuint VBO_1;
        glGenBuffers(1, &VBO_1);     
        glBindBuffer(GL_ARRAY_BUFFER, VBO_1);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
        
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
        glEnableVertexAttribArray(0);


        GLuint VBO_2;
        glGenBuffers(1, &VBO_2);
        glBindBuffer(GL_ARRAY_BUFFER, VBO_2);
        glBufferData(GL_ARRAY_BUFFER, sizeof(redColor), redColor, GL_STATIC_DRAW);

        glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
        glEnableVertexAttribArray(1);


    glBindVertexArray(0);
 
 

  Game LOOP:

        glUseProgram(Program);
        glBindVertexArray(VAO_1);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(0);

        Window.display();
 

Essentially, what I'm trying to do in this application is have one of the VBO objects hold a color, that is then sent to the vertex shader then to the fragment shader (Is there a way to send data directly to the fragment shader from application code?)

 

What could be causing this error? Thanks in advance

Edited by TheStudent111

Share this post


Link to post
Share on other sites
Advertisement
Are you checking whether the individual shaders have compiled correctly? Note you can retrieve a verbose error message from the driver to diagnose any problem here.

Are you checking whether linking the shaders into a program worked correctly? Note you can retrieve a verbose error message from the driver to diagnose any problem here.

Edit: And while I'm not sure what the standard says in newer version you should definitely try to write to gl_Position as your last action in the vertex shader. Edited by BitMaster

Share this post


Link to post
Share on other sites

Thanks for the tips BitMaster.

 

Yeah, you were right. I had to check for linking errors. I get the following linking error:

 

ERROR: Definition for "void main()" not found

 

I did some research and this error is usually caused by a wrong argument with glShaderSource. The problem is caused by an improper loading of the source code. The problem is there is no problems with the arguments (from what I can gather) I currently have:

   GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
   glShaderSource(vertex_shader, 1, vertex_shader_source, nullptr);
   glCompileShader(vertex_shader);

   GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
   glShaderSource(fragment_shader, 1, fragment_shader_source, nullptr);
   glCompileShader(fragment_shader);
 

I don't think its in the shader source code, as I'm not getting errors from the info log.

GLuint shaderProgram = glCreateProgram();
    
    glAttachShader(shaderProgram, vertex_shader);
    glAttachShader(shaderProgram, fragment_shader);
    glLinkProgram(shaderProgram);

    GLint success3;
    GLchar info3[512];
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success3);

    if(!success3)
    {
        glGetProgramInfoLog(shaderProgram, 512, nullptr, info3);
        cout << "Error, Program linkage" << endl;
        for(int i = 0; i < 512; i++)
            cout << info3 ;
    }
 
 

 

 

Share this post


Link to post
Share on other sites
Are you absolutely certain you are correctly checking the compile status of both shaders? The code you posted does not do any checks on the shaders and I cannot think of any other reason for the linker to cop out with such an error message. You should also verify none of the shader objects you attach are 0 for some reason or you deleted them too early.

Share this post


Link to post
Share on other sites

Yeah, with the following:

    GLint success1;
    GLchar info1[512];
    glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success1);

    if(!success1)
    {
            glGetShaderInfoLog(vertex_shader, 512, nullptr, info1);
        
            for(int i = 0; i < 512; i++)
                cout << info1;

    }

    GLint success2;
    GLchar info2[512];
    glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success2);

    if(!success2)
    {
            glGetShaderInfoLog(fragment_shader, 512, nullptr, info2);
        
            for(int i = 0; i < 512; i++)
                cout << info2;

    }
 

The following code is written after the following command:

    glCompileShader(fragment_shader);
 
// <-- Here

Essentially, this is what my function looks like:

GLuint compiler_shader()
{

    static const GLchar * vertex_shader_source[] =
    {
        "#version 400 core                                                     \n",
        "                                                                    \n",
        "layout (location = 0) in vec3 position;                            \n",
        "                                                                    \n",
        "layout (location = 1) in vec4 color;                                \n",
        "out vec4 FS_color;                                                    \n",
        "                                                                    \n",
        "void main(void)                                                    \n",
        "{                                                                    \n",
        "                                                                    \n",
        "  FS_color = color;                                                \n",
        "  gl_Position = vec4(position.x, position.y, position.z, 1.0f);    \n",
        "                                                                    \n",
        "}                                                                    \n",
    

    };

    static const GLchar * fragment_shader_source[] =
    {
        "#version 400 core               \n",
        "                                \n",
        "in vec4 FS_color;               \n",
        "out vec4 color_output;          \n",
        "                                \n",
        "void main(void)                 \n",
        "{                               \n",
        "  color_output = FS_color;      \n",
        "                                \n",
        "                                \n",
        "}                               \n"
        
    };


    GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, vertex_shader_source, nullptr);
    glCompileShader(vertex_shader);

    GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, fragment_shader_source, nullptr);
    glCompileShader(fragment_shader);

    GLint success1;
    GLchar info1[512];
    glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success1);

    if(!success1)
    {
            glGetShaderInfoLog(vertex_shader, 512, nullptr, info1);
        
            for(int i = 0; i < 512; i++)
                cout << info1;

    }

    GLint success2;
    GLchar info2[512];
    glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success2);

    if(!success2)
    {
            glGetShaderInfoLog(fragment_shader, 512, nullptr, info2);
        
            for(int i = 0; i < 512; i++)
                cout << info2;

    }




    GLuint shaderProgram = glCreateProgram();
    
    glAttachShader(shaderProgram, vertex_shader);
    glAttachShader(shaderProgram, fragment_shader);
    glLinkProgram(shaderProgram);

    GLint success3;
    GLchar info3[512];
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success3);

    if(!success3)
    {
        glGetProgramInfoLog(shaderProgram, 512, nullptr, info3);
        cout << "Error, Program linkage" << endl;
        for(int i = 0; i < 512; i++)
            cout << info3 ;
    }

    glDetachShader(shaderProgram, vertex_shader);
    glDetachShader(shaderProgram, fragment_shader);

    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);

    return shaderProgram;
}
 
 

 

You should also verify none of the shader objects you attach are 0 for some reason or you deleted them too early.

Curious how one goes about doing this? Is there a built in OpenGL function that does this? Having trouble finding it.

Edited by TheStudent111

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!