I would like to display a triangle.
After reading some things about buffers, I tried it myself, but it doesn’t work and I think I don’t really understand them:
here is what I have:
void Data::render() {
const GLfloat color[] = { 1.0f, 0.0f, 0.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, color);
glUseProgram(renderingProgram);
// The type used for names in OpenGL is GLuint
GLuint buffer;
// Create a buffer
glCreateBuffers(1, &buffer);
// Specify the data store parameters for the buffer
glNamedBufferStorage(
buffer, // Name of the buffer
1024 * 1024, // 1 MiB of Space
NULL, // No Initial Data
GL_MAP_WRITE_BIT); // Allow map for writing
// Now bind to the context using the GL_ARRAY_BUFFER binding point
glBindBuffer(GL_ARRAY_BUFFER, buffer);
// Place Data into the buffer object
static const float data[] =
{
0.25, -0.25, 0.5, 1.0,
-0.25, -0.25, 0.5, 1.0,
0.25, -0.25, 0.5, 1.0
};
// Get a pointer to the buffer's data store
void * ptr = glMapNamedBuffer(buffer, GL_WRITE_ONLY);
// Copy our data into it...
memcpy(ptr, data, sizeof(data));
// Tell OpenGL that we're done with this pointer
glUnmapNamedBuffer(GL_ARRAY_BUFFER);
// First bind a vertex buffer to the VAO
glVertexArrayVertexBuffer(
vertexArrayObject, // Vertex array object
0, // First vertex buffer binding
buffer, // Buffer object
0, // Start from the beginning
sizeof(vmath::vec4)); // Each vertex is one vec4
// Now, describe the data to OpenGL, tell it where it is, and turn on automatic
// vertex fetching for the specified attribute
glVertexArrayAttribFormat(
vertexArrayObject, // Vertex array object
0, // First attribute
4, // Four components
GL_FLOAT, // Floating-point data
GL_FALSE, // Normalized - ignored for floats
0); // First element of the vertex
glEnableVertexArrayAttrib(vertexArrayObject, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
and my vertex shader:
#version 430 core
layout (location = 0) in vec4 position;
void main(void)
{
gl_Position = position;
}
In the shader I’am not sure how to access the data.
The data comes as float-array. but how can I access the data for one vertex?
Because each of the three vertex uses one vector of the data (for float-values).
but how can I tell the first vertex to use the first vec4 in data and the seccond vertex to use the seccond vec4?
Can anyone explain me how to do this correctly, please?