parent
8281e26cb8
commit
9f9461690c
@ -0,0 +1,253 @@
|
||||
use std::ops::{Add, Div, Mul, Neg, Sub};
|
||||
|
||||
pub trait Dot<Rhs = Self> {
|
||||
/// The resulting type after applying the `*` operator.
|
||||
type Output;
|
||||
|
||||
/// Performs the `*` operation.
|
||||
#[must_use]
|
||||
fn dot(self, rhs: Rhs) -> Self::Output;
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct Vector2f { pub x: f32, pub y: f32 }
|
||||
|
||||
impl Add for Vector2f {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x + other.x,
|
||||
y: self.y + other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul for Vector2f {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x * other.x,
|
||||
y: self.y * other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f32> for Vector2f {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, rhs: f32) -> Self::Output {
|
||||
Vector2f {
|
||||
x: self.x * rhs,
|
||||
y: self.y * rhs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dot for Vector2f {
|
||||
type Output = f32;
|
||||
|
||||
fn dot(self, other: Self) -> f32 {
|
||||
self.x * other.x + self.y * other.y
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Vector2f {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x - other.x,
|
||||
y: self.y - other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Div for Vector2f {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x / other.x,
|
||||
y: self.y / other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Vector2f {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Self {
|
||||
x: -self.x,
|
||||
y: -self.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct Vector2i { pub x: i32, pub y: i32 }
|
||||
|
||||
impl Add for Vector2i {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x + other.x,
|
||||
y: self.y + other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul for Vector2i {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x * other.x,
|
||||
y: self.y * other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<i32> for Vector2i {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, rhs: i32) -> Self::Output {
|
||||
Vector2i {
|
||||
x: self.x * rhs,
|
||||
y: self.y * rhs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dot for Vector2i {
|
||||
type Output = i32;
|
||||
|
||||
fn dot(self, other: Self) -> i32 {
|
||||
self.x * other.x + self.y * other.y
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Vector2i {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x - other.x,
|
||||
y: self.y - other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Div for Vector2i {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x / other.x,
|
||||
y: self.y / other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Vector2i {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Self {
|
||||
x: -self.x,
|
||||
y: -self.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct Vector3f { pub x: f32, pub y: f32, pub z: f32 }
|
||||
|
||||
impl Add for Vector3f {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x + other.x,
|
||||
y: self.y + other.y,
|
||||
z: self.z + other.z,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul for Vector3f {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x * other.x,
|
||||
y: self.y * other.y,
|
||||
z: self.z * other.z,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f32> for Vector3f {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, other: f32) -> Self {
|
||||
Self {
|
||||
x: self.x * other,
|
||||
y: self.y * other,
|
||||
z: self.z * other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dot for Vector3f {
|
||||
type Output = f32;
|
||||
|
||||
fn dot(self, other: Self) -> f32 {
|
||||
self.x * other.x + self.y * other.y + self.z * other.z
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Vector3f {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x - other.x,
|
||||
y: self.y - other.y,
|
||||
z: self.z - other.z,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Div for Vector3f {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x / other.x,
|
||||
y: self.y / other.y,
|
||||
z: self.z / other.z,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Vector3f {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Self {
|
||||
x: -self.x,
|
||||
y: -self.y,
|
||||
z: -self.z,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct Vector4f { pub x: f32, pub y: f32, pub z: f32, pub w: f32 }
|
||||
|
@ -1,71 +0,0 @@
|
||||
bool CLCaster::create_viewport(int width, int height, float v_fov, float h_fov) {
|
||||
|
||||
|
||||
|
||||
sf::Vector2i view_res(width, height);
|
||||
|
||||
This creates a buffer to give the kernal a [x,y] coord set
|
||||
if (!create_buffer("viewport_resolution", sizeof(int) * 2, &view_res))
|
||||
return false;
|
||||
|
||||
|
||||
// And an array of vectors describing the way the "lens" of our
|
||||
// camera works
|
||||
viewport_matrix = new sf::Vector4f[width * height * 4];
|
||||
|
||||
for (int y = -view_res.y / 2; y < view_res.y / 2; y++) {
|
||||
|
||||
for (int x = -view_res.x / 2; x < view_res.x / 2; x++) {
|
||||
|
||||
// The base ray direction to slew from
|
||||
sf::Vector3f ray(-800, x, y);
|
||||
|
||||
// correct for the base ray pointing to (1, 0, 0) as (0, 0). Should equal (1.57, 0)
|
||||
ray = sf::Vector3f(
|
||||
static_cast<float>(ray.z * sin(1.57) + ray.x * cos(1.57)),
|
||||
static_cast<float>(ray.y),
|
||||
static_cast<float>(ray.z * cos(1.57) - ray.x * sin(1.57))
|
||||
);
|
||||
|
||||
// ray.y += (rand() % 1000) / 100000.0;
|
||||
// ray.x += (rand() % 1000) / 100000.0;
|
||||
// ray.z += (rand() % 1000) / 100000.0;
|
||||
|
||||
ray = Normalize(ray);
|
||||
int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2);
|
||||
|
||||
viewport_matrix[index] = sf::Vector4f(
|
||||
ray.x,
|
||||
ray.y,
|
||||
ray.z,
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!create_buffer("viewport_matrix", sizeof(float) * 4 * view_res.x * view_res.y, viewport_matrix))
|
||||
return false;
|
||||
|
||||
// Create the image that opencl's rays write to
|
||||
viewport_image = new sf::Uint8[width * height * 4];
|
||||
|
||||
for (int i = 0; i < width * height * 4; i += 4) {
|
||||
|
||||
viewport_image[i] = 255; // R
|
||||
viewport_image[i + 1] = 255; // G
|
||||
viewport_image[i + 2] = 255; // B
|
||||
viewport_image[i + 3] = 100; // A
|
||||
}
|
||||
|
||||
// Interop lets us keep a reference to it as a texture
|
||||
viewport_texture.create(width, height);
|
||||
viewport_texture.update(viewport_image);
|
||||
viewport_sprite.setTexture(viewport_texture);
|
||||
|
||||
// Pass the buffer to opencl
|
||||
if (!create_image_buffer("image", sizeof(sf::Uint8) * width * height * 4, &viewport_texture, CL_MEM_WRITE_ONLY))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
Loading…
Reference in new issue