diff options
| author | Andrew <saintruler@gmail.com> | 2019-05-04 23:19:05 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2019-05-04 23:19:05 +0400 |
| commit | 1d3889e93f547df9f6c578b107552807c27fc6d4 (patch) | |
| tree | da6859e98634255f1799611a74adcc5d49d8a81c /Canvas.h | |
i commit
Diffstat (limited to 'Canvas.h')
| -rw-r--r-- | Canvas.h | 62 |
1 files changed, 62 insertions, 0 deletions
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 <glad/glad.h> +#include <GLFW/glfw3.h> + +#include <iostream> + +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 |