Skip to content

Commit

Permalink
weird error
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Oct 12, 2024
1 parent 7c85abf commit 92a74ec
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 92 deletions.
98 changes: 10 additions & 88 deletions include/vector3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../include/vector2d.hpp"

using std::sqrt;

namespace graphics {
// Represents a three-dimensional vector
class Vector3D {
Expand All @@ -24,95 +25,16 @@ class Vector3D {
// The z-coordinate of the vector
double z;

// Overload the subtraction operator
Vector3D operator-(const Vector3D& vec) const {
return Vector3D(this->x - vec.x, this->y - vec.y, this->z - vec.z);
}

// Overload the addition operator
Vector3D operator+=(const Vector3D& vec) {
this->x += vec.x;
this->y += vec.y;
this->z += vec.z;
return *this;
}

// Overload the multiplication operator (vector * scalar)
Vector3D operator*=(double t) {
this->x *= t;
this->y *= t;
this->z *= t;
return *this;
}

// Overload the division operator (vector / scalar)
Vector3D operator/=(double t) {
this->x /= t;
this->y /= t;
this->z /= t;
return *this;
}
// Project the vector onto a 2D plane
Vector2D project() const;

// Get the magnitude of the vector
double magnitude() const {
return this->x * this->x + this->y * this->y + this->z * this->z;
}
// Rotate the vector around the x-axis
void rotateX(double theta);

// Get the length of the vector
double length() const { return sqrt(magnitude()); }
// Rotate the vector around the y-axis
void rotateY(double theta);

Vector2D project() const { return Vector2D(x * 128 / z, y * 128 / z); }
// Rotate the vector around the z-axis
void rotateZ(double theta);
};

// Type alias for Point3D
using Point3D = Vector3D;

// Overload the standard output stream insertion operator
inline std::ostream& operator<<(std::ostream& out, const Vector3D& vec) {
return out << vec.x << ' ' << vec.y << ' ' << vec.z;
}

// Overload the addition operator
inline Vector3D operator+(const Vector3D& u, const Vector3D& v) {
return Vector3D(u.x + v.x, u.y + v.y, u.z + v.z);
}

// Overload the subtraction operator
inline Vector3D operator-(const Vector3D& u, const Vector3D& v) {
return Vector3D(u.x - v.x, u.y - v.y, u.z - v.z);
}

// Overload the multiplication operator (vector * vector)
inline Vector3D operator*(const Vector3D& u, const Vector3D& v) {
return Vector3D(u.x * v.x, u.y * v.y, u.z * v.z);
}

// Overload the multiplication operator (vector * scalar)
inline Vector3D operator*(double t, const Vector3D& vec) {
return Vector3D(t * vec.x, t * vec.y, t * vec.z);
}

// Overload the multiplication operator (vector * scalar)
inline Vector3D operator*(const Vector3D& vec, double t) {
return Vector3D(t * vec.x, t * vec.y, t * vec.z);
}

// Overload the division operator (vector / scalar)
inline Vector3D operator/(Vector3D vec, double t) {
return Vector3D(vec.x / t, vec.y / t, vec.z / t);
}

// Get the dot product of two vectors
inline double dot(const Vector3D& u, const Vector3D& v) {
return u.x * v.x + u.y * v.y + u.z * v.z;
}

// Get the cross product of two vectors
inline Vector3D cross(const Vector3D& u, const Vector3D& v) {
return Vector3D(
u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x);
}

// Get the unit vector of a vector
inline Vector3D unit_vector(Vector3D vec) { return vec / vec.length(); }
} // namespace graphics
} // namespace graphics
17 changes: 13 additions & 4 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Display::Display() {
frameBuffer = std::make_unique<FrameBuffer>(displayMode.w, displayMode.h);

// Initialize the vertices
// Start loading my array of vectors
// From -1 to 1 (in this 9x9x9 cube)
for (float x = -1; x <= 1; x += 0.25) {
for (float y = -1; y <= 1; y += 0.25) {
for (float z = -1; z <= 1; z += 0.25) {
Expand Down Expand Up @@ -83,7 +85,14 @@ Display::~Display() {
SDL_Quit();
}

void Display::update() {}
void Display::update() {
// Rotate the vertices
for (auto &vertex : vertices) {
vertex.rotateX(0.01);
vertex.rotateY(0.01);
vertex.rotateZ(0.01);
}
}

void Display::render() {
// Clear the renderer
Expand Down Expand Up @@ -111,9 +120,6 @@ void Display::render() {
// Copy the texture to the renderer
SDL_RenderCopy(renderer, texture, nullptr, nullptr);

// Clear the frame buffer
frameBuffer->clear(Color(0, 0, 0, 0));

// Present the renderer
SDL_RenderPresent(renderer);
}
Expand All @@ -130,6 +136,9 @@ void Display::clear() {
// Clear the renderer
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

// Clear the frame buffer
frameBuffer->clear(Color(0, 0, 0, 0));
}

bool Display::shouldClose() const { return SDL_QuitRequested(); }
Expand Down
29 changes: 29 additions & 0 deletions src/vector3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "../include/vector3d.hpp"

#include <cmath>
namespace graphics {
Vector2D Vector3D::project() const {
return Vector2D(x * 128 / z, y * 128 / z);
}

void Vector3D::rotateX(double theta) {
double y_new = y * cos(theta) - z * sin(theta);
double z_new = y * sin(theta) + z * cos(theta);
y = y_new;
z = z_new;
}

void Vector3D::rotateY(double theta) {
double x_new = x * cos(theta) - z * sin(theta);
double z_new = x * sin(theta) + z * cos(theta);
x = x_new;
z = z_new;
}

void Vector3D::rotateZ(double theta) {
double x_new = x * cos(theta) - y * sin(theta);
double y_new = x * sin(theta) + y * cos(theta);
x = x_new;
y = y_new;
}
} // namespace graphics

0 comments on commit 92a74ec

Please sign in to comment.