Skip to content
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

Submission #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
353 changes: 80 additions & 273 deletions README.md

Large diffs are not rendered by default.

154 changes: 154 additions & 0 deletions src/glslUtility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// GLSL Utility: A utility class for loading GLSL shaders, for Patrick Cozzi's CIS565: GPU Computing at the University of Pennsylvania
// Written by Varun Sampath and Patrick Cozzi, Copyright (c) 2012 University of Pennsylvania

#include "glslUtility.h"

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using std::ios;

namespace glslUtility {

typedef struct {
GLuint vertex;
GLuint fragment;
} shaders_t;

char* loadFile(const char *fname, GLint &fSize)
{
// file read based on example in cplusplus.com tutorial
std::ifstream file (fname, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
unsigned int size = (unsigned int)file.tellg();
fSize = size;
char *memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
//std::cout << "file " << fname << " loaded" << std::endl;
return memblock;
}

std::cout << "Unable to open file " << fname << std::endl;
exit(1);
}

// printShaderInfoLog
// From OpenGL Shading Language 3rd Edition, p215-216
// Display (hopefully) useful error messages if shader fails to compile
void printShaderInfoLog(GLint shader)
{
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;

glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLen);

if (infoLogLen > 1)
{
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetShaderInfoLog(shader,infoLogLen, &charsWritten, infoLog);
//std::cout << "InfoLog:" << std::endl << infoLog << std::endl;
delete [] infoLog;
}
}

void printLinkInfoLog(GLint prog)
{
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;

glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &infoLogLen);

if (infoLogLen > 1)
{
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetProgramInfoLog(prog,infoLogLen, &charsWritten, infoLog);
//std::cout << "InfoLog:" << std::endl << infoLog << std::endl;
delete [] infoLog;
}
}

shaders_t loadShaders(const char * vert_path, const char * frag_path) {
GLuint f, v;

char *vs,*fs;

v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);

// load shaders & get length of each
GLint vlen;
GLint flen;
vs = loadFile(vert_path,vlen);
fs = loadFile(frag_path,flen);

const char * vv = vs;
const char * ff = fs;

glShaderSource(v, 1, &vv,&vlen);
glShaderSource(f, 1, &ff,&flen);

GLint compiled;

glCompileShader(v);
glGetShaderiv(v, GL_COMPILE_STATUS, &compiled);
if (!compiled)
{
std::cout << "Vertex shader not compiled." << std::endl;
}
printShaderInfoLog(v);

glCompileShader(f);
glGetShaderiv(f, GL_COMPILE_STATUS, &compiled);
if (!compiled)
{
std::cout << "Fragment shader not compiled." << std::endl;
}
printShaderInfoLog(f);

shaders_t out; out.vertex = v; out.fragment = f;

delete [] vs; // dont forget to free allocated memory, or else really bad things start happening
delete [] fs; // we allocated this in the loadFile function...

return out;
}

void attachAndLinkProgram( GLuint program, shaders_t shaders) {
glAttachShader(program, shaders.vertex);
glAttachShader(program, shaders.fragment);

glLinkProgram(program);
GLint linked;
glGetProgramiv(program,GL_LINK_STATUS, &linked);
if (!linked)
{
std::cout << "Program did not link." << std::endl;
}
printLinkInfoLog(program);
}

GLuint createProgram(const char *vertexShaderPath, const char *fragmentShaderPath, const char *attributeLocations[], GLuint numberOfLocations)
{
glslUtility::shaders_t shaders = glslUtility::loadShaders(vertexShaderPath, fragmentShaderPath);

GLuint program = glCreateProgram();

for (GLuint i = 0; i < numberOfLocations; ++i)
{
glBindAttribLocation(program, i, attributeLocations[i]);
}

glslUtility::attachAndLinkProgram(program, shaders);

return program;
}
}
20 changes: 20 additions & 0 deletions src/glslUtility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// GLSL Utility: A utility class for loading GLSL shaders, for Patrick Cozzi's CIS565: GPU Computing at the University of Pennsylvania
// Written by Varun Sampath and Patrick Cozzi, Copyright (c) 2012 University of Pennsylvania

#ifndef GLSLUTILITY_H_
#define GLSLUTILITY_H_

#ifdef __APPLE__
#include <GL/glfw.h>
#else
#include <GL/glew.h>
#endif

namespace glslUtility
{

GLuint createProgram(const char *vertexShaderPath, const char *fragmentShaderPath, const char *attributeLocations[], GLuint numberOfLocations);

}

#endif
5 changes: 2 additions & 3 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
// Yining Karl Li's TAKUA Render, a massively parallel pathtracing renderer: http://www.yiningkarlli.com

#include <iostream>
#include <stb_image/stb_image_write.h>

#include "image.h"
#include "stb_image/stb_image_write.h"
#include "utilities.h"

