在一个demo里面可以正确的加载显示贴图纹理 ,但有些问题 现在需要在lib3ds 里面显示贴图文件 现在问题不知道 贴图坐标放在什么地方
// 开始以g_ViewMode模式绘制
glBegin(g_ViewMode);
// 遍历所有的面
for(int j = 0; j < pObject->numOfFaces; j++)
{
// 遍历三角形的所有点
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
// 获得面对每个点的索引
int index = pObject->pFaces[j].vertIndex[whichVertex];
// 给出法向量
glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
// 如果对象具有纹理
if(pObject->bHasTexture) {
// 确定是否有UVW纹理坐标
if(pObject->pTexVerts) {
glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
}
} else {
if(g_3DModel.pMaterials.size() && pObject->materialID >= 0)
{
BYTE *pColor = g_3DModel.pMaterials[pObject->materialID].color;
glColor3ub(pColor[0], pColor[1], pColor[2]);
}
}
glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
}
}
glEnd(); // 绘制结束
这个是demo里面的代码 现在需要在 lib3ds 下显示文理 lib3ds 的render 代码如下
if (!node->user_id)
{
mesh = lib3ds_file_mesh_for_node(m_pModel, node);
assert(mesh);
if (!mesh) {
return;
}
node->user_id = glGenLists(1);
glNewList(node->user_id, GL_COMPILE);
norm_verts = (Lib3dsVector*) malloc(3*sizeof(Lib3dsVector)*mesh->nfaces);
norm_faces = (Lib3dsVector*) malloc(sizeof(Lib3dsVector)*mesh->nfaces);
lib3ds_matrix_copy(M, mesh->matrix);
lib3ds_matrix_inv(M);
glMultMatrixf(&M[0][0]);
lib3ds_mesh_calculate_face_normals(mesh, (float (*)[3])norm_faces);
lib3ds_mesh_calculate_vertex_normals(mesh, (float (*)[3])norm_verts);
for (fi=0; fi<mesh->nfaces; ++fi) {
face = &(mesh->faces[fi]);
mat = 0;
if (face->material >= 0 && face->material < m_pModel->nmaterials)
mat = m_pModel->materials[face->material];
if (mat)
{
// 使用材质
float s = pow(2, 10.0*mat->shininess);
if (s>128.0) s = 128.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, mat->ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat->diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat->specular);
glMaterialf(GL_FRONT, GL_SHININESS, s);
}
else {
// 使用贴图
float a[]={0.2, 0.2, 0.2, 1.0};
float d[]={0.8, 0.8, 0.8, 1.0};
float s[]={0.0, 0.0, 0.0, 1.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, a);
glMaterialfv(GL_FRONT, GL_DIFFUSE, d);
glMaterialfv(GL_FRONT, GL_SPECULAR, s);
}
// Draw tri-face
glBegin(GL_TRIANGLES);
glNormal3fv(norm_faces[fi].v); // face normal
for (i=0; i<3; ++i) {
glNormal3fv(norm_verts[3*fi+i].v); // vertex normal
glVertex3fv(mesh->vertices[face->index[i]]);
}
glEnd();
}
free(norm_faces);
free(norm_verts);
glEndList();
}
if (node->user_id) {
glPushMatrix();
meshData = (Lib3dsMeshInstanceNode*) node;
glMultMatrixf(&node->matrix[0][0]);
glTranslatef(-meshData->pivot[0], -meshData->pivot[1], -meshData->pivot[2]);
glCallList(node->user_id);
// glutSolidSphere(50.0, 20,20);
glPopMatrix();
}
}
不知道贴纹理坐标的地方在哪儿?怎么贴 希望知道的解答下