0

I am getting an error when trying to use VAO's inside of SFML and not sure if it is SFML or it is my own opengl code

GLenum err = glewInit();
if (err != GLEW_OK)
{
    std::cout << "NOT WORKING" << std::endl;
}
    std::vector<sf::Vector3f> g_vertext_buffer_data;
g_vertex_buffer_data.push_back({ -1.0f, -1.0f, 0.0f });
g_vertex_buffer_data.push_back({1.0f, -1.0f, 0.0f});
g_vertex_buffer_data.push_back({ 0.0f, 1.0f, 0.0f });

const char* vertexShaderSource =
    "#version 330\n\
                             in vec4 position;\
                                                  void main(void){\                                                                                     gl_Position = position;\
                                                                                                         }";
// compile fragment shader source
const GLchar* fragmentShaderSource = 
    "#version 330\n\
                                    void main(void) {\
                out vec4 fragcolor;                                         fragcolor= vec4(1.0,1.0,1.0,1.0);\
                                                                                                     }";

/* Creating Shader */
this->programId = glCreateProgram();
this->vId = glCreateShader(GL_VERTEX_SHADER);
this->fId = glCreateShader(GL_FRAGMENT_SHADER);
/* Get Shader Size */
int vertexShaderLength = strlen(vertexShaderSource);
int fragmentShaderLength = strlen(fragmentShaderSource);

/* Loading and binding shader */
glShaderSource(this->vId, 1, &vertexShaderSource, NULL);
glShaderSource(this->fId, 1, &fragmentShaderSource, NULL);

/* Compile Shaders */
glCompileShader(vId);
glCompileShader(fId);
/* Attach Shaders */
glAttachShader(this->programId, this->vId);
glAttachShader(this->programId, this->fId);

/* Linkg program */
glLinkProgram(this->programId);
/* Use and bind attribute */
glUseProgram(this->programId);
this->positionId = glGetAttribLocation(this->programId, "position");
glUseProgram(0);
/* VAO Time */
glGenVertexArrays(1, &this->vaoId);
glBindVertexArray(this->vaoId);

/* VBO Time assigning to VAO */
glGenBuffers(1, &this->vboId);
glBindBuffer(GL_ARRAY_BUFFER, this->vboId);
glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data.size() * sizeof(sf::Vector3f), &g_vertex_buffer_data[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(this->positionId);
glVertexAttribPointer(this->positionId, 2, GL_FLOAT, GL_FALSE, sizeof(sf::Vector3f), 0);

/* Close out bindings */
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
  while(1)
  {
glUseProgram(this->programId);
glBindVertexArray(this->vaoId);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);    
 gameWindow.glPushStates();
  }

The error code I get is: opengl error in user code (1282)​

I have changed the size() issue that was brought up in the blBufferData() but still am getting the issue.

