From 3a94328b3cff255ac64a208ac98919cc3e530aa0 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 5 May 2019 22:53:12 +0400 Subject: 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 --- .gitignore | 1 + CMakeLists.txt | 53 +++++++++-- Canvas/Canvas.cpp | 98 +++++++++++++++++++++ Canvas/Canvas.h | 97 +++----------------- Canvas/cpu/CalculateCanvas.cpp | 60 +++++++++++++ Canvas/cpu/CalculateCanvas.h | 15 ++++ Canvas/cuda/CalculateCanvasGPU.cu | 52 ----------- Canvas/cuda/CalculateCanvasGPU.h | 20 ----- Canvas/gpu/CalculateCanvas.cu | 52 +++++++++++ Canvas/gpu/CalculateCanvas.h | 16 ++++ libraries/point/Point.cpp | 75 ++++++++++++++++ libraries/point/Point.h | 30 +++++++ main.cpp | 181 +++----------------------------------- point.h | 87 ------------------ 14 files changed, 417 insertions(+), 420 deletions(-) create mode 100644 Canvas/Canvas.cpp create mode 100644 Canvas/cpu/CalculateCanvas.cpp create mode 100644 Canvas/cpu/CalculateCanvas.h delete mode 100644 Canvas/cuda/CalculateCanvasGPU.cu delete mode 100644 Canvas/cuda/CalculateCanvasGPU.h create mode 100644 Canvas/gpu/CalculateCanvas.cu create mode 100644 Canvas/gpu/CalculateCanvas.h create mode 100644 libraries/point/Point.cpp create mode 100644 libraries/point/Point.h delete mode 100644 point.h diff --git a/.gitignore b/.gitignore index 1b72d0a..8af9fb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ cmake-build-debug +CMakeFiles .idea \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index a4ab4ee..cd9516e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libraries") find_package(OpenGL REQUIRED) find_package(CUDA) -add_executable(${PROJECT_NAME} main.cpp point.h Canvas/Canvas.h) +add_executable(${PROJECT_NAME} main.cpp) # GLFW find_package(PkgConfig REQUIRED) @@ -16,6 +16,7 @@ pkg_search_module(GLFW REQUIRED glfw3) include_directories(${GLFW_INCLUDE_DIRS}) target_link_libraries(${PROJECT_NAME} ${GLFW_STATIC_LIBRARIES}) + # GLAD set(GLAD_DIR "${LIB_DIR}/glad") set(GLAD_NAME "glad") @@ -24,16 +25,52 @@ target_include_directories(${GLAD_NAME} PRIVATE "${GLAD_DIR}/include") target_include_directories(${PROJECT_NAME} PRIVATE "${GLAD_DIR}/include") target_link_libraries(${PROJECT_NAME} ${GLAD_NAME} "${CMAKE_DL_LIBS}") + # stb_image target_include_directories(${PROJECT_NAME} PRIVATE "${LIB_DIR}/stb_image") + # shader target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/learnopengl") -# CUDA -set(CUDA_LIB "cudalib") -#target_include_directories(${PROJECT_NAME} PRIVATE "Canvas/") -cuda_add_library(${CUDA_LIB} STATIC Canvas/cuda/CalculateCanvasGPU.cu Canvas/cuda/CalculateCanvasGPU.h OPTIONS -std=c++11) -target_compile_features(${CUDA_LIB} PUBLIC cxx_std_11) -set_target_properties(${CUDA_LIB} PROPERTIES CUDA_SEPARABLE_COMPILATION ON) -target_link_libraries(${PROJECT_NAME} ${CUDA_LIB}) + +# Point +set(POINT_LIB "point_lib") +set(POINT_DIR "${LIB_DIR}/point") +add_library(${POINT_LIB} "${POINT_DIR}/Point.cpp") +target_link_libraries(${PROJECT_NAME} ${POINT_LIB}) +target_include_directories(${PROJECT_NAME} PRIVATE ${POINT_DIR}) + + +# Canvas +set(CANVAS_LIB "canvas_lib") +add_library(${CANVAS_LIB} Canvas/Canvas.cpp Canvas/Canvas.h) + +target_include_directories(${CANVAS_LIB} PRIVATE ${POINT_DIR}) +target_include_directories(${CANVAS_LIB} PRIVATE "${GLAD_DIR}/include") +target_include_directories(${CANVAS_LIB} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/learnopengl") + +target_link_libraries(${PROJECT_NAME} ${CANVAS_LIB}) +target_include_directories(${PROJECT_NAME} PRIVATE "Canvas/") + + +if(${CUDA_FOUND}) + set(GENERATE_CANVAS_LIB "generate_canvas_lib_cuda") + + cuda_add_library(${GENERATE_CANVAS_LIB} STATIC Canvas/gpu/CalculateCanvas.cu Canvas/gpu/CalculateCanvas.h) + target_compile_features(${GENERATE_CANVAS_LIB} PUBLIC cxx_std_11) + set_target_properties(${GENERATE_CANVAS_LIB} PROPERTIES CUDA_SEPARABLE_COMPILATION ON) + + target_include_directories(${CANVAS_LIB} PRIVATE "Canvas/gpu") +else() + set(GENERATE_CANVAS_LIB "generate_canvas_lib") + + add_library(${GENERATE_CANVAS_LIB} Canvas/cpu/CalculateCanvas.cpp Canvas/cpu/CalculateCanvas.h) + target_compile_features(${GENERATE_CANVAS_LIB} PUBLIC cxx_std_11) + + target_include_directories(${CANVAS_LIB} PRIVATE "Canvas/cpu") +endif() + +target_include_directories(${GENERATE_CANVAS_LIB} PRIVATE ${POINT_DIR}) + +target_link_libraries(${CANVAS_LIB} ${GENERATE_CANVAS_LIB}) 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; +} diff --git a/Canvas/Canvas.h b/Canvas/Canvas.h index 84dbe56..ee6c844 100644 --- a/Canvas/Canvas.h +++ b/Canvas/Canvas.h @@ -7,6 +7,9 @@ #include #include +#include +#include +#include "gpu/CalculateCanvas.h" #include @@ -18,100 +21,22 @@ struct Color 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); - } + Canvas(int width, int height, Point* points, int nPoints); - unsigned int* GetTexture() - { - return &(this->texture); - } + void SetPixel(int x, int y, Color color); + void UpdateTexture(); + void DrawTexture(); + void DeleteCanvas(); private: - int width, height; + int width, height, nPoints; unsigned char* canvas; unsigned int texture; + Point* points; unsigned int VBO, VAO, EBO; - float* vertices; - unsigned int* indices; + Shader shader = Shader("4.1.texture.vs", "4.1.texture.fs"); }; #endif //OPENGLTEST_CANVAS_H diff --git a/Canvas/cpu/CalculateCanvas.cpp b/Canvas/cpu/CalculateCanvas.cpp new file mode 100644 index 0000000..75a9996 --- /dev/null +++ b/Canvas/cpu/CalculateCanvas.cpp @@ -0,0 +1,60 @@ +// +// Created by saintruler on 05.05.19. +// +#include "CalculateCanvas.h" + +void calculate(unsigned char* canvas, int width, Point* points, int nPoints, int canvasX, int canvasY) +{ + int pathX, pathY; + int length_squared; + double f = 0; + + for (int i = 0; i < nPoints; i++) + { + pathX = points[i].x - canvasX; + pathY = points[i].y - canvasY; + + length_squared = pathX * pathX + pathY * pathY; + f += (1e5 * 20) / (float) length_squared; + } + + int l = (int) f; + if (l > 255) l = 255; + + canvas[canvasY * width * 3 + canvasX * 3 + 0] = l; + canvas[canvasY * width * 3 + canvasX * 3 + 1] = 0; + canvas[canvasY * width * 3 + canvasX * 3 + 2] = 0; +} + +//void calculate(unsigned char* canvas, int width, Point* points, int nPoints, int canvasX, int canvasY) +// point canvas_pt(canvasX, canvasY); +// double f = 0; +// for (point pt : points) +// { +// point path = pt - canvas_pt; +// +// f += (1e5 * 20 * 1) / path.length_sqared(); +// } +// +// int l = (int) f; +// if (l > 255) l = 255; +// +// +// canvas[y * SCR_WIDTH * 3 + x * 3 + 0] = l; +// canvas[y * SCR_WIDTH * 3 + x * 3 + 1] = 0; +// canvas[y * SCR_WIDTH * 3 + x * 3 + 2] = 0; +//} + + + +void generate_canvas(int width, int height, unsigned char* canvas, Point* points, int nPoints) +{ + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + calculate(canvas, width, points, nPoints, x, y); + } + } +} + diff --git a/Canvas/cpu/CalculateCanvas.h b/Canvas/cpu/CalculateCanvas.h new file mode 100644 index 0000000..c1a1662 --- /dev/null +++ b/Canvas/cpu/CalculateCanvas.h @@ -0,0 +1,15 @@ +// +// Created by saintruler on 05.05.19. +// + +#ifndef OPENGLTEST_CALCULATECANVAS_H +#define OPENGLTEST_CALCULATECANVAS_H + +#include + + +void calculate(unsigned char* canvas, int width, Point* points, int nPoints, int canvasX, int canvasY); + +void generate_canvas(int width, int height, unsigned char* canvas, Point* points, int nPoints); + +#endif //OPENGLTEST_CALCULATECANVAS_H diff --git a/Canvas/cuda/CalculateCanvasGPU.cu b/Canvas/cuda/CalculateCanvasGPU.cu deleted file mode 100644 index c8c6fad..0000000 --- a/Canvas/cuda/CalculateCanvasGPU.cu +++ /dev/null @@ -1,52 +0,0 @@ -#include "CalculateCanvasGPU.h" - -__global__ -void calculate(unsigned char* canvas, int width, cuda_point* points, int nPoints) -{ - int canvasX = blockIdx.x * blockDim.x + threadIdx.x; - int canvasY = blockIdx.y * blockDim.y + threadIdx.y; - - int pathX, pathY; - int length_squared; - double f = 0; - - for (int i = 0; i < nPoints; i++) - { - pathX = points[i].x - canvasX; - pathY = points[i].y - canvasY; - - length_squared = pathX * pathX + pathY * pathY; - f += (1e5 * 20) / (float) length_squared; - } - - int l = (int) f; - if (l > 255) l = 255; - - canvas[canvasY * width * 3 + canvasX * 3 + 0] = l; - canvas[canvasY * width * 3 + canvasX * 3 + 1] = 0; - canvas[canvasY * width * 3 + canvasX * 3 + 2] = 0; -} - -void generate_canvas(int width, int height, unsigned char* canvas, cuda_point* points, int nPoints) -{ - const int canvasSize = width * height * 3 * sizeof(unsigned char); - const int pointsSize = nPoints * sizeof(cuda_point); - - unsigned char* gpuCanvas; - cuda_point* gpuPoints; - - cudaMalloc((void**) &gpuCanvas, canvasSize); - cudaMalloc((void**) &gpuPoints, pointsSize); - - cudaMemcpy(gpuCanvas, canvas, canvasSize, cudaMemcpyHostToDevice); - cudaMemcpy(gpuPoints, points, pointsSize, cudaMemcpyHostToDevice); - - dim3 threadsPerBlock(16, 16); - dim3 numBlocks(width / threadsPerBlock.x, height / threadsPerBlock.y); - calculate<<>>(gpuCanvas, width, gpuPoints, nPoints); - - cudaMemcpy(canvas, gpuCanvas, canvasSize, cudaMemcpyDeviceToHost); - - cudaFree(gpuCanvas); - cudaFree(gpuPoints); -} diff --git a/Canvas/cuda/CalculateCanvasGPU.h b/Canvas/cuda/CalculateCanvasGPU.h deleted file mode 100644 index 46f54c5..0000000 --- a/Canvas/cuda/CalculateCanvasGPU.h +++ /dev/null @@ -1,20 +0,0 @@ -// -// Created by saintruler on 05.05.19. -// - -#ifndef OPENGLTEST_CALCULATECANVASGPU_H -#define OPENGLTEST_CALCULATECANVASGPU_H - -#include -#include -#include - -typedef struct -{ - int x, y; -} cuda_point; - - -void generate_canvas(int width, int height, unsigned char* canvas, cuda_point* points, int nPoints); - -#endif //OPENGLTEST_CALCULATECANVASGPU_H diff --git a/Canvas/gpu/CalculateCanvas.cu b/Canvas/gpu/CalculateCanvas.cu new file mode 100644 index 0000000..3a73067 --- /dev/null +++ b/Canvas/gpu/CalculateCanvas.cu @@ -0,0 +1,52 @@ +#include "CalculateCanvas.h" + +__global__ +void calculate(unsigned char* canvas, int width, Point* points, int nPoints) +{ + int canvasX = blockIdx.x * blockDim.x + threadIdx.x; + int canvasY = blockIdx.y * blockDim.y + threadIdx.y; + + int pathX, pathY; + int length_squared; + double f = 0; + + for (int i = 0; i < nPoints; i++) + { + pathX = points[i].x - canvasX; + pathY = points[i].y - canvasY; + + length_squared = pathX * pathX + pathY * pathY; + f += (1e5 * 20) / (float) length_squared; + } + + int l = (int) f; + if (l > 255) l = 255; + + canvas[canvasY * width * 3 + canvasX * 3 + 0] = l; + canvas[canvasY * width * 3 + canvasX * 3 + 1] = 0; + canvas[canvasY * width * 3 + canvasX * 3 + 2] = 0; +} + +void generate_canvas(int width, int height, unsigned char* canvas, Point* points, int nPoints) +{ + const int canvasSize = width * height * 3 * sizeof(unsigned char); + const int pointsSize = nPoints * sizeof(Point); + + unsigned char* gpuCanvas; + Point* gpuPoints; + + cudaMalloc((void**) &gpuCanvas, canvasSize); + cudaMalloc((void**) &gpuPoints, pointsSize); + + cudaMemcpy(gpuCanvas, canvas, canvasSize, cudaMemcpyHostToDevice); + cudaMemcpy(gpuPoints, points, pointsSize, cudaMemcpyHostToDevice); + + dim3 threadsPerBlock(16, 16); + dim3 numBlocks(width / threadsPerBlock.x, height / threadsPerBlock.y); + calculate<<>>(gpuCanvas, width, gpuPoints, nPoints); + + cudaMemcpy(canvas, gpuCanvas, canvasSize, cudaMemcpyDeviceToHost); + + cudaFree(gpuCanvas); + cudaFree(gpuPoints); +} diff --git a/Canvas/gpu/CalculateCanvas.h b/Canvas/gpu/CalculateCanvas.h new file mode 100644 index 0000000..0de853f --- /dev/null +++ b/Canvas/gpu/CalculateCanvas.h @@ -0,0 +1,16 @@ +// +// Created by saintruler on 05.05.19. +// + +#ifndef OPENGLTEST_CALCULATECANVAS_H +#define OPENGLTEST_CALCULATECANVAS_H + +#include +#include +#include + +#include + +void generate_canvas(int width, int height, unsigned char* canvas, Point* points, int nPoints); + +#endif //OPENGLTEST_CALCULATECANVAS_H diff --git a/libraries/point/Point.cpp b/libraries/point/Point.cpp new file mode 100644 index 0000000..4ca4a8c --- /dev/null +++ b/libraries/point/Point.cpp @@ -0,0 +1,75 @@ +// +// Created by saintruler on 05.05.19. +// +#include "Point.h" + +Point::Point(double x, double y) +{ + this->x = x; + this->y = y; +} + +Point operator+(const Point& left, const Point& right) +{ + return Point(left.x + right.x, left.y + right.y); +} + +Point operator+=(Point& left, const Point& right) +{ + left.x += right.x; + left.y += right.y; + return left; +} + +Point operator-(const Point& left, const Point& right) +{ + return Point(left.x - right.x, left.y - right.y); +} + +Point operator-=(Point& left, const double right) +{ + left.x -= right; + left.y -= right; + return left; +} + +Point operator/(const Point& left, const double right) +{ + return Point(left.x / right, left.y / right); +} + +Point operator/=(Point& left, const double right) +{ + left.x /= right; + left.y /= right; + return left; +} + +Point operator*(const Point& left, const double right) +{ + return Point(left.x * right, left.y * right); +} + +Point operator*=(Point& left, const double right) +{ + left.x *= right; + left.y *= right; + return left; +} + +double Point::length() +{ + return sqrt(pow(x, 2) + pow(y, 2)); +} + +double Point::length_squared() +{ + return pow(x, 2) + pow(y, 2); +} + +void Point::normalize() +{ + const double l = length(); + this->x /= l; + this->y /= l; +} diff --git a/libraries/point/Point.h b/libraries/point/Point.h new file mode 100644 index 0000000..8163f8f --- /dev/null +++ b/libraries/point/Point.h @@ -0,0 +1,30 @@ +// +// Created by saintruler on 11.04.19. +// + +#ifndef OPENGLTEST_POINT_H +#define OPENGLTEST_POINT_H + +#include + +class Point +{ +public: + Point(double x, double y); + + friend Point operator+(const Point& left, const Point& right); + friend Point operator+=(Point& left, const Point& right); + friend Point operator-(const Point& left, const Point& right); + friend Point operator-=(Point& left, double right); + friend Point operator/(const Point& left, double right); + friend Point operator/=(Point& left, double right); + friend Point operator*(const Point& left, double right); + friend Point operator*=(Point& left, double right); + + double length(); + double length_squared(); + void normalize(); + double x, y; +}; + +#endif //OPENGLTEST_POINT_H diff --git a/main.cpp b/main.cpp index f281df8..cbe6b80 100644 --- a/main.cpp +++ b/main.cpp @@ -5,9 +5,8 @@ #include #include -#include "Canvas/Canvas.h" -#include "point.h" -#include "Canvas/cuda/CalculateCanvasGPU.h" +#include +#include #include #include @@ -16,19 +15,10 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); -void loadTexture(std::string path, unsigned int* texture, int* width, int* height); -void generateCanvasTexture(unsigned int *texture, unsigned char color); -void window_size_callback(GLFWwindow* window, int width, int height); - // settings unsigned int SCR_WIDTH = 768; unsigned int SCR_HEIGHT = 768; -const std::string PROJECT_PATH = "/home/saintruler/Documents/opengl_gravity/"; -unsigned char* canvas = (unsigned char*) malloc(SCR_HEIGHT * SCR_WIDTH * 3); - -std::vector points; -const double c = 1e5; -cuda_point* gpuPoints = (cuda_point*) malloc(3 * sizeof(cuda_point)); +Point* points = (Point*) malloc(3 * sizeof(Point)); int main() { @@ -59,62 +49,11 @@ int main() return -1; } - // build and compile our shader zprogram - // ------------------------------------ - Shader ourShader((PROJECT_PATH + "4.1.texture.vs").c_str(), (PROJECT_PATH + "4.1.texture.fs").c_str()); - - // set up vertex data (and buffer(s)) and configure vertex attributes - // ------------------------------------------------------------------ - - 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 - }; - unsigned int indices[] = { - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle - }; - unsigned int VBO, VAO, EBO; - 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); - - - // load and create a texture - // ------------------------- - unsigned int texture; - glGenTextures(1, &texture); - unsigned char i = 0; - - points.emplace_back(point(100, 100)); - points.emplace_back(point(500, 500)); - points.emplace_back(point(200, 375)); - - gpuPoints[0] = cuda_point{.x=100, .y=100}; - gpuPoints[1] = cuda_point{.x=500, .y=500}; - gpuPoints[2] = cuda_point{.x=200, .y=200}; + points[0] = Point(100, 100); + points[1] = Point(500, 500); + points[2] = Point(200, 200); + Canvas canvas = Canvas(SCR_WIDTH, SCR_HEIGHT, points, 3); // render loop // ----------- @@ -129,14 +68,7 @@ int main() glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - generateCanvasTexture(&texture, i++); - // bind Texture - glBindTexture(GL_TEXTURE_2D, texture); - - // render container - ourShader.use(); - glBindVertexArray(VAO); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + canvas.DrawTexture(); // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) // ------------------------------------------------------------------------------- @@ -144,11 +76,8 @@ int main() glfwPollEvents(); } - // optional: de-allocate all resources once they've outlived their purpose: - // ------------------------------------------------------------------------ - glDeleteVertexArrays(1, &VAO); - glDeleteBuffers(1, &VBO); - glDeleteBuffers(1, &EBO); + canvas.DeleteCanvas(); + delete[] points; // glfw: terminate, clearing all previously allocated GLFW resources. // ------------------------------------------------------------------ @@ -157,76 +86,6 @@ int main() } -void loadTexture(std::string path, unsigned int* texture, int* width, int* height) -{ - glGenTextures(1, texture); - glBindTexture(GL_TEXTURE_2D, *texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object - // set the texture wrapping parameters - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method) - 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); - // load image, create texture and generate mipmaps - int nrChannels; - unsigned char* data = stbi_load(path.c_str(), width, height, &nrChannels, 0); - - if (data) - { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, *width, *height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); - } - else - { - std::cout << "Failed to load texture" << std::endl; - } - stbi_image_free(data); -} - - -void generateCanvasTexture(unsigned int *texture, unsigned char color) -{ - glBindTexture(GL_TEXTURE_2D, *texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object - // set the texture wrapping parameters - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method) - 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); - // load image, create texture and generate mipmaps - - -// for (int x = 0; x < SCR_WIDTH; x++) -// { -// for (int y = 0; y < SCR_HEIGHT; y++) -// { -// -// point canvas_pt(x, y); -// double f = 0; -// for (point pt : points) -// { -// point path = pt - canvas_pt; -// -// f += (c * 20 * 1) / path.length_sqared(); -// } -// -// int l = (int) f; -// if (l > 255) l = 255; -// -// -// canvas[y * SCR_WIDTH * 3 + x * 3 + 0] = l; -// canvas[y * SCR_WIDTH * 3 + x * 3 + 1] = 0; -// canvas[y * SCR_WIDTH * 3 + x * 3 + 2] = 0; -// } -// } - - generate_canvas(SCR_WIDTH, SCR_HEIGHT, canvas, gpuPoints, 3); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char*) canvas); - glGenerateMipmap(GL_TEXTURE_2D); -} - - // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly // --------------------------------------------------------------------------------------------------------- void processInput(GLFWwindow *window) @@ -234,23 +93,11 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) - points[0].x -= 20; - if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) - points[0].x += 20; - - if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) - points[0].y -= 20; - if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) - points[0].y += 20; - - double xpos, ypos; + double xPos = 0, yPos = 0; //getting cursor position - glfwGetCursorPos(window, &xpos, &ypos); -// points[0].x = xpos; -// points[0].y = SCR_HEIGHT - ypos; - gpuPoints[0].x = xpos; - gpuPoints[0].y = SCR_HEIGHT - ypos; + glfwGetCursorPos(window, &xPos, &yPos); + points[0].x = (int) xPos; + points[0].y = SCR_HEIGHT - (int) yPos; } // glfw: whenever the window size changed (by OS or user resize) this callback function executes diff --git a/point.h b/point.h deleted file mode 100644 index 36cfda9..0000000 --- a/point.h +++ /dev/null @@ -1,87 +0,0 @@ -// -// Created by saintruler on 11.04.19. -// - -#ifndef OPENGLTEST_POINT_H -#define OPENGLTEST_POINT_H - -#include - -class point -{ -public: - point(double x, double y) - { - this->x = x; - this->y = y; - } - - friend point operator+(const point& left, const point& right) - { - return point(left.x + right.x, left.y + right.y); - } - - friend point operator+=(point& left, const point& right) - { - left.x += right.x; - left.y += right.y; - return left; - } - - friend point operator-(const point& left, const point& right) - { - return point(left.x - right.x, left.y - right.y); - } - - friend point operator-=(point& left, const double right) - { - left.x -= right; - left.y -= right; - return left; - } - - friend point operator/(const point& left, const double right) - { - return point(left.x / right, left.y / right); - } - - friend point operator/=(point& left, const double right) - { - left.x /= right; - left.y /= right; - return left; - } - - friend point operator*(const point& left, const double right) - { - return point(left.x * right, left.y * right); - } - - friend point operator*=(point& left, const double right) - { - left.x *= right; - left.y *= right; - return left; - } - - double length() - { - return sqrt(pow(x, 2) + pow(y, 2)); - } - - double length_sqared() - { - return pow(x, 2) + pow(y, 2); - } - - void normalize() - { - const double l = length(); - this->x /= l; - this->y /= l; - } - - double x, y; -}; - -#endif //OPENGLTEST_POINT_H -- cgit v1.2.3