RoshHoul

Untitled

Mar 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.38 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, ballCordVBO, ballTextVBO, EBO ;
  130.     glGenVertexArrays(1, &VAO);
  131.     glGenBuffers(1, &ballCordVBO);
  132.     glGenBuffers(1, &ballTextVBO);
  133.     glGenBuffers(1, &EBO);
  134.  
  135.     glBindVertexArray(VAO);
  136.  
  137.     makeSphere();
  138.     //NEXT 4 LINES - defining VBO
  139.     glBindBuffer(GL_ARRAY_BUFFER, ballCordVBO);
  140.     glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
  141.  
  142.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
  143.     glEnableVertexAttribArray(0);
  144.  
  145.     glBindBuffer(GL_ARRAY_BUFFER, ballTextVBO);
  146.     glBufferData(GL_ARRAY_BUFFER, vertices  .size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
  147.  
  148.  
  149.  
  150.  
  151.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  152.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLint), &indices[0], GL_STATIC_DRAW);
  153.  
  154.    
  155.  
  156.    
  157.  
  158.  
  159.  
  160.     glBindVertexArray(0);
  161.  
  162.     // TEXTURE LOADING
  163.     GLuint texture;
  164.     glGenTextures(1, &texture);
  165.     glBindTexture(GL_TEXTURE_2D, texture);
  166.  
  167.     int text_w, text_h;
  168.     unsigned char* image = SOIL_load_image("13ball.jpg", &text_w, &text_h, 0, SOIL_LOAD_RGB);
  169.  
  170.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, text_w, text_h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  171.     glGenerateMipmap(GL_TEXTURE_2D);
  172.  
  173.     SOIL_free_image_data(image);
  174.     glBindTexture(GL_TEXTURE_2D, 0);
  175.  
  176.  
  177.  
  178.     // MAIN GAME LOOP
  179.     while (!glfwWindowShouldClose(window))     // check if we should close the window
  180.     {
  181.         // Calculate deltatime of current frame
  182.         GLfloat currentFrame = glfwGetTime();
  183.         deltaTime = currentFrame - lastFrame;
  184.         lastFrame = currentFrame;
  185.  
  186.         glfwPollEvents();
  187.         do_movement();
  188.  
  189.         glClearColor(0.2f, 0.2f, 0.2f, 0.0f);  // Clear the screen after rendering
  190.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear the Color Buffer
  191.         glBindTexture(GL_TEXTURE_2D, texture);
  192.         ourShader.Use();
  193.         //CAMERA DECLARATION
  194.         cameraFront = glm::normalize(modelpos - cameraPos); // Front vector of the camera
  195.         glm::mat4 projection;
  196.         projection = glm::perspective(glm::radians(fov), (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
  197.         //PASSING TO SHADERS
  198.         GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
  199.         GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
  200.         GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
  201.         glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
  202.  
  203.         glm::mat4 view;
  204.  
  205.         view = glm::lookAt(cameraPos, modelpos, cameraUp);
  206.         glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
  207.         // DRAWING
  208.         glBindVertexArray(VAO);
  209.         glm::mat4 model;
  210.         glm::vec3 forward(0, 0, -1);
  211.         /*modelpos = modelpos + forward;
  212.         cout << modelpos[2];
  213.         model = glm::translate(model, modelpos);
  214.         */glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  215.         //glDrawArrays(GL_TRIANGLES, 0, 36);
  216.         glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
  217.         glBindVertexArray(0);
  218.  
  219.        
  220.         glfwSwapBuffers(window);               // call second buffer
  221.     }
  222.     // END OF GAME LOOP
  223.     glDeleteVertexArrays(1, &VAO);
  224.     glDeleteBuffers(1, &ballCordVBO);
  225.     glDeleteBuffers(1, &EBO);
  226.     glfwTerminate();
  227.     return 0;
  228. }
  229.  
  230. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  231. {
  232.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  233.         glfwSetWindowShouldClose(window, GL_TRUE);
  234.     if (key >= 0 && key < 1024)
  235.     {
  236.         if (action == GLFW_PRESS)
  237.             keys[key] = true;
  238.         else if (action == GLFW_RELEASE)
  239.             keys[key] = false;
  240.     }
  241. }
  242.  
  243. void do_movement()
  244. {
  245.     glm::vec3  cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp));
  246.     GLfloat cameraSpeed = 0.005f;
  247.     if (keys[GLFW_KEY_W])
  248.         cameraPos += cameraFront * cameraSpeed;
  249.     if (keys[GLFW_KEY_S])
  250.         cameraPos -= cameraFront * cameraSpeed;
  251.     if (keys[GLFW_KEY_A])
  252.         cameraPos -= cameraRight * cameraSpeed;
  253.     if (keys[GLFW_KEY_D])
  254.         cameraPos += cameraRight * cameraSpeed;
  255. }
  256.  
  257.  
  258. void makeSphere() {
  259.  
  260.     int Stacks = 20;
  261.     int Slices = 20;
  262.     float Radius = 1;
  263.  
  264.     // Calc The Vertices
  265.         for (int i = 0; i <= Stacks; ++i) {
  266.  
  267.             float V = i / (float)Stacks;
  268.             float phi = V * glm::pi <float>();
  269.  
  270.             // Loop Through Slices
  271.             for (int j = 0; j <= Slices; ++j) {
  272.  
  273.                 float U = j / (float)Slices;
  274.                 float theta = U * (glm::pi <float>() * 2);
  275.  
  276.                 // Calc The Vertex Positions
  277.                 float x = cosf(theta) * sinf(phi);
  278.                 float y = cosf(phi);
  279.                 float z = sinf(theta) * sinf(phi);
  280.  
  281.                 // Push Back Vertex Data
  282.                
  283.                 vertices.push_back(x * Radius);
  284.                 vertices.push_back(y * Radius);
  285.                 vertices.push_back(z * Radius);
  286.  
  287.  
  288.             }
  289.         }
  290.  
  291.     // Calc The Index Positions
  292.     for (int i = 0; i < Slices * Stacks + Slices; ++i) {
  293.  
  294.         indices.push_back(i);
  295.         indices.push_back(i + Slices + 1);
  296.         indices.push_back(i + Slices);
  297.  
  298.         indices.push_back(i + Slices + 1);
  299.         indices.push_back(i);
  300.         indices.push_back(i + 1);
  301.     }
  302. }
Add Comment
Please, Sign In to add comment