openglbook-samples icon indicating copy to clipboard operation
openglbook-samples copied to clipboard

Chapter2 ERROR: Could not create the shaders: invalid operation

Open PROgram52bc opened this issue 8 years ago • 4 comments

I downloaded the code chapter.2.2.c, the compilation was smooth, but when I tried to execute the program it output the error "ERROR: Could not create the shaders: invalid operation". I compiled the file using gcc in Ubuntu 14.04, the OpenGL version in my computer is 3.3 (I actually modified the context in the code from 4.0 to 3.3), absolute beginner, having no idea what to do...

PROgram52bc avatar Apr 29 '16 00:04 PROgram52bc

You have to add:

glewExperimental = GL_TRUE;

right before GlewInitResult = glewInit();

DestroyFX avatar May 01 '16 05:05 DestroyFX

I tried, but it didn't work unfortunately Here is my code.cpp:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#define WINDOW_TITLE "First Triangle"

int CurrentWidth = 800;
int CurrentHeight = 600;
int WindowHandle = 0;

unsigned FrameCount = 0;
GLuint VertexShaderId,
       FragmentShaderId,
       ProgramId,
       VaoId,
       VboId,
       ColorBufferId;
const GLchar* VertexShader =
{
    "#version 400\n"\

    "layout(location=0) in vec4 in_Postion;\n"\
    "layout(location=1) in vec4 in_Color;\n"\
    "out vec4 ex_Color;\n"\

    "void main(void)\n"\
    "{\n"\
    "   gl_Position = in_Position;\n"\
    "   ex_Color = in_Color;\n"\
    "}\n"
};
const GLchar* FragmentShader = 
{
    "#version 400\n"\

    "in vec4 ex_Color;\n"\
    "out vec4 out_Color;\n"\

    "void main(void)\n"\
    "{\n"\
    "   out_Color = ex_Coor;\n"\
    "}\n"
};
void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void ResizeFunction(int, int);
void RenderFunction(void);
void TimerFunction(int);
void IdleFunction(void);

void Cleanup(void);
void CreateVBO(void);
void DestroyVBO(void);
void CreateShaders(void);
void DestroyShaders(void);

int main(int argc, char* argv[])
{
    Initialize(argc, argv);

    glutMainLoop();

    exit(EXIT_SUCCESS);
}

void Initialize(int argc, char* argv[])
{
    GLenum GlewInitResult;

    InitWindow(argc, argv);

    glewExperimental = GL_TRUE;

    GlewInitResult = glewInit();

    if (GLEW_OK != GlewInitResult){
    fprintf(
                stdout, 
                "ERROR: %s\n",
                glGetString(GL_VERSION)
           );
    }
    CreateShaders();
    CreateVBO();
    glClearColor(0.3f, 0.4f, 0.5f, 0.0f);
}

void InitWindow(int argc, char* argv[])
{
    glutInit(&argc, argv);

    glutInitContextVersion(3, 3);
    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
    glutInitContextProfile(GLUT_CORE_PROFILE);

    glutSetOption(
                GLUT_ACTION_ON_WINDOW_CLOSE,
                GLUT_ACTION_GLUTMAINLOOP_RETURNS
                );

    glutInitWindowSize(CurrentWidth, CurrentHeight);

    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

    WindowHandle = glutCreateWindow(WINDOW_TITLE);

    if(WindowHandle < 1){
        fprintf(
                    stderr,
                    "ERROR: Could not create a new rendering window.\n"
              );
        exit(EXIT_FAILURE);
    }

    glutReshapeFunc(ResizeFunction);
    glutDisplayFunc(RenderFunction);
    glutIdleFunc(IdleFunction);
    glutTimerFunc(0, TimerFunction, 0);
    glutCloseFunc(Cleanup);
}

void ResizeFunction(int Width, int Height)
{
    CurrentWidth = Width;
    CurrentHeight = Height;
    glViewport(0, 0, CurrentWidth, CurrentHeight);
}

void RenderFunction(void)
{
    ++FrameCount;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glutSwapBuffers();
    glutPostRedisplay();
}

void IdleFunction(void)
{
    glutPostRedisplay();
}

