Skip to content
Snippets Groups Projects
Commit d523a61e authored by bobarna's avatar bobarna
Browse files

Turned in version

parent df0b1f1f
Branches master
No related tags found
No related merge requests found
//============================================================================================= //=============================================================================================
// Computer Graphics Sample Program: Ray-tracing-let // Computer Graphics 2nd Homework
//
// A beadott program csak ebben a fajlban lehet, a fajl 1 byte-os ASCII karaktereket tartalmazhat, BOM kihuzando.
// Tilos:
// - mast "beincludolni", illetve mas konyvtarat hasznalni
// - faljmuveleteket vegezni a printf-et kiveve
// - Mashonnan atvett programresszleteket forrasmegjeloles nelkul felhasznalni es
// - felesleges programsorokat a beadott programban hagyni!!!!!!!
// - felesleges kommenteket a beadott programba irni a forrasmegjelolest kommentjeit kiveve
// ---------------------------------------------------------------------------------------------
// A feladatot ANSI C++ nyelvu forditoprogrammal ellenorizzuk, a Visual Studio-hoz kepesti elteresekrol
// es a leggyakoribb hibakrol (pl. ideiglenes objektumot nem lehet referencia tipusnak ertekul adni)
// a hazibeado portal ad egy osszefoglalot.
// ---------------------------------------------------------------------------------------------
// A feladatmegoldasokban csak olyan OpenGL fuggvenyek hasznalhatok, amelyek az oran a feladatkiadasig elhangzottak
// A keretben nem szereplo GLUT fuggvenyek tiltottak.
//
// NYILATKOZAT
// ---------------------------------------------------------------------------------------------
// Nev : Borcsok Barnabas
// Neptun : WC624D
// ---------------------------------------------------------------------------------------------
// ezennel kijelentem, hogy a feladatot magam keszitettem, es ha barmilyen segitseget igenybe vettem vagy
// mas szellemi termeket felhasznaltam, akkor a forrast es az atvett reszt kommentekben egyertelmuen jeloltem.
// A forrasmegjeloles kotelme vonatkozik az eloadas foliakat es a targy oktatoi, illetve a
// grafhazi doktor tanacsait kiveve barmilyen csatornan (szoban, irasban, Interneten, stb.) erkezo minden egyeb
// informaciora (keplet, program, algoritmus, stb.). Kijelentem, hogy a forrasmegjelolessel atvett reszeket is ertem,
// azok helyessegere matematikai bizonyitast tudok adni. Tisztaban vagyok azzal, hogy az atvett reszek nem szamitanak
// a sajat kontribucioba, igy a feladat elfogadasarol a tobbi resz mennyisege es minosege alapjan szuletik dontes.
// Tudomasul veszem, hogy a forrasmegjeloles kotelmenek megsertese eseten a hazifeladatra adhato pontokat
// negativ elojellel szamoljak el es ezzel parhuzamosan eljaras is indul velem szemben.
//============================================================================================= //=============================================================================================
#include "framework.h" #include "framework.h"
...@@ -297,23 +329,39 @@ struct Light { ...@@ -297,23 +329,39 @@ struct Light {
float rnd() { return (float) rand() / RAND_MAX; } float rnd() { return (float) rand() / RAND_MAX; }
const float epsilon = 0.0001f; const float epsilon = 0.0001f;
const int nLightSampling = 20;
class Scene { class Scene {
std::vector<Intersectable *> objects; std::vector<Intersectable *> objects;
std::vector<Light *> lights; std::vector<Light *> lights;
Camera camera; Camera camera;
vec3 La; vec3 La;
vec3 L_sky;
vec3 sun = vec3(6.0f, 6.0f, 6.0f);
vec3 sunDir = normalize(vec3(3, -3, -2));
std::vector<vec3> samplingPoints;
public: public:
void build() { void build() {
vec3 eye = vec3(0.0, 1.8f, 0.0f), vup = vec3(0, 0, 1), lookat = vec3(0, 0, 0); vec3 eye = vec3(0.0, 1.8f, -0.4f), vup = vec3(0, 0, 1), lookat = vec3(0, 0, 0);
float fov = 80 * M_PI / 180; float fov = 80 * M_PI / 180;
camera.set(eye, lookat, vup, fov); camera.set(eye, lookat, vup, fov);
//LIGHTING //LIGHTING
La = vec3(0.2f, 0.2f, 0.4f); La = vec3(0.28f, 0.28f, 0.28f);
L_sky = vec3(0.25f, 0.25f, 0.48f);
vec3 lightDirection(3, 2, 2), Le(1, 1, 1); vec3 lightDirection(3, 2, 2), Le(1, 1, 1);
lights.push_back(new Light(lightDirection, Le)); lights.push_back(new Light(lightDirection, Le));
//generating random points [-0.308, 0.308]x[-0.308, 0.308]
for (int i = 0; i < nLightSampling; i++) {
float sampleX = rnd(), sampleY = rnd();
while (sqrt(sampleX * sampleX + sampleY * sampleY) > 1) {
sampleX = rnd();
sampleY = rnd();
}
samplingPoints.emplace_back(sampleX * 0.616f - 0.308f, sampleY * 0.616f - 0.308f, 0.95f);
}
//MATERIALS //MATERIALS
vec3 kd1(0.3f, 0.2f, 0.1f), ks(1, 1, 1); vec3 kd1(0.3f, 0.2f, 0.1f), ks(1, 1, 1);
...@@ -324,7 +372,7 @@ public: ...@@ -324,7 +372,7 @@ public:
Material *materialSilver = new ReflectiveMaterial(nSilver, kSilver); Material *materialSilver = new ReflectiveMaterial(nSilver, kSilver);
vec3 kd2(0.2f, 0.2f, 0.4f); vec3 kd2(0.2f, 0.2f, 0.4f);
Material *materialCylinder = new RoughMaterial(kd2, ks, 50); Material *materialCylinder = new RoughMaterial(kd2, ks, 50);
vec3 kd3(0.2f, 0.4f, 0.2f); vec3 kd3(0.13f, 0.4f, 0.2f);
Material *materialVial = new RoughMaterial(kd3, ks, 50); Material *materialVial = new RoughMaterial(kd3, ks, 50);
//BULDING THE ROOM //BULDING THE ROOM
...@@ -337,7 +385,8 @@ public: ...@@ -337,7 +385,8 @@ public:
//BUILDING THE LIGHT "TUBE" //BUILDING THE LIGHT "TUBE"
Quadric *lightTube = new Hyperboloid(0.6f, 0.6f, 0.3f, materialSilver); Quadric *lightTube = new Hyperboloid(0.6f, 0.6f, 0.3f, materialSilver);
lightTube->Scale(1, 1, 10.0f); lightTube->Scale(1, 1, 10.0f);
lightTube->intersectors.push_back(new Plane(vec3(0.0f, 0.0f, 0.95f), vec3(0.0f, 0.0f, -1.0f), materialCylinder)); lightTube->intersectors.push_back(
new Plane(vec3(0.0f, 0.0f, 0.95f), vec3(0.0f, 0.0f, -1.0f), materialCylinder));
lightTube->intersectors.push_back(new Plane(vec3(0.0f, 0.0f, 4.0f), vec3(0.0f, 0.0f, 1.0f), materialCylinder)); lightTube->intersectors.push_back(new Plane(vec3(0.0f, 0.0f, 4.0f), vec3(0.0f, 0.0f, 1.0f), materialCylinder));
// BULDING THE BLUE CYLINDER // BULDING THE BLUE CYLINDER
...@@ -363,7 +412,7 @@ public: ...@@ -363,7 +412,7 @@ public:
objects.push_back(cylinder); objects.push_back(cylinder);
objects.push_back(paraboloid); objects.push_back(paraboloid);
objects.push_back(vial); objects.push_back(vial);
// objects.push_back(new Sphere(vec3(0,0,0), 0.1f, materialCylinder)); //objects.push_back(new Sphere(vec3(0,0,0.95f), 0.616f, materialCylinder));
} }
void render(std::vector<vec4> &image) { void render(std::vector<vec4> &image) {
...@@ -393,26 +442,33 @@ public: ...@@ -393,26 +442,33 @@ public:
vec3 trace(Ray ray, int depth = 0) { vec3 trace(Ray ray, int depth = 0) {
if (depth > 10) return La; if (depth > 10) return L_sky + sun * pow(dot(ray.dir, sunDir), 10);
Hit hit = firstIntersect(ray); Hit hit = firstIntersect(ray);
if (hit.t < 0) return La;
if (hit.t < 0) return L_sky + sun * pow(dot(ray.dir, sunDir), 10);
vec3 outRadiance(0, 0, 0); vec3 outRadiance(0, 0, 0);
if (hit.material->type == ROUGH) { if (hit.material->type == ROUGH) {
outRadiance = hit.material->ka * La; outRadiance = hit.material->ka * La;
for (Light *light : lights) { for (Light *light : lights) {
// TODO: sampling points from the hole on the top of the room for (vec3 samplePoint: samplingPoints) {
Ray shadowRay(hit.position + hit.normal * epsilon, light->direction); Ray shadowRay(hit.position + hit.normal * epsilon, samplePoint - hit.position);
float cosTheta = dot(hit.normal, light->direction); if (!shadowIntersect(shadowRay)) { // shadow computation
if (cosTheta > 0 && !shadowIntersect(shadowRay)) { // shadow computation float cosTheta = fmaxf(0, dot(hit.normal, shadowRay.dir));
outRadiance = outRadiance + light->Le * hit.material->kd * cosTheta; vec3 halfway = normalize(ray.dir + samplePoint);
vec3 halfway = normalize(-ray.dir + light->direction); float cosDelta = fmaxf(0, dot(hit.normal, halfway));
float cosDelta = dot(hit.normal, halfway); // {0,0,-1} is the normal vector of all the lights in the scene
if (cosDelta > 0) float deltaOmega = (0.308f * 0.308f * M_PI) / nLightSampling *
outRadiance = outRadiance + light->Le * hit.material->ks * dot(vec3(0.0f, 0.0f, -1.0f), shadowRay.dir) /
powf(cosDelta, hit.material->shininess); dot(samplePoint - hit.position, samplePoint - hit.position);
vec3 lightIn = trace(shadowRay, depth + 1);
vec3 brdf = hit.material->ka * (
hit.material->kd * cosTheta + hit.material->ks * powf(cosDelta, hit.material->shininess)
);
outRadiance = outRadiance + lightIn * brdf * dot(shadowRay.dir, hit.normal);
}
} }
} }
} }
...@@ -534,4 +590,6 @@ void onMouseMotion(int pX, int pY) { ...@@ -534,4 +590,6 @@ void onMouseMotion(int pX, int pY) {
// Idle event indicating that some time elapsed: do animation here // Idle event indicating that some time elapsed: do animation here
void onIdle() { void onIdle() {
//glutPostRedisplay(); //glutPostRedisplay();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment