Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //========================================================================
- // Simple multi-window example
- // Copyright (c) Camilla Löwy <[email protected]>
- //
- // This software is provided 'as-is', without any express or implied
- // warranty. In no event will the authors be held liable for any damages
- // arising from the use of this software.
- //
- // Permission is granted to anyone to use this software for any purpose,
- // including commercial applications, and to alter it and redistribute it
- // freely, subject to the following restrictions:
- //
- // 1. The origin of this software must not be misrepresented; you must not
- // claim that you wrote the original software. If you use this software
- // in a product, an acknowledgment in the product documentation would
- // be appreciated but is not required.
- //
- // 2. Altered source versions must be plainly marked as such, and must not
- // be misrepresented as being the original software.
- //
- // 3. This notice may not be removed or altered from any source
- // distribution.
- //
- //========================================================================
- /* MODIFIED GLFW EXAMPLE */
- #include <glad/glad.h>
- #define GLFW_INCLUDE_NONE
- #include <GLFW/glfw3.h>
- using namespace std;
- #include <iostream>
- #include <vector>
- #include <string>
- #include <glm/glm.hpp>
- #include <glm/ext.hpp>
- #define STB_IMAGE_IMPLEMENTATION
- #include "stb_image.h"
- typedef unsigned int ID;
- static const unsigned int SCR_WIDTH = 800;
- const unsigned int SCR_HEIGHT = 380;
- static const float TILE_WIDTH = 64.0f; // = 0.6f;
- static const float TILE_HEIGHT = 32.0f; // = -0.16f;
- class GraphicEngine {
- public:
- GraphicEngine();
- ~GraphicEngine();
- void initShaders(const char* vertexShaderSource, const char* fragmentShaderSource);
- void initRenderData(float vertices[], unsigned int size);
- std::vector<ID> initTextures(std::vector<std::string>& paths);
- void drawTextures(std::vector<ID> testuresIds, float wndRatio);
- private:
- GraphicEngine(GraphicEngine&) = delete;
- GraphicEngine(GraphicEngine&&) = delete;
- GraphicEngine& operator=(const GraphicEngine& other) = delete;
- private:
- unsigned int VBO = 0;
- unsigned int VAO = 0;
- unsigned int EBO = 0;
- unsigned int shaderProgram;
- };
- GraphicEngine::GraphicEngine() {
- }
- GraphicEngine::~GraphicEngine() {
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- glDeleteBuffers(1, &EBO);
- }
- void GraphicEngine::initShaders(const char* vertexShaderSource, const char* fragmentShaderSource) {
- #if 1
- glEnable(GL_BLEND);
- glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- #endif
- #if 0
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glBlendEquation(GL_FUNC_ADD);
- #endif
- #if 0
- glEnable(GL_BLEND);
- glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
- glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
- #endif
- #if 0
- glHint(GL_POINT_SMOOTH, GL_NICEST);
- glHint(GL_LINE_SMOOTH, GL_NICEST);
- glHint(GL_POLYGON_SMOOTH, GL_NICEST);
- glEnable(GL_POINT_SMOOTH);
- glEnable(GL_LINE_SMOOTH);
- glEnable(GL_POLYGON_SMOOTH);
- #endif
- unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
- unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- shaderProgram = glCreateProgram();
- glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
- glCompileShader(vertexShader);
- glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
- glCompileShader(fragmentShader);
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
- }
- void GraphicEngine::initRenderData(float vertices[], unsigned int size) {
- unsigned int indices[] = {
- 0, 1, 3,
- 1, 2, 3
- };
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
- glEnableVertexAttribArray(1);
- }
- std::vector<ID> GraphicEngine::initTextures(std::vector<std::string>& paths) {
- std::vector<ID> ids(paths.size());
- stbi_set_flip_vertically_on_load(true);
- for (int i = 0; i < paths.size(); i++) {
- glGenTextures(1, &ids[i]);
- glBindTexture(GL_TEXTURE_2D, ids[i]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // GL_LINEAR); // GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // GL_LINEAR); // GL_NEAREST);
- int width, height, nrChannels;
- unsigned char* data = stbi_load(paths[i].c_str(), &width, &height, &nrChannels, STBI_rgb_alpha);
- if (data) {
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
- glGenerateMipmap(GL_TEXTURE_2D);
- }
- stbi_image_free(data);
- }
- return ids;
- }
- void GraphicEngine::drawTextures(std::vector<ID> testuresIds, float wndRatio) {
- const int countx = 3, county = 3; /* number of tiles */
- const float scale = 100.0f; /* zoom */
- const glm::mat4 mvp = glm::ortho(-wndRatio * scale, wndRatio * scale, -scale, scale, 2.0f, -2.0f);
- const float offx = -((countx * TILE_WIDTH * 2.0f) * 0.5f - TILE_WIDTH);
- const float offy = -TILE_WIDTH * 0.5f;
- for (auto testureId : testuresIds) {
- for (int y = 0; y < county; y++) {
- for (int x = 0; x < countx - (y & 1 ? 1 : 0); x++) {
- const glm::mat4 transform = mvp * glm::translate(glm::mat4(1.0f), glm::vec3(
- offx + x * TILE_WIDTH * 2.0f + (y & 1 ? TILE_WIDTH : 0.0f),
- offy + y * TILE_HEIGHT, 0.0f));
- glBindTexture(GL_TEXTURE_2D, testureId);
- const GLint transformLoc = glGetUniformLocation(shaderProgram, "transform");
- glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
- glUseProgram(shaderProgram);
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- }
- }
- }
- }
- class Window {
- protected:
- GLFWwindow* window;
- GraphicEngine graphicEngine;
- public:
- Window::Window() :
- window(nullptr)
- {}
- Window::~Window() {
- glfwTerminate();
- }
- bool Window::initWindowResources() {
- bool result = false;
- if (glfwInit() == GLFW_TRUE) {
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
- if (window != nullptr) {
- glfwMakeContextCurrent(window);
- if (glfwSetFramebufferSizeCallback(window, [](GLFWwindow* window, int width, int height) {
- glViewport(0, 0, width, height); }) == NULL) {
- if (gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
- result = true;
- }
- }
- }
- }
- return result;
- }
- void Window::mainWindowLoop() {
- const char* vertexShaderSource =
- "#version 330 core\n"
- "layout(location = 0) in vec3 aPos;\n"
- "layout(location = 1) in vec2 aTexCoord;\n"
- "out vec2 TexCoord;\n"
- "uniform mat4 transform;\n"
- "void main()\n"
- "{\n"
- " gl_Position = transform * vec4(aPos, 1.0);\n"
- " TexCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
- "}\n\0";
- const char* fragmentShaderSource =
- "#version 330 core\n"
- "out vec4 FragColor;\n"
- "in vec3 ourColor;\n"
- "in vec2 TexCoord;\n"
- "uniform sampler2D texture1;\n"
- "void main()\n"
- "{\n"
- " vec4 c = texture(texture1, TexCoord);\n"
- " //if (c.a > 0.8f)\n"
- " FragColor = c; \n"
- " //else discard; \n"
- "}\n\0";
- graphicEngine.initShaders(vertexShaderSource, fragmentShaderSource);
- std::vector<std::string> pathsTextures = {
- //"IlbtL.png" /* original tile image, premultiplied alpha */
- //"IlbtLmod0.png" /* enlarge 64x32 tile image, premultiplied alpha */
- //"testb.png" /* modified tile image, not masked */
- "sea2.png" /* modified tile image, 64x32, with marked edges */
- //"sea3.png" /* modified tile image, 64x32, w/o marked edges */
- //"sea4.png" /* modified tile image, 64x32, w/o marked edges, darker */
- };
- float vertices[] = {
- // positions // texture coords
- -TILE_WIDTH, TILE_HEIGHT, 0.0f, 1.0f, 1.0f, // top right
- -TILE_WIDTH, -TILE_HEIGHT, 0.0f, 1.0f, 0.0f, // bottom right
- TILE_WIDTH, -TILE_HEIGHT, 0.0f, 0.0f, 0.0f, // bottom left
- TILE_WIDTH, TILE_HEIGHT, 0.0f, 0.0f, 1.0f // top left
- };
- graphicEngine.initRenderData(vertices, sizeof(vertices));
- std::vector<ID> idsTextures = graphicEngine.initTextures(pathsTextures);
- while (!glfwWindowShouldClose(window))
- {
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- const float wndRatio = width / (float)height;
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- graphicEngine.drawTextures(idsTextures, wndRatio);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- }
- };
- int main(int argc, char* argv[])
- {
- Window window;
- if (window.initWindowResources()) {
- window.mainWindowLoop();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement