Advertisement
Guest User

Untitled

a guest
May 30th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include "Dependencies\glew\glew.h"
  2. #include "Dependencies\freeglut\freeglut.h"
  3. #include <iostream>
  4. #include <vector>
  5. #include "Shader_Loader.h"
  6. #include <glm/glm.hpp>
  7. #include <glm/gtc/matrix_transform.hpp>
  8.  
  9.  
  10. #define BUFFER_OFFSET(offset) ((void*)(offset))
  11.  
  12.  
  13. GLuint SolidSphere(float radius, int slices, int stacks)
  14. {
  15. using namespace glm;
  16. using namespace std;
  17.  
  18. const float pi = 3.1415926535897932384626433832795f;
  19. const float _2pi = 2.0f * pi;
  20.  
  21. vector<vec4> positions;
  22. vector<vec4> normals;
  23. vector<vec2> textureCoords;
  24.  
  25. for (int i = 0; i <= stacks; ++i)
  26. {
  27. // wspolrzedna tekstury v
  28. float V = i / (float)stacks;
  29. float phi = V * pi;
  30.  
  31. for (int j = 0; j <= slices; ++j)
  32. {
  33. // wspolrzedna tekstury u
  34. float U = j / (float)slices;
  35. float theta = U * _2pi;
  36.  
  37. float X = cos(theta) * sin(phi);
  38. float Y = cos(phi);
  39. float Z = sin(theta) * sin(phi);
  40.  
  41. positions.push_back(vec4(X, Y, Z, 1.0) * radius);
  42. normals.push_back(vec4(X, Y, Z, 0.0));
  43. textureCoords.push_back(vec2(U, V));
  44. }
  45. }
  46.  
  47. // bufor indeksow
  48. vector<GLuint> indicies;
  49.  
  50. for (int i = 0; i < slices * stacks + slices; ++i)
  51. {
  52. indicies.push_back(i);
  53. indicies.push_back(i + slices + 1);
  54. indicies.push_back(i + slices);
  55.  
  56. indicies.push_back(i + slices + 1);
  57. indicies.push_back(i);
  58. indicies.push_back(i + 1);
  59. }
  60.  
  61. GLuint vao;
  62. glGenVertexArrays(1, &vao);
  63. glBindVertexArray(vao);
  64.  
  65. GLuint vbos[4];
  66. glGenBuffers(4, vbos);
  67.  
  68. glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
  69. glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(vec4), positions.data(), GL_STATIC_DRAW);
  70. glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
  71. glEnableVertexAttribArray(0);
  72.  
  73. glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
  74. glBufferData(GL_ARRAY_BUFFER, textureCoords.size() * sizeof(vec2), textureCoords.data(), GL_STATIC_DRAW);
  75. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
  76. glEnableVertexAttribArray(1);
  77.  
  78. glBindBuffer(GL_ARRAY_BUFFER, vbos[2]);
  79. glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(vec4), normals.data(), GL_STATIC_DRAW);
  80. glVertexAttribPointer(2, 4, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(0));
  81. glEnableVertexAttribArray(2);
  82.  
  83. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[3]);
  84. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicies.size() * sizeof(GLuint), indicies.data(), GL_STATIC_DRAW);
  85.  
  86. glBindVertexArray(0);
  87. glBindBuffer(GL_ARRAY_BUFFER, 0);
  88. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  89.  
  90. return vao;
  91. }
  92.  
  93.  
  94. GLuint LoadTexture(const std::string& file)
  95. {
  96. GLuint textureId = SOIL_load_OGL_texture(file.c_str(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS);
  97. glBindTexture(GL_TEXTURE_2D, textureId);
  98. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  99. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  100. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  101. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  102. glBindTexture(GL_TEXTURE_2D, 0);
  103.  
  104. return textureId;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement