使用opengl写生成球体的代码
时间: 2024-02-23 20:58:14 浏览: 16
以下是使用OpenGL生成球体的代码:
```c++
#include <GL/glut.h>
#include <cmath>
void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
glEnable(GL_DEPTH_TEST);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
/* modeling transformation */
glRotatef(45.0, 0.0, 1.0, 0.0);
glutWireSphere(1.0, 20, 16);
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
```
这段代码使用了OpenGL的Wire Sphere函数来绘制球体,该函数使用三个参数:球的半径、经线数目和纬线数目。经线和纬线数目越大,球体的细节就越丰富。在上面的代码中,经线和纬线数目分别为20和16。