Start reviewing IQM loader...

This commit is contained in:
Ray 2018-07-13 20:29:23 +02:00
parent cadcc05a84
commit 971a7419ec
2 changed files with 44 additions and 44 deletions

View file

@ -12,7 +12,6 @@
#include "raylib.h" #include "raylib.h"
#define RIQM_IMPLEMENTATION #define RIQM_IMPLEMENTATION
#define RAYMATH_IMPLEMENTATION
#include "riqm.h" #include "riqm.h"
int main() int main()
@ -34,7 +33,7 @@ int main()
// Load the animated model mesh and basic data // Load the animated model mesh and basic data
AnimatedModel model = LoadAnimatedModel("resources/guy.iqm"); AnimatedModel model = LoadAnimatedModel("resources/guy.iqm");
printf("VER %i\n",rlGetVersion());
// Load model texture and set material // Load model texture and set material
// NOTE: There is only 1 mesh and 1 material (both at index 0), thats what the 2 0's are // NOTE: There is only 1 mesh and 1 material (both at index 0), thats what the 2 0's are
model = AnimatedModelAddTexture(model, "resources/guytex.png"); // REPLACE! model = AnimatedModelAddTexture(model, "resources/guytex.png"); // REPLACE!

View file

@ -34,13 +34,6 @@
#ifndef RIQM_H #ifndef RIQM_H
#define RIQM_H #define RIQM_H
// TODO dont break everything
static bool vaoSupported = false;
static Matrix modelview;
static Matrix projection;
#include <stdio.h> // Required for: FILE
//#define RIQM_STATIC //#define RIQM_STATIC
#ifdef RIQM_STATIC #ifdef RIQM_STATIC
#define RIQMDEF static // Functions just visible to module including this file #define RIQMDEF static // Functions just visible to module including this file
@ -81,23 +74,30 @@ typedef struct Animation {
} Animation; } Animation;
typedef struct AnimatedMesh { typedef struct AnimatedMesh {
char name[MESH_NAME_LENGTH];
//Mesh mesh;
// Mesh struct defines:
//-------------------------
int vertexCount; int vertexCount;
int triangleCount; int triangleCount;
float *vertices; float *vertices;
float *normals; float *normals;
float *texcoords; float *texcoords;
float *animVertices; unsigned short *triangles; //equivalent to mes.indices
float *animNormals;
unsigned short *triangles;
int *weightId;
float *weightBias;
unsigned int vaoId; unsigned int vaoId;
unsigned int vboId[7]; unsigned int vboId[7];
//-------------------------
char name[MESH_NAME_LENGTH];
float *animVertices;
float *animNormals;
float *weightBias;
int *weightId;
} AnimatedMesh; } AnimatedMesh;
typedef struct AnimatedModel { typedef struct AnimatedModel {
@ -155,7 +155,7 @@ RIQMDEF void DrawAnimatedModelEx(AnimatedModel model,Vector3 position,Vector3 ro
#include "raymath.h" // Required for: Vector3, Quaternion functions #include "raymath.h" // Required for: Vector3, Quaternion functions
#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2 #include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2
#include "glad.h" #include "glad.h" // Required for OpenGL functions > TO BE REMOVED!
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -193,17 +193,6 @@ typedef struct IQMMesh {
unsigned int first_triangle, num_triangles; unsigned int first_triangle, num_triangles;
} IQMMesh; } IQMMesh;
typedef enum {
IQM_POSITION = 0,
IQM_TEXCOORD = 1,
IQM_NORMAL = 2,
IQM_TANGENT = 3, // tangents unused by default
IQM_BLENDINDEXES = 4,
IQM_BLENDWEIGHTS = 5,
IQM_COLOR = 6, // vertex colors unused by default
IQM_CUSTOM = 0x10 // custom vertex values unused by default
} IQMVertexType;
typedef struct IQMTriangle { typedef struct IQMTriangle {
unsigned int vertex[3]; unsigned int vertex[3];
} IQMTriangle; } IQMTriangle;
@ -245,6 +234,18 @@ typedef struct IQMBounds { // bounds unused by default
float xyradius, radius; float xyradius, radius;
} IQMBounds; } IQMBounds;
typedef enum {
IQM_POSITION = 0,
IQM_TEXCOORD = 1,
IQM_NORMAL = 2,
IQM_TANGENT = 3, // tangents unused by default
IQM_BLENDINDEXES = 4,
IQM_BLENDWEIGHTS = 5,
IQM_COLOR = 6, // vertex colors unused by default
IQM_CUSTOM = 0x10 // custom vertex values unused by default
} IQMVertexType;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Global Variables Definition // Global Variables Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -289,7 +290,7 @@ TraceLog(LOG_WARNING, "OGL ES2");
int drawHint = GL_STATIC_DRAW; int drawHint = GL_STATIC_DRAW;
if (dynamic) drawHint = GL_DYNAMIC_DRAW; if (dynamic) drawHint = GL_DYNAMIC_DRAW;
if (vaoSupported) //if (vaoSupported)
{ {
// Initialize Quads VAO (Buffer A) // Initialize Quads VAO (Buffer A)
glGenVertexArrays(1, &amesh->vaoId); glGenVertexArrays(1, &amesh->vaoId);
@ -400,15 +401,8 @@ TraceLog(LOG_WARNING, "OGL ES2");
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*amesh->triangleCount*3, amesh->triangles, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*amesh->triangleCount*3, amesh->triangles, GL_STATIC_DRAW);
} }
if (vaoSupported) if (amesh->vaoId > 0) TraceLog(LOG_INFO, "[VAO ID %i] Mesh uploaded successfully to VRAM (GPU)", amesh->vaoId);
{ else TraceLog(LOG_WARNING, "Mesh could not be uploaded to VRAM (GPU)");
if (amesh->vaoId > 0) TraceLog(LOG_INFO, "[VAO ID %i] Mesh uploaded successfully to VRAM (GPU)", amesh->vaoId);
else TraceLog(LOG_WARNING, "Mesh could not be uploaded to VRAM (GPU)");
}
else
{
TraceLog(LOG_INFO, "[VBOs] Mesh uploaded successfully to VRAM (GPU)");
}
#endif #endif
} }
@ -443,7 +437,7 @@ void rlUpdateAnimatedMesh(AnimatedMesh *amesh)
{ {
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Activate mesh VAO // Activate mesh VAO
if (vaoSupported) glBindVertexArray(amesh->vaoId); glBindVertexArray(amesh->vaoId);
// Update positions data // Update positions data
glBindBuffer(GL_ARRAY_BUFFER, amesh->vboId[0]); glBindBuffer(GL_ARRAY_BUFFER, amesh->vboId[0]);
@ -454,7 +448,7 @@ void rlUpdateAnimatedMesh(AnimatedMesh *amesh)
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*amesh->vertexCount, amesh->animNormals); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*amesh->vertexCount, amesh->animNormals);
// Unbind the current VAO // Unbind the current VAO
if (vaoSupported) glBindVertexArray(0); glBindVertexArray(0);
//mesh.vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); //mesh.vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
// Now we can modify vertices // Now we can modify vertices
@ -523,12 +517,12 @@ void rlDrawAnimatedMesh(AnimatedMesh amesh, Material material, Matrix transform)
(float)material.maps[MAP_SPECULAR].color.b/255.0f, (float)material.maps[MAP_SPECULAR].color.b/255.0f,
(float)material.maps[MAP_SPECULAR].color.a/255.0f); (float)material.maps[MAP_SPECULAR].color.a/255.0f);
if (material.shader.locs[LOC_MATRIX_VIEW] != -1) SetShaderValueMatrix(material.shader, material.shader.locs[LOC_MATRIX_VIEW], modelview); if (material.shader.locs[LOC_MATRIX_VIEW] != -1) SetShaderValueMatrix(material.shader, material.shader.locs[LOC_MATRIX_VIEW], GetMatrixModelview());
if (material.shader.locs[LOC_MATRIX_PROJECTION] != -1) SetShaderValueMatrix(material.shader, material.shader.locs[LOC_MATRIX_PROJECTION], projection); if (material.shader.locs[LOC_MATRIX_PROJECTION] != -1) SetShaderValueMatrix(material.shader, material.shader.locs[LOC_MATRIX_PROJECTION], projection);
// At this point the modelview matrix just contains the view matrix (camera) // At this point the modelview matrix just contains the view matrix (camera)
// That's because BeginMode3D() sets it an no model-drawing function modifies it, all use rlPushMatrix() and rlPopMatrix() // That's because BeginMode3D() sets it an no model-drawing function modifies it, all use rlPushMatrix() and rlPopMatrix()
Matrix matView = modelview; // View matrix (camera) Matrix matView = GetMatrixModelview(); // View matrix (camera)
Matrix matProjection = projection; // Projection matrix (perspective) Matrix matProjection = projection; // Projection matrix (perspective)
// Calculate model-view matrix combining matModel and matView // Calculate model-view matrix combining matModel and matView
@ -547,7 +541,10 @@ void rlDrawAnimatedMesh(AnimatedMesh amesh, Material material, Matrix transform)
glUniform1i(material.shader.locs[LOC_MAP_DIFFUSE + i], i); glUniform1i(material.shader.locs[LOC_MAP_DIFFUSE + i], i);
} }
} }
glBindVertexArray(amesh.vaoId);
/*
// Bind vertex array objects (or VBOs) // Bind vertex array objects (or VBOs)
if (vaoSupported) glBindVertexArray(amesh.vaoId); if (vaoSupported) glBindVertexArray(amesh.vaoId);
else else
@ -608,6 +605,7 @@ void rlDrawAnimatedMesh(AnimatedMesh amesh, Material material, Matrix transform)
if (amesh.triangles != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, amesh.vboId[6]); if (amesh.triangles != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, amesh.vboId[6]);
} }
*/
int eyesCount = 1; int eyesCount = 1;
#if defined(SUPPORT_VR_SIMULATOR) #if defined(SUPPORT_VR_SIMULATOR)
@ -640,6 +638,9 @@ void rlDrawAnimatedMesh(AnimatedMesh amesh, Material material, Matrix transform)
else glBindTexture(GL_TEXTURE_2D, 0); // Unbind current active texture else glBindTexture(GL_TEXTURE_2D, 0); // Unbind current active texture
} }
glBindVertexArray(0);
/*
// Unind vertex array objects (or VBOs) // Unind vertex array objects (or VBOs)
if (vaoSupported) glBindVertexArray(0); if (vaoSupported) glBindVertexArray(0);
else else
@ -647,7 +648,7 @@ void rlDrawAnimatedMesh(AnimatedMesh amesh, Material material, Matrix transform)
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
if (amesh.triangles != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); if (amesh.triangles != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
*/
// Unbind shader program // Unbind shader program
glUseProgram(0); glUseProgram(0);