Support 4 components mesh.tangent data
Added struct Vector4 for convenience
This commit is contained in:
parent
6d64327a87
commit
077bef4286
4 changed files with 31 additions and 18 deletions
23
src/models.c
23
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] };
|
||||
|
@ -2364,16 +2364,21 @@ static Mesh LoadOBJ(const char *fileName)
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
10
src/raylib.h
10
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)
|
||||
|
||||
|
|
14
src/rlgl.c
14
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]);
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue