Jump to content
  • Advertisement
Sign in to follow this  
sunsflower

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

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

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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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.

Edited by vstrakh

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!