请教OpenGl ,同样的数据为什么glBegin(GL_TRIANGLES)和glDrawArrays()结果显示就不一样呢?
调用中需要使用到的数据结构:
typedef struct
{
float x, /* The x component of the vertex position */
y, /* The y component of the vertex position */
z; /* The z component of the vertex position */
} ObjVertex;
typedef struct
{
float x, /* The x component of the normal vector */
y, /* The y component of the normal vector */
z; /* The z component of the normal vector */
} ObjNormal;
typedef struct
{
float u, /* The u parametric texturing co-ordinate */
v; /* The v parametric texturing co-ordinate */
} ObjTexCoord;
typedef struct
{
unsigned int m_aVertexIndices[3],
m_aNormalIndices[3],
m_aTexCoordIndicies[3];
} ObjFace;
typedef struct _ObjMesh
{
ObjVertex *m_aVertexArray;
ObjNormal *m_aNormalArray;
ObjTexCoord *m_aTexCoordArray;
ObjFace *m_aFaces;
unsigned int *m_aIndices;
unsigned int m_iNumberOfVertices,
m_iNumberOfNormals,
m_iNumberOfTexCoords,
m_iNumberOfFaces;
} ObjMesh;
(1)使用glBegin()语句:
glBegin(GL_TRIANGLES);
for(i=0;i<pMesh->m_iNumberOfFaces;i++)
{
unsigned int j;
ObjFace *pf = &pMesh->m_aFaces[i];
/*
** Draw the polygons with Normals only
*/
for(j=0;j<3;j++)
{
glNormal3f( pMesh->m_aNormalArray[ pf->m_aNormalIndices[j] ].x,
pMesh->m_aNormalArray[ pf->m_aNormalIndices[j] ].y,
pMesh->m_aNormalArray[ pf->m_aNormalIndices[j] ].z);
glVertex3f( pMesh->m_aVertexArray[ pf->m_aVertexIndices[j] ].x,
pMesh->m_aVertexArray[ pf->m_aVertexIndices[j] ].y,
pMesh->m_aVertexArray[ pf->m_aVertexIndices[j] ].z);
}
}
glEnd();
(2)使用glDrawArrays()语句调用:
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,pMesh->m_aVertexArray);
if(pMesh->m_aNormalArray)
{
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT,0,pMesh->m_aNormalArray);
}
if(pMesh->m_aTexCoordArray)
{
glTexCoordPointer(2,GL_FLOAT,0,pMesh->m_aTexCoordArray);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
glDrawArrays(GL_TRIANGLES,0,pMesh->m_iNumberOfFaces);
相同的数据*pMesh,程序运行调用(1)和(2)的显示结果居然不一样。
求解???????
typedef struct
{
float x, /* The x component of the vertex position */
y, /* The y component of the vertex position */
z; /* The z component of the vertex position */
} ObjVertex;
typedef struct
{
float x, /* The x component of the normal vector */
y, /* The y component of the normal vector */
z; /* The z component of the normal vector */
} ObjNormal;
typedef struct
{
float u, /* The u parametric texturing co-ordinate */
v; /* The v parametric texturing co-ordinate */
} ObjTexCoord;
typedef struct
{
unsigned int m_aVertexIndices[3],
m_aNormalIndices[3],
m_aTexCoordIndicies[3];
} ObjFace;
typedef struct _ObjMesh
{
ObjVertex *m_aVertexArray;
ObjNormal *m_aNormalArray;
ObjTexCoord *m_aTexCoordArray;
ObjFace *m_aFaces;
unsigned int *m_aIndices;
unsigned int m_iNumberOfVertices,
m_iNumberOfNormals,
m_iNumberOfTexCoords,
m_iNumberOfFaces;
} ObjMesh;
(1)使用glBegin()语句:
glBegin(GL_TRIANGLES);
for(i=0;i<pMesh->m_iNumberOfFaces;i++)
{
unsigned int j;
ObjFace *pf = &pMesh->m_aFaces[i];
/*
** Draw the polygons with Normals only
*/
for(j=0;j<3;j++)
{
glNormal3f( pMesh->m_aNormalArray[ pf->m_aNormalIndices[j] ].x,
pMesh->m_aNormalArray[ pf->m_aNormalIndices[j] ].y,
pMesh->m_aNormalArray[ pf->m_aNormalIndices[j] ].z);
glVertex3f( pMesh->m_aVertexArray[ pf->m_aVertexIndices[j] ].x,
pMesh->m_aVertexArray[ pf->m_aVertexIndices[j] ].y,
pMesh->m_aVertexArray[ pf->m_aVertexIndices[j] ].z);
}
}
glEnd();
(2)使用glDrawArrays()语句调用:
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,pMesh->m_aVertexArray);
if(pMesh->m_aNormalArray)
{
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT,0,pMesh->m_aNormalArray);
}
if(pMesh->m_aTexCoordArray)
{
glTexCoordPointer(2,GL_FLOAT,0,pMesh->m_aTexCoordArray);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
glDrawArrays(GL_TRIANGLES,0,pMesh->m_iNumberOfFaces);
相同的数据*pMesh,程序运行调用(1)和(2)的显示结果居然不一样。
求解???????
...全文
相关推荐
opengl实现画点,画线,画面
(GL_TRIANGLES, 0, 12); else glDrawArrays(GL_QUADS, 0, 8);}glBindVertexArray(0);```以上代码片段假设你已经设置了顶点数组对象
多个五角星 颜色大小不一样
渲染五角星时,我们可以使用OpenGL的GL_TRIANGLES模式。首先,开启OpenGL上下文,然后设置投影和模型视图矩阵,以控制五角星的显示方式。
Bezier曲面, Coons曲面, B样条曲面绘制(用OpenGL实现)
设置适当的绘制模式(如GL_TRIANGLES或GL_TRIANGLE_STRIP)并调用glDrawArrays或glDrawElements进行渲染。6.
基于OpenGL和MFC的3D模型导入和读取
glLightfv()和glMaterialfv()用来设置光源和材质属性,glEnable(GL_LIGHTING)启用光照效果。
OpenGL 常用API
**glDrawArrays() 和 glDrawElements()**: 这两个函数用于绘制数组中的顶点数据。


