Advertisement
RoshHoul

ball with IDs

Mar 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #define GLEW_STATIC
  4. #include <GL/glew.h>
  5. #include <GLFW/glfw3.h>
  6. #include <iostream>
  7. #include <SOIL.h>
  8. #include <glm/glm.hpp>
  9. #include <glm/gtc/matrix_transform.hpp>
  10. #include <glm/gtc/type_ptr.hpp>
  11. #include "Shader.h"
  12. #include <vector>
  13. #include <string>
  14.  
  15. using namespace std;
  16. class Ball {
  17. public:
  18. glm::vec3 ballPos;
  19. vector <float> vertices;
  20. vector <int> indices;
  21. vector <float> UV;
  22. string texturePath;
  23. GLuint textures[16] = { 1, 2, 3,
  24. 4, 5, 6,
  25. 7, 8, 9,
  26. 10, 11, 12,
  27. 13, 14, 15, 100 };
  28.  
  29.  
  30. // TEXTURES
  31. string texturePaths[16] = { "1.jpg","2.jpg","3.jpg","4.jpg",
  32. "5.jpg","6.jpg","7.jpg","8.jpg",
  33. "9.jpg","10.jpg","11.jpg","12.jpg",
  34. "13.jpg","14.jpg","15.jpg","0White.jpg" };
  35.  
  36. int textID;
  37. Ball(glm::vec3 position, int textID) {
  38. this->ballPos = position;
  39. this->texturePath = texturePath;
  40. this->textID = textID;
  41.  
  42. }
  43. glm::vec3 getPos() {
  44. return ballPos;
  45. }
  46.  
  47.  
  48. void makeSphere() {
  49.  
  50. int Stacks = 50;
  51. int Slices = 50;
  52. float Radius = 1;
  53.  
  54. // CALCULATING VERTICES
  55. for (int i = 0; i <= Stacks; ++i) {
  56.  
  57. float V = i / (float)Stacks;
  58. float phi = V * glm::pi <float>();
  59.  
  60. for (int j = 0; j <= Slices; ++j) {
  61.  
  62. float U = j / (float)Slices;
  63. float theta = U * (glm::pi <float>() * 2);
  64.  
  65. float x = cosf(theta) * sinf(phi);
  66. float y = cosf(phi);
  67. float z = sinf(theta) * sinf(phi);
  68.  
  69. vertices.push_back(x * Radius);
  70. vertices.push_back(y * Radius);
  71. vertices.push_back(z * Radius);
  72.  
  73.  
  74. }
  75. }
  76.  
  77. // CALCULATING INDEX POSITIONS
  78. for (int i = 0; i < Slices * Stacks + Slices; ++i) {
  79.  
  80. indices.push_back(i);
  81. indices.push_back(i + Slices + 1);
  82. indices.push_back(i + Slices);
  83.  
  84. indices.push_back(i + Slices + 1);
  85. indices.push_back(i);
  86. indices.push_back(i + 1);
  87. }
  88.  
  89. // CALCULATING UV COORDS
  90. // u = 0.5 + arctan2(dz,dx)/(2*pi)
  91. // v = 0.5 - arcsin(dy)/pi
  92. // where d is the unit vector from the point to the origin
  93. // 1. calculate unit vector at each point
  94. // 2. calculate u
  95. // 3. calculate v
  96. // 4. create uv vector
  97.  
  98. for (int i = 0; i < vertices.size(); i++) {
  99. if (i % 3 == 0) {
  100. glm::vec3 d = glm::vec3(vertices[i] - ballPos[0], vertices[i + 1] - ballPos[1], vertices[i + 2] - ballPos[2]);
  101. d = glm::normalize(d);
  102. float u = 1 - (0.5 + (glm::atan(d[2], d[0]) / (glm::pi<float>() * 2)));
  103. float v = 0.5 - (glm::asin(d[1])) / glm::pi<float>();
  104.  
  105. UV.push_back(u);
  106. UV.push_back(v);
  107. }
  108. }
  109.  
  110. }
  111. void drawBall(Shader ourShader) {
  112. // Build and compile our shader program
  113. // Shader ourShader("shader.vs", "shader.frag");
  114. GLuint VAO, VBO, EBO, VBO2;
  115. glGenVertexArrays(1, &VAO);
  116. glGenBuffers(1, &VBO);
  117. glGenBuffers(1, &EBO);
  118. glGenBuffers(1, &VBO2);
  119.  
  120. // BIND VAO
  121. glBindVertexArray(VAO);
  122.  
  123. // CREATE THE SPHERE
  124. //makeSphere();
  125.  
  126. // ASSIGNING VBO AND EBO, POINTERS
  127. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  128. glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
  129.  
  130.  
  131. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
  132. glEnableVertexAttribArray(0);
  133.  
  134. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  135. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLint), &indices[0], GL_STATIC_DRAW);
  136.  
  137. glBindBuffer(GL_ARRAY_BUFFER, VBO2);
  138. glBufferData(GL_ARRAY_BUFFER, UV.size() * sizeof(GLfloat), &UV[0], GL_STATIC_DRAW);
  139.  
  140. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
  141. glEnableVertexAttribArray(2);
  142.  
  143. // UNBIND VAO
  144. glBindVertexArray(0);
  145.  
  146. glBindTexture(GL_TEXTURE_2D, textures[textID]);
  147. GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
  148. //ACTUAL DRAWING
  149. glBindVertexArray(VAO);
  150. glm::mat4 model;
  151. model = glm::translate(model, ballPos);
  152. glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  153. glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
  154. glBindVertexArray(0);
  155.  
  156. // GETTING RID OF EVERYTHING BEFORE NEXT GAME LOOP
  157. glDeleteVertexArrays(1, &VAO);
  158. glDeleteBuffers(1, &VBO);
  159. glDeleteBuffers(1, &EBO);
  160. glDeleteBuffers(1, &VBO2);
  161.  
  162.  
  163. }
  164.  
  165. void loadTexture() {
  166.  
  167.  
  168. glGenTextures(1, &textures[textID]);
  169. glBindTexture(GL_TEXTURE_2D, textures[textID]);
  170.  
  171. int text_w, text_h;
  172. unsigned char* image = SOIL_load_image(texturePaths[textID].c_str(), &text_w, &text_h, 0, SOIL_LOAD_RGB);
  173.  
  174. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, text_w, text_h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  175. glGenerateMipmap(GL_TEXTURE_2D);
  176.  
  177. SOIL_free_image_data(image);
  178. glBindTexture(GL_TEXTURE_2D, 0);
  179. }
  180.  
  181. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement