Jump to content
  • Advertisement
Sign in to follow this  
tmason

OpenGL glEnableVertexAttribArray Produces "Invalid Value" - OpenGL 3.1

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

Hello,

 

I guess I have a weird problem which I am not sure how to fix. This code seems to have worked before but now I get a blank screen.

 

I get an "invalid value" right after this line:

 

glEnableVertexAttribArray(Vertex_VBO)

 

Any idea why that might be?

 

Thank you for your time.

glGenVertexArrays(1, &Pointer_VAO);
		glBindVertexArray(Pointer_VAO);
	
		// Create Vertex Buffer Object
		glGenBuffers(1, &Vertex_VBO);

		if ((errCode = glGetError()) != GL_NO_ERROR) {
			errString = gluErrorString(errCode);
			fprintf(stderr, "OpenGL Error: %s\n", errString);
		}

		// Save vertex attributes into GPU
		glBindBuffer(GL_ARRAY_BUFFER, Vertex_VBO);
		if ((errCode = glGetError()) != GL_NO_ERROR) {
			errString = gluErrorString(errCode);
			fprintf(stderr, "OpenGL Error: %s\n", errString);
		}
		glBufferData(GL_ARRAY_BUFFER, TotalVertexCount * Vertex_Stride * sizeof(GLfloat), &AllVertices[0], GL_STATIC_DRAW);
		if ((errCode = glGetError()) != GL_NO_ERROR) {
			errString = gluErrorString(errCode);
			fprintf(stderr, "OpenGL Error: %s\n", errString);
		}
		glEnableVertexAttribArray(Vertex_VBO);
		if ((errCode = glGetError()) != GL_NO_ERROR) {
			errString = gluErrorString(errCode);
			fprintf(stderr, "OpenGL Error: %s\n", errString);
		}

		glVertexAttribPointer(Vertex_VBO, Vertex_Stride, GL_FLOAT, GL_FALSE, 0, 0);

		if ((errCode = glGetError()) != GL_NO_ERROR) {
			errString = gluErrorString(errCode);
			fprintf(stderr, "OpenGL Error: %s\n", errString);
		}

		delete[] Vertices;

		if (HasNormals)
		{
			glGenBuffers(1, &Normal_VBO);
			glBindBuffer(GL_ARRAY_BUFFER, Normal_VBO);
			glBufferData(GL_ARRAY_BUFFER, TotalVertexCount * Normal_Stride * sizeof(GLfloat), &AllNormals[0], GL_STATIC_DRAW);
			glEnableVertexAttribArray(Normal_VBO);
			glVertexAttribPointer(Normal_VBO, Normal_Stride, GL_FLOAT, GL_FALSE, 0, 0);

			if ((errCode = glGetError()) != GL_NO_ERROR) {
				errString = gluErrorString(errCode);
				fprintf(stderr, "OpenGL Error: %s\n", errString);
			}

			delete[] Normals;
		}

		if (HasUVs)
		{
			glGenBuffers(1, &UV_VBO);
			glBindBuffer(GL_ARRAY_BUFFER, UV_VBO);
			glBufferData(GL_ARRAY_BUFFER, TotalVertexCount * UV_Stride * sizeof(GLfloat), &AllUVs[0], GL_STATIC_DRAW);
			glEnableVertexAttribArray(UV_VBO);
			glVertexAttribPointer(UV_VBO, UV_Stride, GL_FLOAT, GL_FALSE, 0, 0);

			if ((errCode = glGetError()) != GL_NO_ERROR) {
				errString = gluErrorString(errCode);
				fprintf(stderr, "OpenGL Error: %s\n", errString);
			}

			delete[] UVs;
		}

		glGenBuffers(1, &Index_VBO);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Index_VBO);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, TotalPolygonCount * Triangle_Vertex_Count * sizeof(GLuint), &ALLIndices[0], GL_STATIC_DRAW);

		if ((errCode = glGetError()) != GL_NO_ERROR) {
			errString = gluErrorString(errCode);
			fprintf(stderr, "OpenGL Error: %s\n", errString);
		}

		delete[] Indices;
		glBindVertexArray(0);

Share this post


Link to post
Share on other sites
Advertisement

Additional information.

 

From my code above ALL of my glVertexAttribPointer calls produce "invalid value".

 

Is there something wrong with my buffer data or the actual information I am putting into the function glVertexAttribPointer?

Share this post


Link to post
Share on other sites


I get an "invalid value" right after this line:

glEnableVertexAttribArray(Vertex_VBO)

Any idea why that might be?

 

Because it needs the attribute index as input and not the VBO identifier.

Share this post


Link to post
Share on other sites

Your parameters to glVertexAttribPointer are also incorrect; I'd suggest that you review the documentation: https://www.opengl.org/wiki/GLAPI/glVertexAttribPointer

 

The first parameter is not a buffer object, it is the attribute index.

The second parameter is not a stride, it is the number of items in the attribute vector (1, 2, 3 or 4).

The third parameter is the type, in this case GL_FLOAT.

The fourth parameter is GL_TRUE if normalized, GL_FALSE if not (if you don't understand this you probably need GL_FALSE).

The fifth parameter is the stride, or 0 if tightly-packed.

The sixth parameter is the pointer (for client-side arrays) or offset (for buffer objects).

Edited by mhagain

Share this post


Link to post
Share on other sites

Very same applies to glVertexAttribPointer:

// enable index 0
glEnableVertexAttribArray(0);
// index 0: 3 floats, don't normalize, stride is size of our vertex struct, and x is located at ...
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*) offsetof(vertex_t, x));
Edited by Kaptein

Share this post


Link to post
Share on other sites

Everyone was right, I think I was thrown off because I have an enumeration before with Vertex_VBO = 0, Normal_VBO = 1, etc.

 

Changed them to the actual numbers and it worked!

 

@mhagain, thank you for the link. I will check it out.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!