<commit_before>// -----------------------------------------------------------------------------
// Copyright  : (C) 2014 <NAME>
// License    : MIT (see the file LICENSE)
// Maintainer : <NAME> <<EMAIL>>
// Stability  : experimental
//
// Renderer
// -----------------------------------------------------------------------------

#include <math.h>
#include "renderer.hpp"

//#include "scene.hpp"

Renderer::Renderer()
    :
    scene_{},
    colorbuffer_(scene_.xres_*scene_.yres_, Color{}),
    ppm_(scene_.xres_, scene_.yres_, "default.ppm")
    {}

Renderer::Renderer(Scene scene)
    : /*width_{scene->xres_}
    , height_{scene->yres_}
    , colorbuffer_(w*h, Color(0.0, 0.0, 0.0))
    , filename_(file)
    , ppm_(width_, height_)*/
    scene_{scene},
    colorbuffer_(scene.xres_*scene.yres_, Color{}),
    ppm_(scene.xres_, scene.yres_, scene.filename)
    {}



//0.5 Alpha / 2 M_PI
void Renderer::render()
{

    float pic_ymax =2/scene_.xres_*scene_.yres_;



        float distance=(((scene_.camera.angle()/360)*2*3.1415)*0.5)/2*3.1415;
        std::cout <<"min. Distanz: " <<distance << "\n";
        int height_= scene_.yres_;
        int width_= scene_.xres_;

        float h = height_/2;
    for (unsigned y = 0; y < height_; ++y) {
        //std::cout << y << "\n";


            float w = -width_/2;
        for (unsigned x = 0; x < width_; ++x) {

            glm::vec3 onedirection {w/(width_/2),h/(height_/2), distance};
            Ray camray = scene_.camera.castray(onedirection);
//            Ray camray{scene_.camera.pos(), onedirection};
            Pixel p(x,y);
//            Hit hitteter=kugel.intersect(camray);
            Shape* first_hit;
            double shortest = 999999.9;

            for (std::vector<std::shared_ptr<Shape>>::iterator i = scene_.shapes_ptr.begin();i != scene_.shapes_ptr.end();++i){
                Hit hit = (*i)->intersect(camray);

                if(hit.hit_ == true){
                    if(hit.distance_ < shortest){
                        shortest = hit.distance_;
                        first_hit = hit.sptr_;

                        p.color = shade(camray, hit);
                    }
                }
            }

            if(shortest == 999999.9){
                p.color = scene_.background;
            }

            write(p);
            ++w;
        }
        --h;
    }
    ppm_.save(scene_.filename);
    std::cout << "saved file in: " << scene_.filename << " amazing! \n";
//float pic_z =1/(0.5*1.0/*alpha*/);//alpha von der camera aus der scene "angle"
//glm::vec3 p1{-1.0,};
//  height_=(2/scene.width_);
//prinzipieller aufbau
    /*
while (int x = 0; x < width; ++x)
{
    for (int y = 0; y < height; ++y)
    {
        Pixel p(x,y,pic_z);
    }
}*/

//kurze Anmerkung: momentan bewegt sich der raytracer in 0,005er schritten von -1 an der x ache weiter bis zu 0,995 => es gibt 400 pixelspalten beginnend bei dem wert x= 1.0 bis zu x = 0,995 um das ganze exakt zu berechnen

//  const std::size_t checkersize = 20;

/*        float distance=(((scene_.camera.angle()/360)*2*3.1415)*0.5)/2*3.1415;
        glm::vec3 mittelp{0.0,0.0,-30.0};
        float rad = 1;
        Sphere kugel{mittelp, rad};
        glm::vec3 onedirection{0.0,0.0,-1.0};
        int height_= scene_.yres_;
        int width_= scene_.xres_;

        float h = height_/2;
    for (unsigned y = 0; y < height_; ++y) {
        //std::cout << y << "\n";


            float w = -width_/2;
        for (unsigned x = 0; x < width_; ++x) {
                //std::cout << "x = " << x << "\n";
            //
            // p.color = raytrace(ray, depth);
            glm::vec3 origin{w/(width_/2),h/(height_/2),0.0};
//      std::cout <<"origin vec " << origin.x << ", " << origin.y << ", " << origin.z << " \n";
            Ray camray{origin, onedirection};
            Pixel p(x,y);
            Hit hitteter=kugel.intersect(camray);
            if (hitteter.hit_ == true){
                p.color = Color(hitteter.normal_.x,hitteter.normal_.y,hitteter.normal_.z);
            } else{
                p.color = Color(0.0,0.0,0.0);
            }
            write(p);
            ++w;
        }
        --h;
    }
    ppm_.save(scene_.filename);
    std::cout << "saved file in: " << scene_.filename << " amazing! \n";*/
}

Color Renderer::shade(Ray const& ray, Hit const& hit){
    Color ka = hit.sptr_->getmaterial().ka;
    Color kd = hit.sptr_->getmaterial().kd;
    Color ks = hit.sptr_->getmaterial().ks;

    Color Ia = scene_.amblight;
    Color L_amb = Ia * ka;
    Color Id;
    Color L_diff_spec;
    Color L_gesamt;

    float angle1;
    float angle2;

    std::vector<Color> Id_vec;
    std::vector<float> a1_vec;
    std::vector<float> a2_vec;

    glm::vec3 norm = glm::normalize(hit.normal_);
    glm::vec3 rref;
    glm::vec3 ray_inv_dir = glm::normalize(-ray.direction);
    glm::vec3 lightvec;


    for (std::vector<Light>::iterator i = scene_.lights.begin();i != scene_.lights.end();++i){

        Ray lightray{hit.target_ , i->pos_ - (hit.target_ + norm)};

            for (std::vector<std::shared_ptr<Shape>>::iterator j = scene_.shapes_ptr.begin();j != scene_.shapes_ptr.end();++j){
                Hit shadowhit = (*j)->intersect(lightray);
                lightvec = i->pos_ - hit.target_;

                if(shadowhit.distance_ < std::abs(lightvec.x) + std::abs(lightvec.y) + std::abs(lightvec.z)){
                    Id = {0.0,0.0,0.0};
                    angle1 = 0.0;
                    angle2 = 0.0;

                    break;
                }

                else{

                    Id = i->ld_;
                    glm::vec3 norm_lightvec = glm::normalize(lightvec);
                    angle1 = std::max(0.0f, glm::dot(norm, norm_lightvec));

                    rref = 2.0f * angle1 * norm - norm_lightvec;
                    angle2 = std::max(0.0f, glm::dot(rref, ray_inv_dir));

                }
            }
<commit_msg>shadowbias added<commit_after>// -----------------------------------------------------------------------------
// Copyright  : (C) 2014 <NAME>
// License    : MIT (see the file LICENSE)
// Maintainer : <NAME> <<EMAIL>>
// Stability  : experimental
//
// Renderer
// -----------------------------------------------------------------------------

#include <math.h>
#include "renderer.hpp"

//#include "scene.hpp"

Renderer::Renderer()
    :
    scene_{},
    colorbuffer_(scene_.xres_*scene_.yres_, Color{}),
    ppm_(scene_.xres_, scene_.yres_, "default.ppm")
    {}

Renderer::Renderer(Scene scene)
    : /*width_{scene->xres_}
    , height_{scene->yres_}
    , colorbuffer_(w*h, Color(0.0, 0.0, 0.0))
    , filename_(file)
    , ppm_(width_, height_)*/
    scene_{scene},
    colorbuffer_(scene.xres_*scene.yres_, Color{}),
    ppm_(scene.xres_, scene.yres_, scene.filename)
    {}



//0.5 Alpha / 2 M_PI
void Renderer::render()
{

    float pic_ymax =2/scene_.xres_*scene_.yres_;



        float distance=(((scene_.camera.angle()/360)*2*3.1415)*0.5)/2*3.1415;
        std::cout <<"min. Distanz: " <<distance << "\n";
        int height_= scene_.yres_;
        int width_= scene_.xres_;

        float h = height_/2;
    for (unsigned y = 0; y < height_; ++y) {
        //std::cout << y << "\n";


            float w = -width_/2;
        for (unsigned x = 0; x < width_; ++x) {

            glm::vec3 onedirection {w/(width_/2),h/(height_/2), distance};
            Ray camray = scene_.camera.castray(onedirection);
//            Ray camray{scene_.camera.pos(), onedirection};
            Pixel p(x,y);
//            Hit hitteter=kugel.intersect(camray);
            Shape* first_hit;
            double shortest = 999999.9;

            for (std::vector<std::shared_ptr<Shape>>::iterator i = scene_.shapes_ptr.begin();i != scene_.shapes_ptr.end();++i){
                Hit hit = (*i)->intersect(camray);

                if(hit.hit_ == true){
                    if(hit.distance_ < shortest){
                        shortest = hit.distance_;
                        first_hit = hit.sptr_;

                        p.color = shade(camray, hit);
                    }
                }
            }

            if(shortest == 999999.9){
                p.color = scene_.background;
            }

            write(p);
            ++w;
        }
        --h;
    }
    ppm_.save(scene_.filename);
    std::cout << "saved file in: " << scene_.filename << " amazing! \n";
//float pic_z =1/(0.5*1.0/*alpha*/);//alpha von der camera aus der scene "angle"
//glm::vec3 p1{-1.0,};
//  height_=(2/scene.width_);
//prinzipieller aufbau
    /*
while (int x = 0; x < width; ++x)
{
    for (int y = 0; y < height; ++y)
    {
        Pixel p(x,y,pic_z);
    }
}*/

//kurze Anmerkung: momentan bewegt sich der raytracer in 0,005er schritten von -1 an der x ache weiter bis zu 0,995 => es gibt 400 pixelspalten beginnend bei dem wert x= 1.0 bis zu x = 0,995 um das ganze exakt zu berechnen

//  const std::size_t checkersize = 20;

/*        float distance=(((scene_.camera.angle()/360)*2*3.1415)*0.5)/2*3.1415;
        glm::vec3 mittelp{0.0,0.0,-30.0};
        float rad = 1;
        Sphere kugel{mittelp, rad};
        glm::vec3 onedirection{0.0,0.0,-1.0};
        int height_= scene_.yres_;
        int width_= scene_.xres_;

        float h = height_/2;
    for (unsigned y = 0; y < height_; ++y) {
        //std::cout << y << "\n";


            float w = -width_/2;
        for (unsigned x = 0; x < width_; ++x) {
                //std::cout << "x = " << x << "\n";
            //
            // p.color = raytrace(ray, depth);
            glm::vec3 origin{w/(width_/2),h/(height_/2),0.0};
//      std::cout <<"origin vec " << origin.x << ", " << origin.y << ", " << origin.z << " \n";
            Ray camray{origin, onedirection};
            Pixel p(x,y);
            Hit hitteter=kugel.intersect(camray);
            if (hitteter.hit_ == true){
                p.color = Color(hitteter.normal_.x,hitteter.normal_.y,hitteter.normal_.z);
            } else{
                p.color = Color(0.0,0.0,0.0);
            }
            write(p);
            ++w;
        }
        --h;
    }
    ppm_.save(scene_.filename);
    std::cout << "saved file in: " << scene_.filename << " amazing! \n";*/
}

Color Renderer::shade(Ray const& ray, Hit const& hit){
    Color ka = hit.sptr_->getmaterial().ka;
    Color kd = hit.sptr_->getmaterial().kd;
    Color ks = hit.sptr_->getmaterial().ks;

    Color Ia = scene_.amblight;
    Color L_amb = Ia * ka;
    Color Id;
    Color L_diff_spec;
    Color L_gesamt;

    float angle1;
    float angle2;
    float shadowbias = 0.009f;

    std::vector<Color> Id_vec;
    std::vector<float> a1_vec;
    std::vector<float> a2_vec;

    glm::vec3 norm = glm::normalize(hit.normal_);
    glm::vec3 rref;
    glm::vec3 ray_inv_dir = glm::normalize(-ray.direction);
    glm::vec3 lightvec;


    for (std::vector<Light>::iterator i = scene_.lights.begin();i != scene_.lights.end();++i){

        Ray lightray{hit.target_ + (shadowbias*norm) , i->pos_ - (hit.target_ + (shadowbias* norm))};

            for (std::vector<std::shared_ptr<Shape>>::iterator j = scene_.shapes_ptr.begin();j != scene_.shapes_ptr.end();++j){
                Hit shadowhit = (*j)->intersect(lightray);
                lightvec = i->pos_ - hit.target_;

                if(shadowhit.distance_ < std::abs(lightvec.x) + std::abs(lightvec.y) + std::abs(lightvec.z)){
                    Id = {0.0,0.0,0.0};
                    angle1 = 0.0;
                    angle2 = 0.0;

                    break;
                }

                else{

                    Id = i->ld_;
                    glm::vec3 norm_lightvec = glm::normalize(lightvec);
                    angle1 = std::max(0.0f, glm::dot(norm, norm_lightvec));

                    rref = 2.0f * angle1 * norm - norm_lightvec;
                    angle2 = std::max(0.0f, glm::dot(rref, ray_inv_dir));

                }
            }

{'fraction_non_alphanumeric': 0.18903052666890305, 'fraction_numerical': 0.027507547802750755, 'mean_word_length': 1.713928083750569, 'pattern_counts': {'":': 0, '<': 137, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2721277', 'n_tokens_mistral': 4228, 'n_tokens_neox': 3968, 'n_words': 812}