user5600884
  • 119
  • 1
  • 8
  • 1
    Welcome to SO! Assemble those code fragments into a [MVCE](http://stackoverflow.com/help/mcve), please, otherwise we'd have to guess what happens in the code not shown. – decltype_auto Nov 24 '15 at 18:08
  • This is all the code that is used in the opengGl code. As I am not sure where is the issue pertains I just included all of the OpenGl code – user5600884 Nov 24 '15 at 18:09
  • 3
    It's quite well defined what is considered an MVCE here, without that, your question just didn't happen. I will not spend time on writing a test app to make your code fragments runnable, and I'd doubt others other will. – decltype_auto Nov 24 '15 at 18:16

2 Answers2

2

There is at least a problem with the size that is passed to glBufferData:

glBufferData(GL_ARRAY_BUFFER,
             sizeof(g_vertex_buffer_data) * sizeof(sf::Vector3f), 
             g_vertex_buffer_data[0], GL_STATIC_DRAW);

sizeof(g_vertex_buffer_data) is equal to sizeof(std::vector<?>) which is the size of the vector object and not the size of the data contained. Try using

    glBufferData(GL_ARRAY_BUFFER,
             g_vertex_buffer_data.size() * sizeof(sf::Vector3f), 
             g_vertex_buffer_data[0], GL_STATIC_DRAW);

Another thing: In OpenGL 3.3 Core Profile there is no gl_FragColor variable. You will have to define an out variable.

Next: Your vertex shader seems to be empty. You have to write to gl_Position otherwise nothing will be shown.

BDL
  • 21,052
  • 22
  • 49
  • 55
  • I edited the code and still am having the issues and getting the 1282 error. Any other ideas? – user5600884 Nov 24 '15 at 18:50
  • Find out in which line the error happens. Try moving the glGetError until you know which OpenGL command causes the error. – BDL Nov 24 '15 at 18:51
  • okay so I get an shader compile error in my fragment shader but my vertex shader is okay. But glError() returns 0 – user5600884 Nov 24 '15 at 19:00
  • okay so I have gotten rid of a few issues but I am still getting 1282 opengl error detected in user code at line: game.gameWindow.pushGLStates(); – user5600884 Nov 24 '15 at 19:25
1

Possible error codes for glGetAttribLocation are:

GL_INVALID_OPERATION

Which don't have a fixed value. Try to get the error string with gluErrorString() or take a look in the header to which of those 1282 maps.

• check your shader got compiled without error?

• check your shader got linked without error?

What type have positionId? All object id's must be GLuint type.

And btw allways enable shader compilation-linking error check, and debug will be more informative.

I do that in this way (OpenGL-ES 2.0):

    m_nVertexShader = glCreateShader(GL_VERTEX_SHADER);
    m_nPixelShader = glCreateShader(GL_FRAGMENT_SHADER);


    glShaderSource(m_nVertexShader, 1, &lpszVertexBuffer, NULL);
    glShaderSource(m_nPixelShader, 1, &lpszFragmentBuffer, NULL);

    glCompileShader(m_nVertexShader);

    int iIsOk = 0;

    glGetShaderiv(m_nVertexShader, GL_COMPILE_STATUS, &iIsOk);

    if(!iIsOk)
    {
        GLint infoLen = 0;

        glGetShaderiv(m_nVertexShader, GL_INFO_LOG_LENGTH, &infoLen);

        if(infoLen > 1)
        {
            char* infoLog = (char*)malloc(sizeof(char) * infoLen);

            glGetShaderInfoLog(m_nVertexShader, infoLen, NULL, infoLog);


            QMessageBox::warning(this, QString("Error"),
                                 QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);

            free(infoLog);
        }

        glDeleteShader(m_nVertexShader);

        return;
    }

    glCompileShader(m_nPixelShader);

    glGetShaderiv(m_nPixelShader, GL_COMPILE_STATUS, &iIsOk);

    if(!iIsOk)
    {
        GLint infoLen = 0;

        glGetShaderiv(m_nPixelShader, GL_INFO_LOG_LENGTH, &infoLen);

        if(infoLen > 1)
        {
            char* infoLog = (char*)malloc(sizeof(char) * infoLen);

            glGetShaderInfoLog(m_nPixelShader, infoLen, NULL, infoLog);


            QMessageBox::warning(this, QString("Error"),
                                 QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);

            free(infoLog);
        }

        glDeleteShader(m_nPixelShader);

        return;
    }

    m_nProgram = glCreateProgram();

    glAttachShader(m_nProgram, m_nVertexShader);
    glAttachShader(m_nProgram, m_nPixelShader);

    glBindAttribLocation(m_nProgram, 0, "rm_Vertex");

    glLinkProgram(m_nProgram);

    glGetProgramiv(m_nProgram, GL_LINK_STATUS, &iIsOk);

    // Fail to pass status validation
    if(!iIsOk)
    {
        GLint infoLen = 0;

        glGetProgramiv(m_nProgram, GL_INFO_LOG_LENGTH, &infoLen);

        if(infoLen > 1)
        {
            char* infoLog = (char*)malloc(sizeof(char) * infoLen);

            glGetProgramInfoLog(m_nProgram, infoLen, NULL, infoLog);


            QMessageBox::warning(this, QString("Error"),
                                 QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);

            free(infoLog);
        }

        glDeleteProgram(m_nProgram);

        return;
    }

    glUseProgram(m_nProgram);

As you use GLSL 3.3, fist you must specify fragment rendertarget output by calling

glBindFragDataLocation(this->programId, 0, "fragcolor");

Secondly your fragment shader must be like

"#version 330
out vec4 fragcolor;

void main(void) {
     fragcolor= vec4(1.0,1.0,1.0,1.0); 
}

The example of using this kind of shaders is on OpenGL 3.3 + GLSL 1.5 Sample.

Mykola
  • 3,343
  • 6
  • 23
  • 39
  • Okay so I went ahead and put before gLinkProgram: glBindAttribLocation(this->programId, 0, "position") ... but I still get the same error. – user5600884 Nov 24 '15 at 18:12
  • @user5600884: What type have `positionId`? – Mykola Nov 24 '15 at 18:23
  • 1282 is a GL_INVALID_OPERATION – BDL Nov 24 '15 at 18:25
  • okay so when I use shader compile check the vertex shader compiles fine, but the fragment shader is not compiling okay. – user5600884 Nov 24 '15 at 18:58
  • @user5600884: here is the problem paste your fragment shader code to your question we shall investigate it. – Mykola Nov 24 '15 at 18:59
  • @Mykola okay so that got rid of my shader issue! But I still have an issues with the 1282 opengl error detected in user code at line: game.gameWindow.pushGLStates(); – user5600884 Nov 24 '15 at 19:25
  • @user5600884: where did you pop your pushed states. `gameWindow.glPushStates();` i think it can be stackoverflow. – Mykola Nov 24 '15 at 19:33
  • @Mykola I run my openGLcode... then game.gameWindow.pushGLstates(); then I run my sf::Sprite render code ... then I game.gameWindow.popGlStates() and display the information – user5600884 Nov 24 '15 at 19:34
  • @user5600884: your while loop is infinite and I do not see any popStates. – Mykola Nov 24 '15 at 19:36
  • @user5600884: `while(1) { glUseProgram(this->programId); glBindVertexArray(this->vaoId); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); glUseProgram(0); gameWindow.glPushStates(); }` – Mykola Nov 24 '15 at 19:37
  • @Mykola you are my savoir!!! After you pointed out where I was pushingMyGlstates() I realized that VAO's can't be processed through shared memory windows. So I setAsActive when I pushed and it all worked! – user5600884 Nov 24 '15 at 19:41
  • @user5600884 I am glad for you. Good luck! – Mykola Nov 24 '15 at 19:43