mirror of
https://github.com/MartinOpat/cuda-based-raytrace.git
synced 2025-06-07 02:13:10 +02:00
Refactored functionality into separate files
This commit is contained in:
@@ -1,43 +1,16 @@
|
||||
#include <cuda_runtime.h>
|
||||
#include <device_launch_parameters.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cmath>
|
||||
|
||||
#include "linalg/linalg.h"
|
||||
#include "sphere.h"
|
||||
#include "img/handler.h"
|
||||
|
||||
#define WIDTH 3840
|
||||
#define HEIGHT 2160
|
||||
#define SAMPLES_PER_PIXEL 8
|
||||
|
||||
struct Vec3 {
|
||||
double x, y, z;
|
||||
|
||||
__host__ __device__ Vec3() : x(0), y(0), z(0) {}
|
||||
__host__ __device__ Vec3(double x, double y, double z) : x(x), y(y), z(z) {}
|
||||
|
||||
__host__ __device__ Vec3 operator+(const Vec3& b) const { return Vec3(x + b.x, y + b.y, z + b.z); }
|
||||
__host__ __device__ Vec3 operator-(const Vec3& b) const { return Vec3(x - b.x, y - b.y, z - b.z); }
|
||||
__host__ __device__ Vec3 operator*(double b) const { return Vec3(x * b, y * b, z * b); }
|
||||
__host__ __device__ Vec3 operator-() const { return Vec3(-x, -y, -z); }
|
||||
__host__ __device__ double dot(const Vec3& b) const { return x * b.x + y * b.y + z * b.z; }
|
||||
__host__ __device__ Vec3 normalize() const { double len = sqrt(x * x + y * y + z * z); return Vec3(x / len, y / len, z / len); }
|
||||
};
|
||||
|
||||
struct Sphere {
|
||||
Vec3 center;
|
||||
double radius;
|
||||
Vec3 color;
|
||||
|
||||
__device__ bool intersect(const Vec3& rayOrigin, const Vec3& rayDir, double& t) const {
|
||||
Vec3 oc = rayOrigin - center;
|
||||
double b = oc.dot(rayDir);
|
||||
double c = oc.dot(oc) - radius * radius;
|
||||
double h = b * b - c;
|
||||
if (h < 0.0) return false;
|
||||
h = sqrt(h);
|
||||
t = -b - h;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
__device__ Vec3 phongShading(const Vec3& point, const Vec3& normal, const Vec3& lightDir, const Vec3& viewDir, const Vec3& color) {
|
||||
double ambientStrength = 0.1;
|
||||
@@ -93,14 +66,7 @@ __global__ void renderKernel(unsigned char* framebuffer, Sphere* spheres, int nu
|
||||
framebuffer[pixelIndex + 2] = static_cast<unsigned char>(fmin(color.z, 1.0) * 255);
|
||||
}
|
||||
|
||||
void saveImage(const char* filename, unsigned char* framebuffer) {
|
||||
std::ofstream imageFile(filename, std::ios::out | std::ios::binary);
|
||||
imageFile << "P6\n" << WIDTH << " " << HEIGHT << "\n255\n";
|
||||
for (int i = 0; i < WIDTH * HEIGHT * 3; i++) {
|
||||
imageFile << framebuffer[i];
|
||||
}
|
||||
imageFile.close();
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
Sphere spheres[] = {
|
||||
@@ -125,7 +91,7 @@ int main() {
|
||||
cudaDeviceSynchronize();
|
||||
|
||||
cudaMemcpy(h_framebuffer, d_framebuffer, WIDTH * HEIGHT * 3, cudaMemcpyDeviceToHost);
|
||||
saveImage("output.ppm", h_framebuffer);
|
||||
saveImage("output.ppm", h_framebuffer, WIDTH, HEIGHT);
|
||||
|
||||
cudaFree(d_framebuffer);
|
||||
cudaFree(d_spheres);
|
||||
|
||||
Reference in New Issue
Block a user