summaryrefslogtreecommitdiff
path: root/Canvas/Canvas.h
blob: 84dbe565dde21f2bad82659ed8babe92dc3243c7 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// 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)
    {
//        vertices = {
//            // positions        // colors           // texture coords
//            1.f,  1.f, 0.0f,    1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
//            1.f, -1.f, 0.0f,    0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
//            -1.f, -1.f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
//            -1.f,  1.f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left
//        };
//
//        indices = {
//            0, 1, 3, // first triangle
//            1, 2, 3  // second triangle
//        };

        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);
        glGenBuffers(1, &EBO);

        glBindVertexArray(VAO);

        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

        // position attribute
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
        // color attribute
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
        glEnableVertexAttribArray(1);
        // texture coord attribute
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
        glEnableVertexAttribArray(2);


        this->width = width;
        this->height = height;

        canvas = (unsigned char*) malloc(width * height * 3);
        glGenTextures(1, &texture);
    }

    void SetPixel(int x, int y, Color color)
    {
        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;

        UpdateTexture();
    }

    void UpdateTexture()
    {
        // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
        glBindTexture(GL_TEXTURE_2D, texture);

        // 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);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, width, 0, GL_RGB, GL_UNSIGNED_BYTE, canvas);
        glGenerateMipmap(GL_TEXTURE_2D);
    }

    void DrawTexture()
    {
        glBindTexture(GL_TEXTURE_2D, texture);
        // render container
//        ourShader.use();
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    }

    unsigned int* GetTexture()
    {
        return &(this->texture);
    }

private:
    int width, height;
    unsigned char* canvas;
    unsigned int texture;

    unsigned int VBO, VAO, EBO;

    float* vertices;
    unsigned int* indices;
};

#endif //OPENGLTEST_CANVAS_H