四、面
面有好多种,主要包括:
独立三角形 GL_TRIANGLES
三角形条带 GL_TRIANGLE_STRIP
三角形扇面 GL_TRIANGLE_FAN
1,独立三角形
def drawPolygon():
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT)
# 改变投影矩阵
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
glColor3f(0.0, 1.0, 0.0)
glBegin(GL_TRIANGLES)
glVertex3f(-0.5, -0.5, 0.0)
glVertex3f(-0.5, 0.5, 0.0)
glVertex3f(0.5, -0.5, 0.0)
glEnd()
glFlush()
跟线一样,三角形是三个顶点一块出现的,不能配对的点将被舍弃。
2,三角形条带
def drawPolygon():
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT)
# 改变投影矩阵
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
glColor3f(0.0, 1.0, 0.0)
glBegin(GL_TRIANGLE_STRIP)
glVertex3f(-0.5, 0.5, 0.0)
glVertex3f(-0.5, -0.5, 0.0)
glVertex3f(0.5, -0.5, 0.0)
glVertex3f(0.5, 0.5, 0.0)
glEnd()
glFlush()
3,三角形扇面
def myDisplay():
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT)
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
glBegin(GL_TRIANGLE_FAN)
glColor3f(0.0, 1.0, 0.0)
glVertex3f(0.5, 0.5, 0)
glVertex3f(0.5, 0.0, 0)
glVertex3f(0.8536, 0.1464, 0)
glVertex3f(1.0, 0.5, 0)
glVertex3f(0.8536, 0.8536, 0)
glVertex3f(0.5, 1.0, 0)
glVertex3f(0.1464, 0.8536, 0)
glVertex3f(0.0, 0.5, 0)
glVertex3f(0.1464, 0.1464, 0)
glVertex3f(0.5, 0.0, 0)
glEnd()
glFlush()
使用GL_TRIANGLE_FAN参数是将第一个端点作为基点,与后面的两两端点成三角形。例如123456这六个端点,组成的三角形为123,134,145,156。这样的话,多个三角形就会组成不规则扇形。
4,四边形
def drawPolygon():
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT)
# 改变投影矩阵
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
glColor3f(0.0, 1.0, 0.0)
glBegin(GL_QUADS)
glVertex3f(-0.5, 0.5, 0.0)
glVertex3f(-0.5, -0.5, 0.0)
glVertex3f(0.5, -0.5, 0.0)
glVertex3f(0.5, 0.5, 0.0)
glEnd()
glFlush()
5,多边形
def drawPolygon():
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT)
# 改变投影矩阵
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
glColor3f(0.0, 1.0, 0.0)
glBegin(GL_POLYGON)
glVertex3f(-0.5, 0.5, 0.0)
glVertex3f(-0.5, -0.5, 0.0)
glVertex3f(0.5, -0.5, 0.0)
glVertex3f(0.5, 0.5, 0.0)
glVertex3f(0.0, 0.8, 0.0)
glEnd()
glFlush()