1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
|