Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Floor: public Object
- {
- public:
- Floor(double floor_width, double tile_width){
- reference_point.x = -floor_width/2.0;
- reference_point.y = -floor_width/2.0;
- reference_point.z = 0;
- length = tile_width;
- height = 0;
- }
- struct point getNormal(struct point pointed){
- struct point normal;
- normal.x = 0;
- normal.y = 0;
- normal.z = 1;
- return normal;
- }
- double getT(Ray *r){
- struct point normal = getNormal(reference_point);
- return ((-1.0) * dotProduct(normal, r->start) / dotProduct(normal, r->dir));
- }
- double intersect(Ray *r, struct point* current_color, int level){
- double t = getT(r);
- //if(t <= 0) return -1;
- struct point intersection;
- intersection.x = r->start.x + r->dir.x * t;
- intersection.y = r->start.y + r->dir.y * t;
- intersection.z = r->start.z + r->dir.z * t;
- if(intersection.x < reference_point.x || intersection.x > -reference_point.x || intersection.y < reference_point.y || intersection.y > -reference_point.y){
- return -1;
- }
- if(level == 0) return t;
- int x = (intersection.x - reference_point.x) / length;
- int y = (intersection.y - reference_point.y) / length;
- if((x + y) % 2 == 0){
- color[0] = color[1] = color[2] = 0;
- }
- else {
- color[0] = color[1] = color[2] = 1;
- }
- current_color->x = color[0] * co_efficients[AMBIENT];
- current_color->y = color[1] * co_efficients[AMBIENT];
- current_color->z = color[2] * co_efficients[AMBIENT];
- struct point normal = getNormal(intersection);
- struct point reflection = getReflection(r, normal);
- for(int i = 0; i < lights.size(); i++){
- struct point ldir;
- ldir.x = lights[i].x - intersection.x;
- ldir.y = lights[i].y - intersection.y;
- ldir.z = lights[i].z - intersection.z;
- double len = sqrt(pow(ldir.x,2) + pow(ldir.y,2) + pow(ldir.z,2));
- ldir = normalize(ldir);
- struct point lstart;
- lstart.x = intersection.x + ldir.x * 1.0;
- lstart.y = intersection.y + ldir.y * 1.0;
- lstart.z = intersection.z + ldir.z * 1.0;
- Ray L(lstart, ldir);
- bool obscured = false;
- for(int j = 0; j < objects.size(); j++){
- double possibleobscure = objects[j]->getT(&L);
- //printf("%f ", possibleobscure);
- if(possibleobscure > 0 && abs(possibleobscure) > len){
- obscured = true;
- break;
- }
- }
- //printf("\n");
- if(!obscured){
- //printf("direct");
- //L.dir.x = -L.dir.x;
- //L.dir.y = -L.dir.y;
- //L.dir.z = -L.dir.z;
- double lambert = dotProduct(L.dir, normal);
- double phong = dotProduct(reflection, r->dir);
- //if(lambert > 0 || phong > 0) printf("emon to hobar kotha na\n");
- lambert = lambert > 0?lambert:0;
- phong = phong > 0?phong:0;
- current_color->x += source_factor * color[0] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
- current_color->y += source_factor * color[1] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
- current_color->z += source_factor * color[2] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
- }
- if(level < recursion_level){
- struct point rs;
- rs.x = intersection.x + reflection.x * 1.0;
- rs.y = intersection.y + reflection.y * 1.0;
- rs.z = intersection.z + reflection.z * 1.0;
- //reflection = normalize(reflection);
- Ray reflectionRay(rs, reflection);
- int nearest = -1;
- struct point reflectColor;
- double t_min = 9999;
- for(int k = 0; k < objects.size(); k++){
- double t = objects[k]->getT(&reflectionRay);
- if(t <= 0) continue;
- else if(t < t_min) {
- t_min = t;
- nearest = k;
- }
- }
- if(nearest != -1){
- double t = objects[nearest]->intersect(&reflectionRay, &reflectColor, level + 1);
- if(t!=-1){
- current_color->x += reflectColor.x*co_efficients[REFLECTION];
- current_color->y += reflectColor.y*co_efficients[REFLECTION];
- current_color->z += reflectColor.z*co_efficients[REFLECTION];
- }
- if(current_color->x > 1) current_color->x = 1;
- if(current_color->x < 0) current_color->x = 0;
- if(current_color->y > 1) current_color->y = 1;
- if(current_color->y < 0) current_color->y = 0;
- if(current_color->z > 1) current_color->z = 1;
- if(current_color->z < 0) current_color->z = 0;
- }
- }
- //Check whether all current_color pixel value is within 1 or 0 if not set it
- }
- return t;
- }
- void draw(){
- struct point temp = reference_point;
- int black = 1;
- while(temp.y < -reference_point.y){
- while(temp.x < -reference_point.x){
- if(black == 1){
- glColor3f(0, 0, 0);
- }
- else{
- glColor3f(1, 1, 1);
- }
- black = 1 - black;
- glBegin(GL_QUADS);
- glVertex3f(temp.x, temp.y, temp.z);
- glVertex3f(temp.x, temp.y + length, temp.z);
- glVertex3f(temp.x + length, temp.y + length, temp.z);
- glVertex3f(temp.x + length, temp.y, temp.z);
- glEnd();
- temp.x += length;
- }
- temp.x = reference_point.x;
- temp.y += length;
- black = 1 - black;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement