I've imported control points, normals, and indices successfully and now i'm trying to import vertex colors, but something is not right. Here's the code:
FbxLayerElementMaterial* pMaterialLayer = layer->GetMaterials();
int numMatIndices = pMaterialLayer->GetIndexArray().GetCount();
GLfloat *colors = new GLfloat[numMatIndices * 9];
for (int i = 0; i <numMatIndices; i++)
{
//int matIndex = array->operator[](i);
int matIndex = pMaterialLayer->GetIndexArray()[i];
FbxSurfaceMaterial *smat = mesh->GetNode()->GetMaterial(matIndex);
if (smat->GetClassId().Is(FbxSurfaceLambert::ClassId))
{
FbxSurfaceLambert *lam = (FbxSurfaceLambert*)smat;
FbxPropertyT<FbxDouble3> p = lam->Diffuse;
FbxDouble3 info = p.Get();
for (int j = 0; j < 3; j++)
{
colors[i * 9 + j * 3 + 0] = (GLfloat)info[0];
colors[i * 9 + j * 3 + 1] = (GLfloat)info[1];
colors[i * 9 + j * 3 + 2] = (GLfloat)info[2];
}
}
}
Here's what I exported from max, and what I got in my OpenGL application.
I was inspecting memory dump and all colors are like they should be (in the order material indices were written in fbx file). Three SAME colors for three vertices in one triangle (total nine floats for one triangle). How is it even possible to have different vertex colors in one triangle?
Does it have something to do because I'm using index buffer for drawing vertices? My VertexBufferObject contains vertex positions, normals, indices, and colors. I'm drawing with glDrawElements call.
Can anyone help me with this?
@EDIT For those who asked, here's whole method for importing mesh:
bool Mesh::Create(FbxMesh *mesh)
{
FbxScene *scene = mesh->GetScene();
FbxAnimEvaluator *eval = scene->GetAnimationEvaluator();
FbxMatrix transform = eval->GetNodeGlobalTransform(mesh->GetNode());
glm::mat4x4 glmMat = FbxMatToGlm(transform);
// *************** VERTEX POSITIONS ***************
int numVertices = mesh->GetControlPointsCount();
GLfloat *vertices = new GLfloat[numVertices * 3];
for (int i = 0; i < numVertices; i++)
{
FbxVector4 vec = transform.MultNormalize(mesh->GetControlPointAt(i));
vertices[i * 3 + 0] = (GLfloat)vec.mData[0];
vertices[i * 3 + 1] = (GLfloat)vec.mData[1];
vertices[i * 3 + 2] = (GLfloat)vec.mData[2];
}
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)* 3 * numVertices, vertices, GL_STATIC_DRAW);
delete[] vertices;
// ****************** INDICES ***************
numIndices = mesh->GetPolygonVertexCount();
int *indices = mesh->GetPolygonVertices();
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)* numIndices, indices, GL_STATIC_DRAW);
delete[] indices;
// *********** NORMALS *********
FbxGeometryElementNormal *normalElement = mesh->GetElementNormal();
if (normalElement)
{
int numNormals = mesh->GetPolygonCount() * 3;
GLfloat *normals = new GLfloat[numNormals * 3];
int vertexCounter = 0;
for (int polyCounter = 0; polyCounter < mesh->GetPolygonCount(); polyCounter++)
{
for (int i = 0; i < 3; i++)
{
FbxVector4 normal = normalElement->GetDirectArray().GetAt(vertexCounter);
glm::vec4 glmNormal = FbxVecToGlm(normal);
glmNormal = glmMat * glmNormal;
normals[vertexCounter * 3 + 0] = (GLfloat)glmNormal[0];
normals[vertexCounter * 3 + 1] = (GLfloat)glmNormal[1];
normals[vertexCounter * 3 + 2] = (GLfloat)glmNormal[2];
vertexCounter++;
}
}
glGenBuffers(1, &normalsBuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalsBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)* 3 * numNormals, normals, GL_STATIC_DRAW);
delete[] normals;
}
// ********* COLORS *************
FbxLayer *layer = mesh->GetLayer(0);
FbxLayerElementMaterial* pMaterialLayer = layer->GetMaterials();
int numMatIndices = pMaterialLayer->GetIndexArray().GetCount();
GLfloat *colors = new GLfloat[numMatIndices * 9];
for (int i = 0; i < numMatIndices; i++)
{
//int matIndex = array->operator[](i);
int matIndex = pMaterialLayer->GetIndexArray()[i];
FbxSurfaceMaterial *smat = mesh->GetNode()->GetMaterial(matIndex);
if (smat->GetClassId().Is(FbxSurfaceLambert::ClassId))
{
FbxSurfaceLambert *lam = (FbxSurfaceLambert*)smat;
FbxPropertyT<FbxDouble3> p = lam->Diffuse;
FbxDouble3 info = p.Get();
for (int j = 0; j < 3; j++)
{
colors[i * 9 + j * 3 + 0] = (GLfloat)info[0];
colors[i * 9 + j * 3 + 1] = (GLfloat)info[1];
colors[i * 9 + j * 3 + 2] = (GLfloat)info[2];
}
}
}
glGenBuffers(1, &colorsBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorsBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)* 9 * numMatIndices, colors, GL_STATIC_DRAW);
delete[] colors;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, colorsBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, normalsBuffer);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
return true;
}