# 【OpenGL学习】顶点数组的应用

564人阅读 评论(0)

void drawOneLine(GLfloat x1,GLfloat y1,GLfloat x2,GLfloat y2)
{
glBegin(GL_LINES);
glVertex2f ((x1),(y1));
glVertex2f ((x2),(y2));
glEnd();
}

# 将坐标存在一个数组中

void drawLineWithArray()
{
GLint vertices[]={25,25,
100,100};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2,GL_INT,0,vertices);
glBegin(GL_LINES);
glArrayElement(0);
glArrayElement(1);
glEnd();

}

vertices记录了两个坐标(x1,y1)=(25,25),(x2,y2)=(100,100)

glVertexPointer则用于这个功能,可以称这种数组为混合数组,参数指定了顶点数组的配对点(如坐标定位为2个,颜色则为3个),数据类型及数组

glArrayElement则根据顶点数组来调用相应的函数,每次只调用1个顶点

# 启用多个顶点数组

void drawTwoLineWithArray()
{
GLint vertices[]={25,25,
100,100,
120,120,
200,200};
GLfloat colors[]={1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2,GL_INT,0,vertices);
glColorPointer(3,GL_FLOAT,0,colors);
glBegin(GL_LINES);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glArrayElement(3);
glEnd();
}

# 跨距

GLfloat data[]=  {1.0, 0.0, 0.0,25.0,25.0,
1.0, 0.0, 0.0,100.0,100.0,
0.0, 1.0, 0.0,120.0,120.0,
0.0, 1.0, 0.0,200.0,200.0};

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3,GL_FLOAT,5*sizeof(GLfloat),&data[0]);
glVertexPointer(2,GL_FLOAT,5*sizeof(GLfloat),&data[3]);

# 解引用顶点数组列表

1.其提供了一个glDrawElements函数用于循环调用glArrayElement,但需要定义一个索引的数组

void drawTwoLineWithArray2()
{
GLfloat data[]=  {1.0, 0.0, 0.0,25.0,25.0,
1.0, 0.0, 0.0,100.0,100.0,
0.0, 1.0, 0.0,120.0,120.0,
0.0, 1.0, 0.0,200.0,200.0};
GLubyte index[]= {0,1,2,3} ;

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3,GL_FLOAT,5*sizeof(GLfloat),&data[0]);
glVertexPointer(2,GL_FLOAT,5*sizeof(GLfloat),&data[3]);

glDrawElements(GL_LINES,4,GL_UNSIGNED_BYTE,index);

}

2.从索引项开始

glDrawArrays(GL_LINES,0,4);

# 混合数组

glInterleavedArrays函数将会根据参数,激活各种顶点数组,并存储顶点,如下

void drawTwoLineWithArray3()
{
GLfloat data[]=  {1.0, 0.0, 0.0,25.0,25.0,0.0,
1.0, 0.0, 0.0,100.0,100.0,0.0,
0.0, 1.0, 0.0,120.0,120.0,0.0,
0.0, 1.0, 0.0,200.0,200.0,0.0};

glInterleavedArrays(GL_C3F_V3F,0,data);
glDrawArrays(GL_LINES,0,4);

}


GLfloat data[]=  {25.0,25.0,
100.0,100.0,
120.0,120.0,
200.0,200.0};
glInterleavedArrays(GL_V2F,0,data);
glDrawArrays(GL_LINES,0,4);

个人资料
等级：
访问量： 16万+
积分： 1655
排名： 2万+
最新评论