Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // dear imgui: standalone example application for GLFW + OpenGL 3, using programmable pipeline
- // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
- // (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
- #include "imgui.h"
- #include "imgui_impl_glfw.h"
- #include "imgui_impl_opengl3.h"
- #include <glm/gtc/matrix_transform.hpp>
- #include <glfw/glfw3.h>
- #include <glm/gtc/quaternion.hpp>
- #include "glm/gtc/matrix_transform.hpp"
- #include <vector>
- //#include <glm/gtx/quaternion.hpp>
- #include <stdio.h>
- // About OpenGL function loaders: modern OpenGL doesn't have a standard header file and requires individual function pointers to be loaded manually.
- // Helper libraries are often used for this purpose! Here we are supporting a few common ones: gl3w, glew, glad.
- // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
- #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
- #include <GL/gl3w.h> // Initialize with gl3wInit()
- #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
- #include <GL/glew.h> // Initialize with glewInit()
- #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
- #include <glad/glad.h> // Initialize with gladLoadGL()
- #else
- #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM
- #endif
- #include <GLFW/glfw3.h> // Include glfw3.h after our OpenGL definitions
- #include <iostream>
- static void glfw_error_callback(int error, const char* description)
- {
- fprintf(stderr, "Glfw Error %d: %s\n", error, description);
- }
- float Distance(float x1, float x2, float y1, float y2)
- {
- return sqrtf((x2 - x1)*(x2 - x1)+(y2 - y1)*(y2 - y1));
- }
- ImVec4 BGcolor = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
- #pragma region Dywan
- //static void Carpet(int step, GLfloat maxX, GLfloat maxY, GLfloat minX, GLfloat minY, GLuint &VBO, GLuint &VAO, GLuint &program)
- //{
- // if (step == 0)
- // return;
- //
- // GLfloat white[] = {
- // minX, maxY, 0.0f,
- // maxX, maxY, 0.0f,
- // maxX, minY, 0.0f,
- // minX, maxY, 0.0f,
- // minX, minY, 0.0f,
- // maxX, minY, 0.0f
- // };
- // /*GLfloat black[] = {
- // minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
- // minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
- // minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f,
- // minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
- // minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f,
- // minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f
- // };*/
- // GLfloat black[] = {
- // minX, minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
- // minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
- // minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f,
- // minX, minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
- // minX, minY + (maxY - minY)*(1.0f / 3.0f), 0.0f,
- // minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f
- // };
- //
- // glBindBuffer(GL_ARRAY_BUFFER, VBO);
- //
- // glBufferData(GL_ARRAY_BUFFER, sizeof(white), white, GL_DYNAMIC_DRAW);
- // glUseProgram(program);
- // int vertexColorLocation = glGetUniformLocation(program, "ourColor");
- // glUniform4f(vertexColorLocation, BGcolor.x, BGcolor.y, BGcolor.z, 1.0f);
- // glBindVertexArray(VAO);
- // glDrawArrays(GL_TRIANGLES, 0, 6);
- //
- // glBufferData(GL_ARRAY_BUFFER, sizeof(black), black, GL_DYNAMIC_DRAW);
- // glUseProgram(program);
- // glUniform4f(vertexColorLocation, 0.0f, 0.0f, 0.0f, 1.0f);
- // glBindVertexArray(VAO);
- // glDrawArrays(GL_TRIANGLES, 0, 6);
- //
- // Carpet(step-1, minX + (maxX - minX)*(1.0f / 3.0f), maxY, minX, minY + (maxY - minY)*(2.0f / 3.0f), VBO, VAO, program); //TOP LEFT
- // Carpet(step-1, minX + (maxX - minX)*(2.0f / 3.0f), maxY, minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), VBO, VAO, program); //TOP CENTER
- // Carpet(step-1, maxX, maxY, minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), VBO, VAO, program); //TOP RIGHT
- // //Carpet(step-1, minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), minX, minY + (maxY - minY)*(1.0f / 3.0f), VBO, VAO, program); //CENTER LEFT
- // Carpet(step-1, maxX, minY + (maxY - minY)*(2.0f / 3.0f), minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), VBO, VAO, program); //CENTER RIGHT
- // Carpet(step-1, minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), minX, minY, VBO, VAO, program); //BOTTOM LEFT
- // Carpet(step-1, minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), minX + (maxX - minX)*(1.0f / 3.0f), minY, VBO, VAO, program); //BOTTOM CENTER
- // Carpet(step-1, maxX, minY + (maxY - minY)*(1.0f / 3.0f), minX + (maxX - minX)*(2.0f / 3.0f), minY, VBO, VAO, program); //BOTTOM RIGHT
- //}
- #pragma endregion
- #pragma region MengerCube
- void Square(glm::vec3 max, glm::vec3 min, std::vector<GLfloat>* vertices, int* triangleCount, std::vector<GLfloat>* UV)
- {
- vertices->push_back(max[0]); //TOP FACE
- vertices->push_back(max[1]);
- vertices->push_back(max[2]);
- vertices->push_back(min[0]);
- vertices->push_back(max[1]);
- vertices->push_back(max[2]);
- vertices->push_back(min[0]);
- vertices->push_back(max[1]);
- vertices->push_back(min[2]);
- UV->push_back(0.000059f);
- UV->push_back(1.0f - 0.000004f);
- UV->push_back(0.000103f);
- UV->push_back(1.0f - 0.336048f);
- UV->push_back(0.335973f);
- UV->push_back(1.0f - 0.335903f);
- (*triangleCount)++;
- vertices->push_back(min[0]);
- vertices->push_back(max[1]);
- vertices->push_back(min[2]);
- vertices->push_back(max[0]);
- vertices->push_back(max[1]);
- vertices->push_back(min[2]);
- vertices->push_back(max[0]);
- vertices->push_back(max[1]);
- vertices->push_back(max[2]);
- UV->push_back(1.000023f);
- UV->push_back(1.0f - 0.000013f);
- UV->push_back(0.667979f);
- UV->push_back(1.0f - 0.335851f);
- UV->push_back(0.999958f);
- UV->push_back(1.0f - 0.336064f);
- (*triangleCount)++;
- vertices->push_back(max[0]); //WEST FACE
- vertices->push_back(max[1]);
- vertices->push_back(max[2]);
- vertices->push_back(max[0]);
- vertices->push_back(max[1]);
- vertices->push_back(min[2]);
- vertices->push_back(max[0]);
- vertices->push_back(min[1]);
- vertices->push_back(min[2]);
- UV->push_back(0.667979f);
- UV->push_back(1.0f - 0.335851f);
- UV->push_back(0.336024f);
- UV->push_back(1.0f - 0.671877f);
- UV->push_back(0.667969f);
- UV->push_back(1.0f - 0.671889f);
- (*triangleCount)++;
- vertices->push_back(max[0]);
- vertices->push_back(min[1]);
- vertices->push_back(min[2]);
- vertices->push_back(max[0]);
- vertices->push_back(min[1]);
- vertices->push_back(max[2]);
- vertices->push_back(max[0]);
- vertices->push_back(max[1]);
- vertices->push_back(max[2]);
- UV->push_back(1.000023f);
- UV->push_back(1.0f - 0.000013f);
- UV->push_back(0.668104f);
- UV->push_back(1.0f - 0.000013f);
- UV->push_back(0.667979f);
- UV->push_back(1.0f - 0.335851f);
- (*triangleCount)++;
- vertices->push_back(max[0]); //SOUTH FACE
- vertices->push_back(max[1]);
- vertices->push_back(min[2]);
- vertices->push_back(max[0]);
- vertices->push_back(min[1]);
- vertices->push_back(min[2]);
- vertices->push_back(min[0]);
- vertices->push_back(min[1]);
- vertices->push_back(min[2]);
- UV->push_back(0.000059f);
- UV->push_back(1.0f - 0.000004f);
- UV->push_back(0.335973f);
- UV->push_back(1.0f - 0.335903f);
- UV->push_back(0.336098f);
- UV->push_back(1.0f - 0.000071f);
- (*triangleCount)++;
- vertices->push_back(min[0]);
- vertices->push_back(min[1]);
- vertices->push_back(min[2]);
- vertices->push_back(min[0]);
- vertices->push_back(max[1]);
- vertices->push_back(min[2]);
- vertices->push_back(max[0]);
- vertices->push_back(max[1]);
- vertices->push_back(min[2]);
- UV->push_back(0.667979f);
- UV->push_back(1.0f - 0.335851f);
- UV->push_back(0.335973f);
- UV->push_back(1.0f - 0.335903f);
- UV->push_back(0.336024f);
- UV->push_back(1.0f - 0.671877f);
- (*triangleCount)++;
- vertices->push_back(min[0]); //EAST FACE
- vertices->push_back(max[1]);
- vertices->push_back(min[2]);
- vertices->push_back(min[0]);
- vertices->push_back(min[1]);
- vertices->push_back(min[2]);
- vertices->push_back(min[0]);
- vertices->push_back(min[1]);
- vertices->push_back(max[2]);
- UV->push_back(1.000004f);
- UV->push_back(1.0f - 0.671847f);
- UV->push_back(0.999958f);
- UV->push_back(1.0f - 0.336064f);
- UV->push_back(0.667979f);
- UV->push_back(1.0f - 0.335851f);
- (*triangleCount)++;
- vertices->push_back(min[0]);
- vertices->push_back(min[1]);
- vertices->push_back(max[2]);
- vertices->push_back(min[0]);
- vertices->push_back(max[1]);
- vertices->push_back(max[2]);
- vertices->push_back(min[0]);
- vertices->push_back(max[1]);
- vertices->push_back(min[2]);
- UV->push_back(0.668104f);
- UV->push_back(1.0f - 0.000013f);
- UV->push_back(0.335973f);
- UV->push_back(1.0f - 0.335903f);
- UV->push_back(0.667979f);
- UV->push_back(1.0f - 0.335851f);
- (*triangleCount)++;
- vertices->push_back(min[0]); //NORTH FACE
- vertices->push_back(max[1]);
- vertices->push_back(max[2]);
- vertices->push_back(min[0]);
- vertices->push_back(min[1]);
- vertices->push_back(max[2]);
- vertices->push_back(max[0]);
- vertices->push_back(min[1]);
- vertices->push_back(max[2]);
- UV->push_back(0.335973f);
- UV->push_back(1.0f - 0.335903f);
- UV->push_back(0.668104f);
- UV->push_back(1.0f - 0.000013f);
- UV->push_back(0.336098f);
- UV->push_back(1.0f - 0.000071f);
- (*triangleCount)++;
- vertices->push_back(max[0]);
- vertices->push_back(min[1]);
- vertices->push_back(max[2]);
- vertices->push_back(max[0]);
- vertices->push_back(max[1]);
- vertices->push_back(max[2]);
- vertices->push_back(min[0]);
- vertices->push_back(max[1]);
- vertices->push_back(max[2]);
- UV->push_back(0.000103f);
- UV->push_back(1.0f - 0.336048f);
- UV->push_back(0.000004f);
- UV->push_back(1.0f - 0.671870f);
- UV->push_back(0.336024f);
- UV->push_back(1.0f - 0.671877f);
- (*triangleCount)++;
- vertices->push_back(max[0]); //DOWN FACE
- vertices->push_back(min[1]);
- vertices->push_back(max[2]);
- vertices->push_back(max[0]);
- vertices->push_back(min[1]);
- vertices->push_back(min[2]);
- vertices->push_back(min[0]);
- vertices->push_back(min[1]);
- vertices->push_back(min[2]);
- UV->push_back(0.000103f);
- UV->push_back(1.0f - 0.336048f);
- UV->push_back(0.336024f);
- UV->push_back(1.0f - 0.671877f);
- UV->push_back(0.335973f);
- UV->push_back(1.0f - 0.335903f);
- (*triangleCount)++;
- vertices->push_back(min[0]);
- vertices->push_back(min[1]);
- vertices->push_back(min[2]);
- vertices->push_back(min[0]);
- vertices->push_back(min[1]);
- vertices->push_back(max[2]);
- vertices->push_back(max[0]);
- vertices->push_back(min[1]);
- vertices->push_back(max[2]);
- UV->push_back(0.667969f);
- UV->push_back(1.0f - 0.671889f);
- UV->push_back(1.000004f);
- UV->push_back(1.0f - 0.671847f);
- UV->push_back(0.667979f);
- UV->push_back(1.0f - 0.335851f);
- (*triangleCount)++;
- }
- void Sponge(glm::vec3 max, glm::vec3 min, int step, std::vector<GLfloat>* vertices, int* triangleCount, std::vector<GLfloat>* UV)
- {
- float xStep = (max[0] - min[0])*(1.0f / 3.0f);
- float yStep = (max[1] - min[1])*(1.0f / 3.0f);
- float zStep = (max[2] - min[2])*(1.0f / 3.0f);
- if (step == 1)
- Square(max, min, vertices, triangleCount, UV);
- else
- {
- Sponge(max, glm::vec3(xStep*2,yStep*2,zStep*2), step - 1, vertices, triangleCount, UV); //TOP LAYER TOP LEFT CORNER
- Sponge(glm::vec3(xStep*2,max[1],max[2]), glm::vec3(xStep,yStep*2,zStep*2), step - 1, vertices, triangleCount, UV); //TOP LAYER TOP CENTER
- Sponge(glm::vec3(xStep,max[1],max[2]), glm::vec3(min[0],yStep*2,zStep*2), step - 1, vertices, triangleCount, UV); //TOP LAYER TOP RIGHT CORNER
- Sponge(glm::vec3(max[0],max[1],zStep*2), glm::vec3(xStep*2,yStep*2,zStep), step - 1, vertices, triangleCount, UV); //TOP LAYER LEFT CENTER
- Sponge(glm::vec3(xStep,max[1],zStep*2), glm::vec3(min[0],yStep*2,zStep), step - 1, vertices, triangleCount, UV); //TOP LAYER RIGHT CENTER
- Sponge(glm::vec3(max[0],max[1],zStep), glm::vec3(xStep*2,yStep*2,min[2]), step - 1, vertices, triangleCount, UV); //TOP LAYER BOTTOM LEFT CORNER
- Sponge(glm::vec3(xStep*2,max[1],zStep), glm::vec3(xStep,yStep*2,min[2]), step - 1, vertices, triangleCount, UV); //TOP LAYER BOTTOM CENTER
- Sponge(glm::vec3(xStep,max[1],zStep), glm::vec3(min[0],yStep*2,min[2]), step - 1, vertices, triangleCount, UV); //TOP LAYER BOTTOM RIGHT CORNER
- Sponge(glm::vec3(max[0],yStep*2,max[2]), glm::vec3(xStep*2,yStep,zStep*2), step - 1, vertices, triangleCount, UV); //MID LAYER TOP LEFT CORNER
- Sponge(glm::vec3(xStep,yStep*2,max[2]), glm::vec3(min[0],yStep,zStep*2), step - 1, vertices, triangleCount, UV); //MID LAYER TOP RIGHT CORNER
- Sponge(glm::vec3(max[0],yStep*2,zStep), glm::vec3(xStep*2,yStep,min[2]), step - 1, vertices, triangleCount, UV); //MID LAYER BOTTOM LEFT CORNER
- Sponge(glm::vec3(xStep,yStep*2,zStep), glm::vec3(min[0],yStep,min[2]), step - 1, vertices, triangleCount, UV); //MID LAYER BOTTOM RIGHT CORNER
- Sponge(glm::vec3(max[0],yStep,max[2]), glm::vec3(xStep*2,min[1],zStep*2), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER TOP LEFT CORNER
- Sponge(glm::vec3(xStep * 2, yStep, max[2]), glm::vec3(min[0], min[1], zStep * 2), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER TOP CENTER
- Sponge(glm::vec3(xStep, yStep, max[2]), glm::vec3(min[0], min[1], zStep * 2), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER TOP RIGHT CORNER
- Sponge(glm::vec3(max[0], yStep, zStep * 2), glm::vec3(xStep * 2, min[1], zStep), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER LEFT CENTER
- Sponge(glm::vec3(xStep, yStep, zStep * 2), glm::vec3(min[0], min[1], zStep), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER RIGHT CENTER
- Sponge(glm::vec3(max[0], yStep, zStep), glm::vec3(xStep * 2, min[1], min[2]), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER BOTTOM LEFT CORNER
- Sponge(glm::vec3(xStep * 2, yStep, zStep), glm::vec3(xStep, min[1], min[2]), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER BOTTOM CENTER
- Sponge(glm::vec3(xStep, yStep, zStep), min, step - 1, vertices, triangleCount, UV); //BOTTOM LAYER BOTTOM RIGHT CORNER
- }
- }
- #pragma endregion
- // position
- glm::vec3 position = glm::vec3(0, 0, 5);
- // horizontal angle : toward -Z
- float horizontalAngle = 3.14f;
- // vertical angle : 0, look at the horizon
- float verticalAngle = 0.0f;
- // Initial Field of View
- float initialFoV = 45.0f;
- float speed = 3.0f; // 3 units / second
- float mouseSpeed = 0.025f;
- double lastTime = 0;
- GLuint loadBMP_custom(const char * imagepath)
- {
- // Data read from the header of the BMP file
- unsigned char header[54]; // Each BMP file begins by a 54-bytes header
- unsigned int dataPos; // Position in the file where the actual data begins
- unsigned int width, height;
- unsigned int imageSize; // = width*height*3
- // Actual RGB data
- unsigned char * data;
- // Open the file
- FILE * file = fopen(imagepath, "rb");
- if (!file) { printf("Image could not be opened\n"); return 0; }
- if (fread(header, 1, 54, file) != 54) { // If not 54 bytes read : problem
- printf("Not a correct BMP file\n");
- return false;
- }
- if (header[0] != 'B' || header[1] != 'M') {
- printf("Not a correct BMP file\n");
- return 0;
- }
- // Read ints from the byte array
- dataPos = *(int*)&(header[0x0A]);
- imageSize = *(int*)&(header[0x22]);
- width = *(int*)&(header[0x12]);
- height = *(int*)&(header[0x16]);
- // Some BMP files are misformatted, guess missing information
- if (imageSize == 0) imageSize = width * height * 3; // 3 : one byte for each Red, Green and Blue component
- if (dataPos == 0) dataPos = 54; // The BMP header is done that way
- // Create a buffer
- data = new unsigned char[imageSize];
- // Read the actual data from the file into the buffer
- fread(data, 1, imageSize, file);
- //Everything is in memory now, the file can be closed
- fclose(file);
- // Create one OpenGL texture
- GLuint textureID;
- glGenTextures(1, &textureID);
- // "Bind" the newly created texture : all future texture functions will modify this texture
- glBindTexture(GL_TEXTURE_2D, textureID);
- // Give the image to OpenGL
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- }
- int main(int, char**)
- {
- // Setup window
- glfwSetErrorCallback(glfw_error_callback);
- if (!glfwInit())
- return 1;
- // Decide GL+GLSL versions
- #if __APPLE__
- // GL 3.2 + GLSL 150
- const char* glsl_version = "#version 150";
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
- #else
- // GL 4.3 + GLSL 430
- const char* glsl_version = "#version 430";
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
- #endif
- // Create window with graphics context
- GLFWwindow* window = glfwCreateWindow(1280, 720, "Menger Sponge", NULL, NULL);
- if (window == NULL)
- return 1;
- glfwMakeContextCurrent(window);
- glfwSwapInterval(1); // Enable vsync
- // Initialize OpenGL loader
- #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
- bool err = gl3wInit() != 0;
- #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
- bool err = glewInit() != GLEW_OK;
- #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
- bool err = !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
- #endif
- if (err)
- {
- fprintf(stderr, "Failed to initialize OpenGL loader!\n");
- return 1;
- }
- // Setup Dear ImGui binding
- IMGUI_CHECKVERSION();
- ImGui::CreateContext();
- ImGuiIO& io = ImGui::GetIO(); (void)io;
- io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
- io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
- ImGui_ImplGlfw_InitForOpenGL(window, true);
- ImGui_ImplOpenGL3_Init(glsl_version);
- // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
- glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1280.0f / 720.0f, 0.1f, 100.0f);
- // Or, for an ortho camera :
- //glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates
- // Camera matrix
- // Camera matrix
- glm::mat4 View = glm::lookAt(
- glm::vec3(-10, 3, -10), // Camera is at (4,3,3), in World Space
- glm::vec3(0, 0, 0), // and looks at the origin
- glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
- );
- // Model matrix : an identity matrix (model will be at the origin)
- glm::mat4 Model = glm::mat4(1.0f);
- // Our ModelViewProjection : multiplication of our 3 matrices
- // Setup style
- ImGui::StyleColorsClassic();
- const char *vertexShaderSource = "#version 330 core\n"
- "layout (location = 0) in vec3 aPos;\n"
- "layout (location = 1) in vec2 vertexUV;\n"
- "uniform mat4 MVP;\n"
- "out vec2 UV;\n"
- "void main()\n"
- "{\n"
- " gl_Position = MVP * vec4(aPos, 1);\n"
- " UV = vertexUV;\n"
- "}\0";
- const char *fragmentShaderSource = "#version 330 core\n"
- "in vec2 UV;\n"
- "out vec4 FragColor;\n"
- "uniform sampler2D myTextureSampler;\n"
- "uniform vec4 bgcolor;\n"
- "void main()\n"
- "{\n"
- " FragColor = texture(myTextureSampler, UV) * vec4(bgcolor);\n"
- "}\n\0";
- GLuint Texture = loadBMP_custom("tex.bmp");
- GLuint vertexShader, fragmentShader;
- vertexShader = glCreateShader(GL_VERTEX_SHADER);
- fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
- glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
- glCompileShader(vertexShader);
- GLint success;
- GLchar infoLog[512];
- glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- return 1;
- glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
- //std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::end;
- }
- glCompileShader(fragmentShader);
- glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- return 2;
- glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
- //std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::end;
- }
- GLuint image;
- int triangleCount = 0;
- std::vector<GLfloat> vertices;
- std::vector<GLfloat> UV;
- GLuint shaderProgram;
- shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
- GLuint VBO, VAO, colorbuffer;
- glGenVertexArrays(1, &VAO);
- glBindVertexArray(VAO);
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
- glGenBuffers(1, &colorbuffer);
- glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
- glBufferData(GL_ARRAY_BUFFER, UV.size()*sizeof(GLfloat), UV.data(), GL_DYNAMIC_DRAW);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- // Get a handle for our "MVP" uniform
- // Only during the initialisation
- GLuint MatrixID = glGetUniformLocation(shaderProgram, "MVP");
- int vertexColorLocation = glGetUniformLocation(shaderProgram, "bgcolor");
- // Main loop
- while (!glfwWindowShouldClose(window))
- {
- bool rotated = false;
- //glUniform4f(vertexColorLocation, 0.1f, 0.1f, 0.2f, 1.0f);
- //glBindBuffer(GL_ARRAY_BUFFER, VBO);
- //glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
- glfwPollEvents();
- ImGui_ImplOpenGL3_NewFrame();
- ImGui_ImplGlfw_NewFrame();
- ImGui::NewFrame();
- //glm::mat4 Model = glm::mat4(1.0f);
- static float x = 5;
- static float y = 1;
- static int f = 3;
- glm::mat4 RotationFull = glm::mat4(1.0f);
- ImGui::Begin("Options"); // Create a window called "Options" and append into it.
- ImGui::Text("Menger Sponge"); // Display some text (you can use a format strings too)
- if(ImGui::SliderFloat("RotationY", &y, 1, 1000))
- {
- Model = glm::mat4(1.0f);
- rotated = true;
- }
- glm::mat4 RotationY = glm::rotate(Model, glm::radians(y), glm::vec3(1.0f, 0.0f, 0.0f));
- if (ImGui::SliderFloat("RotationX", &x, 1, 1000))
- {
- Model = glm::mat4(1.0f);
- rotated = true;
- }
- glm::mat4 RotationX = glm::rotate(Model, glm::radians(x), glm::vec3(0.0f, 1.0f, 0.0f));
- RotationFull = RotationY * RotationX;
- ImGui::SliderInt("Depth", &f, 1, 5);
- ImGui::ColorEdit3("TextureColor", (float*)&BGcolor);
- ImGui::End();
- //Model = glm::mat4(1.0f);
- if(rotated)
- Model *= RotationFull;
- glm::mat4 mvp = Projection * View * Model;
- std::vector<GLfloat> vertices;
- std::vector<GLfloat> UV;
- // Rendering
- ImGui::Render();
- int display_w, display_h;
- glfwMakeContextCurrent(window);
- glfwGetFramebufferSize(window, &display_w, &display_h);
- glViewport(0, 0, display_w, display_h);
- // Send our transformation to the currently bound shader, in the "MVP" uniform
- // This is done in the main loop since each model will have a different MVP matrix (At least for the M part)
- glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp[0][0]);
- glUniform4f(vertexColorLocation, BGcolor.x, BGcolor.y, BGcolor.z, 1.0f);
- glClearColor(.5f, .5f, .5f, 1);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- Sponge(glm::vec3(1, 1, 1), glm::vec3(-1, -1, -1), f, &vertices, &triangleCount, &UV);
- glBindVertexArray(VAO);
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
- glGenBuffers(1, &colorbuffer);
- glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
- glBufferData(GL_ARRAY_BUFFER, UV.size() * sizeof(GLfloat), UV.data(), GL_DYNAMIC_DRAW);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
- glEnableVertexAttribArray(1);
- //Carpet(f, 1.0f, 1.0f, -1.0f, -1.0f, VBO, VAO, shaderProgram);
- glUseProgram(shaderProgram);
- glBindVertexArray(VAO);
- glDrawArrays(GL_TRIANGLES, 0, vertices.size());
- ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
- glfwMakeContextCurrent(window);
- glfwSwapBuffers(window);
- }
- // Cleanup
- ImGui_ImplOpenGL3_Shutdown();
- ImGui_ImplGlfw_Shutdown();
- ImGui::DestroyContext();
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement