Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. void draw_sphere(int n) {
  2.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  3.     glBegin(GL_POLYGON);
  4.     double pi = 3.141591;
  5.     double r = 0.75;
  6.     double step = 2 * pi / n;
  7.     double phi = -pi / 2;
  8.     double theta = 0;
  9.     double phi1;
  10.     for (int i = 0; i < n; ++i) {
  11.         if (i == 0 || n == i - 1) {
  12.             phi1 = phi;
  13.         } else {
  14.             phi1 = phi + step / 2;
  15.             if (phi1 > 2 * pi) {
  16.                 phi1 -= 2 * pi;
  17.             }
  18.         }
  19.         for (int j = 0; j < n; ++j) {
  20.             double theta1 = theta + step;
  21.             if (theta1 > 2 * pi) {
  22.                 theta1 -= 2 * pi;
  23.             }
  24.  
  25.             auto sphericToCortesian = [&r](double phi, double theta) -> std::vector<double> {
  26.                 return {r * cos(phi) * cos(theta), r * cos(phi) * sin(theta), r * sin(phi)};
  27.             };
  28.  
  29.             auto p1 = sphericToCortesian(phi, theta);
  30.             auto p2 = sphericToCortesian(phi, theta1);
  31.             auto p3 = sphericToCortesian(phi1, theta);
  32.             auto p4 = sphericToCortesian(phi1, theta1);
  33.  
  34.  
  35.             auto drawPoint = [&r](const std::vector<double> &p) {
  36.                 static const double light[3] = { 0, 0, -1 };
  37.                 double normal[3] = { p[0] / r, p[1] / r, p[2] / r };
  38.                 double intens = light[0] * normal[0] + light[1] * normal[1] + light[2] * normal[2];
  39.                 glColor3d(intens, intens, intens);
  40.                 glVertex3d(p[0], p[1], p[2]);
  41.             };
  42.  
  43.             drawPoint(p1);
  44.             drawPoint(p2);
  45.             drawPoint(p4);
  46.             drawPoint(p3);
  47.  
  48.             theta += step;
  49.             if (theta > 2 * pi) {
  50.                 theta -= 2 * pi;
  51.             }
  52.         }
  53.         phi += step / 2;
  54.         if (phi > 2 * pi) {
  55.             phi -= 2 * pi;
  56.         }
  57.     }
  58.     glEnd();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement