From 077bef42861644f1f25070d23363a1bfa2e7a4a6 Mon Sep 17 00:00:00 2001 From: Ray Date: Sat, 24 Feb 2018 12:31:32 +0100 Subject: [PATCH] Support 4 components mesh.tangent data Added struct Vector4 for convenience --- src/models.c | 23 ++++++++++++++--------- src/raylib.h | 10 +++++++++- src/rlgl.c | 14 +++++++------- src/rlgl.h | 2 +- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/models.c b/src/models.c index 02e69c0c0..f3a168c98 100644 --- a/src/models.c +++ b/src/models.c @@ -2329,12 +2329,12 @@ static Mesh LoadOBJ(const char *fileName) else { // Attempt to calculate mesh tangents and binormals using positions and texture coordinates - mesh.tangents = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.tangents = (float *)malloc(mesh.vertexCount*4*sizeof(float)); // mesh.binormals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); int vCount = 0; int uvCount = 0; - while (vCount < mesh.vertexCount*3) + while (vCount < mesh.vertexCount*4) { // Calculate mesh vertex positions as Vector3 Vector3 v0 = { mesh.vertices[vCount], mesh.vertices[vCount + 1], mesh.vertices[vCount + 2] }; @@ -2363,17 +2363,22 @@ static Mesh LoadOBJ(const char *fileName) // Calculate vertex tangent Vector3 tangent = Vector3Subtract(t1, t2); Vector3Scale(&tangent, r); + + // TODO: Calculate tangent 4th component // Apply calculated tangents data to mesh struct mesh.tangents[vCount + 0] = tangent.x; mesh.tangents[vCount + 1] = tangent.y; mesh.tangents[vCount + 2] = tangent.z; - mesh.tangents[vCount + 3] = tangent.x; - mesh.tangents[vCount + 4] = tangent.y; - mesh.tangents[vCount + 5] = tangent.z; - mesh.tangents[vCount + 6] = tangent.x; - mesh.tangents[vCount + 7] = tangent.y; - mesh.tangents[vCount + 8] = tangent.z; + mesh.tangents[vCount + 3] = 0.0f; + mesh.tangents[vCount + 4] = tangent.x; + mesh.tangents[vCount + 5] = tangent.y; + mesh.tangents[vCount + 6] = tangent.z; + mesh.tangents[vCount + 7] = 0.0f; + mesh.tangents[vCount + 8] = tangent.x; + mesh.tangents[vCount + 9] = tangent.y; + mesh.tangents[vCount + 10] = tangent.z; + mesh.tangents[vCount + 11] = 0.0f; // TODO: add binormals to mesh struct and assign buffers id and locations properly /* // Calculate vertex binormal @@ -2392,7 +2397,7 @@ static Mesh LoadOBJ(const char *fileName) mesh.binormals[vCount + 8] = binormal.z; */ // Update vertex position and texture coordinates counters - vCount += 9; + vCount += 12; uvCount += 6; } } diff --git a/src/raylib.h b/src/raylib.h index 782dd4e5b..41755dc34 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -322,6 +322,14 @@ typedef struct Vector3 { float z; } Vector3; +// Vector4 type +typedef struct Vector4 { + float x; + float y; + float z; + float w; +} Vector4; + // Matrix type (OpenGL style 4x4 - right handed, column major) typedef struct Matrix { float m0, m4, m8, m12; @@ -422,7 +430,7 @@ typedef struct Mesh { 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 *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 (XYZW - 4 components per vertex) (shader-location = 4) unsigned char *colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) unsigned short *indices;// Vertex indices (in case vertex data comes indexed) diff --git a/src/rlgl.c b/src/rlgl.c index 2cb9d54e7..46e0668d9 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1782,14 +1782,14 @@ void rlLoadMesh(Mesh *mesh, bool dynamic) { glGenBuffers(1, &mesh->vboId[4]); glBindBuffer(GL_ARRAY_BUFFER, mesh->vboId[4]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->tangents, drawHint); - glVertexAttribPointer(4, 3, GL_FLOAT, 0, 0, 0); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*mesh->vertexCount, mesh->tangents, drawHint); + glVertexAttribPointer(4, 4, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(4); } else { // Default tangents vertex attribute - glVertexAttrib3f(4, 0.0f, 0.0f, 0.0f); + glVertexAttrib4f(4, 0.0f, 0.0f, 0.0f, 0.0f); glDisableVertexAttribArray(4); } @@ -1804,7 +1804,7 @@ void rlLoadMesh(Mesh *mesh, bool dynamic) } else { - // Default tangents vertex attribute + // Default texcoord2 vertex attribute glVertexAttrib2f(5, 0.0f, 0.0f); glDisableVertexAttribArray(5); } @@ -1868,8 +1868,8 @@ void rlUpdateMesh(Mesh mesh, int buffer, int numVertex) case 4: // Update tangents (vertex tangents) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); - if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*numVertex, mesh.tangents, GL_DYNAMIC_DRAW); - else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*numVertex, mesh.tangents); + if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*numVertex, mesh.tangents, GL_DYNAMIC_DRAW); + else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*4*numVertex, mesh.tangents); } break; case 5: // Update texcoords2 (vertex second texture coordinates) { @@ -2019,7 +2019,7 @@ void rlDrawMesh(Mesh mesh, Material material, Matrix transform) if (material.shader.locs[LOC_VERTEX_TANGENT] != -1) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); - glVertexAttribPointer(material.shader.locs[LOC_VERTEX_TANGENT], 3, GL_FLOAT, 0, 0, 0); + glVertexAttribPointer(material.shader.locs[LOC_VERTEX_TANGENT], 4, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(material.shader.locs[LOC_VERTEX_TANGENT]); } diff --git a/src/rlgl.h b/src/rlgl.h index 68d8d4eb6..94331389f 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -187,7 +187,7 @@ typedef unsigned char byte; 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 *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 (XYZW - 4 components per vertex) (shader-location = 4) unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3) unsigned short *indices;// vertex indices (in case vertex data comes indexed)