opengl绘制球体的代码
时间: 2023-07-09 12:31:26 浏览: 66
以下是使用OpenGL绘制球体的示例代码:
```
#include <GL/glut.h>
#include <math.h>
// 定义球体的半径和细分度
#define RADIUS 1.0
#define SLICES 32
#define STACKS 32
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// 绘制球体
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
glutSolidSphere(RADIUS, SLICES, STACKS);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("OpenGL Sphere");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
```
以上代码使用OpenGL绘制了一个球体,并使用gluLookAt函数设置了相机位置和朝向。glutSolidSphere函数用于绘制球体,接受三个参数:球体的半径、细分度和细分度。在主函数中,我们启用了深度测试并调用glutMainLoop函数进入主循环。