Jump to content
  • Advertisement
Sign in to follow this  
HawkDeath

OpenGL OpenGL 4.5 - create buffer

This topic is 559 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 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?

 

Share this post


Link to post
Share on other sites
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"
Edited by bartman3000

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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
Edited by bartman3000

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!