Added support for indexed mesh data
This commit is contained in:
parent
ac44db26a2
commit
1ddf594d15
3 changed files with 50 additions and 17 deletions
19
src/models.c
19
src/models.c
|
@ -567,7 +567,9 @@ Model LoadModelEx(Mesh data)
|
||||||
{
|
{
|
||||||
Model model = { 0 };
|
Model model = { 0 };
|
||||||
|
|
||||||
rlglLoadMesh(&data); // Upload vertex data to GPU
|
model.mesh = data;
|
||||||
|
|
||||||
|
rlglLoadMesh(&model.mesh); // Upload vertex data to GPU
|
||||||
|
|
||||||
model.transform = MatrixIdentity();
|
model.transform = MatrixIdentity();
|
||||||
model.material = LoadDefaultMaterial();
|
model.material = LoadDefaultMaterial();
|
||||||
|
@ -693,12 +695,13 @@ Model LoadCubicmap(Image cubicmap)
|
||||||
void UnloadModel(Model model)
|
void UnloadModel(Model model)
|
||||||
{
|
{
|
||||||
// Unload mesh data
|
// Unload mesh data
|
||||||
free(model.mesh.vertices);
|
if (model.mesh.vertices != NULL) free(model.mesh.vertices);
|
||||||
free(model.mesh.texcoords);
|
if (model.mesh.texcoords != NULL) free(model.mesh.texcoords);
|
||||||
if (model.mesh.normals != NULL) free(model.mesh.normals);
|
if (model.mesh.normals != NULL) free(model.mesh.normals);
|
||||||
if (model.mesh.colors != NULL) free(model.mesh.colors);
|
if (model.mesh.colors != NULL) free(model.mesh.colors);
|
||||||
if (model.mesh.tangents != NULL) free(model.mesh.tangents);
|
if (model.mesh.tangents != NULL) free(model.mesh.tangents);
|
||||||
if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2);
|
if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2);
|
||||||
|
if (model.mesh.indices != NULL) free(model.mesh.indices);
|
||||||
|
|
||||||
TraceLog(INFO, "Unloaded model data from RAM (CPU)");
|
TraceLog(INFO, "Unloaded model data from RAM (CPU)");
|
||||||
|
|
||||||
|
@ -708,8 +711,11 @@ void UnloadModel(Model model)
|
||||||
rlDeleteBuffers(model.mesh.vboId[3]); // colors
|
rlDeleteBuffers(model.mesh.vboId[3]); // colors
|
||||||
rlDeleteBuffers(model.mesh.vboId[4]); // tangents
|
rlDeleteBuffers(model.mesh.vboId[4]); // tangents
|
||||||
rlDeleteBuffers(model.mesh.vboId[5]); // texcoords2
|
rlDeleteBuffers(model.mesh.vboId[5]); // texcoords2
|
||||||
|
rlDeleteBuffers(model.mesh.vboId[6]); // indices
|
||||||
|
|
||||||
rlDeleteVertexArrays(model.mesh.vaoId);
|
rlDeleteVertexArrays(model.mesh.vaoId);
|
||||||
|
|
||||||
|
UnloadMaterial(model.material);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load material data (from file)
|
// Load material data (from file)
|
||||||
|
@ -743,6 +749,13 @@ Material LoadDefaultMaterial(void)
|
||||||
return material;
|
return material;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UnloadMaterial(Material material)
|
||||||
|
{
|
||||||
|
rlDeleteTextures(material.texDiffuse.id);
|
||||||
|
rlDeleteTextures(material.texNormal.id);
|
||||||
|
rlDeleteTextures(material.texSpecular.id);
|
||||||
|
}
|
||||||
|
|
||||||
// Link a texture to a model
|
// Link a texture to a model
|
||||||
void SetModelTexture(Model *model, Texture2D texture)
|
void SetModelTexture(Model *model, Texture2D texture)
|
||||||
{
|
{
|
||||||
|
|
|
@ -368,18 +368,20 @@ typedef struct BoundingBox {
|
||||||
|
|
||||||
// Vertex data definning a mesh
|
// Vertex data definning a mesh
|
||||||
typedef struct Mesh {
|
typedef struct Mesh {
|
||||||
int vertexCount; // num vertices
|
int vertexCount; // number of vertices stored in arrays
|
||||||
float *vertices; // vertex position (XYZ - 3 components per vertex) (shader-location = 0)
|
float *vertices; // vertex position (XYZ - 3 components per vertex) (shader-location = 0)
|
||||||
float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
|
float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
|
||||||
float *texcoords2; // vertex second texture coordinates (useful for lightmaps) (shader-location = 5)
|
float *texcoords2; // vertex second texture coordinates (useful for lightmaps) (shader-location = 5)
|
||||||
float *normals; // vertex normals (XYZ - 3 components per vertex) (shader-location = 2)
|
float *normals; // vertex normals (XYZ - 3 components per vertex) (shader-location = 2)
|
||||||
float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4)
|
float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4)
|
||||||
unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
|
unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
|
||||||
|
unsigned short *indices; // vertex indices (in case vertex data comes indexed)
|
||||||
|
int trianglesCount; // number of triangles to draw
|
||||||
|
|
||||||
BoundingBox bounds; // mesh limits defined by min and max points
|
BoundingBox bounds; // mesh limits defined by min and max points
|
||||||
|
|
||||||
unsigned int vaoId; // OpenGL Vertex Array Object id
|
unsigned int vaoId; // OpenGL Vertex Array Object id
|
||||||
unsigned int vboId[6]; // OpenGL Vertex Buffer Objects id (6 types of vertex data)
|
unsigned int vboId[7]; // OpenGL Vertex Buffer Objects id (7 types of vertex data)
|
||||||
} Mesh;
|
} Mesh;
|
||||||
|
|
||||||
// Shader type (generic shader)
|
// Shader type (generic shader)
|
||||||
|
@ -813,6 +815,7 @@ void SetModelTexture(Model *model, Texture2D texture); // Link a textur
|
||||||
|
|
||||||
Material LoadMaterial(const char *fileName); // Load material data (from file)
|
Material LoadMaterial(const char *fileName); // Load material data (from file)
|
||||||
Material LoadDefaultMaterial(void); // Load default material (uses default models shader)
|
Material LoadDefaultMaterial(void); // Load default material (uses default models shader)
|
||||||
|
void UnloadMaterial(Material material); // Unload material textures from VRAM
|
||||||
|
|
||||||
void DrawModel(Model model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set)
|
void DrawModel(Model model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set)
|
||||||
void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model with extended parameters
|
void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model with extended parameters
|
||||||
|
|
35
src/rlgl.c
35
src/rlgl.c
|
@ -783,16 +783,16 @@ void rlDisableDepthTest(void)
|
||||||
// Unload texture from GPU memory
|
// Unload texture from GPU memory
|
||||||
void rlDeleteTextures(unsigned int id)
|
void rlDeleteTextures(unsigned int id)
|
||||||
{
|
{
|
||||||
glDeleteTextures(1, &id);
|
if (id != 0) glDeleteTextures(1, &id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload render texture from GPU memory
|
// Unload render texture from GPU memory
|
||||||
void rlDeleteRenderTextures(RenderTexture2D target)
|
void rlDeleteRenderTextures(RenderTexture2D target)
|
||||||
{
|
{
|
||||||
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
||||||
glDeleteFramebuffers(1, &target.id);
|
if (target.id != 0) glDeleteFramebuffers(1, &target.id);
|
||||||
glDeleteTextures(1, &target.texture.id);
|
if (target.texture.id != 0) glDeleteTextures(1, &target.texture.id);
|
||||||
glDeleteTextures(1, &target.depth.id);
|
if (target.depth.id != 0) glDeleteTextures(1, &target.depth.id);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -800,7 +800,7 @@ void rlDeleteRenderTextures(RenderTexture2D target)
|
||||||
void rlDeleteShader(unsigned int id)
|
void rlDeleteShader(unsigned int id)
|
||||||
{
|
{
|
||||||
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
||||||
glDeleteProgram(id);
|
if (id != 0) glDeleteProgram(id);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -810,7 +810,7 @@ void rlDeleteVertexArrays(unsigned int id)
|
||||||
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
||||||
if (vaoSupported)
|
if (vaoSupported)
|
||||||
{
|
{
|
||||||
glDeleteVertexArrays(1, &id);
|
if (id != 0) glDeleteVertexArrays(1, &id);
|
||||||
TraceLog(INFO, "[VAO ID %i] Unloaded model data from VRAM (GPU)", id);
|
TraceLog(INFO, "[VAO ID %i] Unloaded model data from VRAM (GPU)", id);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1204,10 +1204,13 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires)
|
||||||
glVertexAttribPointer(material.shader.texcoord2Loc, 2, GL_FLOAT, 0, 0, 0);
|
glVertexAttribPointer(material.shader.texcoord2Loc, 2, GL_FLOAT, 0, 0, 0);
|
||||||
glEnableVertexAttribArray(material.shader.texcoord2Loc);
|
glEnableVertexAttribArray(material.shader.texcoord2Loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw call!
|
// Draw call!
|
||||||
glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount);
|
if (mesh.indices != NULL) glDrawElements(GL_TRIANGLES, mesh.trianglesCount*3, GL_UNSIGNED_SHORT, 0); // Indexed vertices draw
|
||||||
|
else glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount);
|
||||||
|
|
||||||
if (material.texNormal.id != 0)
|
if (material.texNormal.id != 0)
|
||||||
{
|
{
|
||||||
|
@ -1225,7 +1228,11 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires)
|
||||||
glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures
|
glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures
|
||||||
|
|
||||||
if (vaoSupported) glBindVertexArray(0); // Unbind VAO
|
if (vaoSupported) glBindVertexArray(0); // Unbind VAO
|
||||||
else glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBOs
|
else
|
||||||
|
{
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBOs
|
||||||
|
if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
glUseProgram(0); // Unbind shader program
|
glUseProgram(0); // Unbind shader program
|
||||||
#endif
|
#endif
|
||||||
|
@ -1682,11 +1689,12 @@ void rlglLoadMesh(Mesh *mesh)
|
||||||
mesh->vboId[3] = 0; // Vertex colors VBO
|
mesh->vboId[3] = 0; // Vertex colors VBO
|
||||||
mesh->vboId[4] = 0; // Vertex tangents VBO
|
mesh->vboId[4] = 0; // Vertex tangents VBO
|
||||||
mesh->vboId[5] = 0; // Vertex texcoords2 VBO
|
mesh->vboId[5] = 0; // Vertex texcoords2 VBO
|
||||||
|
mesh->vboId[6] = 0; // Vertex indices VBO
|
||||||
|
|
||||||
|
|
||||||
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
||||||
GLuint vaoId = 0; // Vertex Array Objects (VAO)
|
GLuint vaoId = 0; // Vertex Array Objects (VAO)
|
||||||
GLuint vboId[6]; // Vertex Buffer Objects (VBOs)
|
GLuint vboId[7]; // Vertex Buffer Objects (VBOs)
|
||||||
|
|
||||||
if (vaoSupported)
|
if (vaoSupported)
|
||||||
{
|
{
|
||||||
|
@ -1775,12 +1783,21 @@ void rlglLoadMesh(Mesh *mesh)
|
||||||
glDisableVertexAttribArray(5);
|
glDisableVertexAttribArray(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mesh->indices != NULL)
|
||||||
|
{
|
||||||
|
glGenBuffers(1, &vboId[6]);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId[6]);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*mesh->trianglesCount*3, mesh->indices, GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
mesh->vboId[0] = vboId[0]; // Vertex position VBO
|
mesh->vboId[0] = vboId[0]; // Vertex position VBO
|
||||||
mesh->vboId[1] = vboId[1]; // Texcoords VBO
|
mesh->vboId[1] = vboId[1]; // Texcoords VBO
|
||||||
mesh->vboId[2] = vboId[2]; // Normals VBO
|
mesh->vboId[2] = vboId[2]; // Normals VBO
|
||||||
mesh->vboId[3] = vboId[3]; // Colors VBO
|
mesh->vboId[3] = vboId[3]; // Colors VBO
|
||||||
mesh->vboId[4] = vboId[4]; // Tangents VBO
|
mesh->vboId[4] = vboId[4]; // Tangents VBO
|
||||||
mesh->vboId[5] = vboId[5]; // Texcoords2 VBO
|
mesh->vboId[5] = vboId[5]; // Texcoords2 VBO
|
||||||
|
mesh->vboId[6] = vboId[6]; // Indices VBO
|
||||||
|
|
||||||
if (vaoSupported)
|
if (vaoSupported)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue