Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. void CGLRenderer::DrawShpere(float radius, int steps, int stacks)
  2. {
  3. if (SphereCoor.size() > 0) {
  4. SphereCoor.clear();
  5. }
  6. float dAlpha = M_PI * 2 / steps;
  7. float dTheta = M_PI / stacks;
  8.  
  9. float radiusPrim, x, y, z, tempThetaAngle;
  10. for (float theta = -M_PI / 2; theta <= M_PI/2 + dTheta; theta += dTheta) {
  11. for (float alpha = 0.0f; alpha <= M_PI * 2; alpha += dAlpha) {
  12.  
  13. radiusPrim = cos(theta) * radius;
  14.  
  15. x = radiusPrim * sin(alpha);
  16. z = radiusPrim * cos(alpha);
  17. y = radius * sin(theta);
  18.  
  19. SphereCoor.push_back(x); SphereCoor.push_back(y); SphereCoor.push_back(z);
  20.  
  21. // norm
  22. float nx = cos(theta)*sin(alpha);
  23. float ny = sin(theta);
  24. float nz = cos(theta)*cos(alpha);
  25. SphereCoor.push_back(nx); SphereCoor.push_back(ny); SphereCoor.push_back(nz);
  26.  
  27. // text
  28. float u = 0.5 + atan2(nz, nx) / (2 * M_PI);
  29. float v = 0.5 - asin(ny) / M_PI;
  30. SphereCoor.push_back(v); SphereCoor.push_back(u);
  31.  
  32. tempThetaAngle = theta - dTheta;
  33. radiusPrim = cos(tempThetaAngle) * radius;
  34.  
  35. x = radiusPrim * sin(alpha);
  36. z = radiusPrim * cos(alpha);
  37. y = radius * sin(tempThetaAngle);
  38.  
  39. SphereCoor.push_back(x); SphereCoor.push_back(y); SphereCoor.push_back(z);
  40.  
  41. // norm
  42. nx = cos(tempThetaAngle)*sin(alpha);
  43. ny = sin(tempThetaAngle);
  44. nz = cos(tempThetaAngle)*cos(alpha);
  45. SphereCoor.push_back(nx); SphereCoor.push_back(ny); SphereCoor.push_back(nz);
  46.  
  47.  
  48. //text
  49. u = 0.5 + atan2(nz, nx) / (2 * M_PI);
  50. v = 0.5 - asin(ny) / M_PI;
  51. SphereCoor.push_back(v); SphereCoor.push_back(u);
  52.  
  53. // normals nx = cos(theta)*sin(alpha) ny=sin(theta) nz=cos(theta)*cos(alpha)
  54. }
  55. }
  56.  
  57. glVertexPointer(3, GL_FLOAT, 8 * sizeof(float), SphereCoor.data());
  58. glNormalPointer(GL_FLOAT, 8 * sizeof(float), &SphereCoor.data()[3]);
  59. glTexCoordPointer(2, GL_FLOAT, 8 * sizeof(float), &SphereCoor.data()[6]);
  60.  
  61. glEnableClientState(GL_VERTEX_ARRAY);
  62. glEnableClientState(GL_NORMAL_ARRAY);
  63. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  64.  
  65. glDrawArrays(GL_QUAD_STRIP, 0, SphereCoor.size() / 8);
  66.  
  67. glDisableClientState(GL_VERTEX_ARRAY);
  68. glDisableClientState(GL_NORMAL_ARRAY);
  69. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement