diff --git a/include/display.cuh b/include/display.cuh index d8bd885..9a42ee1 100644 --- a/include/display.cuh +++ b/include/display.cuh @@ -4,7 +4,6 @@ #include #include "../include/display.hpp" - namespace graphics { __global__ void transformVerticesKernel(Vector3D* vertices, Vector2D* projectedVertices, int size, Vector3D rotation, Vector3D camera); } // namespace graphics \ No newline at end of file diff --git a/include/display.hpp b/include/display.hpp index ad49bd5..8d4d316 100644 --- a/include/display.hpp +++ b/include/display.hpp @@ -66,6 +66,6 @@ class Display { virtual void LaunchCuda(); // Method to free Metal - virtual void FreeMetal(); + // virtual void FreeMetal(); }; } // namespace graphics diff --git a/include/vector3d.cuh b/include/vector3d.cuh index e69de29..df0def0 100644 --- a/include/vector3d.cuh +++ b/include/vector3d.cuh @@ -0,0 +1,13 @@ +#pragma once + +#include +#include +#include "../include/vector3d.hpp" +#include "../include/vector2d.hpp" +#include "../include/constants.hpp" + +namespace graphics { +__device__ __host__ void cudaRotate(Vector3D* vertex, double roll, double pitch, double yaw); +__device__ __host__ void cudaTranslate(Vector3D* vertex, double x, double y, double z); +__device__ __host__ void cudaProject(const Vector3D* vertex, Vector2D* projectedVertex); +} // namespace graphics diff --git a/src/display.cpp b/src/display.cpp index 9f98bb7..f76d1cf 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -7,7 +7,6 @@ #include "../include/display.metal" #endif - #include #include "../include/constants.hpp" diff --git a/src/display.cu b/src/display.cu index a8cab76..ac131bb 100644 --- a/src/display.cu +++ b/src/display.cu @@ -1,4 +1,5 @@ #include "../include/display.cuh" +#include "../include/vector3d.cuh" #include "../include/constants.hpp" #include @@ -6,31 +7,19 @@ #include "../include/display.hpp" namespace graphics { -__host__ __device__ void cudaRotate(Vector3D* vertex, Vector2D* projectedVertices, double roll, double pitch, double yaw, int idx) { - vertex->rotate(roll, pitch, yaw); -} -__host__ __device__ void cudaTranslate(Vector3D* vertex, Vector2D* projectedVertices, double x, double y, double z, int idx) { - vertex->x += x; - vertex->y += y; - vertex->z += z; -} -__host__ __device__ void cudaProject(Vector3D* vertex, Vector2D* projectedVertices, int idx) { - projectedVertices[idx].x = (vertex->x * FOV) / vertex->z; - projectedVertices[idx].y = (vertex->y * FOV) / vertex->z; -} __global__ void transformVerticesKernel(Vector3D* vertices, Vector2D* projectedVertices, int size, Vector3D rotation, Vector3D camera) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { Vector3D vertex = vertices[idx]; // Rotate the vertex - cudaRotate(&vertex, projectedVertices, rotation.x, rotation.y, rotation.z, idx); + cudaRotate(&vertex, rotation.x, rotation.y, rotation.z); // Translate the vertex - cudaTranslate(&vertex, projectedVertices, camera.x, camera.y, -camera.z, idx); + cudaTranslate(&vertex, camera.x, camera.y, -camera.z); // Project the transformed vertex - cudaProject(&vertex, projectedVertices, idx); + cudaProject(&vertex, &projectedVertices[idx]); } } void Display::InitalizeCuda() { diff --git a/src/vector3d.cu b/src/vector3d.cu index e69de29..fa2ee61 100644 --- a/src/vector3d.cu +++ b/src/vector3d.cu @@ -0,0 +1,43 @@ +#include "../include/vector3d.cuh" +#include + +namespace graphics { + +__device__ __host__ void cudaRotate(Vector3D* vertex, double roll, double pitch, double yaw) { + // Roll (rotation around X-axis) + double cosR = cos(roll); + double sinR = sin(roll); + double y = vertex->y; + double z = vertex->z; + vertex->y = y * cosR - z * sinR; + vertex->z = z * cosR + y * sinR; + + // Pitch (rotation around Y-axis) + double cosP = cos(pitch); + double sinP = sin(pitch); + double x = vertex->x; + z = vertex->z; + vertex->x = x * cosP + z * sinP; + vertex->z = z * cosP - x * sinP; + + // Yaw (rotation around Z-axis) + double cosY = cos(yaw); + double sinY = sin(yaw); + x = vertex->x; + y = vertex->y; + vertex->x = x * cosY - y * sinY; + vertex->y = y * cosY + x * sinY; +} + +__device__ __host__ void cudaTranslate(Vector3D* vertex, double x, double y, double z) { + vertex->x += x; + vertex->y += y; + vertex->z += z; +} + +__device__ __host__ void cudaProject(const Vector3D* vertex, Vector2D* projectedVertex) { + projectedVertex->x = (vertex->x * FOV) / vertex->z; + projectedVertex->y = (vertex->y * FOV) / vertex->z; +} + +} // namespace graphics