void TimerFunction(int Value)
{
    if(0 != Value) {
        char* TempString = (char*)
            malloc(512 + strlen(WINDOW_TITLE));

            sprintf(
                        TempString,
                        "%s: %d Frames Per Second @ %d x %d",
                        WINDOW_TITLE,
                        FrameCount * 4,
                        CurrentWidth,
                        CurrentHeight
                   );

        glutSetWindowTitle(TempString);
        free(TempString);
    }

    FrameCount = 0;
    glutTimerFunc(250, TimerFunction, 1);
}

void Cleanup(void)
{
    DestroyShaders();
    DestroyVBO();
}

void CreateVBO(void)
{
    GLfloat Vertices[] = {
        -0.8f, -0.8f, -0.0f, -1.0f,
        0.0f, 0.8f, 0.0f, 1.0f,
        0.8f, -0.8f, 0.0f, 1.0f
    };
    GLfloat Colors[] = {
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f
    };


    GLenum ErrorCheckValue = glGetError();

    glGenVertexArrays(1, &VaoId);
    glBindVertexArray(VaoId);

    glGenBuffers(1, &VboId);
    glBindBuffer(GL_ARRAY_BUFFER, VboId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);

    glGenBuffers(1, &ColorBufferId);
    glBindBuffer(GL_ARRAY_BUFFER, ColorBufferId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);

    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
                    stderr,
                    "ERROR: Could not create a VBO: %s \n",
                    gluErrorString(ErrorCheckValue)
               );
        exit(-1);
    }
}

void DestroyVBO(void)
{
    GLenum ErrorCheckValue = glGetError();

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDeleteBuffers(1, &ColorBufferId);
    glDeleteBuffers(1, &VboId);

    glBindVertexArray(0);
    glDeleteVertexArrays(1, &VaoId);

    ErrorCheckValue = glGetError();
    if(ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
                    stderr,
                    "ERROR: Could not destroy the VBO: %s \n",
                    gluErrorString(ErrorCheckValue)
               );

        exit(-1);
    }
}

void CreateShaders(void)
{
    GLenum ErrorCheckValue = glGetError();

    VertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(VertexShaderId, 1, &VertexShader, NULL);
    glCompileShader(VertexShaderId);

    FragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(FragmentShaderId, 1, &FragmentShader, NULL);
    glCompileShader(FragmentShaderId);

    ProgramId = glCreateProgram();
    glAttachShader(ProgramId, VertexShaderId);
    glAttachShader(ProgramId, FragmentShaderId);
    glLinkProgram(ProgramId);
    glUseProgram(ProgramId);

    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
                    stderr,
                    "ERORR: Could not create the shaders: %s \n",
                    gluErrorString(ErrorCheckValue)
               );

        exit(-1);
    }
}

void DestroyShaders(void)
{
    GLenum ErrorCheckValue = glGetError();

    glUseProgram(0);

    glDetachShader(ProgramId, VertexShaderId);
    glDetachShader(ProgramId, FragmentShaderId);

    glDeleteProgram(ProgramId);

    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
                    stderr,
                    "ERROR: Could not destroy the shaders: %s \n",
                    gluErrorString(ErrorCheckValue)
               );
        exit(-1);
    }
}

`

And I got the same error output of: ERORR: Could not create the shaders: invalid operation

PROgram52bc avatar May 01 '16 08:05 PROgram52bc

So I ran your code through vimdiff with mine and found an error on your line 43 you have the line " out_color = ex_Coor;\n"\. Missed an 'l' there.

on line 80 you have stdout should be stderr and are missing the exit failure part to that function.

my glutInitContextVersion has parameters (4,0) (your line 94)

There are some more too. I suggest going back through and looking for these types of typos. Use the example code exactly first before you start modifying it.

stevenwalton avatar May 11 '16 21:05 stevenwalton

Hi, I had the same issue and the opengl version of my computer is also old 3.0 so I've replaced glutInitContextVersion(4, 0); by glutInitContextVersion(3, 0); but the GLSL version supported by my graphics card was not "400" (which in fact means 4.00)... I've found the version supported by my graphics card with the command glxinfo on Ubuntu 18.04 in the output at line "OpenGL core profile shading language version string" there was 3.30, so I replaced in both shaders #version 400 by #version 330

stephanemasper avatar Nov 22 '18 21:11 stephanemasper