RoshHoul

BALL

Mar 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.17 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 <vector>
  14.  
  15. using namespace std;
  16.  
  17. // INPUT FUNCTIONS
  18. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
  19. void do_movement();
  20. void makeSphere();
  21. // THE SIZE OF THE WINDOW
  22. const GLuint WIDTH = 800, HEIGHT = 600;
  23. // CAMERA
  24. glm::vec3 cameraPos = glm::vec3(0.0f, 5.0f, 5.0f); // Position of the camera
  25. glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); // Front vector of the camera
  26. glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); // Up vector of the camera
  27. glm::vec3 modelpos = glm::vec3(0.0f, 0.0f, 0.0f); // Position of the model
  28. GLfloat pitch = 0.0f;
  29. GLfloat yaw = 90.0f;
  30. GLfloat fov = 45.0f;
  31. bool keys[1024];
  32. //GLfloat sphere[144]; // VERTICES FOR A SPHERE
  33. //GLuint indices[156];
  34. vector <float> vertices;
  35. vector <int> indices;
  36.  
  37. // TIME VALUES
  38. GLfloat deltaTime = 0.0f;   // Time between current frame and last frame
  39. GLfloat lastFrame = 0.0f;   // Time of last frame since start
  40.  
  41.  
  42.  
  43.  
  44.  
  45. int main()
  46. {
  47.     // Initializing GLFW window:
  48.     glfwInit();
  49.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  50.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  51.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  52.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  53.     GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Best Pool Game EVER", nullptr, nullptr);
  54.     glfwMakeContextCurrent(window);
  55.     glfwSetKeyCallback(window, key_callback);
  56.     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  57.     // TELLING GLEW TO BE MODERN
  58.     glewExperimental = GL_TRUE;
  59.     glewInit();
  60.     // MATCHING THE VIEWPORT SIZE TO THE WINDOW
  61.     int width, height;
  62.     glfwGetFramebufferSize(window, &width, &height);
  63.     glViewport(0, 0, width, height);
  64.  
  65.     //OPENGL SETTINGS
  66.     glEnable(GL_DEPTH_TEST);
  67.  
  68.     // Build and compile our shader program
  69.     Shader ourShader("shader.vs", "shader.frag");
  70.  
  71.  
  72.     // SETTING VERTICES AND INDICES
  73.     //GLfloat verticesGone[] = {
  74.     //  -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
  75.     //  0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
  76.     //  0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  77.  
  78.     //  0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  79.     //  -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
  80.     //  -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
  81.  
  82.     //  -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  83.     //  0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
  84.     //  0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
  85.  
  86.     //  0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
  87.     //  -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
  88.     //  -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  89.  
  90.     //  -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  91.     //  -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  92.     //  -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  93.  
  94.     //  -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  95.     //  -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  96.     //  -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  97.  
  98.     //  0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  99.     //  0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  100.     //  0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  101.  
  102.     //  0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  103.     //  0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  104.     //  0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  105.  
  106.     //  -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  107.     //  0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
  108.     //  0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
  109.  
  110.     //  0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
  111.     //  -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  112.     //  -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  113.  
  114.     //  -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
  115.     //  0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  116.     //  0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  117.  
  118.     //  0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  119.     //  -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
  120.     //  -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
  121.     //};
  122.     //GLfloat texCoords[] = {
  123.     //  0.0f, 0.0f,  // Lower-left corner  
  124.     //  1.0f, 0.0f,  // Lower-right corner
  125.     //  0.5f, 1.0f   // Top-center corner
  126.     //};
  127.  
  128.     // SETTING VAO AND BINDING VBO, EBO TO IT
  129.     GLuint VAO, VBO, EBO, VBO2;
  130.     glGenVertexArrays(1, &VAO);
  131.     glGenBuffers(1, &VBO);
  132.     glGenBuffers(1, &EBO);
  133.  
  134.     glBindVertexArray(VAO);
  135.  
  136.     makeSphere();
  137.     //NEXT 4 LINES - defining VBO
  138.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  139.     glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
  140.  
  141.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
  142.     glEnableVertexAttribArray(0);
  143.  
  144.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  145.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLint), &indices[0], GL_STATIC_DRAW);
  146.  
  147.    
  148.  
  149.    
  150.  
  151.  
  152.  
  153.     glBindVertexArray(0);
  154.  
  155.     // TEXTURE LOADING
  156.     GLuint texture;
  157.     glGenTextures(1, &texture);
  158.     glBindTexture(GL_TEXTURE_2D, texture);
  159.  
  160.     int text_w, text_h;
  161.     unsigned char* image = SOIL_load_image("fabricRed.jpg", &text_w, &text_h, 0, SOIL_LOAD_RGB);
  162.  
  163.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, text_w, text_h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  164.     glGenerateMipmap(GL_TEXTURE_2D);
  165.  
  166.     SOIL_free_image_data(image);
  167.     glBindTexture(GL_TEXTURE_2D, 0);
  168.  
  169.  
  170.  
  171.     // MAIN GAME LOOP
  172.     while (!glfwWindowShouldClose(window))     // check if we should close the window
  173.     {
  174.         // Calculate deltatime of current frame
  175.         GLfloat currentFrame = glfwGetTime();
  176.         deltaTime = currentFrame - lastFrame;
  177.         lastFrame = currentFrame;
  178.  
  179.         glfwPollEvents();
  180.         do_movement();
  181.  
  182.         glClearColor(0.2f, 0.2f, 0.2f, 0.0f);  // Clear the screen after rendering
  183.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear the Color Buffer
  184.         glBindTexture(GL_TEXTURE_2D, texture);
  185.         ourShader.Use();
  186.         //CAMERA DECLARATION
  187.         cameraFront = glm::normalize(modelpos - cameraPos); // Front vector of the camera
  188.         glm::mat4 projection;
  189.         projection = glm::perspective(glm::radians(fov), (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
  190.         //PASSING TO SHADERS
  191.         GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
  192.         GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
  193.         GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
  194.         glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
  195.  
  196.         glm::mat4 view;
  197.  
  198.         view = glm::lookAt(cameraPos, modelpos, cameraUp);
  199.         glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
  200.         // DRAWING
  201.         glBindVertexArray(VAO);
  202.         glm::mat4 model;
  203.         /*glm::vec3 forward(0, 0, -1);
  204.         modelpos = modelpos + forward;*/
  205. //      cout << modelpos[2];
  206.         model = glm::translate(model, modelpos);
  207.         glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  208.         //glDrawArrays(GL_TRIANGLES, 0, 36);
  209.         glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
  210.         glBindVertexArray(0);
  211.  
  212.        
  213.         glfwSwapBuffers(window);               // call second buffer
  214.     }
  215.     // END OF GAME LOOP
  216.     glDeleteVertexArrays(1, &VAO);
  217.     glDeleteBuffers(1, &VBO);
  218.     glDeleteBuffers(1, &EBO);
  219.     glfwTerminate();
  220.     return 0;
  221. }
  222.  
  223. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  224. {
  225.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  226.         glfwSetWindowShouldClose(window, GL_TRUE);
  227.     if (key >= 0 && key < 1024)
  228.     {
  229.         if (action == GLFW_PRESS)
  230.             keys[key] = true;
  231.         else if (action == GLFW_RELEASE)
  232.             keys[key] = false;
  233.     }
  234. }
  235.  
  236. void do_movement()
  237. {
  238.     glm::vec3  cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp));
  239.     GLfloat cameraSpeed = 0.005f;
  240.     if (keys[GLFW_KEY_W])
  241.         cameraPos += cameraFront * cameraSpeed;
  242.     if (keys[GLFW_KEY_S])
  243.         cameraPos -= cameraFront * cameraSpeed;
  244.     if (keys[GLFW_KEY_A])
  245.         cameraPos -= cameraRight * cameraSpeed;
  246.     if (keys[GLFW_KEY_D])
  247.         cameraPos += cameraRight * cameraSpeed;
  248. }
  249.  
  250.  
  251. void makeSphere() {
  252.  
  253.     int Stacks = 20;
  254.     int Slices = 20;
  255.     float Radius = 1;
  256.  
  257.     // Calc The Vertices
  258.         for (int i = 0; i <= Stacks; ++i) {
  259.  
  260.             float V = i / (float)Stacks;
  261.             float phi = V * glm::pi <float>();
  262.  
  263.             // Loop Through Slices
  264.             for (int j = 0; j <= Slices; ++j) {
  265.  
  266.                 float U = j / (float)Slices;
  267.                 float theta = U * (glm::pi <float>() * 2);
  268.  
  269.                 // Calc The Vertex Positions
  270.                 float x = cosf(theta) * sinf(phi);
  271.                 float y = cosf(phi);
  272.                 float z = sinf(theta) * sinf(phi);
  273.  
  274.                 // Push Back Vertex Data
  275.                
  276.                 vertices.push_back(x * Radius);
  277.                 vertices.push_back(y * Radius);
  278.                 vertices.push_back(z * Radius);
  279.  
  280.  
  281.             }
  282.         }
  283.  
  284.     // Calc The Index Positions
  285.     for (int i = 0; i < Slices * Stacks + Slices; ++i) {
  286.  
  287.         indices.push_back(i);
  288.         indices.push_back(i + Slices + 1);
  289.         indices.push_back(i + Slices);
  290.  
  291.         indices.push_back(i + Slices + 1);
  292.         indices.push_back(i);
  293.         indices.push_back(i + 1);
  294.     }
  295. }
Add Comment
Please, Sign In to add comment