Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include "sphere.h"
  2.  
  3. #include <vector>
  4. #include <iostream>
  5. #include <glm/gtc/matrix_inverse.hpp>
  6. #include <glm/gtc/type_ptr.hpp>
  7. #include <glm/gtx/string_cast.hpp>
  8.  
  9. Sphere::Sphere()
  10. {
  11. isInited = false;
  12. m_vao = 0;
  13. m_vboVertex = 0;
  14. m_vboIndex = 0;
  15.  
  16. lats = 40;
  17. longs = 40;
  18. }
  19.  
  20. Sphere::~Sphere()
  21. {
  22.  
  23. }
  24.  
  25. void Sphere::init(GLuint vertexPositionID)
  26. {
  27. int i, j;
  28. std::vector<GLfloat> vertices;
  29. std::vector<GLuint> indices;
  30. int indicator = 0;
  31. for(i = 0; i <= lats; i++) {
  32. double lat0 = glm::pi<double>() * (-0.5 + (double) (i - 1) / lats);
  33. double z0 = sin(lat0);
  34. double zr0 = cos(lat0);
  35.  
  36. double lat1 = glm::pi<double>() * (-0.5 + (double) i / lats);
  37. double z1 = sin(lat1);
  38. double zr1 = cos(lat1);
  39.  
  40. for(j = 0; j <= longs; j++) {
  41. double lng = 2 * glm::pi<double>() * (double) (j - 1) / longs;
  42. double x = cos(lng);
  43. double y = sin(lng);
  44.  
  45. vertices.push_back(x * zr0);
  46. vertices.push_back(y * zr0);
  47. vertices.push_back(z0);
  48. indices.push_back(indicator);
  49. indicator++;
  50.  
  51. vertices.push_back(x * zr1);
  52. vertices.push_back(y * zr1);
  53. vertices.push_back(z1);
  54. indices.push_back(indicator);
  55. indicator++;
  56. }
  57. indices.push_back(GL_PRIMITIVE_RESTART_FIXED_INDEX);
  58. }
  59.  
  60. // 创建并绑定环境
  61. glGenVertexArrays(1, &m_vao);
  62. glBindVertexArray(m_vao);
  63.  
  64. glGenBuffers(1, &m_vboVertex);
  65. glBindBuffer(GL_ARRAY_BUFFER, m_vboVertex);
  66. glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
  67.  
  68. glVertexAttribPointer(vertexPositionID, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  69. glEnableVertexAttribArray (vertexPositionID);
  70.  
  71. glGenBuffers(1, &m_vboIndex);
  72. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vboIndex);
  73. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
  74.  
  75. numsToDraw = indices.size();
  76.  
  77. isInited = true;
  78. }
  79.  
  80. void Sphere::cleanup()
  81. {
  82. if (!isInited) {
  83. return;
  84. }
  85. if(m_vboVertex) {
  86. glDeleteBuffers(1, &m_vboVertex);
  87. }
  88. if(m_vboIndex) {
  89. glDeleteBuffers(1, &m_vboIndex);
  90. }
  91. if (m_vao) {
  92. glDeleteVertexArrays(1, &m_vao);
  93. }
  94.  
  95. isInited = false;
  96. m_vao = 0;
  97. m_vboVertex = 0;
  98. m_vboIndex = 0;
  99. }
  100.  
  101. void Sphere::draw()
  102. {
  103. if (!isInited) {
  104. std::cout << "please call init() before draw()" << std::endl;
  105. }
  106.  
  107. // draw sphere
  108. glBindVertexArray(m_vao);
  109. glEnable(GL_PRIMITIVE_RESTART);
  110. glPrimitiveRestartIndex(GL_PRIMITIVE_RESTART_FIXED_INDEX);
  111. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vboIndex);
  112. glDrawElements(GL_QUAD_STRIP, numsToDraw, GL_UNSIGNED_INT, NULL);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement