Przeglądaj źródła

Support 4 components mesh.tangent data

Added struct Vector4 for convenience
pull/480/head
Ray 7 lat temu
rodzic
commit
077bef4286
4 zmienionych plików z 31 dodań i 18 usunięć
  1. +14
    -9
      src/models.c
  2. +9
    -1
      src/raylib.h
  3. +7
    -7
      src/rlgl.c
  4. +1
    -1
      src/rlgl.h

+ 14
- 9
src/models.c Wyświetl plik

@ -2329,12 +2329,12 @@ static Mesh LoadOBJ(const char *fileName)
else else
{ {
// Attempt to calculate mesh tangents and binormals using positions and texture coordinates // 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)); // mesh.binormals = (float *)malloc(mesh.vertexCount*3*sizeof(float));
int vCount = 0; int vCount = 0;
int uvCount = 0; int uvCount = 0;
while (vCount < mesh.vertexCount*3)
while (vCount < mesh.vertexCount*4)
{ {
// Calculate mesh vertex positions as Vector3 // Calculate mesh vertex positions as Vector3
Vector3 v0 = { mesh.vertices[vCount], mesh.vertices[vCount + 1], mesh.vertices[vCount + 2] }; 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 // Calculate vertex tangent
Vector3 tangent = Vector3Subtract(t1, t2); Vector3 tangent = Vector3Subtract(t1, t2);
Vector3Scale(&tangent, r); Vector3Scale(&tangent, r);
// TODO: Calculate tangent 4th component
// Apply calculated tangents data to mesh struct // Apply calculated tangents data to mesh struct
mesh.tangents[vCount + 0] = tangent.x; mesh.tangents[vCount + 0] = tangent.x;
mesh.tangents[vCount + 1] = tangent.y; mesh.tangents[vCount + 1] = tangent.y;
mesh.tangents[vCount + 2] = tangent.z; 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 // TODO: add binormals to mesh struct and assign buffers id and locations properly
/* // Calculate vertex binormal /* // Calculate vertex binormal
@ -2392,7 +2397,7 @@ static Mesh LoadOBJ(const char *fileName)
mesh.binormals[vCount + 8] = binormal.z; */ mesh.binormals[vCount + 8] = binormal.z; */
// Update vertex position and texture coordinates counters // Update vertex position and texture coordinates counters
vCount += 9;
vCount += 12;
uvCount += 6; uvCount += 6;
} }
} }

+ 9
- 1
src/raylib.h Wyświetl plik

@ -322,6 +322,14 @@ typedef struct Vector3 {
float z; float z;
} Vector3; } Vector3;
// Vector4 type
typedef struct Vector4 {
float x;
float y;
float z;
float w;
} Vector4;
// Matrix type (OpenGL style 4x4 - right handed, column major) // Matrix type (OpenGL style 4x4 - right handed, column major)
typedef struct Matrix { typedef struct Matrix {
float m0, m4, m8, m12; 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 *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 (XYZW - 4 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) unsigned short *indices;// Vertex indices (in case vertex data comes indexed)

+ 7
- 7
src/rlgl.c Wyświetl plik

@ -1782,14 +1782,14 @@ void rlLoadMesh(Mesh *mesh, bool dynamic)
{ {
glGenBuffers(1, &mesh->vboId[4]); glGenBuffers(1, &mesh->vboId[4]);
glBindBuffer(GL_ARRAY_BUFFER, 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); glEnableVertexAttribArray(4);
} }
else else
{ {
// Default tangents vertex attribute // Default tangents vertex attribute
glVertexAttrib3f(4, 0.0f, 0.0f, 0.0f);
glVertexAttrib4f(4, 0.0f, 0.0f, 0.0f, 0.0f);
glDisableVertexAttribArray(4); glDisableVertexAttribArray(4);
} }
@ -1804,7 +1804,7 @@ void rlLoadMesh(Mesh *mesh, bool dynamic)
} }
else else
{ {
// Default tangents vertex attribute
// Default texcoord2 vertex attribute
glVertexAttrib2f(5, 0.0f, 0.0f); glVertexAttrib2f(5, 0.0f, 0.0f);
glDisableVertexAttribArray(5); glDisableVertexAttribArray(5);
} }
@ -1868,8 +1868,8 @@ void rlUpdateMesh(Mesh mesh, int buffer, int numVertex)
case 4: // Update tangents (vertex tangents) case 4: // Update tangents (vertex tangents)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); 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; } break;
case 5: // Update texcoords2 (vertex second texture coordinates) 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) if (material.shader.locs[LOC_VERTEX_TANGENT] != -1)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); 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]); glEnableVertexAttribArray(material.shader.locs[LOC_VERTEX_TANGENT]);
} }

+ 1
- 1
src/rlgl.h Wyświetl plik

@ -187,7 +187,7 @@ typedef unsigned char byte;
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 (XYZW - 4 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) unsigned short *indices;// vertex indices (in case vertex data comes indexed)

Ładowanie…
Anuluj
Zapisz