Advertisement
RoshHoul

main

Apr 7th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.55 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3.  
  4. #define GLEW_STATIC
  5. #include <GL/glew.h>
  6. #include <GLFW/glfw3.h>
  7. #include <iostream>
  8. #include <SOIL.h>
  9. #include <glm/glm.hpp>
  10. #include <glm/gtc/matrix_transform.hpp>
  11. #include <glm/gtc/type_ptr.hpp>
  12. #include "Shader.h"
  13. #include "Ball.h"
  14. #include "Table.h"
  15. #include "Pocket.h"
  16. #include <vector>
  17. #include <array>
  18.  
  19. #define PI 3.14159265
  20. #define TO_RADS(x) (x * PI / 180.0)
  21.  
  22.  
  23. using namespace std;
  24.  
  25. // FUNCTIONS DECLARATION
  26. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
  27. void do_movement();
  28. void generateMap();
  29. // THE SIZE OF THE WINDOW
  30. const GLuint WIDTH = 800, HEIGHT = 600;
  31. // CAMERA
  32. glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); // Front vector of the camera
  33. glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); // Up vector of the camera
  34. glm::vec3 modelpos = glm::vec3(0.0f, 0.0f, 0.0f); // Position of the model
  35. glm::vec3 cameraPos = glm::vec3(0.0f, 15.0f, 15.0f); // Position of the camera
  36.  
  37. bool cameraTop = false;
  38. GLfloat angle = 0;
  39.  
  40. GLfloat pitch = 0.0f;
  41. GLfloat yaw = 90.0f;
  42. GLfloat fov = 45.0f;
  43. bool keys[1024];
  44. // PROPERTIES FOR SPHERE
  45.  
  46. // TIME VALUES
  47. GLfloat deltaTime = 0.0f;   // Time between current frame and last frame
  48. GLfloat lastFrame = 0.0f;   // Time of last frame since start
  49.  
  50.  
  51.  
  52.  
  53.                             // TEXTURES
  54. string textures[16] = { "1.jpg","2.jpg","3.jpg","4.jpg",
  55. "5.jpg","6.jpg","7.jpg","8.jpg",
  56. "9.jpg","10.jpg","11.jpg","12.jpg",
  57. "13.jpg","3.jpg","15.jpg","0White.jpg" };
  58. // BALL COLLECTION
  59. vector <Ball> ballCollection;
  60. glm::vec3 modelPos[16];
  61.  
  62. // POCKET COLLECTION
  63. vector <Pocket> pockets;
  64. int main()
  65. {
  66.     // Initializing GLFW window:
  67.     glfwInit();
  68.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  69.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  70.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  71.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  72.     GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Best Pool Game EVER", nullptr, nullptr);
  73.     glfwMakeContextCurrent(window);
  74.     glfwSetKeyCallback(window, key_callback);
  75.     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  76.     // TELLING GLEW TO BE MODERN
  77.     glewExperimental = GL_TRUE;
  78.     glewInit();
  79.     // MATCHING THE VIEWPORT SIZE TO THE WINDOW
  80.     int width, height;
  81.     glfwGetFramebufferSize(window, &width, &height);
  82.     glViewport(0, 0, width, height);
  83.  
  84.     //OPENGL SETTINGS
  85.     glEnable(GL_DEPTH_TEST);
  86.     Shader ourShader("shader.vs", "shader.frag");
  87.  
  88.     Table pool(glm::vec3(0.0, -1, 0.0), "pool_table.png");
  89.     pool.loadTexture();
  90.     generateMap();
  91.     // MAIN GAME LOOP
  92.     while (!glfwWindowShouldClose(window))     // check if we should close the window
  93.     {
  94.         // Calculate deltatime of current frame
  95.         GLfloat currentFrame = glfwGetTime();
  96.         deltaTime = currentFrame - lastFrame;
  97.         lastFrame = currentFrame;
  98.  
  99.         //Setting up the environment
  100.         glfwPollEvents();
  101.  
  102.         glClearColor(0.2f, 0.2f, 0.2f, 0.0f);  // Clear the screen after rendering
  103.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear the Color Buffer
  104.  
  105.         ourShader.Use();
  106.         //CAMERA DECLARATION
  107.         cameraFront = glm::normalize(modelpos - cameraPos); // Front vector of the camera
  108.         glm::mat4 projection;
  109.         projection = glm::perspective(glm::radians(fov), (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
  110.         //PASSING TO SHADERS
  111.         GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
  112.         GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
  113.         glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
  114.  
  115.         glm::mat4 view;
  116.         //CAMERA SWITCH
  117.         if (cameraTop == false) {
  118.             view = glm::lookAt(cameraPos, ballCollection.at(15).ballPos, cameraUp);
  119.         }
  120.  
  121.         if (cameraTop == true) {
  122.             view = glm::lookAt(glm::vec3(0.0f, 95.0f, 0.0), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
  123.         }
  124.         glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
  125.         // DRAWING
  126.  
  127.  
  128.         pool.drawTable(ourShader);
  129.  
  130.         for (int i = 0; i < pockets.size(); i++) {
  131.             pockets.at(i).drawBall(ourShader);
  132.             pockets.at(i).ballOut();
  133.         }
  134.  
  135.         for (int i = 0; i < 16; i++) {
  136.             if (ballCollection.at(i).playable == true) {
  137.            
  138.                 ballCollection.at(i).drawBall(ourShader);
  139.                 ballCollection.at(i).collision();
  140.                 ballCollection.at(i).collideWall();
  141.             }
  142.             else {
  143.                 cout << "ball num is " << i << " is not playable" << endl;
  144.             }
  145.         }
  146.         cameraPos = glm::vec3(ballCollection.at(15).ballPos.x - 12 * sin(TO_RADS(angle)), ballCollection.at(15).ballPos.y + 6, ballCollection.at(15).ballPos.z - 12 * cos(TO_RADS(angle)));
  147.  
  148.         do_movement();
  149.         glfwSwapBuffers(window);               // call second buffer
  150.     }
  151.  
  152.     glfwTerminate();
  153.     return 0;
  154. }
  155.  
  156.  
  157.  
  158. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  159. {
  160.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  161.         glfwSetWindowShouldClose(window, GL_TRUE);
  162.  
  163.  
  164.     if (key >= 0 && key < 1024) {
  165.         if (action == GLFW_PRESS)
  166.             keys[key] = true;
  167.         else if (action == GLFW_RELEASE)
  168.             keys[key] = false;
  169.     }
  170. }
  171.  
  172. float power;
  173. void do_movement()
  174. {
  175.  
  176.     GLfloat radius = 10.0f;
  177.  
  178.  
  179.     glm::vec3  cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp));
  180.     GLfloat cameraSpeed = 50.0f;
  181.  
  182.     glm::vec3 aboveBall = glm::vec3(ballCollection.at(15).ballPos.x, ballCollection.at(15).ballPos.y + 6 , ballCollection.at(15).ballPos.z);
  183.     glm::vec3 cameraFwd = aboveBall - cameraPos;
  184.  
  185.     if (keys[GLFW_KEY_W])
  186.         ballCollection.at(15).ballPos = ballCollection.at(15).ballPos + glm::vec3(0.0f, 0.0f, -0.1f);
  187.     if (keys[GLFW_KEY_S])
  188.         ballCollection.at(15).ballPos = ballCollection.at(15).ballPos + glm::vec3(0.0f, 0.0f, 0.1f);
  189.     if (keys[GLFW_KEY_A]) {
  190.         angle += 50 * deltaTime;
  191.     }
  192.     if (keys[GLFW_KEY_D]) {
  193.         angle -= 50 * deltaTime;
  194.     }
  195.     if (keys[GLFW_KEY_SPACE]) {
  196.         power += 2.0f;
  197.     }
  198.     if (keys[GLFW_KEY_F]) {
  199.         ballCollection.at(15).F = power*glm::normalize(cameraFwd);
  200.         //power = 0.0f;
  201.     }
  202.     if (keys[GLFW_KEY_E]) {
  203.         ballCollection.at(15).F = -100.0f * glm::normalize(cameraFwd);
  204.     }
  205.     if (keys[GLFW_KEY_T]) {
  206.         cameraTop = true;
  207.     }
  208.     if (keys[GLFW_KEY_P]) {
  209.         cameraTop = false;
  210.     }
  211.  
  212.     if (keys[GLFW_KEY_R]) {
  213.         power = 0.0f;
  214.     }
  215. }
  216.  
  217. void generateMap() {
  218.  
  219.     int elemNum = 0;
  220.  
  221.     Ball ball1(glm::vec3(0.0, 0.0, 0.0), textures[0]);
  222.     Ball ball2(glm::vec3(0.0, 0.0, 0.0), textures[1]);
  223.     Ball ball3(glm::vec3(0.0, 0.0, 0.0), textures[2]);
  224.     Ball ball4(glm::vec3(0.0, 0.0, 0.0), textures[3]);
  225.     Ball ball5(glm::vec3(0.0, 0.0, 0.0), textures[4]);
  226.     Ball ball6(glm::vec3(0.0, 0.0, 0.0), textures[5]);
  227.     Ball ball7(glm::vec3(0.0, 0.0, 0.0), textures[6]);
  228.     Ball ball8(glm::vec3(0.0, 0.0, 0.0), textures[7]);
  229.     Ball ball9(glm::vec3(0.0, 0.0, 0.0), textures[8]);
  230.     Ball ball10(glm::vec3(0.0, 0.0, 0.0), textures[9]);
  231.     Ball ball11(glm::vec3(0.0, 0.0, 0.0), textures[10]);
  232.     Ball ball12(glm::vec3(0.0, 0.0, 0.0), textures[11]);
  233.     Ball ball13(glm::vec3(0.0, 0.0, 0.0), textures[12]);
  234.     Ball ball14(glm::vec3(0.0, 0.0, 0.0), textures[13]);
  235.     Ball ball15(glm::vec3(0.0, 0.0, 0.0), textures[14]);
  236.     Ball ball16(glm::vec3(0.0, 0.0, 0.0), textures[15]);
  237.  
  238.     for (int i = 0; i < 16; i++) {
  239.         if (i == 0) {
  240.             modelPos[i] = modelPos[i] + glm::vec3(-5.0f, 0.0f, 30.0f);
  241.         }
  242.  
  243.         if (i > 0 && i < 5) {
  244.             modelPos[i] = modelPos[i - 1] + glm::vec3(2.3f, 0.0f, 0.0f);
  245.         }
  246.  
  247.         if (i > 4 && i < 9) {
  248.             modelPos[i] = modelPos[i - 5] + glm::vec3(1.3f, 0.0f, -2.3f);
  249.         }
  250.  
  251.         if (i > 8 && i < 12) {
  252.             modelPos[i] = modelPos[i - 4] + glm::vec3(1.3f, 0.0f, -2.3f);
  253.         }
  254.  
  255.         if (i == 12 || i == 13) {
  256.             modelPos[i] = modelPos[i - 3] + glm::vec3(1.3f, 0.0f, -2.3f);
  257.         }
  258.  
  259.         if (i == 14) {
  260.             modelPos[i] = modelPos[i - 2] + glm::vec3(1.3f, 0.0f, -2.3f);
  261.         }
  262.     }
  263.  
  264.  
  265.  
  266.  
  267.  
  268.     ball1.ballPos = modelPos[0];
  269.     ball1.makeSphere();
  270.     ball1.loadTexture();
  271.     ballCollection.push_back(ball1);
  272.  
  273.     ball2.ballPos = modelPos[1];
  274.     ball2.makeSphere();
  275.     ball2.loadTexture();
  276.     ballCollection.push_back(ball2);
  277.  
  278.     ball3.ballPos = modelPos[2];
  279.     ball3.makeSphere();
  280.     ball3.loadTexture();
  281.     ballCollection.push_back(ball3);
  282.  
  283.     ball4.ballPos = modelPos[3];
  284.     ball4.makeSphere();
  285.     ball4.loadTexture();
  286.     ballCollection.push_back(ball4);
  287.  
  288.  
  289.     ball5.ballPos = modelPos[4];
  290.     ball5.makeSphere();
  291.     ball5.loadTexture();
  292.     ballCollection.push_back(ball5);
  293.  
  294.  
  295.     ball6.ballPos = modelPos[5];
  296.     ball6.makeSphere();
  297.     ball6.loadTexture();
  298.     ballCollection.push_back(ball6);
  299.  
  300.  
  301.     ball7.ballPos = modelPos[6];
  302.     ball7.makeSphere();
  303.     ball7.loadTexture();
  304.     ballCollection.push_back(ball7);
  305.  
  306.  
  307.     ball8.ballPos = modelPos[7];
  308.     ball8.makeSphere();
  309.     ball8.loadTexture();
  310.     ballCollection.push_back(ball8);
  311.  
  312.  
  313.     ball9.ballPos = modelPos[8];
  314.     ball9.makeSphere();
  315.     ball9.loadTexture();
  316.     ballCollection.push_back(ball9);
  317.  
  318.  
  319.     ball10.makeSphere();
  320.     ball10.loadTexture();
  321.     ball10.ballPos = modelPos[9];
  322.     ballCollection.push_back(ball10);
  323.  
  324.     ball11.makeSphere();
  325.     ball11.loadTexture();
  326.     ball11.ballPos = modelPos[10];
  327.     ballCollection.push_back(ball11);
  328.  
  329.     ball12.ballPos = modelPos[11];
  330.     ball12.makeSphere();
  331.     ball12.loadTexture();
  332.     ballCollection.push_back(ball12);
  333.  
  334.     ball13.makeSphere();
  335.     ball13.loadTexture();
  336.     ball13.ballPos = modelPos[12];
  337.     ballCollection.push_back(ball13);
  338.  
  339.     ball14.makeSphere();
  340.     ball14.loadTexture();
  341.     ball14.ballPos = modelPos[13];
  342.     ballCollection.push_back(ball14);
  343.  
  344.     ball15.makeSphere();
  345.     ball15.loadTexture();
  346.     ball15.ballPos = modelPos[14];
  347.     ballCollection.push_back(ball15);
  348.  
  349.  
  350.     modelPos[15] = glm::vec3{ 0.0f, 0.0f, -10.0f };
  351.     Ball white(modelPos[15], textures[15]);
  352.     white.makeSphere();
  353.     white.loadTexture();
  354.     ballCollection.push_back(white);
  355.  
  356.  
  357.  
  358.     Pocket bottRight(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
  359.     Pocket bottLeft(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
  360.     Pocket midRight(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
  361.     Pocket midLeft(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
  362.     Pocket topRight(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
  363.     Pocket topLeft(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
  364.  
  365.  
  366.     bottRight.pocketPos = glm::vec3(-19.0f, 1.0f, -39.0f);
  367.     bottRight.makeSphere();
  368.     bottRight.loadTexture();
  369.     pockets.push_back(bottRight);
  370.  
  371.     bottLeft.pocketPos = glm::vec3(19.0f, 1.0f, -39.0f);
  372.     bottLeft.makeSphere();
  373.     bottLeft.loadTexture();
  374.     pockets.push_back(bottLeft);
  375.  
  376.     midRight.pocketPos = glm::vec3(-19.0f, 1.0f, 0.f);
  377.     midRight.makeSphere();
  378.     midRight.loadTexture();
  379.     pockets.push_back(midRight);
  380.  
  381.     midLeft.pocketPos = glm::vec3(19.0f, 1.0f, 1.0f);
  382.     midLeft.makeSphere();
  383.     midLeft.loadTexture();
  384.     pockets.push_back(midLeft);
  385.  
  386.     topRight.pocketPos = glm::vec3(-19.0f, 1.0f, 39.0f);
  387.     topRight.makeSphere();
  388.     topRight.loadTexture();
  389.     pockets.push_back(topRight);
  390.  
  391.     topLeft.pocketPos = glm::vec3(19.0f, 1.0f, 39.0f);
  392.     topLeft.makeSphere();
  393.     topLeft.loadTexture();
  394.     pockets.push_back(topLeft);
  395.  
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement