# OpenGL SFML - VAO issue

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

## Recommended Posts

Hey guys! I have spent the last day trying to debug this and I am beyong stuck. 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;
}
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 });

"#version 330\n\
in vec4 position;\
void main(void) {\
gl_Position = position;\
}";
"#version 330\n\
void main(void) {\
gl_FragColor = vec4(1.0,1.0,1.0,1.0);\
}";

this->programId = glCreateProgram();

/* 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, sizeof(g_vertex_buffer_data) * 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);



Then the draw function:

        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)?

Any help would be greatly apprciated

##### Share on other sites

One thing that sticks out to me is that you are unbinding the VBO before you unbind the VAO.  Try doing that the other way around.

cheers,

Bob

##### Share on other sites

Hey Bob!

Thanks for the reply I went ahead and switched those up and am getting the same issue.

Thanks!

##### Share on other sites
The second argument to glVertexAttribPointer(...) is the number of components in each element. In your case that should be 3, not 2.

##### Share on other sites

I don't know if this is true but I was under the impression you also needed to provide an element array buffer when you use DrawArrays. Otherwise, what arrays does it draw? Is is just guessing that you want to draw vertices 0, 1, 2 sequentially?

from:

https://www.opengl.org/sdk/docs/man/html/glDrawArrays.xhtml

first

Specifies the starting index in the enabled arrays.

Now I don't see any element arrays being enabled in your code, perhaps this is where the issue lies.

Edit: I tried doing this without an element array bound and it failed to work for me but I didn't get the same error as you (I got 1280 - Invalid enum). Btw your error is GL_INVALID_OPERATION if that helps somehow.

Also whoever down voted this would perhaps be kind enough to correct me.

Edited by Nanoha

##### Share on other sites

I was under the impression you also needed to provide an element array buffer when you use DrawArrays

That would be glDrawElements().

You do not need (and can not use) indices with glDrawArrays(). Edited by swiftcoder

##### Share on other sites

This isn't doing what you think it's doing:

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


Specifically, g_vertex_buffer_data looks to be a std::vector, so what you really want is g_vertex_buffer_data.size() instead of sizeof(g_vertex_buffer_data).

##### Share on other sites

I was under the impression you also needed to provide an element array buffer when you use DrawArrays

That would be glDrawElements().

You do not need (and can not use) indices with glDrawArrays().

Thanks for the correction.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002290
×