Merge pull request #4607 from samcake/orange

Add support for Color attribute loading from FBX model
This commit is contained in:
Brad Davis 2015-04-07 17:26:16 -07:00
commit b55bb998d2
17 changed files with 108 additions and 17 deletions

View file

@ -458,6 +458,18 @@ FBXNode parseFBX(QIODevice* device) {
return top; return top;
} }
QVector<glm::vec4> createVec4Vector(const QVector<double>& doubleVector) {
QVector<glm::vec4> values;
for (const double* it = doubleVector.constData(), *end = it + (doubleVector.size() / 4 * 4); it != end; ) {
float x = *it++;
float y = *it++;
float z = *it++;
float w = *it++;
values.append(glm::vec4(x, y, z, w));
}
return values;
}
QVector<glm::vec3> createVec3Vector(const QVector<double>& doubleVector) { QVector<glm::vec3> createVec3Vector(const QVector<double>& doubleVector) {
QVector<glm::vec3> values; QVector<glm::vec3> values;
for (const double* it = doubleVector.constData(), *end = it + (doubleVector.size() / 3 * 3); it != end; ) { for (const double* it = doubleVector.constData(), *end = it + (doubleVector.size() / 3 * 3); it != end; ) {
@ -739,6 +751,11 @@ public:
bool normalsByVertex; bool normalsByVertex;
QVector<glm::vec3> normals; QVector<glm::vec3> normals;
QVector<int> normalIndices; QVector<int> normalIndices;
bool colorsByVertex;
QVector<glm::vec4> colors;
QVector<int> colorIndices;
QVector<glm::vec2> texCoords; QVector<glm::vec2> texCoords;
QVector<int> texCoordIndices; QVector<int> texCoordIndices;
@ -776,6 +793,23 @@ void appendIndex(MeshData& data, QVector<int>& indices, int index) {
} }
} }
glm::vec4 color;
bool hasColors = (data.colors.size() > 1);
if (hasColors) {
int colorIndex = data.colorsByVertex ? vertexIndex : index;
if (data.colorIndices.isEmpty()) {
if (colorIndex < data.colors.size()) {
color = data.colors.at(colorIndex);
}
} else if (colorIndex < data.colorIndices.size()) {
colorIndex = data.colorIndices.at(colorIndex);
if (colorIndex >= 0 && colorIndex < data.colors.size()) {
color = data.colors.at(colorIndex);
}
}
}
if (data.texCoordIndices.isEmpty()) { if (data.texCoordIndices.isEmpty()) {
if (index < data.texCoords.size()) { if (index < data.texCoords.size()) {
vertex.texCoord = data.texCoords.at(index); vertex.texCoord = data.texCoords.at(index);
@ -810,6 +844,9 @@ void appendIndex(MeshData& data, QVector<int>& indices, int index) {
data.extracted.mesh.vertices.append(position); data.extracted.mesh.vertices.append(position);
data.extracted.mesh.normals.append(normal); data.extracted.mesh.normals.append(normal);
data.extracted.mesh.texCoords.append(vertex.texCoord); data.extracted.mesh.texCoords.append(vertex.texCoord);
if (hasColors) {
data.extracted.mesh.colors.append(glm::vec3(color));
}
if (hasMoreTexcoords) { if (hasMoreTexcoords) {
data.extracted.mesh.texCoords1.append(vertex.texCoord1); data.extracted.mesh.texCoords1.append(vertex.texCoord1);
} }
@ -852,6 +889,28 @@ ExtractedMesh extractMesh(const FBXNode& object, unsigned int& meshIndex) {
// hack to work around wacky Makehuman exports // hack to work around wacky Makehuman exports
data.normalsByVertex = true; data.normalsByVertex = true;
} }
} else if (child.name == "LayerElementColor") {
data.colorsByVertex = false;
bool indexToDirect = false;
foreach (const FBXNode& subdata, child.children) {
if (subdata.name == "Colors") {
data.colors = createVec4Vector(getDoubleVector(subdata));
} else if (subdata.name == "ColorsIndex") {
data.colorIndices = getIntVector(subdata);
} else if (subdata.name == "MappingInformationType" && subdata.properties.at(0) == "ByVertice") {
data.colorsByVertex = true;
} else if (subdata.name == "ReferenceInformationType" && subdata.properties.at(0) == "IndexToDirect") {
indexToDirect = true;
}
}
if (indexToDirect && data.normalIndices.isEmpty()) {
// hack to work around wacky Makehuman exports
data.colorsByVertex = true;
}
} else if (child.name == "LayerElementUV") { } else if (child.name == "LayerElementUV") {
if (child.properties.at(0).toInt() == 0) { if (child.properties.at(0).toInt() == 0) {
AttributeData attrib; AttributeData attrib;

View file

@ -2505,7 +2505,7 @@ int Model::renderMeshesFromList(QVector<int>& list, gpu::Batch& batch, RenderMod
} }
if (mesh.colors.isEmpty()) { if (mesh.colors.isEmpty()) {
GLBATCH(glColor4f)(0.0f, 1.0f, 0.0f, 1.0f); GLBATCH(glColor4f)(1.0f, 1.0f, 1.0f, 1.0f);
} }
qint64 offset = 0; qint64 offset = 0;

View file

@ -21,6 +21,9 @@ uniform sampler2D diffuseMap;
// the interpolated normal // the interpolated normal
varying vec4 normal; varying vec4 normal;
varying vec3 color;
void main(void) { void main(void) {
// Fetch diffuse map // Fetch diffuse map
vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st);
@ -30,7 +33,7 @@ void main(void) {
packDeferredFragment( packDeferredFragment(
normalize(normal.xyz), normalize(normal.xyz),
evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a),
getMaterialDiffuse(mat) * diffuse.rgb, getMaterialDiffuse(mat) * diffuse.rgb * color,
getMaterialSpecular(mat), getMaterialSpecular(mat),
getMaterialShininess(mat)); getMaterialShininess(mat));
} }

View file

@ -19,14 +19,15 @@ const int MAX_TEXCOORDS = 2;
uniform mat4 texcoordMatrices[MAX_TEXCOORDS]; uniform mat4 texcoordMatrices[MAX_TEXCOORDS];
// the interpolated normal // the interpolated normal
varying vec4 normal; varying vec4 normal;
varying vec3 color;
void main(void) { void main(void) {
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; color = gl_Color.xyz;
// and the texture coordinates // and the texture coordinates
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);

View file

@ -26,6 +26,8 @@ uniform vec2 emissiveParams;
// the interpolated normal // the interpolated normal
varying vec4 normal; varying vec4 normal;
varying vec3 color;
// the interpolated texcoord1 // the interpolated texcoord1
varying vec2 interpolatedTexcoord1; varying vec2 interpolatedTexcoord1;
@ -39,7 +41,7 @@ void main(void) {
packDeferredFragmentLightmap( packDeferredFragmentLightmap(
normalize(normal.xyz), normalize(normal.xyz),
evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a),
getMaterialDiffuse(mat) * diffuse.rgb, getMaterialDiffuse(mat) * diffuse.rgb * color,
getMaterialSpecular(mat), getMaterialSpecular(mat),
getMaterialShininess(mat), getMaterialShininess(mat),
(vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb));

View file

@ -28,9 +28,12 @@ varying vec4 normal;
// the interpolated texcoord1 // the interpolated texcoord1
varying vec2 interpolatedTexcoord1; varying vec2 interpolatedTexcoord1;
varying vec3 color;
void main(void) { void main(void) {
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; color = gl_Color.xyz;
// and the texture coordinates // and the texture coordinates
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);

View file

@ -34,6 +34,8 @@ varying vec4 interpolatedTangent;
varying vec2 interpolatedTexcoord1; varying vec2 interpolatedTexcoord1;
varying vec3 color;
void main(void) { void main(void) {
// compute the view normal from the various bits // compute the view normal from the various bits
vec3 normalizedNormal = normalize(vec3(interpolatedNormal)); vec3 normalizedNormal = normalize(vec3(interpolatedNormal));
@ -52,7 +54,7 @@ void main(void) {
packDeferredFragmentLightmap( packDeferredFragmentLightmap(
normalize(viewNormal.xyz), normalize(viewNormal.xyz),
evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a),
getMaterialDiffuse(mat) * diffuse.rgb, getMaterialDiffuse(mat) * diffuse.rgb * color,
getMaterialSpecular(mat), getMaterialSpecular(mat),
getMaterialShininess(mat), getMaterialShininess(mat),
(vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb));

View file

@ -34,13 +34,15 @@ varying vec4 interpolatedTangent;
// the interpolated texcoord1 // the interpolated texcoord1
varying vec2 interpolatedTexcoord1; varying vec2 interpolatedTexcoord1;
varying vec3 color;
void main(void) { void main(void) {
// transform and store the normal and tangent for interpolation // transform and store the normal and tangent for interpolation
//interpolatedNormal = gl_ModelViewMatrix * vec4(gl_Normal, 0.0); //interpolatedNormal = gl_ModelViewMatrix * vec4(gl_Normal, 0.0);
//interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0); //interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0);
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; color = gl_Color.xyz;
// and the texture coordinates // and the texture coordinates
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);

View file

@ -37,6 +37,9 @@ varying vec4 interpolatedTangent;
varying vec2 interpolatedTexcoord1; varying vec2 interpolatedTexcoord1;
varying vec3 color;
void main(void) { void main(void) {
// compute the view normal from the various bits // compute the view normal from the various bits
vec3 normalizedNormal = normalize(vec3(interpolatedNormal)); vec3 normalizedNormal = normalize(vec3(interpolatedNormal));
@ -56,7 +59,7 @@ void main(void) {
packDeferredFragmentLightmap( packDeferredFragmentLightmap(
normalize(viewNormal.xyz), normalize(viewNormal.xyz),
evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a),
getMaterialDiffuse(mat) * diffuse.rgb, getMaterialDiffuse(mat) * diffuse.rgb * color,
specular, // no use of getMaterialSpecular(mat) specular, // no use of getMaterialSpecular(mat)
getMaterialShininess(mat), getMaterialShininess(mat),
(vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb));

View file

@ -31,6 +31,8 @@ varying vec4 normal;
varying vec2 interpolatedTexcoord1; varying vec2 interpolatedTexcoord1;
varying vec3 color;
void main(void) { void main(void) {
// set the diffuse, normal, specular data // set the diffuse, normal, specular data
vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st);
@ -42,7 +44,7 @@ void main(void) {
packDeferredFragmentLightmap( packDeferredFragmentLightmap(
normalize(normal.xyz), normalize(normal.xyz),
evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a),
getMaterialDiffuse(mat) * diffuse.rgb, getMaterialDiffuse(mat) * diffuse.rgb * color,
specular, // no use of getMaterialSpecular(mat) specular, // no use of getMaterialSpecular(mat)
getMaterialShininess(mat), getMaterialShininess(mat),
(vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb));

View file

@ -28,6 +28,8 @@ varying vec4 interpolatedNormal;
// the interpolated tangent // the interpolated tangent
varying vec4 interpolatedTangent; varying vec4 interpolatedTangent;
varying vec3 color;
void main(void) { void main(void) {
// compute the view normal from the various bits // compute the view normal from the various bits
vec3 normalizedNormal = normalize(vec3(interpolatedNormal)); vec3 normalizedNormal = normalize(vec3(interpolatedNormal));
@ -44,7 +46,7 @@ void main(void) {
packDeferredFragment( packDeferredFragment(
normalize(viewNormal.xyz), normalize(viewNormal.xyz),
evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a),
getMaterialDiffuse(mat) * diffuse.rgb, getMaterialDiffuse(mat) * diffuse.rgb * color,
getMaterialSpecular(mat), getMaterialSpecular(mat),
getMaterialShininess(mat)); getMaterialShininess(mat));
} }

View file

@ -29,13 +29,15 @@ varying vec4 interpolatedNormal;
// the interpolated tangent // the interpolated tangent
varying vec4 interpolatedTangent; varying vec4 interpolatedTangent;
varying vec3 color;
void main(void) { void main(void) {
// transform and store the normal and tangent for interpolation // transform and store the normal and tangent for interpolation
//interpolatedNormal = gl_ModelViewMatrix * vec4(gl_Normal, 0.0); //interpolatedNormal = gl_ModelViewMatrix * vec4(gl_Normal, 0.0);
//interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0); //interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0);
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; color = gl_Color.xyz;
// and the texture coordinates // and the texture coordinates
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);

View file

@ -31,6 +31,8 @@ varying vec4 interpolatedNormal;
// the interpolated tangent // the interpolated tangent
varying vec4 interpolatedTangent; varying vec4 interpolatedTangent;
varying vec3 color;
void main(void) { void main(void) {
// compute the view normal from the various bits // compute the view normal from the various bits
vec3 normalizedNormal = normalize(vec3(interpolatedNormal)); vec3 normalizedNormal = normalize(vec3(interpolatedNormal));
@ -49,7 +51,7 @@ void main(void) {
packDeferredFragment( packDeferredFragment(
normalize(viewNormal.xyz), normalize(viewNormal.xyz),
evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a),
getMaterialDiffuse(mat) * diffuse.rgb, getMaterialDiffuse(mat) * diffuse.rgb * color,
specular, //getMaterialSpecular(mat), specular, //getMaterialSpecular(mat),
getMaterialShininess(mat)); getMaterialShininess(mat));
} }

View file

@ -25,6 +25,8 @@ uniform sampler2D specularMap;
// the interpolated normal // the interpolated normal
varying vec4 normal; varying vec4 normal;
varying vec3 color;
void main(void) { void main(void) {
// set the diffuse, normal, specular data // set the diffuse, normal, specular data
vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st);
@ -35,7 +37,7 @@ void main(void) {
packDeferredFragment( packDeferredFragment(
normalize(normal.xyz), normalize(normal.xyz),
evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a),
getMaterialDiffuse(mat) * diffuse.rgb, getMaterialDiffuse(mat) * diffuse.rgb * color,
specular, //getMaterialSpecular(mat), specular, //getMaterialSpecular(mat),
getMaterialShininess(mat)); getMaterialShininess(mat));
} }

View file

@ -21,6 +21,8 @@ uniform sampler2D diffuseMap;
varying vec4 normal; varying vec4 normal;
varying vec3 color;
void main(void) { void main(void) {
// Fetch diffuse map // Fetch diffuse map
@ -31,7 +33,7 @@ void main(void) {
packDeferredFragmentTranslucent( packDeferredFragmentTranslucent(
normalize(normal.xyz), normalize(normal.xyz),
getMaterialOpacity(mat) * diffuse.a, getMaterialOpacity(mat) * diffuse.a,
getMaterialDiffuse(mat) * diffuse.rgb, getMaterialDiffuse(mat) * diffuse.rgb * color,
getMaterialSpecular(mat), getMaterialSpecular(mat),
getMaterialShininess(mat)); getMaterialShininess(mat));

View file

@ -28,6 +28,8 @@ attribute vec4 clusterWeights;
// the interpolated normal // the interpolated normal
varying vec4 normal; varying vec4 normal;
varying vec3 color;
void main(void) { void main(void) {
vec4 position = vec4(0.0, 0.0, 0.0, 0.0); vec4 position = vec4(0.0, 0.0, 0.0, 0.0);
normal = vec4(0.0, 0.0, 0.0, 0.0); normal = vec4(0.0, 0.0, 0.0, 0.0);
@ -39,7 +41,7 @@ void main(void) {
} }
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; color = gl_Color.xyz;
// and the texture coordinates // and the texture coordinates
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);

View file

@ -34,6 +34,8 @@ varying vec4 interpolatedNormal;
// the interpolated tangent // the interpolated tangent
varying vec4 interpolatedTangent; varying vec4 interpolatedTangent;
varying vec3 color;
void main(void) { void main(void) {
vec4 interpolatedPosition = vec4(0.0, 0.0, 0.0, 0.0); vec4 interpolatedPosition = vec4(0.0, 0.0, 0.0, 0.0);
interpolatedNormal = vec4(0.0, 0.0, 0.0, 0.0); interpolatedNormal = vec4(0.0, 0.0, 0.0, 0.0);
@ -47,7 +49,7 @@ void main(void) {
} }
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; color = gl_Color.xyz;
// and the texture coordinates // and the texture coordinates
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);