diff options
| author | Andrew <saintruler@gmail.com> | 2019-05-05 22:53:12 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2019-05-05 22:53:12 +0400 |
| commit | 3a94328b3cff255ac64a208ac98919cc3e530aa0 (patch) | |
| tree | 377ab395a13e95b5c12a442645c2eaa4b2a644f8 /Canvas/Canvas.cpp | |
| parent | 15ccb946f43283bfc91d80ddf012d9ae2a0333ed (diff) | |
Made changes to CMakeLists.txt so CUDA version of program builds when CUDA is installed.
Replaced C-style approach to generating texture to OOP-style with Canvas.h
Diffstat (limited to 'Canvas/Canvas.cpp')
| -rw-r--r-- | Canvas/Canvas.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/Canvas/Canvas.cpp b/Canvas/Canvas.cpp new file mode 100644 index 0000000..6034633 --- /dev/null +++ b/Canvas/Canvas.cpp @@ -0,0 +1,98 @@ +// +// Created by saintruler on 05.05.19. +// +#include "Canvas.h" + +Canvas::Canvas(int width, int height, Point *points, int nPoints) +{ + float 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 + }; + + int 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; + this->nPoints = nPoints; + + this->points = points; + canvas = (unsigned char*) malloc(width * height * 3); + glGenTextures(1, &texture); +} + +void Canvas::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 Canvas::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); + + generate_canvas(width, height, canvas, points, nPoints); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, width, 0, GL_RGB, GL_UNSIGNED_BYTE, canvas); + glGenerateMipmap(GL_TEXTURE_2D); +} + +void Canvas::DrawTexture() +{ + UpdateTexture(); + + glBindTexture(GL_TEXTURE_2D, texture); + shader.use(); + + glBindVertexArray(VAO); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); +} + +void Canvas::DeleteCanvas() +{ + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &VBO); + glDeleteBuffers(1, &EBO); + + delete[] canvas; +} |