penrose

program for generating penrose tilings.
Log | Files | Refs | README | LICENSE

commit 3e9f96ebcd5b958ce2fb6d4010591764f9733d7c
parent 0e88df6dfd6e874fb2cfb180e1c4acace7ea2602
Author: mpizzzle <m@michaelpercival.xyz>
Date:   Sat, 14 Nov 2020 15:57:35 +0000

safety commit. trying to generate images of arbitrary size

Diffstat:
MMakefile | 6+++---
Mpenrose.cpp | 29+++++++++++++++++++++++++----
Apng_writer.cpp | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Apng_writer.hpp | 11+++++++++++
4 files changed, 91 insertions(+), 7 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,10 +1,10 @@ CXX = clang++ CXXFLAGS = -O2 -Wall -Wextra -pedantic -std=c++17 -I. -LIBS = -lGL -lglfw -lGLEW +LIBS = -lGL -lglfw -lGLEW -lpng GLM = /usr/include/glm -DEPS = shader.hpp $(GLM) +DEPS = shader.hpp png_writer.hpp $(GLM) ODIR = obj -_OBJ = penrose.o shader.o +_OBJ = penrose.o shader.o png_writer.o OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) $(ODIR)/%.o: %.cpp $(DEPS) diff --git a/penrose.cpp b/penrose.cpp @@ -10,15 +10,17 @@ #include <glm/gtx/rotate_vector.hpp> #include <array> +#include <iostream> #include <vector> #include <random> #include "shader.hpp" +#include "png_writer.hpp" -static const uint32_t window_w = 1920; -static const uint32_t window_h = 1080; -static const uint32_t depth = 7; //recursion depth -static const bool p2 = false; //tiling type (p2, p3) +static const uint32_t window_w = 1920 * 4; +static const uint32_t window_h = 1080 * 4; +static const uint32_t depth = 10; //recursion depth + static const bool p2 = true; //tiling type (p2, p3) static const float line_w = 2.0f; //line width static const float phi = 1.0 / ((1.0 + sqrt(5.0)) / 2); @@ -160,6 +162,7 @@ int main() { GLint paint = glGetUniformLocation(programID, "paint"); while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 && paint != -1) { + glViewport(-1.0 * (window_w / 2.5), -1.0 * (window_h / 2.5), window_w, window_h); glClearColor(colours.back().x, colours.back().y, colours.back().z, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -177,5 +180,23 @@ int main() { glfwPollEvents(); } + //int width, height; + + //glfwGetFramebufferSize(window, &width, &height); + //std::cout << "width: "<< width << " height: " << height << std::endl; + //GLsizei nrChannels = 4; + //GLsizei stride = nrChannels * width; + //stride += (stride % 4) ? (4 - stride % 4) : 0; + //GLsizei bufferSize = stride * height; + //std::vector<char> buffer(bufferSize); + //glPixelStorei(GL_PACK_ALIGNMENT, 4); + //glReadBuffer(GL_FRONT); + //glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer.data()); + + //stbi_flip_vertically_on_write(true); + //stbi_write_png(filepath, width, height, nrChannels, buffer.data(), stride); + + //PngWriter::write_png_file("test.png", window_w, window_h, nullptr); + return 0; } diff --git a/png_writer.cpp b/png_writer.cpp @@ -0,0 +1,52 @@ +/* + * © 2020 Michael Percival <m@michaelpercival.xyz> + * See LICENSE file for copyright and license details. + */ + +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> + +#define PNG_DEBUG 3 +#include <png.h> + +#include "png_writer.hpp" + +void PngWriter::write_png_file(std::string file_name, int width, int height, std::byte* row_pointers) { + png_structp png_ptr; + png_infop info_ptr; + + /* create file */ + FILE *fp = fopen(file_name.c_str(), "wb"); + + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + info_ptr = png_create_info_struct(png_ptr); + setjmp(png_jmpbuf(png_ptr)); + png_init_io(png_ptr, fp); + + setjmp(png_jmpbuf(png_ptr)); + + png_set_IHDR(png_ptr, info_ptr, width, height, + 16, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + + png_write_info(png_ptr, info_ptr); + + setjmp(png_jmpbuf(png_ptr)); + + png_write_image(png_ptr, (png_bytep *)row_pointers); + + setjmp(png_jmpbuf(png_ptr)); + + png_write_end(png_ptr, NULL); + + /* cleanup heap allocation */ + //for (uint32_t y = 0; y < height; y++) + // free(row_pointers[y]); + + //free(row_pointers); + + fclose(fp); +} diff --git a/png_writer.hpp b/png_writer.hpp @@ -0,0 +1,11 @@ +/* + * © 2020 Michael Percival <m@michaelpercival.xyz> + * See LICENSE file for copyright and license details. + */ + +#include <string> + +class PngWriter { +public: + static void write_png_file(std::string file_name, int width, int height, std::byte* row_pointers); +};