Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- #include <iostream>
- #include <csignal> //for the breakpoint
- #include "glad.h"
- #include "GLFW/glfw3.h"
- #include "glm/glm.hpp"
- #include "glm/gtc/type_ptr.hpp"
- #include "glm/matrix.hpp"
- #include "Atlas.hpp"
- #include "window.hpp"
- #include "shader.hpp"
- #include "cubeFace.hpp"
- #include "world.hpp"
- #include "Player.hpp"
- #include "Meteor.hpp"
- #include "Spawner.hpp"
- //CREDITS IN Aaron Clifford
- #define FPS 75
- #define ZOOM_SPEED 200
- int mouseX;
- int mouseY;
- bool shouldLookAround;
- void Movement(float deltaTime, GLFWwindow* window);
- void MouseCallback(GLFWwindow* window, double xpos, double ypos);
- int main(){
- if(!glfwInit()){
- std::cout << "Failed to INIT GLFW\n";
- return -1;
- }
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_SAMPLES, 32);
- #ifdef __APPLE__
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
- #endif
- const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
- glfwWindowHint(GLFW_RED_BITS, mode->redBits);
- glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
- glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
- Window window(mode->width, mode->height);
- if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
- std::cout << "Failed to INIT GLAD\n";
- return -2;
- }
- glClearColor((float)128/355, (float)10/355, (float)0, 1.f);
- glEnable(GL_DEPTH_TEST); // putting these in window function just causes a segfault so keep them somewhere after glad is initialized
- glEnable(GL_MULTISAMPLE);
- glDisable(GL_DEPTH_CLAMP);
- glViewport(0, 0, mode->width, mode->height);
- Shader shader((char*)"TextureShader.frag", (char*)"TextureShader.vert");
- Shader meteorShader((char*)"MeteorShader.frag", (char*)"MeteorShader.vert");
- LoadAtlas();
- World world(shader);
- Player player(mode->width, mode->height, world);
- glfwSetInputMode(window.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- glfwSetCursorPosCallback(window.window, MouseCallback);
- float deltaTime = 0;
- float prevFrame = 0;
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glfwSwapInterval(0);
- glm::mat4 proj = glm::mat4(1.0f);
- glm::mat4 view = glm::mat4(1.0f);
- proj = glm::perspective((float)45.f, (float)mode->width/mode->height, 0.1f, 10000.0f);
- view = glm::lookAt(player.pos, player.pos + player.cameraFront, player.cameraUp);
- float fov = glm::radians(60.f);
- float walkFov = glm::radians(60.f);
- float runFov = glm::radians(80.f);
- Spawner spawner(world);
- while(!glfwWindowShouldClose(window.window)){
- deltaTime = glfwGetTime() - prevFrame;
- prevFrame = glfwGetTime();
- player.Look(mouseX, mouseY);
- if(glfwGetKey(window.window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS){
- if(fov != runFov)
- fov += glm::radians(deltaTime * ZOOM_SPEED);
- if(fov > runFov)
- fov = runFov;
- }
- else{
- if(fov != walkFov)
- fov -= glm::radians(deltaTime * ZOOM_SPEED);
- if(fov < walkFov)
- fov = walkFov;
- }
- proj = glm::perspective(fov, (float)mode->width/mode->height, 0.1f, 10000.0f);
- view = glm::lookAt(player.pos, player.pos + player.cameraFront, player.cameraUp);
- player.Movement(deltaTime, window.window, world);
- shader.UseShader();
- shader.UpdateProjection(proj, view);
- meteorShader.UseShader();
- meteorShader.UpdateProjection(proj, view);
- // spawner.SpawnMeteors(world, player.pos);
- // spawner.UpdateMeteors(deltaTime);
- player.MeteorCollision(spawner);
- window.Draw(world, shader, player.pos, meteorShader, spawner);
- glfwPollEvents();
- if(glfwGetKey(window.window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window.window, GLFW_TRUE);
- //glFinish();
- }
- glfwTerminate();
- }
- int lastX = 1920/2;
- int lastY = 1080/2;
- void MouseCallback(GLFWwindow* window, double xpos, double ypos){
- mouseX = xpos;
- mouseY = ypos;
- shouldLookAround = true;
- }
- window.cpp
- #include <iostream>
- #include "glad.h"
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <glm/matrix.hpp>
- #include "window.hpp"
- #include "world.hpp"
- #include "Texture.hpp"
- #include "shader.hpp"
- #include "Spawner.hpp"
- #include "Meteor.hpp"
- Window::Window(int width, int height){
- window = glfwCreateWindow(width, height, "\0", glfwGetPrimaryMonitor(), NULL);//glfwGetPrimaryMonitor()
- glfwMakeContextCurrent(window);
- }
- //toptex =0
- void Window::Draw(World world, Shader shader, glm::vec3 playerPos, Shader meteorShader,
- Spawner spawner){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- ActivateTextures();
- glm::mat4 model = glm::mat4(1.0f);
- shader.UseShader();
- shader.UpdateModel(model);
- for(int i = 0; i < 3; i++){
- glBindVertexArray(world.VAO[i]);
- shader.UseTexture(i);
- glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, world.amountOfTriangles[i]);
- }
- meteorShader.UseShader();
- // spawner.DrawMeteors(meteorShader);
- /*Meteor meteor((std::string)"meteor.obj");
- meteor.Draw();*/// ez meteor drawing code for testing
- glfwSwapBuffers(window);
- }
- void Window::ActivateTextures(){
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, atlas[0].texture);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, atlas[1].texture);
- glActiveTexture(GL_TEXTURE2);
- glBindTexture(GL_TEXTURE_2D, atlas[2].texture);
- }
- Window::~Window(){
- glfwDestroyWindow(window);
- } //when the meteor hits the ground it porgressively gets smaller until it dissapears and a coin or something appears in its place
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement