c++ OpenGL FBX 模型未正确加载
c++ OpenGL FBX model not loaded properly
我正在尝试使用 OpenGL 加载 FBX 模型,我让导入器工作并读取网格数据以便我可以绘制它
but some parts are not displayed correctly.
这是我用的
FbxMesh*mesh = pNode->GetMesh();
//================= Get Vertices ====================================
int numVerts = mesh->GetControlPointsCount();
for(int j = 0; j < numVerts; j++)
{
FbxVector4 vert = mesh->GetControlPointAt(j);
vertices[numVertices].x=(float)vert.mData[0];
vertices[numVertices].y=(float)vert.mData[1];
vertices[numVertices++].z=(float)vert.mData[2];
printf("MeshVert: x: %f y: %f z: %f\n", vertices[numVertices-1].x, vertices[numVertices-1].y, vertices[numVertices-1].z);
}
//================= Get Indices ====================================
numIndices = mesh->GetPolygonVertexCount();
int triangleCount = numIndices / 3;
indices = new int[numIndices];
indices = mesh->GetPolygonVertices();
printf("numIndices: %i\n", numIndices);
printf("TriangleCount: %i\n", triangleCount);
//================= Get Normals ====================================
FbxGeometryElementNormal*normalEl = mesh->GetElementNormal();
if(normalEl)
{
int numNormals = mesh->GetPolygonCount()*3;
normals = new float[numNormals*3];
int vertexCounter = 0;
for(int polyCounter = 0 ; polyCounter<mesh->GetPolygonCount(); polyCounter++)
{
for(int k = 0; k < 3; k++)
{
FbxVector4 normal = normalEl->GetDirectArray().GetAt(vertexCounter);
normals[vertexCounter*3+0] = (float)normal[0];
normals[vertexCounter*3+1] = (float)normal[1];
normals[vertexCounter*3+2] = (float)normal[2];
//cout<<"\n"<<normals[vertexCounter*3+0]<<" "<<normals[vertexCounter*3+1]<<" "<<normals[vertexCounter*3+2];
vertexCounter++;
}
}
}
和图元绘制
for(int i = 0; i < numIndices - 3; i++)
{
glBegin(GL_TRIANGLES);
glNormal3f(normals[i*3+0], normals[i*3+1], normals[i*3+2]);
for(int j = i; j <= i + 2; j++)
{
glVertex3f(vertices[indices[j]].x, vertices[indices[j]].y, vertices[indices[j]].z);
glColor3f(0.3f, 0.3f, 0.3f);
}
glEnd();
}
只是想知道是否有人可以帮助实现它。
我的其余项目代码在 https://github.com/buttburger/FBXEssentials/tree/master/OpenGL2
已修复
for(int i = 0; i < numIndices - 3; i+=3)
{
glBegin(GL_TRIANGLES);
glNormal3f(normals[i*3+0], normals[i*3+1], normals[i*3+2]);
for(int j = i; j <= i + 2; j++)
{
glVertex3f(vertices[indices[j]].x, vertices[indices[j]].y, vertices[indices[j]].z);
glColor3f(1.0f, 1.0f, 1.0f);
}
}
glEnd();
glFlush();
glutSwapBuffers();
我正在尝试使用 OpenGL 加载 FBX 模型,我让导入器工作并读取网格数据以便我可以绘制它 but some parts are not displayed correctly.
这是我用的
FbxMesh*mesh = pNode->GetMesh();
//================= Get Vertices ====================================
int numVerts = mesh->GetControlPointsCount();
for(int j = 0; j < numVerts; j++)
{
FbxVector4 vert = mesh->GetControlPointAt(j);
vertices[numVertices].x=(float)vert.mData[0];
vertices[numVertices].y=(float)vert.mData[1];
vertices[numVertices++].z=(float)vert.mData[2];
printf("MeshVert: x: %f y: %f z: %f\n", vertices[numVertices-1].x, vertices[numVertices-1].y, vertices[numVertices-1].z);
}
//================= Get Indices ====================================
numIndices = mesh->GetPolygonVertexCount();
int triangleCount = numIndices / 3;
indices = new int[numIndices];
indices = mesh->GetPolygonVertices();
printf("numIndices: %i\n", numIndices);
printf("TriangleCount: %i\n", triangleCount);
//================= Get Normals ====================================
FbxGeometryElementNormal*normalEl = mesh->GetElementNormal();
if(normalEl)
{
int numNormals = mesh->GetPolygonCount()*3;
normals = new float[numNormals*3];
int vertexCounter = 0;
for(int polyCounter = 0 ; polyCounter<mesh->GetPolygonCount(); polyCounter++)
{
for(int k = 0; k < 3; k++)
{
FbxVector4 normal = normalEl->GetDirectArray().GetAt(vertexCounter);
normals[vertexCounter*3+0] = (float)normal[0];
normals[vertexCounter*3+1] = (float)normal[1];
normals[vertexCounter*3+2] = (float)normal[2];
//cout<<"\n"<<normals[vertexCounter*3+0]<<" "<<normals[vertexCounter*3+1]<<" "<<normals[vertexCounter*3+2];
vertexCounter++;
}
}
}
和图元绘制
for(int i = 0; i < numIndices - 3; i++)
{
glBegin(GL_TRIANGLES);
glNormal3f(normals[i*3+0], normals[i*3+1], normals[i*3+2]);
for(int j = i; j <= i + 2; j++)
{
glVertex3f(vertices[indices[j]].x, vertices[indices[j]].y, vertices[indices[j]].z);
glColor3f(0.3f, 0.3f, 0.3f);
}
glEnd();
}
只是想知道是否有人可以帮助实现它。
我的其余项目代码在 https://github.com/buttburger/FBXEssentials/tree/master/OpenGL2
已修复
for(int i = 0; i < numIndices - 3; i+=3)
{
glBegin(GL_TRIANGLES);
glNormal3f(normals[i*3+0], normals[i*3+1], normals[i*3+2]);
for(int j = i; j <= i + 2; j++)
{
glVertex3f(vertices[indices[j]].x, vertices[indices[j]].y, vertices[indices[j]].z);
glColor3f(1.0f, 1.0f, 1.0f);
}
}
glEnd();
glFlush();
glutSwapBuffers();