image::image(int x, int y){
Expand Down Expand Up @@ -39,7 +38,7 @@ image::~image(){
//------------------------

float image::applyGamma(float f){
//apply gamma correction, use simple power law gamma for now.
//apply gamma correction, use simple power law gamma for now. TODO: sRGB
return pow(f/float(gamma.divisor), gamma.gamma);
}

Expand Down
40 changes: 22 additions & 18 deletions src/interactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct AbsorptionAndScatteringProperties{
float reducedScatteringCoefficient;
};

// Forward declaration
//forward declaration
__host__ __device__ bool calculateScatterAndAbsorption(ray& r, float& depth, AbsorptionAndScatteringProperties& currentAbsorptionAndScattering, glm::vec3& unabsorbedColor, material m, float randomFloatForScatteringDistance, float randomFloat2, float randomFloat3);
__host__ __device__ glm::vec3 getRandomDirectionInSphere(float xi1, float xi2);
__host__ __device__ glm::vec3 calculateTransmission(glm::vec3 absorptionCoefficient, float distance);
Expand All @@ -27,29 +27,29 @@ __host__ __device__ glm::vec3 calculateReflectionDirection(glm::vec3 normal, glm
__host__ __device__ Fresnel calculateFresnel(glm::vec3 normal, glm::vec3 incident, float incidentIOR, float transmittedIOR, glm::vec3 reflectionDirection, glm::vec3 transmissionDirection);
__host__ __device__ glm::vec3 calculateRandomDirectionInHemisphere(glm::vec3 normal, float xi1, float xi2);

// TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ glm::vec3 calculateTransmission(glm::vec3 absorptionCoefficient, float distance) {
return glm::vec3(0,0,0);
}

// TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ bool calculateScatterAndAbsorption(ray& r, float& depth, AbsorptionAndScatteringProperties& currentAbsorptionAndScattering,
glm::vec3& unabsorbedColor, material m, float randomFloatForScatteringDistance, float randomFloat2, float randomFloat3){
return false;
}

// TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ glm::vec3 calculateTransmissionDirection(glm::vec3 normal, glm::vec3 incident, float incidentIOR, float transmittedIOR) {
return glm::vec3(0,0,0);
}

// TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ glm::vec3 calculateReflectionDirection(glm::vec3 normal, glm::vec3 incident) {
//nothing fancy here
return glm::vec3(0,0,0);
}

// TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ Fresnel calculateFresnel(glm::vec3 normal, glm::vec3 incident, float incidentIOR, float transmittedIOR, glm::vec3 reflectionDirection, glm::vec3 transmissionDirection) {
Fresnel fresnel;

Expand All @@ -58,16 +58,16 @@ __host__ __device__ Fresnel calculateFresnel(glm::vec3 normal, glm::vec3 inciden
return fresnel;
}

// LOOK: This function demonstrates cosine weighted random direction generation in a sphere!
//LOOK: This function demonstrates cosine weighted random direction generation in a sphere!
__host__ __device__ glm::vec3 calculateRandomDirectionInHemisphere(glm::vec3 normal, float xi1, float xi2) {

// Crucial difference between this and calculateRandomDirectionInSphere: THIS IS COSINE WEIGHTED!
//crucial difference between this and calculateRandomDirectionInSphere: THIS IS COSINE WEIGHTED!

float up = sqrt(xi1); // cos(theta)
float over = sqrt(1 - up * up); // sin(theta)
float around = xi2 * TWO_PI;

// 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.
//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) {
Expand All @@ -78,24 +78,28 @@ __host__ __device__ glm::vec3 calculateRandomDirectionInHemisphere(glm::vec3 nor
directionNotNormal = glm::vec3(0, 0, 1);
}

// Use not-normal direction to generate two perpendicular directions
//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 ( up * normal ) + ( cos(around) * over * perpendicularDirection1 ) + ( sin(around) * over * perpendicularDirection2 );

}

// TODO: IMPLEMENT THIS FUNCTION
// Now that you know how cosine weighted direction generation works, try implementing
// non-cosine (uniform) weighted random direction generation.
// This should be much easier than if you had to implement calculateRandomDirectionInHemisphere.
//TODO: IMPLEMENT THIS FUNCTION
//Now that you know how cosine weighted direction generation works, try implementing non-cosine (uniform) weighted random direction generation.
//This should be much easier than if you had to implement calculateRandomDirectionInHemisphere.
//TODO: IMPLEMENT THIS FUNCTION
//Generates a random point on a given sphere
__host__ __device__ glm::vec3 getRandomDirectionInSphere(float xi1, float xi2) {
return glm::vec3(0,0,0);
}
float x = 2*3.14159*xi1;
float y = 2*xi2-1;
float z = sqrt(1-y*y);

// TODO (PARTIALLY OPTIONAL): IMPLEMENT THIS FUNCTION
// Returns 0 if diffuse scatter, 1 if reflected, 2 if transmitted.
return glm::vec3(y,z*cos(x), z*sin(x));
}
//TODO (PARTIALLY OPTIONAL): IMPLEMENT THIS FUNCTION
//returns 0 if diffuse scatter, 1 if reflected, 2 if transmitted.
__host__ __device__ int calculateBSDF(ray& r, glm::vec3 intersect, glm::vec3 normal, glm::vec3 emittedColor,
AbsorptionAndScatteringProperties& currentAbsorptionAndScattering,
glm::vec3& color, glm::vec3& unabsorbedColor, material m){
Expand Down
Loading