So, ive been delving into the depths of ray tracing and ive come to find that my solution of casting a ray is very in-efficient.
for(int y = 0; y < screenHeight; y++) {
for(int x = 0; x < screenWidth; x++) {
vec3 ray((x * (2 / screenWidth)) - 1, (y * (2 / screenHeight)) - 1, 0);
window.setPixel(x, y, RGB(1, 1, 1));
for(int i = 0; i < castDistance; i++) {
ray.z += 1; //ray position goes in diagnol line(Forwards-left)
ray.x -= 1;
if(ray.x >= quad.x && ray.x <= quad.x + quad.size.x &&
ray.y >= quad.y && ray.y <= quad.y + quad.size.y &&
ray.z >= quad.z && ray.z <= quad.z + quad.size.z) {
window.setPixel(x, y, RGB(1, 0, 0));
}
}
}
}
Is there a better way for me to do an operation like this?
There are a couple of things, you want to take into consideration:
First not all rays are sent parallel to the z axis, while doing ray casting. Instead you shoot them from an origin (eye) through your virtual screen.
Second the checking for intersections is not done by tracing the ray
step by step but by checking for collisions with objects of your
scene. In your case this would be one single axis aligned box and it
is easy to calculate the intersection of a ray and an axis aligned
box. For example it is described here:
https://tavianator.com/fast-branchless-raybounding-box-intersections/