OpenGL画圆球代码
时间: 2023-12-20 09:29:16 浏览: 40
是使用OpenGL画圆球的代码:
```python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
def drawSphere(radius, slices, stacks):
for i in range(stacks):
lat0 = math.pi * (-0.5 + float(float(i - 1) / float(stacks)))
z0 = radius * math.sin(lat0)
zr0 = radius * math.cos(lat0)
lat1 = math.pi * (-0.5 + float(float(i) / float(stacks)))
z1 = radius * math.sin(lat1)
zr1 = radius * math.cos(lat1)
glBegin(GL_QUAD_STRIP)
for j in range(slices):
lng = 2 * math.pi * float(float(j - 1) / float(slices))
x = math.cos(lng)
y = math.sin(lng)
glNormal3f(x * zr0, y * zr0, z0)
glVertex3f(x * zr0, y * zr0, z0)
glNormal3f(x * zr1, y * zr1, z1)
glVertex3f(x * zr1, y * zr1, z1)
glEnd()
def draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0)
glColor3f(1, 1, 1)
drawSphere(1, 50, 50)
glutSwapBuffers()
if __name__ == '__main__':
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(800, 600)
glutCreateWindow("OpenGL Sphere")
glEnable(GL_DEPTH_TEST)
glutDisplayFunc(draw)
glutMainLoop()
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)