summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2019-05-05 22:53:12 +0400
committerAndrew <saintruler@gmail.com>2019-05-05 22:53:12 +0400
commit3a94328b3cff255ac64a208ac98919cc3e530aa0 (patch)
tree377ab395a13e95b5c12a442645c2eaa4b2a644f8 /main.cpp
parent15ccb946f43283bfc91d80ddf012d9ae2a0333ed (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 'main.cpp')
-rw-r--r--main.cpp181
1 files changed, 14 insertions, 167 deletions
diff --git a/main.cpp b/main.cpp
index f281df8..cbe6b80 100644
--- a/main.cpp
+++ b/main.cpp
@@ -5,9 +5,8 @@
#include <stb_image.h>
#include <shader.h>
-#include "Canvas/Canvas.h"
-#include "point.h"
-#include "Canvas/cuda/CalculateCanvasGPU.h"
+#include <Canvas.h>
+#include <Point.h>
#include <iostream>
#include <cmath>
@@ -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<point> 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