Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <glad/glad.h>
- #include <GLFW/glfw3.h>
- #include "stb_image.h"
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <irrKlang.h>
- #include "Shader.h"
- #include "Texture.h"
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- #include <vector>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <string>
- #include <thread>
- #include "OBJ_Loader.h"
- #include "InkZone.h"
- using namespace irrklang;
- struct Vertex {
- Vertex() {}
- Vertex(float x, float y, float z, float u, float v, float r, float g, float b) : pos(x, y, z), texCoord(u, v), color(r, g, b) {}
- glm::vec3 pos;
- glm::vec2 texCoord;
- glm::vec3 color;
- };
- #define TERRAIN_SIZE 256
- void framebuffer_size_callback(GLFWwindow* window, int width, int height);
- void processInput(GLFWwindow *window);
- // settings
- const unsigned int SCR_WIDTH = 800;
- const unsigned int SCR_HEIGHT = 600;
- int width, height;
- float cameraX = 0.0f, cameraZ = 0.0f;
- ISoundEngine* soundEngine;
- int amountOfTeam1Color = 0, amountOfTeam2Color = 0;
- Vertex terrainVertices[(TERRAIN_SIZE * TERRAIN_SIZE)];
- int terrainIndices[(TERRAIN_SIZE * TERRAIN_SIZE) * 6];
- objl::Loader loader;
- std::vector<Vertex> modelVertices;
- std::vector<int> modelIndices;
- int numVert, numInd;
- std::vector<InkZone*> inkZones;
- void placeInkSplotch(int originX, int originZ, int radius, float r, float g) {
- for (int i = 0; i < inkZones.size(); i++) { //Check which zone the origin is in
- if (originX > inkZones.at(i)->x1) {
- if (originX < inkZones.at(i)->x2) {
- if (originZ > inkZones.at(i)->z1) {
- if (originZ < inkZones.at(i)->z2) {
- for (int j = 0; j < inkZones.at(i)->verticesInZone.size(); j++) {
- float xDistance = fabs(modelVertices.at(inkZones.at(i)->verticesInZone.at(j)).pos.x - originX);
- float zDistance = fabs(modelVertices.at(inkZones.at(i)->verticesInZone.at(j)).pos.z - originZ);
- if (xDistance < radius) {
- if (zDistance < radius) {
- modelVertices.at(inkZones.at(i)->verticesInZone.at(j)).color = glm::vec3(r, g, 0.0f);
- }
- }
- }
- }
- }
- }
- }
- }
- glBufferData(GL_ARRAY_BUFFER, modelVertices.size() * sizeof(Vertex), &modelVertices[0], GL_DYNAMIC_DRAW);
- }
- unsigned int VBO, VAO, EBO;
- void initTerrainRender() {
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, modelVertices.size() * sizeof(Vertex), &modelVertices[0], GL_DYNAMIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(5 * sizeof(float)));
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, modelIndices.size() * sizeof(int), &modelIndices[0], GL_STATIC_DRAW);
- }
- void prepareTerrainRender() {
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glEnable(GL_DEPTH_TEST);
- }
- unsigned int HUDVBO, HUDVAO;
- void initHUDRender() {
- glGenVertexArrays(1, &HUDVAO);
- glGenBuffers(1, &HUDVBO);
- glBindVertexArray(HUDVAO);
- glBindBuffer(GL_ARRAY_BUFFER, HUDVBO);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
- }
- void prepareHUDRender() {
- glBindVertexArray(HUDVAO);
- glBindBuffer(GL_ARRAY_BUFFER, HUDVBO);
- glDisable(GL_DEPTH_TEST);
- }
- int countTeam1Ink() {
- int amount = 0;
- for (int i = 0; i < (TERRAIN_SIZE * TERRAIN_SIZE); i++) {
- if (modelVertices[i].color.r > 0.5f) amount++;
- }
- return amount;
- }
- int countTeam2Ink() {
- int amount = 0;
- for (int i = 0; i < (TERRAIN_SIZE * TERRAIN_SIZE); i++) {
- if (modelVertices[i].color.g > 0.5f) amount++;
- }
- return amount;
- }
- glm::mat4 proj;
- glm::mat4 view;
- glm::mat4 model;
- glm::mat4 identity;
- Shader inkShader;
- Shader HUDShader;
- Shader postShader;
- int fontWidths[256];
- void renderString(std::string s, int x, int y, float r, float g, float b) {
- Vertex HUDVertices[6];
- char ch;
- float u, v, u2, v2;
- HUDShader.setVec3("color", glm::vec3(r, g, b));
- for (int i = 0; i < s.length(); i++) {
- ch = s[i] - 32;
- u = (float)((ch % 32) * 32) / 1024;
- v = (float)((ch / 32) * 32) / 256;
- u2 = (float)(((ch % 32) * 32) + 32) / 1024;
- v2 = (float)(((ch / 32) * 32) + 32) / 256;
- HUDVertices[0] = Vertex{ 0.0f, 1.0f, 0.0f, u, v2, 0.0f, 0.0f, 0.0f };
- HUDVertices[1] = Vertex{ 1.0f, 0.0f, 0.0f, u2, v, 0.0f, 0.0f, 0.0f };
- HUDVertices[2] = Vertex{ 0.0f, 0.0f, 0.0f, u, v, 0.0f, 0.0f, 0.0f };
- HUDVertices[3] = Vertex{ 0.0f, 1.0f, 0.0f, u, v2, 0.0f, 0.0f, 0.0f };
- HUDVertices[4] = Vertex{ 1.0f, 1.0f, 0.0f, u2, v2, 0.0f, 0.0f, 0.0f };
- HUDVertices[5] = Vertex{ 1.0f, 0.0f, 0.0f, u2, v, 0.0f, 0.0f, 0.0f };
- glBufferData(GL_ARRAY_BUFFER, sizeof(HUDVertices), HUDVertices, GL_DYNAMIC_DRAW);
- model = identity;
- model = glm::translate(model, glm::vec3(x, y, 0.0f));
- model = glm::scale(model, glm::vec3(32, 32, 1));
- HUDShader.setMat4("model", model);
- glDrawArrays(GL_TRIANGLES, 0, 6);
- x += fontWidths[s[i]];
- }
- HUDVertices[0] = Vertex{ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f };
- HUDVertices[1] = Vertex{ 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f };
- HUDVertices[2] = Vertex{ 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
- HUDVertices[3] = Vertex{ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f };
- HUDVertices[4] = Vertex{ 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f };
- HUDVertices[5] = Vertex{ 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f };
- glBufferData(GL_ARRAY_BUFFER, sizeof(HUDVertices), HUDVertices, GL_DYNAMIC_DRAW);
- }
- void loadFontWidthsFromFile() {
- std::ifstream fontDataFile;
- std::stringstream fontDataStream;
- std::string fontDataString;
- fontDataFile.open("res/FontData.csv");
- fontDataStream << fontDataFile.rdbuf();
- fontDataFile.close();
- fontDataString = fontDataStream.str();
- for (int i = 0; i < fontDataString.length() - 5; i++) {
- if (fontDataString.substr(i, 5) == "Char ") {
- i += 5;
- int j = 0;
- while (fontDataString.substr(i + j, 1) != " ") {
- j++;
- }
- std::string charNum = fontDataString.substr(i, j);
- i += j;
- if (fontDataString.substr(i, 12) == " Base Width,") {
- i += 12;
- j = 0;
- while (fontDataString.substr(i + j, 1) != "\n") {
- j++;
- }
- std::string widthNum = fontDataString.substr(i, j);
- i += j;
- fontWidths[std::stoi(charNum)] = std::stoi(widthNum);
- }
- }
- }
- }
- int fps = 0;
- int lastTime = 0;
- time_t now;
- struct tm *date, *lastDate;
- unsigned int FBO;
- unsigned int FBOTex;
- int main() {
- time_t begin_time; //time at the start of the loop
- time_t current_time; //time when checking for a one-second interval
- double frames = 0; //number of times passed through the loop
- time(&begin_time); //sets begin_time
- loadFontWidthsFromFile();
- /*
- loader.LoadFile("res/cave.obj");
- numVert = loader.LoadedVertices.size();
- numInd = loader.LoadedIndices.size();
- for (int i = 0; i < numVert; i++) {
- modelVertices.push_back({(float)loader.LoadedVertices.at(i).Position.X, (float)loader.LoadedVertices.at(i).Position.Y, (float)loader.LoadedVertices.at(i).Position.Z, (float)0.0f, (float)0.0f, 0.0f, 0.0f, 0.0f});
- }
- for (int i = 0; i < numInd; i++) {
- modelIndices.push_back(i);
- }
- */
- soundEngine = createIrrKlangDevice();
- //soundEngine->play2D("res/audio/Kinetosis (Diss-Pair) [Patch 4.0] - Splatoon 2 Soundtrack.mp3", TRUE);
- width = SCR_WIDTH;
- height = SCR_HEIGHT;
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- #ifdef __APPLE__
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
- #endif
- GLFWwindow* window = glfwCreateWindow(width, height, "OpenGL Game 3D", NULL, NULL);
- if (window == NULL) {
- std::cout << "Failed to create GLFW window" << std::endl;
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
- std::cout << "Failed to initialize GLAD" << std::endl;
- return -1;
- }
- glGenFramebuffers(1, &FBO);
- glBindFramebuffer(GL_FRAMEBUFFER, FBO);
- glDrawBuffer(GL_COLOR_ATTACHMENT0);
- glGenTextures(1, &FBOTex);
- glActiveTexture(GL_TEXTURE2);
- glBindTexture(GL_TEXTURE_2D, FBOTex);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, FBOTex, 0);
- for (int i = 0; i < TERRAIN_SIZE * TERRAIN_SIZE; i++) {
- terrainVertices[i] = Vertex{ (float)(i % TERRAIN_SIZE), 0.0f, (float)(i / TERRAIN_SIZE), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
- }
- for (int i = 0; i < (TERRAIN_SIZE - 1) * (TERRAIN_SIZE - 1); i++) {
- terrainIndices[(i * 6)] = i + TERRAIN_SIZE;
- terrainIndices[(i * 6) + 1] = i + 1;
- terrainIndices[(i * 6) + 2] = i;
- terrainIndices[(i * 6) + 3] = i + TERRAIN_SIZE;
- terrainIndices[(i * 6) + 4] = i + TERRAIN_SIZE + 1;
- terrainIndices[(i * 6) + 5] = i + 1;
- }
- for (int i = 0; i < (TERRAIN_SIZE * TERRAIN_SIZE); i++) {
- modelVertices.push_back(terrainVertices[i]);
- }
- for (int i = 0; i < (TERRAIN_SIZE * TERRAIN_SIZE) * 6; i++) {
- modelIndices.push_back(terrainIndices[i]);
- }
- for (int i = 0; i < 64; i++) {
- float z1 = (((float)TERRAIN_SIZE / 64) * i) - 1;
- float z2 = z1 + ((float)TERRAIN_SIZE / 64) + 1;
- inkZones.push_back(new InkZone(0.0f, (float)TERRAIN_SIZE, z1, z2));
- }
- int j = 0;
- for (int i = 0; i < inkZones.size(); i++) {
- for (int j = 0; j < modelVertices.size(); j++) {
- if (modelVertices.at(j).pos.x > inkZones.at(i)->x1) {
- if (modelVertices.at(j).pos.x < inkZones.at(i)->x2) {
- if (modelVertices.at(j).pos.z > inkZones.at(i)->z1) {
- if (modelVertices.at(j).pos.z < inkZones.at(i)->z2) {
- inkZones.at(i)->verticesInZone.push_back(j);
- }
- }
- }
- }
- }
- }
- for (int i = 0; i < inkZones.size(); i++) {
- printf("(%g, %g), (%g, %g), %d\n", inkZones.at(i)->x1, inkZones.at(i)->x2, inkZones.at(i)->z1, inkZones.at(i)->z2, inkZones.at(i)->verticesInZone.size());
- }
- initTerrainRender();
- /*
- for (int i = 0; i < 256; i++) {
- placeInkSplotch(128 + ((glfwGetTime() * 4) * sin(glfwGetTime())), 128 + ((glfwGetTime() * 4) * cos(glfwGetTime())), 10, 1, 0);
- placeInkSplotch(rand() % TERRAIN_SIZE, rand() % TERRAIN_SIZE, rand() % 10, 0, 1);
- }
- */
- initHUDRender();
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- Texture grass("res/textures/34293708-soil-seamless-texture.jpg");
- Texture font("res/textures/SplatFont.bmp");
- Texture dudv("res/textures/dudv.png");
- grass.bindToTexUnit(0);
- font.bindToTexUnit(1);
- dudv.bindToTexUnit(3);
- srand(time(NULL));
- inkShader.initShader("res/shaders/inkVertex.glsl", "res/shaders/inkFragment.glsl");
- HUDShader.initShader("res/shaders/vertex.glsl", "res/shaders/fragment.glsl");
- postShader.initShader("res/shaders/postVertex.glsl", "res/shaders/postFragment.glsl");
- while (!glfwWindowShouldClose(window)) {
- frames++; //increments frames
- time(¤t_time); //sets current_time
- processInput(window);
- glBindFramebuffer(GL_FRAMEBUFFER, FBO);
- glViewport(0, 0, width, height);
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glClearColor(0.5f, 0.5f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- prepareTerrainRender();
- placeInkSplotch(rand() % TERRAIN_SIZE, rand() % TERRAIN_SIZE, rand() % 10, 1, 0);
- placeInkSplotch(rand() % TERRAIN_SIZE, rand() % TERRAIN_SIZE, rand() % 10, 0, 1);
- proj = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 1000.0f);
- cameraX = 128;// (128 + (128 * sin((float)glfwGetTime() * 0.4f)));
- cameraZ = 0;// (128 + (128 * cos((float)glfwGetTime() * 0.4f)));
- view = identity;
- view = glm::lookAt(glm::vec3(cameraX, 20, cameraZ), glm::vec3(128, 0, 128), glm::vec3(0, 1, 0));
- model = identity;
- //model = glm::scale(model, glm::vec3(32.0f, 32, 32));
- inkShader.use();
- inkShader.setMat4("projection", proj);
- inkShader.setMat4("view", view);
- inkShader.setMat4("model", model);
- inkShader.setInt("tex", 0);
- inkShader.setVec3("team1Color", 0.0f, 0.0f, 1.0f);
- inkShader.setVec3("team2Color", 1.0f, 1.0f, 0.0f);
- inkShader.setFloat("fogEnabled", 0);
- glDrawElements(GL_TRIANGLES, (TERRAIN_SIZE * TERRAIN_SIZE) * 6, GL_UNSIGNED_INT, (void*) 0);
- prepareHUDRender();
- proj = identity;
- proj = glm::ortho(0.0f, (float)width, (float)height, 0.0f, -1.0f, 1.0f);
- view = identity;
- model = identity;
- model = glm::translate(model, glm::vec3(100, 100, 0.0f));
- model = glm::scale(model, glm::vec3(256, 256, 1));
- HUDShader.use();
- HUDShader.setMat4("projection", proj);
- HUDShader.setMat4("view", view);
- HUDShader.setInt("tex", 1);
- //amountOfTeam1Color = countTeam1Ink();
- //amountOfTeam2Color = countTeam2Ink();
- renderString("FPS: " + std::to_string(fps), 10, 10, 1.0f, 1.0f, 1.0f);
- renderString("Amount of Team 1 Ink: " + std::to_string(amountOfTeam1Color), 10, 42, 1.0f, 1.0f, 1.0f);
- renderString("Amount of Team 2 Ink: " + std::to_string(amountOfTeam2Color), 10, 74, 1.0f, 1.0f, 1.0f);
- renderString("L A M P", 10, 100, 0.0f, 0.0f, 1.0f);
- renderString("The connection is unstable.", 10, 200, 1.0f, 1.0f, 0.0f);
- if (amountOfTeam1Color > amountOfTeam2Color) {
- renderString("Team 1 is winning!", 100, 300, 1.0f, 1.0f, 1.0f);
- } else if(amountOfTeam1Color < amountOfTeam2Color) {
- renderString("Team 2 is winning!", 100, 300, 1.0f, 1.0f, 1.0f);
- } else {
- renderString("It's a draw!", 100, 300, 1.0f, 1.0f, 1.0f);
- }
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- model = identity;
- model = glm::scale(model, glm::vec3(width, height, 1.0f));
- glActiveTexture(GL_TEXTURE2);
- glBindTexture(GL_TEXTURE_2D, FBOTex);
- postShader.use();
- postShader.setMat4("projection", proj);
- postShader.setMat4("model", model);
- postShader.setInt("tex", 2);
- postShader.setInt("dudv", 3);
- postShader.setFloat("time", glfwGetTime());
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glDrawArrays(GL_TRIANGLES, 0, 6);
- glfwSwapBuffers(window);
- glfwPollEvents();
- if (difftime(current_time, begin_time) >= 1.0) {
- fps = frames;
- frames = 0;
- time(&begin_time);
- }
- if (difftime(current_time, begin_time) < 0.0) {
- time(&begin_time);
- }
- }
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- grass.destroy();
- glfwTerminate();
- return 0;
- }
- void processInput(GLFWwindow *window) {
- if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window, true);
- int count;
- const unsigned char* axes = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &count);
- }
- void framebuffer_size_callback(GLFWwindow* window, int wwidth, int wheight) {
- if ((wwidth > 0) && (wheight > 0)) { //To fix crashing caused by minimizing the window
- width = wwidth;
- height = wheight;
- glViewport(0, 0, wwidth, wheight);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, wwidth, wheight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment