使用OpenGL编程实现一个三维渲染三棱柱
时间: 2024-02-21 15:54:24 浏览: 153
以下是使用OpenGL编程实现一个三维渲染三棱柱的代码示例:
```python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0)
glBegin(GL_TRIANGLES)
glColor3f(1, 0, 0)
glVertex3f(-1, -1, 0)
glVertex3f(1, -1, 0)
glVertex3f(0, 1, 0)
glColor3f(0, 1, 0)
glVertex3f(-1, -1, 0)
glVertex3f(0, -1, 1)
glVertex3f(0, 1, 0)
glColor3f(0, 0, 1)
glVertex3f(1, -1, 0)
glVertex3f(0, -1, 1)
glVertex3f(0, 1, 0)
glEnd()
glutSwapBuffers()
glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(400, 400)
glutCreateWindow("OpenGL 3D")
glutDisplayFunc(draw)
glEnable(GL_DEPTH_TEST)
glutMainLoop()
```
这段代码使用OpenGL绘制了一个三棱柱,其中gluLookAt()函数用于设置视角,glBegin()和glEnd()函数用于绘制三角形,glColor3f()函数用于设置颜色,glVertex3f()函数用于设置顶点坐标。
阅读全文