Advertisement
Guest User

Untitled

a guest
Jun 21st, 2022
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.17 KB | None | 0 0
  1. main.cpp
  2. #include <iostream>
  3. #include <csignal> //for the breakpoint
  4.  
  5. #include "glad.h"
  6. #include "GLFW/glfw3.h"
  7. #include "glm/glm.hpp"
  8. #include "glm/gtc/type_ptr.hpp"
  9. #include "glm/matrix.hpp"
  10.  
  11.  
  12. #include "Atlas.hpp"
  13. #include "window.hpp"
  14. #include "shader.hpp"
  15. #include "cubeFace.hpp"
  16. #include "world.hpp"
  17. #include "Player.hpp"
  18. #include "Meteor.hpp"
  19. #include "Spawner.hpp"
  20.  
  21.  
  22. //CREDITS IN Aaron Clifford
  23.  
  24. #define FPS 75
  25.  
  26. #define ZOOM_SPEED 200
  27.  
  28. int mouseX;
  29. int mouseY;
  30.  
  31. bool shouldLookAround;
  32.  
  33. void Movement(float deltaTime, GLFWwindow* window);
  34. void MouseCallback(GLFWwindow* window, double xpos, double ypos);
  35.  
  36.  
  37. int main(){
  38.    if(!glfwInit()){
  39.        std::cout << "Failed to INIT GLFW\n";
  40.        return -1;
  41.    }
  42.  
  43.  
  44.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  45.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  46.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  47.     glfwWindowHint(GLFW_SAMPLES, 32);
  48.  
  49.  
  50.     #ifdef __APPLE__
  51.         glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
  52.     #endif
  53.  
  54.     const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  55.  
  56.  
  57.     glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  58.     glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  59.     glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  60.  
  61.     Window window(mode->width, mode->height);
  62.     if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
  63.         std::cout << "Failed to INIT GLAD\n";
  64.         return -2;
  65.     }
  66.  
  67.        
  68.     glClearColor((float)128/355, (float)10/355, (float)0, 1.f);
  69.     glEnable(GL_DEPTH_TEST); // putting these in window function just causes a segfault so keep them somewhere after glad is initialized
  70.     glEnable(GL_MULTISAMPLE);
  71.     glDisable(GL_DEPTH_CLAMP);
  72.  
  73.     glViewport(0, 0, mode->width, mode->height);
  74.    
  75.     Shader shader((char*)"TextureShader.frag", (char*)"TextureShader.vert");
  76.     Shader meteorShader((char*)"MeteorShader.frag", (char*)"MeteorShader.vert");
  77.    
  78.     LoadAtlas();  
  79.     World world(shader);
  80.  
  81.  
  82.     Player player(mode->width, mode->height, world);  
  83.    
  84.     glfwSetInputMode(window.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  85.     glfwSetCursorPosCallback(window.window, MouseCallback);
  86.     float deltaTime = 0;
  87.     float prevFrame = 0;
  88.     //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  89.     glfwSwapInterval(0);
  90.  
  91.     glm::mat4 proj = glm::mat4(1.0f);
  92.     glm::mat4 view = glm::mat4(1.0f);  
  93.     proj = glm::perspective((float)45.f, (float)mode->width/mode->height, 0.1f, 10000.0f);
  94.     view = glm::lookAt(player.pos, player.pos + player.cameraFront, player.cameraUp);
  95.  
  96.     float fov = glm::radians(60.f);
  97.  
  98.     float walkFov = glm::radians(60.f);
  99.     float runFov = glm::radians(80.f);
  100.  
  101.     Spawner spawner(world);
  102.  
  103.     while(!glfwWindowShouldClose(window.window)){
  104.             deltaTime = glfwGetTime() - prevFrame;
  105.             prevFrame = glfwGetTime();
  106.  
  107.             player.Look(mouseX, mouseY);
  108.  
  109.             if(glfwGetKey(window.window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS){
  110.                 if(fov != runFov)
  111.                     fov += glm::radians(deltaTime * ZOOM_SPEED);
  112.                 if(fov > runFov)
  113.                     fov = runFov;
  114.             }
  115.             else{
  116.                 if(fov != walkFov)
  117.                     fov -= glm::radians(deltaTime * ZOOM_SPEED);
  118.                 if(fov < walkFov)
  119.                     fov = walkFov;
  120.             }
  121.             proj = glm::perspective(fov, (float)mode->width/mode->height, 0.1f, 10000.0f);
  122.             view = glm::lookAt(player.pos, player.pos + player.cameraFront, player.cameraUp);
  123.  
  124.             player.Movement(deltaTime, window.window, world);
  125.  
  126.             shader.UseShader();
  127.             shader.UpdateProjection(proj, view);
  128.             meteorShader.UseShader();
  129.             meteorShader.UpdateProjection(proj, view);
  130.  
  131.            // spawner.SpawnMeteors(world, player.pos);
  132.            // spawner.UpdateMeteors(deltaTime);
  133.  
  134.             player.MeteorCollision(spawner);
  135.            
  136.             window.Draw(world, shader, player.pos, meteorShader, spawner);  
  137.    
  138.  
  139.             glfwPollEvents();
  140.             if(glfwGetKey(window.window, GLFW_KEY_ESCAPE) == GLFW_PRESS)    
  141.                 glfwSetWindowShouldClose(window.window, GLFW_TRUE);
  142.  
  143.             //glFinish();
  144.     }
  145.  
  146.  
  147.     glfwTerminate();
  148. }
  149.  
  150. int lastX = 1920/2;
  151. int lastY = 1080/2;
  152.  
  153. void MouseCallback(GLFWwindow* window, double xpos, double ypos){  
  154.     mouseX = xpos;
  155.     mouseY = ypos;
  156.     shouldLookAround = true;
  157. }
  158.  
  159. window.cpp
  160.  
  161.  
  162. #include <iostream>
  163.  
  164. #include "glad.h"
  165. #include <GLFW/glfw3.h>
  166. #include <glm/glm.hpp>
  167. #include <glm/gtc/type_ptr.hpp>
  168. #include <glm/matrix.hpp>
  169.  
  170. #include "window.hpp"
  171. #include "world.hpp"
  172. #include "Texture.hpp"
  173. #include "shader.hpp"
  174. #include "Spawner.hpp"
  175. #include "Meteor.hpp"
  176.  
  177. Window::Window(int width, int height){
  178.     window = glfwCreateWindow(width, height, "\0", glfwGetPrimaryMonitor(), NULL);//glfwGetPrimaryMonitor()
  179.    
  180.     glfwMakeContextCurrent(window);
  181. }
  182. //toptex =0
  183. void Window::Draw(World world, Shader shader, glm::vec3 playerPos, Shader meteorShader,
  184. Spawner spawner){
  185.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  186.  
  187.     ActivateTextures();
  188.     glm::mat4 model = glm::mat4(1.0f);
  189.  
  190.  
  191.     shader.UseShader();
  192.     shader.UpdateModel(model);
  193.     for(int i = 0; i < 3; i++){
  194.         glBindVertexArray(world.VAO[i]);
  195.         shader.UseTexture(i);
  196.         glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, world.amountOfTriangles[i]);
  197.     }
  198.  
  199.     meteorShader.UseShader();
  200.    // spawner.DrawMeteors(meteorShader);
  201.  
  202.  
  203.     /*Meteor meteor((std::string)"meteor.obj");
  204.     meteor.Draw();*/// ez meteor drawing code for testing
  205.  
  206.  
  207.     glfwSwapBuffers(window);
  208. }
  209.  
  210. void Window::ActivateTextures(){
  211.     glActiveTexture(GL_TEXTURE0);
  212.     glBindTexture(GL_TEXTURE_2D, atlas[0].texture);
  213.     glActiveTexture(GL_TEXTURE1);
  214.     glBindTexture(GL_TEXTURE_2D, atlas[1].texture);
  215.     glActiveTexture(GL_TEXTURE2);
  216.     glBindTexture(GL_TEXTURE_2D, atlas[2].texture);
  217. }
  218.  
  219. Window::~Window(){
  220.     glfwDestroyWindow(window);
  221.     } //when the meteor hits the ground it porgressively gets smaller until it dissapears and a coin or something appears in its place
  222.    
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement