如果您使用struct
这样的方式,您的顶点不再紧密排列,您需要指定一个步幅:
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, sizeof( Vertex ), &verts[0].pos );
glDrawArrays( GL_TRIANGLES, 0, 3 );
glDisableClientState(GL_VERTEX_ARRAY);
像这样:
#include <GL/glut.h>
#include <vector>
struct Vertex
{
float pos[3];
float tex[2];
float norm[3];
int index_mtl;
};
std::vector< Vertex > verts;
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -2, 2, -2, 2, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub( 255, 0, 0 );
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, sizeof( Vertex ), &verts[0].pos );
glDrawArrays( GL_TRIANGLES, 0, 3 );
glDisableClientState(GL_VERTEX_ARRAY);
glutSwapBuffers();
}
int main( int argc, char **argv )
{
Vertex tmp;
tmp.pos[0] = 0;
tmp.pos[1] = 0;
tmp.pos[2] = 0;
verts.push_back( tmp );
tmp.pos[0] = 1;
tmp.pos[1] = 0;
tmp.pos[2] = 0;
verts.push_back( tmp );
tmp.pos[0] = 1;
tmp.pos[1] = 1;
tmp.pos[2] = 0;
verts.push_back( tmp );
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}