Created and tested vec3.

This commit is contained in:
Afonso Clerigo Mendes de Sousa 2025-06-12 12:19:36 +01:00
parent 5dfe527b27
commit b2c8aaaf09
6 changed files with 122 additions and 2 deletions

8
.gitignore vendored
View File

@ -100,3 +100,11 @@ CTestTestfile.cmake
_deps
CMakeUserPresets.json
# Build / Compiled program
build/Render
build/bin/*
Render
build/Render.exe
Render.exe

View File

@ -5,13 +5,17 @@ This roadmap breaks down the essential steps to build your CPU-based raytracer f
---
## Phase 1: Foundations & Core Engine
- [ ] Set up vector math utilities (`vec3`): add, subtract, dot, cross, normalize
- [x] Set up vector math utilities (`vec3`): add, subtract, dot, cross, normalize
- [ ] Implement Ray class — origin, direction, basic functions
- [ ] Basic Sphere object — position, radius, material properties
- [ ] Ray-Sphere intersection logic — the core of your renderer
- [ ] Camera setup — generate rays through the viewport
- [ ] Scene structure — manage objects and lights
# Objectives:
- [ ] Render a single sphere with basic color
- [ ] Output to a simple PPM file format
---
## Phase 2: Lighting & Shading Basics

Binary file not shown.

View File

@ -0,0 +1,49 @@
#include "utils_vec.hpp"
vec3 vec3::operator+(const vec3 &other) const
{
return vec3(x + other.x, y + other.y, z + other.z);
}
vec3 vec3::operator-(const vec3 &other) const
{
return vec3(x - other.x, y - other.y, z - other.z);
}
vec3 vec3::operator*(float scalar) const
{
return vec3(x * scalar, y * scalar, z * scalar);
}
vec3 vec3::operator/(float scalar) const
{
if (scalar == 0)
{
fprintf(stderr, "Error: Division by zero in vec3::operator/.\n");
}
return vec3(x / scalar, y / scalar, z / scalar);
}
vec3 vec3::dot(const vec3 &other) const
{
return vec3(x * other.x, y * other.y, z * other.z);
}
vec3 vec3::cross(const vec3 &other) const
{
return vec3(
y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x);
}
vec3 vec3::normalize() const
{
float length = sqrt(x * x + y * y + z * z);
if (length == 0)
{
fprintf(stderr, "Error: Normalization of zero vector in vec3::normalize.\n");
return vec3(0, 0, 0); // Return zero vector if normalization fails
}
return vec3(x / length, y / length, z / length);
}

View File

@ -0,0 +1,37 @@
#ifndef UTILS_VEC_HPP
#define UTILS_VEC_HPP
#include <cstddef>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
class vec3
{
private:
float x, y, z;
public:
vec3() : x(0), y(0), z(0) {}
vec3(float x, float y, float z) : x(x), y(y), z(z) {}
float getX() const { return x; }
float getY() const { return y; }
float getZ() const { return z; }
void setX(float val) { this->x = val; }
void setY(float val) { this->y = val; }
void setZ(float val) { this->z = val; }
vec3 operator+(const vec3 &other) const;
vec3 operator-(const vec3 &other) const;
vec3 operator*(float scalar) const;
vec3 operator/(float scalar) const;
vec3 dot(const vec3 &other) const;
vec3 cross(const vec3 &other) const;
vec3 normalize() const;
};
#endif // !UTILS_VEC_HPP

View File

@ -1,7 +1,29 @@
#include <iostream>
// Include the vector utilities header file
#include "vector/utils_vec.hpp"
int main()
{
std::cout << "Hello, World!" << std::endl;
vec3 *v1 = (vec3 *)malloc(sizeof(vec3) * 2);
if (v1 == nullptr)
{
fprintf(stderr, "Error: Memory allocation failed for vec3 array.\n");
return 1; // Exit with error code
}
for (int i = 0; i < 2; i++)
{
v1[i].setX(i + 1.0f);
v1[i].setY(i + 2.0f);
v1[i].setZ(i + 3.0f);
}
vec3 v2 = v1[0] + v1[1];
std::cout << "v2: (" << v2.getX() << ", " << v2.getY() << ", " << v2.getZ() << ")" << std::endl;
free(v1); // Free the allocated memory
return 0;
}