From 1d3889e93f547df9f6c578b107552807c27fc6d4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 4 May 2019 23:19:05 +0400 Subject: i commit --- Canvas.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Canvas.h (limited to 'Canvas.h') diff --git a/Canvas.h b/Canvas.h new file mode 100644 index 0000000..b2f943d --- /dev/null +++ b/Canvas.h @@ -0,0 +1,62 @@ +// +// Created by saintruler on 04.05.19. +// + +#ifndef OPENGLTEST_CANVAS_H +#define OPENGLTEST_CANVAS_H + +#include +#include + +#include + +struct Color +{ + unsigned int r = 0, g = 0, b = 0; +}; + +class Canvas +{ +public: + Canvas(int width, int height) + { + this->width = width; + this->height = height; + + canvas = (unsigned char*) malloc(width * height * 3); + glGenTextures(1, &texture); + } + + void SetPixel(int x, int y, Color color) + { + glBindTexture(GL_TEXTURE_2D, texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object + + // set texture wrapping to GL_REPEAT (default wrapping method) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + 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); + + canvas[y * width * 3 + x * 3 + 0] = color.r; + canvas[y * width * 3 + x * 3 + 1] = color.g; + canvas[y * width * 3 + x * 3 + 2] = color.b; + + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, width, 0, GL_RGB, GL_UNSIGNED_BYTE, canvas); + glGenerateMipmap(GL_TEXTURE_2D); + } + + unsigned int* GetTexture() + { + return &(this->texture); + } + +private: + int width, height; + unsigned char* canvas; + unsigned int texture; +}; + +#endif //OPENGLTEST_CANVAS_H -- cgit v1.2.3