Skip to content

Commit

Permalink
changed formatting and replaced macros with constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
xkevio committed Feb 24, 2021
1 parent a0fa537 commit 9c7e043
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 150 deletions.
37 changes: 19 additions & 18 deletions cpu_version/Light.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#include "Vec3f.h"

class Light {
private:
float diffuse;
float specular;
float ambient;
private:
float diffuse;
float specular;
float ambient;

Vec3f position;
Vec3f color;
public:
Light(Vec3f position, Vec3f color) : position(position), color(color) {}
Vec3f get_position() const { return position; }
Vec3f get_color() const { return color; }
Vec3f position;
Vec3f color;

void set_light(float amb, float diff, float spec) {
diffuse = diff;
specular = spec;
ambient = amb;
}
public:
Light(Vec3f position, Vec3f color) : position(position), color(color) {}
Vec3f get_position() const { return position; }
Vec3f get_color() const { return color; }

float get_diffuse() const { return diffuse; }
float get_specular() const { return specular; }
float get_ambient() const { return ambient; }
void set_light(float amb, float diff, float spec) {
ambient = amb;
diffuse = diff;
specular = spec;
}

float get_diffuse() const { return diffuse; }
float get_specular() const { return specular; }
float get_ambient() const { return ambient; }
};
53 changes: 18 additions & 35 deletions cpu_version/Vec3f.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Vec3f.h"

#include <cmath>
#include <iostream>

Expand All @@ -8,65 +9,47 @@ Vec3f::Vec3f(float x_, float y_, float z_) {
z = z_;
}

Vec3f::Vec3f(const Vec3f &a) {
Vec3f::Vec3f(const Vec3f& a) {
x = a.x;
y = a.y;
z = a.z;
}

Vec3f::Vec3f() {
x = 0; y = 0; z = 0;
x = 0;
y = 0;
z = 0;
}

Vec3f Vec3f::operator+(const Vec3f &a) const {
return Vec3f(x + a.x, y + a.y, z + a.z);
}
Vec3f Vec3f::operator+(const Vec3f& a) const { return Vec3f(x + a.x, y + a.y, z + a.z); }

Vec3f Vec3f::operator-(const Vec3f &a) const {
return Vec3f(x - a.x, y - a.y, z - a.z);
}
Vec3f Vec3f::operator-(const Vec3f& a) const { return Vec3f(x - a.x, y - a.y, z - a.z); }

Vec3f Vec3f::operator/(const float &a) const {
return scale(1 / a);
}
Vec3f Vec3f::operator/(const float& a) const { return scale(1 / a); }

Vec3f operator*(const float &a, const Vec3f &b) {
return Vec3f(a * b.x_(), a * b.y_(), a * b.z_());
}
Vec3f operator*(const float& a, const Vec3f& b) { return Vec3f(a * b.x_(), a * b.y_(), a * b.z_()); }

Vec3f Vec3f::operator^(const Vec3f &a) const {
return Vec3f(y*a.z - z*a.y, z*a.x - x*a.z, x*a.y - y*a.x);
}
Vec3f Vec3f::operator^(const Vec3f& a) const { return Vec3f(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x); }

Vec3f Vec3f::operator&(const Vec3f &a) const {
return Vec3f(x * a.x, y * a.y, z * a.z);
}
Vec3f Vec3f::operator&(const Vec3f& a) const { return Vec3f(x * a.x, y * a.y, z * a.z); }

Vec3f Vec3f::scale(float factor) const {
return Vec3f(factor * x, factor * y, factor * z);
}
Vec3f Vec3f::scale(float factor) const { return Vec3f(factor * x, factor * y, factor * z); }

Vec3f Vec3f::normalize() const {
return scale(1 / length());
}
Vec3f Vec3f::normalize() const { return scale(1 / length()); }

Vec3f Vec3f::cap(float max) const {
float nx = x;
float ny = y;
float nz = z;
if(x > max) nx = max;
if(y > max) ny = max;
if(z > max) nz = max;
if (x > max) nx = max;
if (y > max) ny = max;
if (z > max) nz = max;
return Vec3f(nx, ny, nz);
}

float Vec3f::length() const {
return sqrt(pow(x, 2) + pow(y,2) + pow(z, 2));
}
float Vec3f::length() const { return sqrt(x*x + y*y + z*z); }

void Vec3f::display() {
std::cout << "(" << x << " " << y << " " << z << ")^T" << std::endl;
}
void Vec3f::display() { std::cout << "(" << x << " " << y << " " << z << ")^T" << std::endl; }

float Vec3f::x_() const { return x; }
float Vec3f::y_() const { return y; }
Expand Down
46 changes: 22 additions & 24 deletions cpu_version/Vec3f.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
#ifndef __VEC3F_H__
#define __VEC3F_H__
class Vec3f {
public:
Vec3f(float x_, float y_, float z_);
Vec3f(const Vec3f& a);
Vec3f();
Vec3f operator+(const Vec3f& a) const;
Vec3f operator-(const Vec3f& a) const;
Vec3f operator/(const float& a) const;
Vec3f operator^(const Vec3f& a) const; // cross product
Vec3f operator&(const Vec3f& a) const; // element wise multiplication
friend Vec3f operator*(const float& a, const Vec3f& b);

public:
Vec3f(float x_, float y_, float z_);
Vec3f(const Vec3f &a);
Vec3f();
Vec3f operator+(const Vec3f &a) const;
Vec3f operator-(const Vec3f &a) const;
Vec3f operator/(const float &a) const;
Vec3f operator^(const Vec3f &a) const; //cross product
Vec3f operator&(const Vec3f &a) const; //element wise multiplication
friend Vec3f operator*(const float &a, const Vec3f &b);
float length() const;
Vec3f normalize() const;
Vec3f scale(float factor) const;
Vec3f cap(float max) const;

float length() const;
Vec3f normalize() const;
Vec3f scale(float factor) const;
Vec3f cap(float max) const;

void display();
float x_() const;
float y_() const;
float z_() const;

private:
float x;
float y;
float z;
void display();
float x_() const;
float y_() const;
float z_() const;

private:
float x;
float y;
float z;
};
#endif
Loading

0 comments on commit 9c7e043

Please sign in to comment.