diff --git a/src/engine/display/primitive.c b/src/engine/display/primitive.c new file mode 100644 index 00000000..3f01dbf0 --- /dev/null +++ b/src/engine/display/primitive.c @@ -0,0 +1,84 @@ +#include "primitive.h" + +primitive_t * primitiveCreate(int32_t verticeCount) { + primitive_t *primitive = malloc(sizeof(primitive_t)); + + primitive->verticeCount = verticeCount; + + size_t sizeIndices = sizeof(uint32_t) * verticeCount; + size_t sizePositions = sizeof(float) * verticeCount * PRIMITIVE_POSITIONS_PER_VERTICE; + size_t sizeCoordinates = sizeof(float) * verticeCount * PRIMITIVE_COORDINATES_PER_VERTICE; + + //Setup Vertex Array Object + glGenVertexArrays(1, &primitive->vertexArray); + glBindVertexArray(primitive->vertexArray); + + // Create some buffers, one for the vertex data, one for the indices + GLuint buffer[2]; + glGenBuffers(2, &buffer); + + primitive->vertexBuffer = buffer[0]; + primitive->indexBuffer = buffer[1]; + + //Buffer an empty set of data then buffer each component + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeIndices, GL_DYNAMIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer); + glBufferData(GL_ARRAY_BUFFER, sizePositions+sizeCoordinates, 0, GL_DYNAMIC_DRAW); + + // Setup the attrib pointers + glVertexAttribPointer(0, PRIMITIVE_POSITIONS_PER_VERTICE, GL_FLOAT, GL_FALSE, 0, (void *)0); + glVertexAttribPointer(1, PRIMITIVE_COORDINATES_PER_VERTICE, GL_FLOAT, GL_FALSE, 0, (void *)0); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + + return primitive; +} + +void primitiveBufferVertices(primitive_t *primitive, + int32_t position, int32_t count, vertice_t *vertices +) { + // Memory + size_t lengthPositions, lengthCoordinates, offsetPositions, offsetCoordinates; + float *positions, *coordinates; + int32_t i; + + // Setup the size of the memory that the positions and coordinates will use + lengthPositions = sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * count; + offsetPositions = sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * position; + + lengthCoordinates = sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * count; + offsetCoordinates = ( + (sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * primitive->verticeCount)+ + (sizeof(float) * PRIMITIVE_COORDINATES_PER_VERTICE * position) + ); + + // Create some memory + positions = malloc(lengthPositions); + coordinates = malloc(lengthCoordinates); + + // Now copy the positions and coordinates from the vertices into the buffer + for(i = 0; i < count; i++) { + positions[i * PRIMITIVE_POSITIONS_PER_VERTICE] = vertices[i]->x; + positions[i * PRIMITIVE_POSITIONS_PER_VERTICE + 1] = vertices[i]->y; + positions[i * PRIMITIVE_POSITIONS_PER_VERTICE + 2] = vertices[i]->z; + + coordinates[i * PRIMITIVE_COORDINATES_PER_VERTICE] = vertices[i]->u; + coordinates[i * PRIMITIVE_COORDINATES_PER_VERTICE + 1] = vertices[i]->v; + } + + // Buffer the data into the GPU + glBufferSubData(GL_ARRAY_BUFFER, offsetPositions, lengthPositions, positions); + glBufferSubData(GL_ARRAY_BUFFER, offsetCoordinates, lengthCoordinates, positions); + + // Free the vertices. + free(positions); + free(coordinates); +} + +void primitiveBufferIndices(primitive_t *primitive, + int32_t position, int32_t count, int32_t *indices +) { + +} diff --git a/src/engine/display/primitive.h b/src/engine/display/primitive.h new file mode 100644 index 00000000..b83264d7 --- /dev/null +++ b/src/engine/display/primitive.h @@ -0,0 +1,33 @@ +#pragma once +#include +#include + +#define PRIMITIVE_POSITIONS_PER_VERTICE 3 +#define PRIMITIVE_COORDINATES_PER_VERTICE 2 + +typedef struct { + int32_t verticeCount; + + GLuint vertexArray; + GLuint vertexBuffer; + GLuint indexBuffer; +} primitive_t; + +typedef struct { + float x, y, z; + float u, v; +} vertice_t; + +primitive_t * primitiveCreate(int32_t verticeCount); + +void primitiveBufferVertices(primitive_t *primitive, + int32_t position, int32_t count, vertice_t *vertices +); + +void primitiveBufferIndices(primitive_t *primitive, + int32_t position, int32_t count, int32_t *indices +); + +void primitiveDraw(primitive_t *primitive, int32_t start, int32_t count); + +void primitiveDispose(primitive_t *primitive); \ No newline at end of file