Skip to content

Commit

Permalink
Version 1 finished
Browse files Browse the repository at this point in the history
  • Loading branch information
blroot committed Aug 23, 2017
1 parent 9b1ace2 commit 31f2962
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
6 changes: 3 additions & 3 deletions core/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ namespace superboy {
int pixelsElement = 0;
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
pixels[pixelsElement] = getColor(i, j).z*255;
pixels[pixelsElement+1] = getColor(i, j).y*255;
pixels[pixelsElement+2] = getColor(i, j).x*255;
pixels[pixelsElement] = getColor(i, j).z*255.0f;
pixels[pixelsElement+1] = getColor(i, j).y*255.0f;
pixels[pixelsElement+2] = getColor(i, j).x*255.0f;
pixelsElement += 3;
}
}
Expand Down
12 changes: 6 additions & 6 deletions core/color/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,24 @@ namespace superboy {

float x, y, z;

if (this->x > 255.0f) {
x = 255.0f;
if (this->x > 1.0f) {
x = 1.0f;
} else if (this->x < 0.0f) {
x = 0.0f;
} else {
x = this->x;
}

if (this->y > 255.0f) {
y = 255.0f;
if (this->y > 1.0f) {
y = 1.0f;
} else if (this->y < 0.0f) {
y = 0.0f;
} else {
y = this->y;
}

if (this->z > 255.0f) {
z = 255.0f;
if (this->z > 1.0f) {
z = 1.0f;
} else if (this->z < 0.0f) {
z = 0.0f;
} else {
Expand Down
13 changes: 5 additions & 8 deletions core/shading/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ namespace superboy {
vec3 surface_normal = intersection.getNormal();
vec3 hitpoint = intersection.getPoint();

// TODO: this is causing troubles
this->eyedir = -1*intersection.getRay()->getDirection();
//this->eyedir = vec3(0.0, 0.0, 0.1);

color colorvec = colorModel(intersection);

// Reflection Ray
if (intersection.getHitObject()->getMaterials().getSpecular() != color()) {
vec3 mirror_vector = intersection.getRay()->getDirection() - 2*(intersection.getRay()->getDirection().dot(surface_normal))*surface_normal;
Ray ray_from_mirror = Ray(hitpoint + surface_normal*1e-4, mirror_vector);
Ray ray_from_mirror = Ray(hitpoint + surface_normal*1e-4, mirror_vector.normalize());
IntersectionInfo mirror_intersection = scene->intersect(ray_from_mirror);

if (mirror_intersection.getHitObject() != nullptr) {
Expand Down Expand Up @@ -74,7 +70,8 @@ namespace superboy {
for (int i = 0; i < this->scene->getLights().size(); i++) {

vec3 light_direction = this->scene->getLights()[i]->getDirection(hitpoint);
vec3 halfvec = (light_direction + this->eyedir).normalize();

vec3 halfvec = (light_direction + -1*intersection.getRay()->getDirection()).normalize();
float distance_to_light = this->scene->getLights()[i]->getDistance(hitpoint);
float attenuation_model = this->scene->getLights()[i]->getAttenuation(hitpoint);

Expand All @@ -89,8 +86,8 @@ namespace superboy {
color diff_light = color(diffuse.x * lightcolor.x, diffuse.y * lightcolor.y, diffuse.z * lightcolor.z);
color spec_light = color(specular.x * lightcolor.x, specular.y * lightcolor.y, specular.z * lightcolor.z);

color lambert = diff_light * std::max(surface_normal.normalize().dot(light_direction), 0.0f);
color phong = spec_light * pow(std::max(surface_normal.dot(halfvec), 0.0f), intersection.getHitObject()->getMaterials().getShininess());
color lambert = diff_light * std::max<float>(surface_normal.normalize().dot(light_direction), 0.0f);
color phong = spec_light * pow(std::max<float>(surface_normal.dot(halfvec), 0.0), (int)intersection.getHitObject()->getMaterials().getShininess());

colorvec += attenuation_model * (lambert + phong);
}
Expand Down
1 change: 0 additions & 1 deletion core/shading/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace superboy {
private:
Scene* scene;
color colorModel(IntersectionInfo& intersection);
vec3 eyedir;
};

} /* namespace superboy */

0 comments on commit 31f2962

Please sign in to comment.