Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void draw_sphere(int n) {
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glBegin(GL_POLYGON);
- double pi = 3.141591;
- double r = 0.75;
- double step = 2 * pi / n;
- double phi = -pi / 2;
- double theta = 0;
- double phi1;
- for (int i = 0; i < n; ++i) {
- if (i == 0 || n == i - 1) {
- phi1 = phi;
- } else {
- phi1 = phi + step / 2;
- if (phi1 > 2 * pi) {
- phi1 -= 2 * pi;
- }
- }
- for (int j = 0; j < n; ++j) {
- double theta1 = theta + step;
- if (theta1 > 2 * pi) {
- theta1 -= 2 * pi;
- }
- auto sphericToCortesian = [&r](double phi, double theta) -> std::vector<double> {
- return {r * cos(phi) * cos(theta), r * cos(phi) * sin(theta), r * sin(phi)};
- };
- auto p1 = sphericToCortesian(phi, theta);
- auto p2 = sphericToCortesian(phi, theta1);
- auto p3 = sphericToCortesian(phi1, theta);
- auto p4 = sphericToCortesian(phi1, theta1);
- auto drawPoint = [&r](const std::vector<double> &p) {
- static const double light[3] = { 0, 0, -1 };
- double normal[3] = { p[0] / r, p[1] / r, p[2] / r };
- double intens = light[0] * normal[0] + light[1] * normal[1] + light[2] * normal[2];
- glColor3d(intens, intens, intens);
- glVertex3d(p[0], p[1], p[2]);
- };
- drawPoint(p1);
- drawPoint(p2);
- drawPoint(p4);
- drawPoint(p3);
- theta += step;
- if (theta > 2 * pi) {
- theta -= 2 * pi;
- }
- }
- phi += step / 2;
- if (phi > 2 * pi) {
- phi -= 2 * pi;
- }
- }
- glEnd();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement