OpenGL几何图元绘制

在glBegin和glEnd之间执行有效的函数

glVertex*()    //设置顶点坐标
glColor*()      //设置当前颜色
glIndex*()     //设置当前颜色索引
glNormal*()   //设置法线向量坐标
glTexCoord*()    //设置纹理坐标
glMultiTexCoord*ARB()  //为多重纹理映射设置纹理坐标
glEdgeFlag*()    //控制边的绘制
glMaterial*()      //设置材质属性
glArrayElement()  //抽取顶点数组数据
glEvalCoord*()、glEvalPoint*()  //生成坐标
glCallList()、glCallLists()        //执行显示列表


  缺省逆时针(GL_CCW)为正方向,可通过glFrontFace来指定面元的正面。

OpenGL几何图元绘制_第1张图片

示例代码:

void DrawMyObjects(void)
{
  /* draw some points */
  glBegin(GL_POINTS);
    glColor3f(1.0,0.0,0.0);
    glVertex2f(-10.0,11.0);
    glColor3f(1.0,1.0,0.0);
    glVertex2f(-9.0,10.0);
    glColor3f(0.0,1.0,1.0);
    glVertex2f(-8.0,12.0);
  glEnd();

  /* draw some line_segments */
  glBegin(GL_LINES);
    glColor3f(1.0,1.0,0.0);
    glVertex2f(-11.0,8.0);
    glVertex2f(-7.0,7.0);
    glColor3f(1.0,0.0,1.0);
    glVertex2f(-11.0,9.0);
    glVertex2f(-8.0,6.0);
  glEnd();

  /* draw one opened_line */
  glBegin(GL_LINE_STRIP);
    glColor3f(0.0,1.0,0.0);
    glVertex2f(-3.0,9.0);
    glVertex2f(2.0,6.0);
    glVertex2f(3.0,8.0);
    glVertex2f(-2.5,6.5);
  glEnd();

  /* draw one closed_line */
  glBegin(GL_LINE_LOOP);
    glColor3f(0.0,1.0,1.0);
    glVertex2f(7.0,7.0);
    glVertex2f(8.0,8.0);
    glVertex2f(9.0,6.5);
    glVertex2f(10.3,7.5);
    glVertex2f(11.5,6.0);
    glVertex2f(7.5,6.0);
  glEnd();

  /* draw one filled_polygon */
  glBegin(GL_POLYGON);
    glColor3f(0.5,0.3,0.7);
    glVertex2f(-7.0,2.0);
    glVertex2f(-8.0,3.0);
    glVertex2f(-10.3,0.5);
    glVertex2f(-7.5,-2.0);
    glVertex2f(-6.0,-1.0);
  glEnd();

  /* draw some filled_quandrangles */
  glBegin(GL_QUADS);
    glColor3f(0.7,0.5,0.2);
    glVertex2f(0.0,2.0);
    glVertex2f(-1.0,3.0);
    glVertex2f(-3.3,0.5);
    glVertex2f(-0.5,-1.0);
    glColor3f(0.5,0.7,0.2);
    glVertex2f(3.0,2.0);
    glVertex2f(2.0,3.0);
    glVertex2f(0.0,0.5);
    glVertex2f(2.5,-1.0);
  glEnd();

  /* draw some filled_strip_quandrangles */
  glBegin(GL_QUAD_STRIP);
    glVertex2f(6.0,-2.0);
    glVertex2f(5.5,1.0);
    glVertex2f(8.0,-1.0);
    glColor3f(0.8,0.0,0.0);
    glVertex2f(9.0,2.0);
    glVertex2f(11.0,-2.0);
    glColor3f(0.0,0.0,0.8);
    glVertex2f(11.0,2.0);
    glVertex2f(13.0,-1.0);
    glColor3f(0.0,0.8,0.0);
    glVertex2f(14.0,1.0);
  glEnd();

  /* draw some filled_triangles */
  glBegin(GL_TRIANGLES);
    glColor3f(0.2,0.5,0.7);
    glVertex2f(-10.0,-5.0);
    glVertex2f(-12.3,-7.5);
    glVertex2f(-8.5,-6.0);
    glColor3f(0.2,0.7,0.5);
    glVertex2f(-8.0,-7.0);
    glVertex2f(-7.0,-4.5);
    glVertex2f(-5.5,-9.0);
  glEnd();

  /* draw some filled_strip_triangles */
  glBegin(GL_TRIANGLE_STRIP);
    glVertex2f(-1.0,-8.0);
    glVertex2f(-2.5,-5.0);
    glColor3f(0.8,0.8,0.0);
    glVertex2f(1.0,-7.0);
    glColor3f(0.0,0.8,0.8);
    glVertex2f(2.0,-4.0);
    glColor3f(0.8,0.0,0.8);
    glVertex2f(4.0,-6.0);
  glEnd();

  /* draw some filled_fan_triangles */
  glBegin(GL_TRIANGLE_FAN);
    glVertex2f(8.0,-6.0);
    glVertex2f(10.0,-3.0);
    glColor3f(0.8,0.2,0.5);
    glVertex2f(12.5,-4.5);
    glColor3f(0.2,0.5,0.8);
    glVertex2f(13.0,-7.5);
    glColor3f(0.8,0.5,0.2);
    glVertex2f(10.5,-9.0);
  glEnd();
}

点的大小[方形]  反走样后点为圆形

void glPointSize(GLfloat size);

线宽

void glLineWidth(GLfloat width);

点画线 glEnable(GL_LINE_STIPPLE)

void glLineStipple(GLint factor, GLushort pattern);

多边形模式

线框模式:glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

void glPolygonMode(GLenum face, GLenum mode);

多边形剔除 glEnable(GL_CULL_FACE)

剔除反面:glCullFace(GL_BACK);

void glCullFace(GLenum mode);

点画多边形 glEnable(GL_POLYGON_STIPPLE)

void glPolygonStipple(const GLubyte *mask);

法线向量

归一化(开销较下要大)   glEnable(GL_NORMALIZE)

均匀缩放归一化     glEnable(GL_RESCALE_NORMAL)

 

你可能感兴趣的:(OpenGL几何图元绘制)