Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #define GLEW_STATIC
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <SOIL.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <glm/gtx/rotate_vector.hpp>
- #include "Shader.h"
- #include <vector>
- #include <string>
- using namespace std;
- class Ball {
- public:
- glm::vec3 ballPos;
- glm::vec3 previousBallPos;
- glm::vec3 dir;
- vector <float> vertices;
- vector <int> indices;
- vector <float> UV;
- string texturePath;
- bool isColliding = 0;
- float radius = 1;
- glm::vec3 v = glm::vec3(0.0f, 0.0f, 0.0f);
- glm::vec3 a = glm::vec3(0.0f, 0.0f, 0.0f);
- glm::vec3 F = glm::vec3(0.0f, 0.0f, 0.0f);
- glm::vec3 friction = glm::vec3(0.0f, 0.0f, 0.0f);
- glm::vec3 fa = glm::vec3(0.0f, 0.0f, 0.0f);
- glm::vec3 fv = glm::vec3(0.0f, 0.0f, 0.0f);
- glm::vec3 allForce = glm::vec3(0.0f, 0.0f, 0.0f);
- float speed;
- float forceAmount = 1.0f;
- GLuint VAO, VBO, EBO, VBO2;
- GLint modelLoc;
- Ball(glm::vec3 position, string texturePath) {
- this->ballPos = position;
- this->texturePath = texturePath;
- }
- void makeSphere() {
- int Stacks = 50;
- int Slices = 50;
- // CALCULATING VERTICES
- for (int i = 0; i <= Stacks; ++i) {
- float V = i / (float)Stacks;
- float phi = V * glm::pi <float>();
- for (int j = 0; j <= Slices; ++j) {
- float U = j / (float)Slices;
- float theta = U * (glm::pi <float>() * 2);
- float x = cosf(theta) * sinf(phi);
- float y = cosf(phi);
- float z = sinf(theta) * sinf(phi);
- vertices.push_back(x * radius);
- vertices.push_back(y * radius);
- vertices.push_back(z * radius);
- }
- }
- // CALCULATING INDEX POSITIONS
- for (int i = 0; i < Slices * Stacks + Slices; ++i) {
- indices.push_back(i);
- indices.push_back(i + Slices + 1);
- indices.push_back(i + Slices);
- indices.push_back(i + Slices + 1);
- indices.push_back(i);
- indices.push_back(i + 1);
- }
- // CALCULATING UV COORDS
- // u = 0.5 + arctan2(dz,dx)/(2*pi)
- // v = 0.5 - arcsin(dy)/pi
- // where d is the unit vector from the point to the origin
- // 1. calculate unit vector at each point
- // 2. calculate u
- // 3. calculate v
- // 4. create uv vector
- for (int i = 0; i < vertices.size(); i++) {
- if (i % 3 == 0) {
- glm::vec3 d = glm::vec3(vertices[i] - ballPos[0], vertices[i + 1] - ballPos[1], vertices[i + 2] - ballPos[2]);
- d = glm::normalize(d);
- float u = 1 - (0.5 + (glm::atan(d[2], d[0]) / (glm::pi<float>() * 2)));
- float v = 0.5 - (glm::asin(d[1])) / glm::pi<float>();
- UV.push_back(u);
- UV.push_back(v);
- }
- }
- }
- void drawBall(Shader ourShader) {
- // Build and compile our shader program
- //Shader ourShader("shader.vs", "shader.frag");
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- glGenBuffers(1, &VBO2);
- // BIND VAO
- glBindVertexArray(VAO);
- // ASSIGNING VBO AND EBO, POINTERS
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLint), &indices[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, VBO2);
- glBufferData(GL_ARRAY_BUFFER, UV.size() * sizeof(GLfloat), &UV[0], GL_STATIC_DRAW);
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
- glEnableVertexAttribArray(2);
- glBindTexture(GL_TEXTURE_2D, texture1);
- // UNBIND VAO
- glBindVertexArray(0);
- modelLoc = glGetUniformLocation(ourShader.Program, "model");
- // GETTING RID OF EVERYTHING BEFORE NEXT GAME LOOP
- //glDeleteVertexArrays(1, &VAO);
- //glDeleteBuffers(1, &VBO);
- //glDeleteBuffers(1, &EBO);
- //glDeleteBuffers(1, &VBO2);
- //a = a / 10.0f;
- }
- void updateBall() {
- // cout << "UPDATING" << endl;
- extern float deltaTime;
- friction = -F;
- fa = (friction / radius);
- fv = fv + (fa * deltaTime);
- a = (F / radius);
- v = v + (a * deltaTime);
- speed = sqrt((a.x*a.x) + (a.y* a.y) + (a.z*a.z));
- allForce = (v + fv)*deltaTime;
- ballPos = ballPos + v*deltaTime;
- collision();
- glBindVertexArray(VAO);
- glm::mat4 model;
- model = glm::translate(model, ballPos);
- model = glm::rotate(model, glm::radians(40.0f), glm::vec3(1.0f, 0.0f, 0.0f));
- glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
- glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- }
- void clearBall() {
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- glDeleteBuffers(1, &EBO);
- glDeleteBuffers(1, &VBO2);
- }
- void collision() {
- extern vector <Ball> ballCollection;
- for (int i = 0; i < ballCollection.size(); i++) {
- if (glm::distance(ballCollection.at(i).ballPos, ballPos) <= (2 * radius)) {
- if (!glm::distance(ballCollection.at(i).ballPos, ballPos) == 0) {
- extern GLfloat forceAmount;
- if (isColliding == 0) {
- glm::vec3 collPoint = glm::vec3((ballPos.x + ballCollection.at(i).ballPos.x) / 2, 0.0f, (ballPos.z + ballCollection.at(i).ballPos.z) / 2);
- glm::vec3 collPointPerp = collPoint - ballPos;
- glm::vec3 otherNew = collPointPerp;
- glm::vec3 thisNew = glm::cross(collPointPerp, glm::vec3(0.0, 1.0, 0.0));
- F = thisNew;
- ballCollection.at(i).F = otherNew;
- glm::vec3 newV;
- glm::vec3 otherNewV;
- newV.x = (v.x * (2 * ballCollection.at(i).radius * ballCollection.at(i).v.x)) / (radius + ballCollection.at(i).radius);
- newV.z = (v.z * (2 * ballCollection.at(i).radius * ballCollection.at(i).v.z)) / (radius + ballCollection.at(i).radius);
- newV.y = 0.0f;
- otherNewV.x = (ballCollection.at(i).v.x * (2 * radius * v.x)) / (radius + ballCollection.at(i).radius);
- otherNewV.z = (ballCollection.at(i).v.z * (2 * radius * v.z)) / (radius + ballCollection.at(i).radius);
- otherNewV.y = 0.0f;
- v.x = newV.x;
- v.z = newV.z;
- ballCollection.at(i).v.x = otherNewV.x;
- ballCollection.at(i).v.z = otherNewV.z;
- }
- isColliding = 1;
- ballCollection.at(i).isColliding = 1;
- }
- }
- }
- isColliding = 0;
- // << isColliding << endl;
- }
- GLuint texture1;
- void loadTexture() {
- glGenTextures(1, &texture1);
- // glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texture1);
- int text_w, text_h;
- unsigned char* image = SOIL_load_image(texturePath.c_str(), &text_w, &text_h, 0, SOIL_LOAD_RGB);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, text_w, text_h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- glGenerateMipmap(GL_TEXTURE_2D);
- SOIL_free_image_data(image);
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement