SFML - VAO issue

Started by
6 comments, last by Nanoha 8 years, 4 months ago

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 });

	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) {\
																gl_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, 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

Advertisement

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


[size="3"]Halfway down the trail to Hell...

Hey Bob!

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

Thanks!

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

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

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().

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

This topic is closed to new replies.

Advertisement