diff --git a/src/engine/display/primitive.c b/src/engine/display/primitive.c index 3f01dbf0..0843b63e 100644 --- a/src/engine/display/primitive.c +++ b/src/engine/display/primitive.c @@ -1,38 +1,42 @@ #include "primitive.h" -primitive_t * primitiveCreate(int32_t verticeCount) { +primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount) { primitive_t *primitive = malloc(sizeof(primitive_t)); primitive->verticeCount = verticeCount; + primitive->indiceCount = indiceCount; - size_t sizeIndices = sizeof(uint32_t) * 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); + size_t sizeIndices = sizeof(indice_t) * indiceCount; // Create some buffers, one for the vertex data, one for the indices - GLuint buffer[2]; - glGenBuffers(2, &buffer); + GLuint *buffer = malloc(sizeof(GLuint) * 2); + glGenBuffers(2, buffer); primitive->vertexBuffer = buffer[0]; primitive->indexBuffer = buffer[1]; + free(buffer); //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); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeIndices, 0, 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); + 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; } @@ -60,15 +64,16 @@ void primitiveBufferVertices(primitive_t *primitive, // 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; + 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; + 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 + glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer); glBufferSubData(GL_ARRAY_BUFFER, offsetPositions, lengthPositions, positions); glBufferSubData(GL_ARRAY_BUFFER, offsetCoordinates, lengthCoordinates, positions); @@ -78,7 +83,28 @@ void primitiveBufferVertices(primitive_t *primitive, } void primitiveBufferIndices(primitive_t *primitive, - int32_t position, int32_t count, int32_t *indices + int32_t position, int32_t count, indice_t *indices ) { + size_t length, offset; + offset = position * sizeof(indice_t); + length = count * sizeof(indice_t); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer); + glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, length, indices); } + +void primitiveDraw(primitive_t *primitive, int32_t start, int32_t count) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer); + glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer); + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void *)( + sizeof(indice_t)*start + )); +} + +void primitiveDispose(primitive_t *primitive) { + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glDeleteBuffers(1, &primitive->vertexBuffer); + glDeleteBuffers(1, &primitive->indexBuffer); + free(primitive); +} \ No newline at end of file diff --git a/src/engine/display/primitive.h b/src/engine/display/primitive.h index b83264d7..9cca0ed5 100644 --- a/src/engine/display/primitive.h +++ b/src/engine/display/primitive.h @@ -1,33 +1,69 @@ #pragma once #include #include +#include #define PRIMITIVE_POSITIONS_PER_VERTICE 3 #define PRIMITIVE_COORDINATES_PER_VERTICE 2 +/** Structure containing information about a primitive */ typedef struct { int32_t verticeCount; + int32_t indiceCount; - GLuint vertexArray; GLuint vertexBuffer; GLuint indexBuffer; } primitive_t; +/** Structure containing vertice position information */ typedef struct { float x, y, z; float u, v; } vertice_t; -primitive_t * primitiveCreate(int32_t verticeCount); +/** Indice that references a specific vertice */ +typedef unsigned int indice_t; +/** + * Creates a new primitive. + * @param verticeCount How many vertices can the primitive hold. + * @param indiceCount How many indices can the primitive hold. + * @return The newly created primitive ready to be buffered to. + */ +primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount); + +/** + * Buffer Vertices to a primitive for use in rendering. + * @param primitive The primitive to buffer vertices into. + * @param position The position (index) to overwrite the vertices of. + * @param count The count of vertices to buffer. + * @param vertices Array of vertices to buffer into the primitive. + */ void primitiveBufferVertices(primitive_t *primitive, int32_t position, int32_t count, vertice_t *vertices ); +/** + * Buffer Indices to a primitive for use in rendering. + * @param primitive The primitive to buffer indices into. + * @param position The position (index) to overwrite the indices of. + * @param count The count of indices to buffer. + * @param indices Array of indices to buffer into the primitive. + */ void primitiveBufferIndices(primitive_t *primitive, - int32_t position, int32_t count, int32_t *indices + int32_t position, int32_t count, indice_t *indices ); +/** + * Draw a primitive. Primitives are drawn by their indices. + * @param primitive Primitive to draw. + * @param start Start indice (index) to draw. + * @param count Count of indices to draw. + */ void primitiveDraw(primitive_t *primitive, int32_t start, int32_t count); +/** + * Cleanup a previously created primitive. + * @param primitive Primitive to cleanup. + */ void primitiveDispose(primitive_t *primitive); \ No newline at end of file diff --git a/src/engine/display/render.c b/src/engine/display/render.c index bbb11da4..2231bdbf 100644 --- a/src/engine/display/render.c +++ b/src/engine/display/render.c @@ -7,6 +7,8 @@ #include "render.h" +primitive_t *primitive; + render_t * renderInit(char *name) { // Initialize the renderer render_t *render = malloc(sizeof(render_t)); @@ -21,24 +23,35 @@ render_t * renderInit(char *name) { glDepthMask(GL_TRUE); glDepthFunc(GL_LEQUAL); + // Test + for(int i = 0; i < 100; i++) { + vertice_t *vertices = malloc(sizeof(vertice_t) * 4); + indice_t *indices = malloc(sizeof(indice_t) * 6); + + vertices[0].x = -1, vertices[0].y = -1, vertices[0].z = 0; + vertices[1].x = 1, vertices[1].y = -1, vertices[1].z = 0; + vertices[2].x = -1, vertices[2].y = 1, vertices[2].z = 0; + vertices[3].x = 1, vertices[3].y = 1, vertices[3].z = 0; + + indices[0] = (indice_t)0; + indices[1] = (indice_t)1; + indices[2] = (indice_t)2; + + indices[3] = (indice_t)1; + indices[4] = (indice_t)2; + indices[5] = (indice_t)3; + + primitive = primitiveCreate(4, 6); + primitiveBufferVertices(primitive, 0, 4, vertices); + primitiveBufferIndices(primitive, 0, 6, indices); + } + return render; } void renderFrame(render_t *render) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - float z = 0.0f; - - glBegin(GL_TRIANGLES); - glColor3f(1, 0, 0); - glVertex3f(-1, -1, z); - - glColor3f(0, 1, 0); - glVertex3f(0, 1, z); - - glColor3f(0, 0, 1); - glVertex3f(1, -1, z); - glEnd(); + primitiveDraw(primitive, 0, 6); } bool renderDispose(render_t *render) { diff --git a/src/engine/display/render.h b/src/engine/display/render.h index 3988e960..abade8ff 100644 --- a/src/engine/display/render.h +++ b/src/engine/display/render.h @@ -13,6 +13,7 @@ #include #include #include +#include "primitive.h" /** * Contains information about the current render state, can be used for querying