opengl生成一个半球
时间: 2023-05-16 11:06:27 浏览: 77
非常感谢您的提问。以下是生成一个半球的 OpenGL 代码示例:
```
#include <GL/glut.h>
#include <math.h>
void display() {
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);
glBegin(GL_QUADS);
for (int i = 0; i < 180; i += 10) {
for (int j = 0; j < 360; j += 10) {
float x1 = sin(i * M_PI / 180) * cos(j * M_PI / 180);
float y1 = sin(i * M_PI / 180) * sin(j * M_PI / 180);
float z1 = cos(i * M_PI / 180);
float x2 = sin((i + 10) * M_PI / 180) * cos(j * M_PI / 180);
float y2 = sin((i + 10) * M_PI / 180) * sin(j * M_PI / 180);
float z2 = cos((i + 10) * M_PI / 180);
float x3 = sin((i + 10) * M_PI / 180) * cos((j + 10) * M_PI / 180);
float y3 = sin((i + 10) * M_PI / 180) * sin((j + 10) * M_PI / 180);
float z3 = cos((i + 10) * M_PI / 180);
float x4 = sin(i * M_PI / 180) * cos((j + 10) * M_PI / 180);
float y4 = sin(i * M_PI / 180) * sin((j + 10) * M_PI / 180);
float z4 = cos(i * M_PI / 180);
glNormal3f(x1, y1, z1);
glVertex3f(x1, y1, z1);
glNormal3f(x2, y2, z2);
glVertex3f(x2, y2, z2);
glNormal3f(x3, y3, z3);
glVertex3f(x3, y3, z3);
glNormal3f(x4, y4, z4);
glVertex3f(x4, y4, z4);
}
}
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Half Sphere");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
```
希望这个代码能够帮助到您。