OpenGL 4.5 - create buffer

Started by
3 comments, last by HawkDeath 6 years, 10 months ago
Hello,
I learn OpenGL 4.5 and I try to create a simple triangle using only OpenGL 4.5 functions. Below I'm posting a code which creates buffer and fills a buffer.

struct Vertex
{
        .....
	glm::vec3 position;
	glm::vec4 color;
};

Vertex object[] = {
		Vertex(glm::vec3( 0.5f,  0.5f, 0.0f), glm::vec4(1.0f, 0.0f, 0.0f,1.0f)),
		Vertex(glm::vec3( 0.5f,  -0.5f, 0.0f), glm::vec4(1.0f, 0.0f, 0.0f,1.0f)),
		Vertex(glm::vec3( -0.5f,  -0.5f, 0.0f), glm::vec4(1.0f, 0.0f, 0.0f,1.0f))
	};

//Init
GLuint vao, buffer;

glCreateVertexArrays(1,&vao);

glCreateBuffers(1,&buffer);
glNamedBufferStorage(buffer, sizeof(object), &object, GL_STATIC_DRAW);

//position
glVertexArrayAttribBinding(vao, 0, 0);
glVertexArrayAttribFormat(vao,0,3,GL_FLOAT,GL_FALSE,offsetof(Vertex,position));
glEnableVertexAttribArray(0);

//color
glVertexArrayAttribBinding(vao, 1, 0);
glVertexArrayAttribFormat(vao,1,4,GL_FLOAT,GL_FALSE,offsetof(Vertex,color));
glEnableVertexAttribArray(1);

glVertexArrayVertexBuffer(vao,0,buffer,0,0);

//Draw
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES,0,3);

What I do wrong?

Junior Software Engineer. Big fan a graphics programming. 

Advertisement

Try this out:

Note: this assumes you have a vertex shader set up to read from attribute locations 0 and 1 for vertex positions and color.

EDIT: If you wanted to strictly use GL 4.5 functions only, then you can remove the call to "glBindVertexArray" and use "glEnableVertexArrayAttrib" instead of "glEnableVertexAttribArray".


Vertex object[] =
{
	Vertex(glm::vec3(0.5f,  0.5f, 0.0f), glm::vec4(0.0f, 1.0f, 0.0f,1.0f)),
	Vertex(glm::vec3(0.5f,  -0.5f, 0.0f), glm::vec4(1.0f, 0.0f, 0.0f,1.0f)),
	Vertex(glm::vec3(-0.5f,  -0.5f, 0.0f), glm::vec4(1.0f, 0.0f, 0.0f,1.0f))
};

//Init

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

glCreateBuffers(1, &buffer);
glNamedBufferStorage(buffer, sizeof(object), object, GL_MAP_READ_BIT); // GL_STATIC_DRAW isn't a valid param
//position
glEnableVertexAttribArray(0);
glVertexArrayAttribFormat(vao, 0, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vao, 0, 0);

//color
glEnableVertexAttribArray(1);
glVertexArrayAttribBinding(vao, 1, 0);
glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec3)); // relative offset is the size in bytes until the first "color" attribute

glVertexArrayVertexBuffer(vao, 0, buffer, 0, sizeof(Vertex)); // The stride is the number of bytes between each "Vertex"

Thanks, work :) . You could tell me how to use indices instead of vertices? That mean, which functions strictly OpenGL 4,5 I should use.

Junior Software Engineer. Big fan a graphics programming. 

Here's an example of using an index buffer. I'd recommend using the website docs.gl, it lets you filter out functions based on GL Version.


GLuint vao, buffer, indexBuffer;

Vertex object[] =
{
	Vertex(glm::vec3(0.5f,  0.5f, 0.0f), glm::vec4(1.0f, 0.0f, 0.0f,1.0f)),
	Vertex(glm::vec3(0.5f,  -0.5f, 0.0f), glm::vec4(0.0f, 1.0f, 0.0f,1.0f)),
	Vertex(glm::vec3(-0.5f,  -0.5f, 0.0f), glm::vec4(0.0f, 0.0f, 1.0f,1.0f)),
	Vertex(glm::vec3(-0.5f,  0.5f, 0.0f), glm::vec4(0.0f, 0.0f, 1.0f,1.0f))
};

unsigned int indices[] = { 0,1,2, 3,0,2};

//Init

glCreateVertexArrays(1, &vao);

glCreateBuffers(1, &buffer);
glNamedBufferStorage(buffer, sizeof(object), object, GL_MAP_READ_BIT); // GL_STATIC_DRAW isn't a valid param
		
//position
glEnableVertexArrayAttrib(vao, 0);
glVertexArrayAttribFormat(vao, 0, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vao, 0, 0);

//color
glEnableVertexArrayAttrib(vao, 1);
glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec3)); // relative offset is the size in bytes until the first "color" attribute
glVertexArrayAttribBinding(vao, 1, 0);

glVertexArrayVertexBuffer(vao, 0, buffer, 0, sizeof(Vertex)); // The stride is the number of bytes between each "Vertex"

// Create index buffer
glCreateBuffers(1, &indexBuffer); 
glNamedBufferStorage(indexBuffer, sizeof(indices), indices, GL_MAP_READ_BIT);
glVertexArrayElementBuffer(vao, indexBuffer);

// Draw
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // Second parameter is the number of indices

Wow, @bartman3000 thank you very much for the help.

Junior Software Engineer. Big fan a graphics programming. 

This topic is closed to new replies.

Advertisement