Jump to content
  • Advertisement
Sign in to follow this  
Radlabgaming

OpenGL SFML - VAO issue

This topic is 1061 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

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

Edited by Radlabgaming

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!