/** * Copyright (c) 2023 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #pragma once #include "dawn.h" #include "dawnopengl.h" #include "colorgl.h" extern int32_t MESH_ACTIVE_COUNT; /** Indice that references a specific vertice */ typedef int32_t meshindice_t; typedef enum { /** Draw the mesh as a list of points */ MESH_DRAW_MODE_POINTS = GL_POINTS, /** Draw the mesh as a list of lines */ MESH_DRAW_MODE_LINES = GL_LINES, /** Draw the mesh as a list of triangles */ MESH_DRAW_MODE_TRIANGLES = GL_TRIANGLES } meshdrawmode_t; typedef struct { /** Pointer to the vertex buffer on the GPU */ GLuint vertexBuffer; /** Pointer to the index buffer on the GPU */ GLuint indexBuffer; /** Pointer to the vertex buffer on the GPU */ GLuint vertexArray; /** How many vertices are in the mesh */ int32_t verticeCount; /** How many indices are in the mesh */ int32_t indiceCount; } mesh_t; /** * Initializes a mesh to a default state. Mesh's must be initialized and * destroyed. * * @param mesh Mesh to initialize. */ void meshInit(mesh_t *mesh); /** * Create a new set of buffers for the mesh to use. * * @param mesh Mesh to create buffers for. * @param verticeCount How many Vertices will this buffer support. * @param indiceCount How many Indices will this buffer support. */ void meshCreateBuffers( mesh_t *mesh, const int32_t verticeCount, const int32_t indiceCount ); /** * Cleanup the buffers on a given mesh. This is useful if you intend to expand * the count of vertices your mesh supports. * * @param mesh Mesh to destroy buffers for. */ void meshDestroyBuffers(mesh_t *mesh); /** * Write vertice coordinates to the mesh. * * @param mesh Mesh to write to. * @param positions Array of coordinates to write. * @param position Position, within the buffer, to write to. * @param count How many coordinates are in the array. */ void meshBufferPositions( const mesh_t *mesh, const vec3_t *positions, const size_t position, const size_t count ); /** * Write texture coordinates to the mesh. * * @param mesh Mesh to write to. * @param coordinates Array of coordinates to write. * @param position Position, within the buffer, to write to. * @param count How many coordinates are in the array. */ void meshBufferCoordinates( const mesh_t *mesh, const vec2_t *coordinates, const size_t position, const size_t count ); /** * Write colors to the mesh. * * @param mesh Mesh to write to. * @param colors Array of colors to write. * @param position Position, within the buffer, to write to. * @param count How many colors are in the array. */ void meshBufferColors( const mesh_t *mesh, const color4f_t *colors, const size_t position, const size_t count ); /** * Write indices to the mesh. * * @param mesh Mesh to write to. * @param indices Array of indices to write. * @param position Position, within the buffer, to write to. * @param count How many indices are in the array. */ void meshBufferIndices( const mesh_t *mesh, const meshindice_t *indices, const size_t position, const size_t count ); /** * Draw a mesh. Meshes are drawn by their indices. * * @param mesh Mesh to draw. * @param mode Which drawing mode to use to draw the mesh. * @param indiceStart Start indice (index) to draw. * @param indiceCount Count of indices to draw. Use -1 to draw all. */ void meshDraw( const mesh_t *mesh, const meshdrawmode_t mode, const int32_t indiceStart, const int32_t indiceCount ); /** * Cleanup a mesh. This will destroy the buffers and free the mesh. * * @param mesh Mesh to cleanup. */ void meshDispose(mesh_t *mesh);