Skip to content

Submission: Ratchpak (Dome) Pongmongkol #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
322 changes: 43 additions & 279 deletions README.md

Large diffs are not rendered by default.

Binary file added img/Capture.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/Capture2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed img/REFERENCE_cornell.5000samp.png
Binary file not shown.
Binary file removed img/REFERENCE_sphere.5000samp.png
Binary file not shown.
Binary file added img/perfs.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

image::image(int x, int y) :
xSize(x),
ySize(y),
pixels(new glm::vec3[x * y]) {
ySize(y){
for (int i = 0; i < x; i++){
for (int j = 0; j < x; j++)
pixels.push_back(glm::vec3());
}
}

image::~image() {
delete pixels;
}

void image::setPixel(int x, int y, const glm::vec3 &pixel) {
Expand Down Expand Up @@ -40,6 +42,6 @@ void image::savePNG(const std::string &baseFilename) {

void image::saveHDR(const std::string &baseFilename) {
std::string filename = baseFilename + ".hdr";
stbi_write_hdr(filename.c_str(), xSize, ySize, 3, (const float *) pixels);
stbi_write_hdr(filename.c_str(), xSize, ySize, 3, (const float *) &(pixels[0]));
std::cout << "Saved " + filename + "." << std::endl;
}
10 changes: 5 additions & 5 deletions src/image.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#pragma once

#include <glm/glm.hpp>
#include <vector>

using namespace std;

class image {
private:
public:
int xSize;
int ySize;
glm::vec3 *pixels;
std::vector<glm::vec3> pixels;

public:
image(int x, int y);
~image();
void setPixel(int x, int y, const glm::vec3 &pixel);
~image();
void setPixel(int x, int y, const glm::vec3 &pixel);
void savePNG(const std::string &baseFilename);
void saveHDR(const std::string &baseFilename);
};
114 changes: 111 additions & 3 deletions src/interactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,47 @@ glm::vec3 calculateRandomDirectionInHemisphere(
+ sin(around) * over * perpendicularDirection2;
}

//compute specular ray

__host__ __device__
glm::vec3 calculateRandomDirectionSpecular(
glm::vec3 normal, thrust::default_random_engine &rng, float shininess) {
thrust::uniform_real_distribution<float> u01(0, 1);

float theta = acos(powf(u01(rng), 1 / (shininess + 1)));
float phi = TWO_PI * u01(rng);


float x = cos(phi) * sin(theta);
float y = sin(phi) * sin(theta);
float z = cos(theta);

// Find a direction that is not the normal based off of whether or not the
// normal's components are all equal to sqrt(1/3) or whether or not at
// least one component is less than sqrt(1/3). Learned this trick from
// Peter Kutz.

glm::vec3 directionNotNormal;
if (abs(normal.x) < SQRT_OF_ONE_THIRD) {
directionNotNormal = glm::vec3(1, 0, 0);
}
else if (abs(normal.y) < SQRT_OF_ONE_THIRD) {
directionNotNormal = glm::vec3(0, 1, 0);
}
else {
directionNotNormal = glm::vec3(0, 0, 1);
}

// Use not-normal direction to generate two perpendicular directions
glm::vec3 perpendicularDirection1 =
glm::normalize(glm::cross(normal, directionNotNormal));
glm::vec3 perpendicularDirection2 =
glm::normalize(glm::cross(normal, perpendicularDirection1));

return z * normal
+ y * perpendicularDirection1
+ x * perpendicularDirection2;
}
/**
* Scatter a ray with some probabilities according to the material properties.
* For example, a diffuse surface scatters in a cosine-weighted hemisphere.
Expand All @@ -65,14 +106,81 @@ glm::vec3 calculateRandomDirectionInHemisphere(
* You may need to change the parameter list for your purposes!
*/
__host__ __device__
void scatterRay(
bool scatterRay(
Ray &ray,
glm::vec3 &color,
glm::vec3 intersect,
glm::vec3 normal,
const Material &m,
thrust::default_random_engine &rng) {
bool outside,
const Material &m,
thrust::default_random_engine rng,
glm::vec2 uv,
Texture* d_texture) {
// TODO: implement this.
// A basic implementation of pure-diffuse shading will just call the
// calculateRandomDirectionInHemisphere defined above.

glm::vec3 diffuseColor = m.color;
if (m.textureid != -1){
Texture t = d_texture[m.textureid];
int x = uv[0] * t.width;
int y = uv[1] * t.height;

diffuseColor = t.d_img[x + y * t.width];
}


if (m.emittance > 0) { //light source
color *= m.color * m.emittance;
return false;
}

if (m.hasRefractive > 0){
glm::vec3 reflectDir = glm::reflect(ray.direction, normal);

float r0 = pow((1 - m.indexOfRefraction) / (1 + m.indexOfRefraction), 2);
float schlick = r0 + (1 - r0)*pow(1 - dot(normal, reflectDir), 5);

thrust::uniform_real_distribution<float> u01(0, 1);
float result = u01(rng);

if (result < schlick){
ray.direction = reflectDir;
ray.origin = intersect + ray.direction * 0.001f;
color *= m.specular.color;
}
else{
float eta = m.indexOfRefraction;
if (outside){
eta = 1.0 / m.indexOfRefraction;
}

ray.direction = glm::refract(ray.direction, normal, eta);
ray.origin = intersect + ray.direction * 0.1f;
}
}
else if (m.hasReflective > 0){
ray.direction = glm::reflect(ray.direction, normal);
ray.origin = intersect + ray.direction * 0.001f;
color *= diffuseColor;
}
else {
//diffuse-specular
thrust::uniform_real_distribution<float> u01(0, 1);
float result = u01(rng);

if (m.specular.exponent == 0 || result < 0.5){
//diffuse
ray.direction = calculateRandomDirectionInHemisphere(normal, rng);
ray.origin = intersect + ray.direction * 0.001f;
color *= diffuseColor;
}
else{
//specular
ray.direction = calculateRandomDirectionSpecular(normal, rng, m.specular.exponent);
ray.origin = intersect + ray.direction * 0.001f;
color *= m.specular.color;
}
}
return true;
}
Loading