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 "Shader.h"
- #include "Ball.h"
- #include "Table.h"
- #include "Pocket.h"
- #include <vector>
- #include <array>
- #define PI 3.14159265
- #define TO_RADS(x) (x * PI / 180.0)
- using namespace std;
- // FUNCTIONS DECLARATION
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
- void do_movement();
- void generateMap();
- // THE SIZE OF THE WINDOW
- const GLuint WIDTH = 800, HEIGHT = 600;
- // CAMERA
- glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); // Front vector of the camera
- glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); // Up vector of the camera
- glm::vec3 modelpos = glm::vec3(0.0f, 0.0f, 0.0f); // Position of the model
- glm::vec3 cameraPos = glm::vec3(0.0f, 15.0f, 15.0f); // Position of the camera
- bool cameraTop = false;
- GLfloat angle = 0;
- GLfloat pitch = 0.0f;
- GLfloat yaw = 90.0f;
- GLfloat fov = 45.0f;
- bool keys[1024];
- // PROPERTIES FOR SPHERE
- // TIME VALUES
- GLfloat deltaTime = 0.0f; // Time between current frame and last frame
- GLfloat lastFrame = 0.0f; // Time of last frame since start
- // TEXTURES
- string textures[16] = { "1.jpg","2.jpg","3.jpg","4.jpg",
- "5.jpg","6.jpg","7.jpg","8.jpg",
- "9.jpg","10.jpg","11.jpg","12.jpg",
- "13.jpg","3.jpg","15.jpg","0White.jpg" };
- // BALL COLLECTION
- vector <Ball> ballCollection;
- glm::vec3 modelPos[16];
- // POCKET COLLECTION
- vector <Pocket> pockets;
- int main()
- {
- // Initializing GLFW window:
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Best Pool Game EVER", nullptr, nullptr);
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, key_callback);
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
- // TELLING GLEW TO BE MODERN
- glewExperimental = GL_TRUE;
- glewInit();
- // MATCHING THE VIEWPORT SIZE TO THE WINDOW
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- glViewport(0, 0, width, height);
- //OPENGL SETTINGS
- glEnable(GL_DEPTH_TEST);
- Shader ourShader("shader.vs", "shader.frag");
- Table pool(glm::vec3(0.0, -1, 0.0), "pool_table.png");
- pool.loadTexture();
- generateMap();
- // MAIN GAME LOOP
- while (!glfwWindowShouldClose(window)) // check if we should close the window
- {
- // Calculate deltatime of current frame
- GLfloat currentFrame = glfwGetTime();
- deltaTime = currentFrame - lastFrame;
- lastFrame = currentFrame;
- //Setting up the environment
- glfwPollEvents();
- glClearColor(0.2f, 0.2f, 0.2f, 0.0f); // Clear the screen after rendering
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the Color Buffer
- ourShader.Use();
- //CAMERA DECLARATION
- cameraFront = glm::normalize(modelpos - cameraPos); // Front vector of the camera
- glm::mat4 projection;
- projection = glm::perspective(glm::radians(fov), (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
- //PASSING TO SHADERS
- GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
- GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
- glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
- glm::mat4 view;
- //CAMERA SWITCH
- if (cameraTop == false) {
- view = glm::lookAt(cameraPos, ballCollection.at(15).ballPos, cameraUp);
- }
- if (cameraTop == true) {
- 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));
- }
- glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
- // DRAWING
- pool.drawTable(ourShader);
- for (int i = 0; i < pockets.size(); i++) {
- pockets.at(i).drawBall(ourShader);
- pockets.at(i).ballOut();
- }
- for (int i = 0; i < 16; i++) {
- if (ballCollection.at(i).playable == true) {
- ballCollection.at(i).drawBall(ourShader);
- ballCollection.at(i).collision();
- ballCollection.at(i).collideWall();
- }
- else {
- cout << "ball num is " << i << " is not playable" << endl;
- }
- }
- 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)));
- do_movement();
- glfwSwapBuffers(window); // call second buffer
- }
- glfwTerminate();
- return 0;
- }
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- if (key >= 0 && key < 1024) {
- if (action == GLFW_PRESS)
- keys[key] = true;
- else if (action == GLFW_RELEASE)
- keys[key] = false;
- }
- }
- float power;
- void do_movement()
- {
- GLfloat radius = 10.0f;
- glm::vec3 cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp));
- GLfloat cameraSpeed = 50.0f;
- glm::vec3 aboveBall = glm::vec3(ballCollection.at(15).ballPos.x, ballCollection.at(15).ballPos.y + 6 , ballCollection.at(15).ballPos.z);
- glm::vec3 cameraFwd = aboveBall - cameraPos;
- if (keys[GLFW_KEY_W])
- ballCollection.at(15).ballPos = ballCollection.at(15).ballPos + glm::vec3(0.0f, 0.0f, -0.1f);
- if (keys[GLFW_KEY_S])
- ballCollection.at(15).ballPos = ballCollection.at(15).ballPos + glm::vec3(0.0f, 0.0f, 0.1f);
- if (keys[GLFW_KEY_A]) {
- angle += 50 * deltaTime;
- }
- if (keys[GLFW_KEY_D]) {
- angle -= 50 * deltaTime;
- }
- if (keys[GLFW_KEY_SPACE]) {
- power += 2.0f;
- }
- if (keys[GLFW_KEY_F]) {
- ballCollection.at(15).F = power*glm::normalize(cameraFwd);
- //power = 0.0f;
- }
- if (keys[GLFW_KEY_E]) {
- ballCollection.at(15).F = -100.0f * glm::normalize(cameraFwd);
- }
- if (keys[GLFW_KEY_T]) {
- cameraTop = true;
- }
- if (keys[GLFW_KEY_P]) {
- cameraTop = false;
- }
- if (keys[GLFW_KEY_R]) {
- power = 0.0f;
- }
- }
- void generateMap() {
- int elemNum = 0;
- Ball ball1(glm::vec3(0.0, 0.0, 0.0), textures[0]);
- Ball ball2(glm::vec3(0.0, 0.0, 0.0), textures[1]);
- Ball ball3(glm::vec3(0.0, 0.0, 0.0), textures[2]);
- Ball ball4(glm::vec3(0.0, 0.0, 0.0), textures[3]);
- Ball ball5(glm::vec3(0.0, 0.0, 0.0), textures[4]);
- Ball ball6(glm::vec3(0.0, 0.0, 0.0), textures[5]);
- Ball ball7(glm::vec3(0.0, 0.0, 0.0), textures[6]);
- Ball ball8(glm::vec3(0.0, 0.0, 0.0), textures[7]);
- Ball ball9(glm::vec3(0.0, 0.0, 0.0), textures[8]);
- Ball ball10(glm::vec3(0.0, 0.0, 0.0), textures[9]);
- Ball ball11(glm::vec3(0.0, 0.0, 0.0), textures[10]);
- Ball ball12(glm::vec3(0.0, 0.0, 0.0), textures[11]);
- Ball ball13(glm::vec3(0.0, 0.0, 0.0), textures[12]);
- Ball ball14(glm::vec3(0.0, 0.0, 0.0), textures[13]);
- Ball ball15(glm::vec3(0.0, 0.0, 0.0), textures[14]);
- Ball ball16(glm::vec3(0.0, 0.0, 0.0), textures[15]);
- for (int i = 0; i < 16; i++) {
- if (i == 0) {
- modelPos[i] = modelPos[i] + glm::vec3(-5.0f, 0.0f, 30.0f);
- }
- if (i > 0 && i < 5) {
- modelPos[i] = modelPos[i - 1] + glm::vec3(2.3f, 0.0f, 0.0f);
- }
- if (i > 4 && i < 9) {
- modelPos[i] = modelPos[i - 5] + glm::vec3(1.3f, 0.0f, -2.3f);
- }
- if (i > 8 && i < 12) {
- modelPos[i] = modelPos[i - 4] + glm::vec3(1.3f, 0.0f, -2.3f);
- }
- if (i == 12 || i == 13) {
- modelPos[i] = modelPos[i - 3] + glm::vec3(1.3f, 0.0f, -2.3f);
- }
- if (i == 14) {
- modelPos[i] = modelPos[i - 2] + glm::vec3(1.3f, 0.0f, -2.3f);
- }
- }
- ball1.ballPos = modelPos[0];
- ball1.makeSphere();
- ball1.loadTexture();
- ballCollection.push_back(ball1);
- ball2.ballPos = modelPos[1];
- ball2.makeSphere();
- ball2.loadTexture();
- ballCollection.push_back(ball2);
- ball3.ballPos = modelPos[2];
- ball3.makeSphere();
- ball3.loadTexture();
- ballCollection.push_back(ball3);
- ball4.ballPos = modelPos[3];
- ball4.makeSphere();
- ball4.loadTexture();
- ballCollection.push_back(ball4);
- ball5.ballPos = modelPos[4];
- ball5.makeSphere();
- ball5.loadTexture();
- ballCollection.push_back(ball5);
- ball6.ballPos = modelPos[5];
- ball6.makeSphere();
- ball6.loadTexture();
- ballCollection.push_back(ball6);
- ball7.ballPos = modelPos[6];
- ball7.makeSphere();
- ball7.loadTexture();
- ballCollection.push_back(ball7);
- ball8.ballPos = modelPos[7];
- ball8.makeSphere();
- ball8.loadTexture();
- ballCollection.push_back(ball8);
- ball9.ballPos = modelPos[8];
- ball9.makeSphere();
- ball9.loadTexture();
- ballCollection.push_back(ball9);
- ball10.makeSphere();
- ball10.loadTexture();
- ball10.ballPos = modelPos[9];
- ballCollection.push_back(ball10);
- ball11.makeSphere();
- ball11.loadTexture();
- ball11.ballPos = modelPos[10];
- ballCollection.push_back(ball11);
- ball12.ballPos = modelPos[11];
- ball12.makeSphere();
- ball12.loadTexture();
- ballCollection.push_back(ball12);
- ball13.makeSphere();
- ball13.loadTexture();
- ball13.ballPos = modelPos[12];
- ballCollection.push_back(ball13);
- ball14.makeSphere();
- ball14.loadTexture();
- ball14.ballPos = modelPos[13];
- ballCollection.push_back(ball14);
- ball15.makeSphere();
- ball15.loadTexture();
- ball15.ballPos = modelPos[14];
- ballCollection.push_back(ball15);
- modelPos[15] = glm::vec3{ 0.0f, 0.0f, -10.0f };
- Ball white(modelPos[15], textures[15]);
- white.makeSphere();
- white.loadTexture();
- ballCollection.push_back(white);
- Pocket bottRight(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
- Pocket bottLeft(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
- Pocket midRight(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
- Pocket midLeft(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
- Pocket topRight(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
- Pocket topLeft(glm::vec3(0.0f, 0.0f, 0.0f), textures[7]);
- bottRight.pocketPos = glm::vec3(-19.0f, 1.0f, -39.0f);
- bottRight.makeSphere();
- bottRight.loadTexture();
- pockets.push_back(bottRight);
- bottLeft.pocketPos = glm::vec3(19.0f, 1.0f, -39.0f);
- bottLeft.makeSphere();
- bottLeft.loadTexture();
- pockets.push_back(bottLeft);
- midRight.pocketPos = glm::vec3(-19.0f, 1.0f, 0.f);
- midRight.makeSphere();
- midRight.loadTexture();
- pockets.push_back(midRight);
- midLeft.pocketPos = glm::vec3(19.0f, 1.0f, 1.0f);
- midLeft.makeSphere();
- midLeft.loadTexture();
- pockets.push_back(midLeft);
- topRight.pocketPos = glm::vec3(-19.0f, 1.0f, 39.0f);
- topRight.makeSphere();
- topRight.loadTexture();
- pockets.push_back(topRight);
- topLeft.pocketPos = glm::vec3(19.0f, 1.0f, 39.0f);
- topLeft.makeSphere();
- topLeft.loadTexture();
- pockets.push_back(topLeft);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement