OpenGL几何图元和缓冲区(四)

线段和闭合线段

图元GL_LINE_STRIP会连接起所有的点,但是首尾两点不连接。以下代码段将演示:

glBegin(GL_LINE_STRIP);

    glVertex3f(0.0f,0.0f,0.0f);

    glVertex3f(10.0f,15.0f,0.0f);

    glVertex3f(100.0f,0.0f,0.0);

glEnd();

另外一个图元GL_LINE_LOOP是用来绘制闭合的线段的:

glBegin(GL_LINE_LOOP);

    glVertex3f(0.0f,0.0f,0.0f);

    glVertex3f(10.0f,15.0f,0.0f);

    glVertex3f(100.0f,0.0f,0.0);

glEnd();

此处的代码的第一个点和最后一个点会连接。

下面程序将使用GL_LINE_STRIP图元绘制一条螺旋曲线:

#include <GL/glut.h>
#include <math.h>

#define GL_PI 3.1415f

void RenderScene(void)
{
GLfloat x,y,z,angle;//坐标和角度
glClear(GL_COLOR_BUFFER_BIT); //清除颜色

z = -50.0f;


//保存矩阵状态并旋转
glPushMatrix();
glRotatef(30.0f,1.0f,0.0f,0.0f);
glRotatef(60.0f,0.0f,1.0f,0.0f);

glBegin(GL_LINE_STRIP);

     z = -50.0f;
  for(angle = 0.0f; angle <= (2.0f * GL_PI)*3.0f; angle += 0.1f)
  {
   x = 50.0f*sin(angle);
   y = 50.0f*cos(angle);

   glVertex3f(x,y,z);
   z += 0.5f;
  }
glEnd();
glPopMatrix();

glutSwapBuffers();
}


//当窗口大小变化时调用
void ChangeSize(GLsizei w,GLsizei h)
{
GLfloat nRange = 100.f;

if(0 == h)
  h = 1;
//设置视口
glViewport(0,0,w,h);

//重置投影矩阵堆栈
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//建立裁剪区
if(w <= h)
  glOrtho(-nRange,nRange,-nRange*h/w,nRange*h/w,-nRange,nRange);
else
  glOrtho(-nRange*w/h,nRange*w/h,-nRange,nRange,-nRange,nRange);

//重置模型视图矩阵堆栈
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

//设置渲染
void SetupRC(void)       
{
glClearColor(0.0f,0.0f,0.0f,1.0f); //背景黑色
glColor3f(0.0f,1.0f,0.0f);    //绘图颜色为绿色
}

int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("OpenGL Sample");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
SetupRC();
glutMainLoop();
return 0;
}

下面是程序的运行截图:

OpenGL几何图元和缓冲区(四)_第1张图片

睡觉......

你可能感兴趣的:(buffer)