opengl vertex buffer binding to attribute problem(very primary question)

Started by
6 comments, last by vstrakh 8 years, 4 months ago
the code is as follows. When I run this program it shows nothing...but I just follow opengl superbible's instruction. What's wrong with the code?
static const char* vs_source[] = {
		"#version 450 core "
		"layout(location = 0) in vec4 position; "
		"layout(location = 1) in vec4 color; "
		"out vec4 vs_color; "
		"void main(void) "
		"{ "
		"	gl_Position.xyz = position.xyz; "
		"	gl_Position.w = 1.0; "
		"	vs_color = color; "
		"} "
	};

	static const char* fs_source[] = {
		"#version 450 core "
		"in vec4 vs_color; "
		"out vec4 color; "
		"void main() "
		"{ "
		"	color = vs_color; "
		"} "
	};



	static const GLfloat positions[] = {
		-1.0f, -1.0f, 0.0f, 1.f,
		1.0f, -1.0f, 0.0f, 1.f,
		0.0f,  0.75f, 0.0f, 1.f,
	};

	static const GLfloat colors[] = {
		1.f, 0.f, 0.f, 1.f,
		0.f, 1.f, 0.f, 1.f,
		0.f, 0.f, 1.f, 1.f,
	};

	GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);    
	glShaderSource(vertex_shader, 1, vs_source, NULL);    
	glCompileShader(vertex_shader);    

	GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);    
	glShaderSource(fragment_shader, 1, fs_source, NULL);    
	glCompileShader(fragment_shader);    

	GLuint program = glCreateProgram();    
	glAttachShader(program, vertex_shader);   
	glAttachShader(program, fragment_shader);   
	glLinkProgram(program);    


	GLuint buffer[2]; 
	GLuint vao; 

	glCreateVertexArrays(1, &vao);
	//glBindVertexArray(vao);

	glCreateBuffers(2, &buffer[0]);

	glNamedBufferStorage(buffer[0], sizeof(positions), positions, 0); 
	glVertexArrayVertexBuffer(vao, 0, buffer[0], 0, sizeof(vec4));
	glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, GL_FALSE, 0); 
	glVertexArrayAttribBinding(vao, 0, 0); 
	glEnableVertexArrayAttrib(vao, 0); 

	glNamedBufferStorage(buffer[1], sizeof(colors), colors, 0); 
	glVertexArrayVertexBuffer(vao, 1, buffer[1], 0, sizeof(vec4));
	glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, GL_FALSE, 0); 
	glVertexArrayAttribBinding(vao, 1, 1);
	glEnableVertexArrayAttrib(vao, 1);


	do {

		glClear(GL_COLOR_BUFFER_BIT);

		glUseProgram(program);
		glDrawArrays(GL_TRIANGLES, 0, 3);

		glfwPollEvents();
	} 
	while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
		glfwWindowShouldClose(window) == 0);

	glDeleteShader(vertex_shader);
	glDeleteShader(fragment_shader);
	glDeleteBuffers(2, buffer);
	glDeleteVertexArrays(1, &vao);
	glDeleteProgram(program);
Advertisement
I use two buffers to feed two vertex attribute but something seems to be wrong.
looks like glVertexArrayAttribBinding is hardly used in other books..I cannot even find it in OpenGl Programming Guide 8th editon.. is it very old or newly added?

is it very old or newly added?

Very, very new. If you don't have OpenGL 4.4 or the GL_ARB_vertex_attrib_binding extension, you are out of luck.

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

oh thanks!...I've checked that and I do have opengl4.4 and gl_abr_vertex_attrib_binding extension......but what's wrong with the code?
Various things may or may not be an issue:
  • glShaderSource() takes a char ** (double pointer), you are passing char *. & that argument, and check for shader compile errors!
  • You aren't calling glBindVertexBuffer(), so your vertex buffers aren't associated with indices.
  • You aren't binding your VAO before calling glDrawArrays().

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

thank you! the problem is the shader is not compiled at all...pretty weird

pretty weird

Try to add newline literal in every line. Let it end with " \n"

It's not required everywhere, at least "#version 450 " requires \n (since there's no semicolon to delimit constructs), and every line that ends with comment.

This topic is closed to new replies.

Advertisement