Advertisement
RoshHoul

Untitled

Apr 1st, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.79 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 <glm/gtx/rotate_vector.hpp>
  12. #include "Shader.h"
  13. #include <vector>
  14. #include <string>
  15.  
  16.  
  17. using namespace std;
  18.  
  19. class Ball {
  20.  
  21. public:
  22.  
  23.     glm::vec3 ballPos;
  24.     glm::vec3 previousBallPos;
  25.     glm::vec3 dir;
  26.     vector <float> vertices;
  27.     vector <int> indices;
  28.     vector <float> UV;
  29.     string texturePath;
  30.     bool isColliding = 0;
  31.  
  32.  
  33.  
  34.     float radius = 1;
  35.     glm::vec3 v = glm::vec3(0.0f, 0.0f, 0.0f);
  36.     glm::vec3 a = glm::vec3(0.0f, 0.0f, 0.0f);
  37.     glm::vec3 F = glm::vec3(0.0f, 0.0f, 0.0f);
  38.     glm::vec3 friction = glm::vec3(0.0f, 0.0f, 0.0f);
  39.     glm::vec3 fa = glm::vec3(0.0f, 0.0f, 0.0f);
  40.     glm::vec3 fv = glm::vec3(0.0f, 0.0f, 0.0f);
  41.     glm::vec3 allForce = glm::vec3(0.0f, 0.0f, 0.0f);
  42.     float speed;
  43.     float forceAmount = 1.0f;
  44.  
  45.     GLuint VAO, VBO, EBO, VBO2;
  46.     GLint modelLoc;
  47.  
  48.     Ball(glm::vec3 position, string texturePath) {
  49.         this->ballPos = position;
  50.         this->texturePath = texturePath;
  51.     }
  52.  
  53.     void makeSphere() {
  54.  
  55.         int Stacks = 50;
  56.         int Slices = 50;
  57.  
  58.  
  59.         // CALCULATING VERTICES
  60.         for (int i = 0; i <= Stacks; ++i) {
  61.  
  62.             float V = i / (float)Stacks;
  63.             float phi = V * glm::pi <float>();
  64.  
  65.             for (int j = 0; j <= Slices; ++j) {
  66.  
  67.                 float U = j / (float)Slices;
  68.                 float theta = U * (glm::pi <float>() * 2);
  69.  
  70.                 float x = cosf(theta) * sinf(phi);
  71.                 float y = cosf(phi);
  72.                 float z = sinf(theta) * sinf(phi);
  73.  
  74.                 vertices.push_back(x * radius);
  75.                 vertices.push_back(y * radius);
  76.                 vertices.push_back(z * radius);
  77.  
  78.  
  79.             }
  80.         }
  81.  
  82.         // CALCULATING INDEX POSITIONS
  83.         for (int i = 0; i < Slices * Stacks + Slices; ++i) {
  84.  
  85.             indices.push_back(i);
  86.             indices.push_back(i + Slices + 1);
  87.             indices.push_back(i + Slices);
  88.  
  89.             indices.push_back(i + Slices + 1);
  90.             indices.push_back(i);
  91.             indices.push_back(i + 1);
  92.         }
  93.  
  94.         // CALCULATING UV COORDS
  95.         // u = 0.5 + arctan2(dz,dx)/(2*pi)
  96.         // v = 0.5 - arcsin(dy)/pi
  97.         // where d is the unit vector from the point to the origin
  98.         // 1. calculate unit vector at each point
  99.         // 2. calculate u
  100.         // 3. calculate v
  101.         // 4. create uv vector
  102.  
  103.         for (int i = 0; i < vertices.size(); i++) {
  104.             if (i % 3 == 0) {
  105.                 glm::vec3 d = glm::vec3(vertices[i] - ballPos[0], vertices[i + 1] - ballPos[1], vertices[i + 2] - ballPos[2]);
  106.                 d = glm::normalize(d);
  107.                 float u = 1 - (0.5 + (glm::atan(d[2], d[0]) / (glm::pi<float>() * 2)));
  108.                 float v = 0.5 - (glm::asin(d[1])) / glm::pi<float>();
  109.  
  110.                 UV.push_back(u);
  111.                 UV.push_back(v);
  112.             }
  113.         }
  114.  
  115.     }
  116.     void drawBall(Shader ourShader) {
  117.  
  118.  
  119.         // Build and compile our shader program
  120.         //Shader ourShader("shader.vs", "shader.frag");
  121.        
  122.         glGenVertexArrays(1, &VAO);
  123.         glGenBuffers(1, &VBO);
  124.         glGenBuffers(1, &EBO);
  125.         glGenBuffers(1, &VBO2);
  126.  
  127.         //  BIND VAO
  128.         glBindVertexArray(VAO);
  129.  
  130.         // ASSIGNING VBO AND EBO, POINTERS
  131.         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  132.         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
  133.  
  134.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
  135.         glEnableVertexAttribArray(0);
  136.  
  137.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  138.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLint), &indices[0], GL_STATIC_DRAW);
  139.  
  140.         glBindBuffer(GL_ARRAY_BUFFER, VBO2);
  141.         glBufferData(GL_ARRAY_BUFFER, UV.size() * sizeof(GLfloat), &UV[0], GL_STATIC_DRAW);
  142.  
  143.         glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
  144.         glEnableVertexAttribArray(2);
  145.  
  146.         glBindTexture(GL_TEXTURE_2D, texture1);
  147.         // UNBIND VAO
  148.         glBindVertexArray(0);
  149.  
  150.         modelLoc = glGetUniformLocation(ourShader.Program, "model");
  151.  
  152.  
  153.         // GETTING RID OF EVERYTHING BEFORE NEXT GAME LOOP
  154.         //glDeleteVertexArrays(1, &VAO);
  155.         //glDeleteBuffers(1, &VBO);
  156.         //glDeleteBuffers(1, &EBO);
  157.         //glDeleteBuffers(1, &VBO2);
  158.  
  159.         //a = a / 10.0f;
  160.     }
  161.  
  162.     void updateBall() {
  163.  
  164. //      cout << "UPDATING" << endl;
  165.  
  166.         extern float deltaTime;
  167.         friction = -F;
  168.         fa = (friction / radius);
  169.         fv = fv + (fa * deltaTime);
  170.  
  171.         a = (F / radius);
  172.         v = v + (a * deltaTime);
  173.  
  174.         speed = sqrt((a.x*a.x) + (a.y* a.y) + (a.z*a.z));
  175.  
  176.         allForce = (v + fv)*deltaTime;
  177.         ballPos = ballPos + v*deltaTime;
  178.  
  179.  
  180.         collision();
  181.  
  182.         glBindVertexArray(VAO);
  183.         glm::mat4 model;
  184.  
  185.         model = glm::translate(model, ballPos);
  186.         model = glm::rotate(model, glm::radians(40.0f), glm::vec3(1.0f, 0.0f, 0.0f));
  187.         glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  188.         glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
  189.         glBindVertexArray(0);
  190.        
  191.     }
  192.  
  193.     void clearBall() {
  194.         glDeleteVertexArrays(1, &VAO);
  195.         glDeleteBuffers(1, &VBO);
  196.         glDeleteBuffers(1, &EBO);
  197.         glDeleteBuffers(1, &VBO2);
  198.  
  199.     }
  200.  
  201.     void collision() {
  202.         extern vector <Ball> ballCollection;
  203.         for (int i = 0; i < ballCollection.size(); i++) {
  204.             if (glm::distance(ballCollection.at(i).ballPos, ballPos) <= (2 * radius)) {
  205.                 if (!glm::distance(ballCollection.at(i).ballPos, ballPos) == 0) {
  206.                     extern GLfloat forceAmount;
  207.                     if (isColliding == 0) {
  208.                         glm::vec3 collPoint = glm::vec3((ballPos.x + ballCollection.at(i).ballPos.x) / 2, 0.0f, (ballPos.z + ballCollection.at(i).ballPos.z) / 2);
  209.                         glm::vec3 collPointPerp = collPoint - ballPos;
  210.  
  211.                         glm::vec3 otherNew = collPointPerp;
  212.                         glm::vec3 thisNew = glm::cross(collPointPerp, glm::vec3(0.0, 1.0, 0.0));
  213.  
  214.                         F = thisNew;
  215.                         ballCollection.at(i).F = otherNew;
  216.  
  217.                         glm::vec3 newV;
  218.                         glm::vec3 otherNewV;
  219.  
  220.                         newV.x = (v.x * (2 * ballCollection.at(i).radius * ballCollection.at(i).v.x)) / (radius + ballCollection.at(i).radius);
  221.                         newV.z = (v.z * (2 * ballCollection.at(i).radius * ballCollection.at(i).v.z)) / (radius + ballCollection.at(i).radius);
  222.                         newV.y = 0.0f;
  223.  
  224.                         otherNewV.x = (ballCollection.at(i).v.x * (2 * radius * v.x)) / (radius + ballCollection.at(i).radius);
  225.                         otherNewV.z = (ballCollection.at(i).v.z * (2 * radius * v.z)) / (radius + ballCollection.at(i).radius);
  226.                         otherNewV.y = 0.0f;
  227.  
  228.                         v.x = newV.x;
  229.                         v.z = newV.z;
  230.  
  231.                         ballCollection.at(i).v.x = otherNewV.x;
  232.                         ballCollection.at(i).v.z = otherNewV.z;
  233.                     }
  234.                     isColliding = 1;
  235.                     ballCollection.at(i).isColliding = 1;
  236.                 }
  237.             }
  238.         }
  239.         isColliding = 0;
  240.         //   << isColliding << endl;
  241.     }
  242.  
  243.     GLuint texture1;
  244.     void loadTexture() {
  245.         glGenTextures(1, &texture1);
  246. //      glActiveTexture(GL_TEXTURE0);
  247.         glBindTexture(GL_TEXTURE_2D, texture1);
  248.  
  249.         int text_w, text_h;
  250.         unsigned char* image = SOIL_load_image(texturePath.c_str(), &text_w, &text_h, 0, SOIL_LOAD_RGB);
  251.  
  252.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, text_w, text_h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  253.         glGenerateMipmap(GL_TEXTURE_2D);
  254.  
  255.         SOIL_free_image_data(image);
  256.         glBindTexture(GL_TEXTURE_2D, 0);
  257.     }
  258. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement