summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2019-05-04 23:19:05 +0400
committerAndrew <saintruler@gmail.com>2019-05-04 23:19:05 +0400
commit1d3889e93f547df9f6c578b107552807c27fc6d4 (patch)
treeda6859e98634255f1799611a74adcc5d49d8a81c /main.cpp
i commit
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp260
1 files changed, 260 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..2d4ecce
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,260 @@
+#include <glad/glad.h>
+#include <GLFW/glfw3.h>
+
+#define STB_IMAGE_IMPLEMENTATION
+#include <stb_image.h>
+
+#include <shader.h>
+#include "Canvas.h"
+#include "point.h"
+
+#include <iostream>
+#include <cmath>
+#include <vector>
+
+void framebuffer_size_callback(GLFWwindow* window, int width, int height);
+void processInput(GLFWwindow *window);
+
+void loadTexture(std::string path, unsigned int* texture, int* width, int* height);
+void generateCanvasTexture(unsigned int *texture, unsigned char color);
+
+// settings
+const unsigned int SCR_WIDTH = 800;
+const unsigned int SCR_HEIGHT = 600;
+const std::string PROJECT_PATH = "/home/saintruler/CLionProjects/opengltest/";
+unsigned char* canvas = (unsigned char*) malloc(SCR_HEIGHT * SCR_WIDTH * 3);
+
+std::vector<point> points;
+const double c = 1e5;
+
+int main()
+{
+ // glfw: initialize and configure
+ // ------------------------------
+ glfwInit();
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+ glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+
+ // glfw window creation
+ // --------------------
+ GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
+ if (window == NULL)
+ {
+ std::cout << "Failed to create GLFW window" << std::endl;
+ glfwTerminate();
+ return -1;
+ }
+ glfwMakeContextCurrent(window);
+ glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
+
+ // glad: load all OpenGL function pointers
+ // ---------------------------------------
+ if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
+ {
+ std::cout << "Failed to initialize GLAD" << std::endl;
+ return -1;
+ }
+
+ // build and compile our shader zprogram
+ // ------------------------------------
+ Shader ourShader((PROJECT_PATH + "4.1.texture.vs").c_str(), (PROJECT_PATH + "4.1.texture.fs").c_str());
+
+ // set up vertex data (and buffer(s)) and configure vertex attributes
+ // ------------------------------------------------------------------
+ float vertices1[] = {
+ // positions // colors // texture coords
+ 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
+ 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
+ -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
+ -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
+ };
+
+ float vertices[] = {
+ // positions // colors // texture coords
+ 1.f, 1.f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
+ 1.f, -1.f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
+ -1.f, -1.f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
+ -1.f, 1.f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
+ };
+ unsigned int indices[] = {
+ 0, 1, 3, // first triangle
+ 1, 2, 3 // second triangle
+ };
+ unsigned int VBO, VAO, EBO;
+ glGenVertexArrays(1, &VAO);
+ glGenBuffers(1, &VBO);
+ glGenBuffers(1, &EBO);
+
+ glBindVertexArray(VAO);
+
+ glBindBuffer(GL_ARRAY_BUFFER, VBO);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
+
+ // position attribute
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
+ glEnableVertexAttribArray(0);
+ // color attribute
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
+ glEnableVertexAttribArray(1);
+ // texture coord attribute
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
+ glEnableVertexAttribArray(2);
+
+
+ // load and create a texture
+ // -------------------------
+ unsigned int texture;
+ glGenTextures(1, &texture);
+ unsigned char i = 0;
+
+ points.emplace_back(point(100, 100));
+ points.emplace_back(point(500, 500));
+ points.emplace_back(point(200, 375));
+
+
+ // render loop
+ // -----------
+ while (!glfwWindowShouldClose(window))
+ {
+ // input
+ // -----
+ processInput(window);
+
+ // render
+ // ------
+ glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ generateCanvasTexture(&texture, i++);
+ // bind Texture
+ glBindTexture(GL_TEXTURE_2D, texture);
+
+ // render container
+ ourShader.use();
+ glBindVertexArray(VAO);
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
+
+ // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
+ // -------------------------------------------------------------------------------
+ glfwSwapBuffers(window);
+ glfwPollEvents();
+ }
+
+ // optional: de-allocate all resources once they've outlived their purpose:
+ // ------------------------------------------------------------------------
+ glDeleteVertexArrays(1, &VAO);
+ glDeleteBuffers(1, &VBO);
+ glDeleteBuffers(1, &EBO);
+
+ // glfw: terminate, clearing all previously allocated GLFW resources.
+ // ------------------------------------------------------------------
+ glfwTerminate();
+ return 0;
+}
+
+
+void loadTexture(std::string path, unsigned int* texture, int* width, int* height)
+{
+ glGenTextures(1, texture);
+ glBindTexture(GL_TEXTURE_2D, *texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
+ // set the texture wrapping parameters
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ // set texture filtering parameters
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ // load image, create texture and generate mipmaps
+ int nrChannels;
+ unsigned char* data = stbi_load(path.c_str(), width, height, &nrChannels, 0);
+
+ if (data)
+ {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, *width, *height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
+ glGenerateMipmap(GL_TEXTURE_2D);
+ }
+ else
+ {
+ std::cout << "Failed to load texture" << std::endl;
+ }
+ stbi_image_free(data);
+}
+
+
+void generateCanvasTexture(unsigned int *texture, unsigned char color)
+{
+ glBindTexture(GL_TEXTURE_2D, *texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
+ // set the texture wrapping parameters
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ // set texture filtering parameters
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ // load image, create texture and generate mipmaps
+
+
+ for (int x = 0; x < SCR_WIDTH; x++)
+ {
+ for (int y = 0; y < SCR_HEIGHT; y++)
+ {
+
+ point canvas_pt(x, y);
+ double f = 0;
+ for (point pt : points)
+ {
+ point path = pt - canvas_pt;
+
+ f += (c * 20 * 1) / path.length_sqared();
+ }
+
+ int l = (int) f;
+ if (l > 255) l = 255;
+
+
+ canvas[y * SCR_WIDTH * 3 + x * 3 + 0] = l;
+ canvas[y * SCR_WIDTH * 3 + x * 3 + 1] = 0;
+ canvas[y * SCR_WIDTH * 3 + x * 3 + 2] = 0;
+ }
+ }
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char*) canvas);
+ glGenerateMipmap(GL_TEXTURE_2D);
+}
+
+
+// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
+// ---------------------------------------------------------------------------------------------------------
+void processInput(GLFWwindow *window)
+{
+ if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
+ glfwSetWindowShouldClose(window, true);
+
+ if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
+ points[0].x -= 20;
+ if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
+ points[0].x += 20;
+
+ if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
+ points[0].y -= 20;
+ if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
+ points[0].y += 20;
+
+ double xpos, ypos;
+ //getting cursor position
+ glfwGetCursorPos(window, &xpos, &ypos);
+ points[0].x = xpos;
+ points[0].y = SCR_HEIGHT - ypos;
+}
+
+// glfw: whenever the window size changed (by OS or user resize) this callback function executes
+// ---------------------------------------------------------------------------------------------
+void framebuffer_size_callback(GLFWwindow* window, int width, int height)
+{
+ // make sure the viewport matches the new window dimensions; note that width and
+ // height will be significantly larger than specified on retina displays.
+ glViewport(0, 0, width, height);
+}
+