Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Oct 18, 2024
1 parent 1724b9a commit 8299279
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 18 deletions.
1 change: 0 additions & 1 deletion include/display.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <device_launch_parameters.h>
#include "../include/display.hpp"


namespace graphics {
__global__ void transformVerticesKernel(Vector3D* vertices, Vector2D* projectedVertices, int size, Vector3D rotation, Vector3D camera);
} // namespace graphics
2 changes: 1 addition & 1 deletion include/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ class Display {
virtual void LaunchCuda();

// Method to free Metal
virtual void FreeMetal();
// virtual void FreeMetal();
};
} // namespace graphics
13 changes: 13 additions & 0 deletions include/vector3d.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#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
1 change: 0 additions & 1 deletion src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "../include/display.metal"
#endif


#include <SDL2/SDL.h>

#include "../include/constants.hpp"
Expand Down
19 changes: 4 additions & 15 deletions src/display.cu
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
#include "../include/display.cuh"
#include "../include/vector3d.cuh"
#include "../include/constants.hpp"

#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#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() {
Expand Down
43 changes: 43 additions & 0 deletions src/vector3d.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "../include/vector3d.cuh"
#include <math.h>

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

0 comments on commit 8299279

Please sign in to comment.