Skip to content
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
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# processing
# Path tracer

## Render Gallery

**All render frames converged within 0.5s on a gtx 1080** <br/> <br/>
**NOTE: if the fps is abysmal on your machine, you may need to lower the SPF located in the fragment shader** <br/> <br/>
![image](https://media.discordapp.net/attachments/640631259730542602/780539227221000243/unknown.png)
![image](https://media.discordapp.net/attachments/764510825623977997/780537058719957012/unknown.png)
![image](https://media.discordapp.net/attachments/640631259730542602/780581710332952576/unknown.png)
![image](https://media.discordapp.net/attachments/640631259730542602/780585666257616907/unknown.png)
![image](https://media.discordapp.net/attachments/764510825623977997/778461138601902110/unknown.png)
![image](https://media.discordapp.net/attachments/640631259730542602/780551399276412929/unknown.png)
![image](https://media.discordapp.net/attachments/764510825623977997/778461107919519764/unknown.png)
## Some earlier renders (images are very noisy)
![image](https://media.discordapp.net/attachments/640631259730542602/777073655359602708/unknown.png)
![image](https://media.discordapp.net/attachments/640631259730542602/777076703276171264/unknown.png)
![image](https://media.discordapp.net/attachments/640631259730542602/777071652403544084/unknown.png)
![image](https://user-images.githubusercontent.com/19439575/99139449-8c85fc00-25ed-11eb-940a-6d6c764ce353.png)
![image](https://user-images.githubusercontent.com/19439575/99134277-d95bd980-25d1-11eb-9052-380018af8ec3.png)
![image](https://user-images.githubusercontent.com/19439575/99129174-b6c1c480-25c1-11eb-9c88-1920dd577207.png)
38 changes: 38 additions & 0 deletions Runner/Buffer.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.nio.*;
import com.jogamp.opengl.GL4;

public class Buffer {
int id;

Buffer() {}

void create() {
int ids[] = new int[1];
gl.glCreateBuffers(1, ids, 0);
id = ids[0];
}

void delete() {
int ids[] = new int[1];
ids[0] = id;
gl.glDeleteBuffers(1, ids, 0);
}

void init(long size, java.nio.Buffer data, int target, int usage) {
bind(target);
gl.glBufferData(target, size, data, usage);
unbind(target);
}

void update(long offset, long size, java.nio.Buffer data) { // not supported by PJOGL
gl.glNamedBufferSubData(id, offset, size, data);
}

void bind(int target) {
gl.glBindBuffer(target, id);
}

void unbind(int target){
gl.glBindBuffer(target, 0);
}
}
105 changes: 105 additions & 0 deletions Runner/Camera.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
public class Camera {
float yaw = 0;
float pitch = 0;
float prevX = screenSize[0]/2;
float prevY = screenSize[1]/2;

vec3 pos;
vec3 dir;
vec3 up;

mat4 view, invView;
mat4 proj, invProj;
final float rotSpeed = 0.05f;
final float moveSpeed = 0.010f;

int shaderProjLoc, shaderViewLoc, invProjLoc, invViewLoc;
boolean wKey = false, sKey = false, aKey = false, dKey = false, spaceKey = false, shiftKey = false, isActive = true;

public Camera(float posx, float posy, float posz, float dirx, float diry, float dirz) {
proj = MMath.perspective(MMath.radians(45.0f), (float)screenSize[0] / (float)screenSize[1], 0.1, 2000.0f);
invProj = MMath.inverse(proj);
pos = new vec3(posx, posy, posz);
dir = new vec3(dirx, diry, dirz);
up = new vec3(0, 1, 0);
view = MMath.lookAt(pos, dir, up);
win = (GLWindow)surface.getNative();
}

public void mouseCallback() {
if(!isActive) return;

float dx = prevX-mouseX;
float dy = prevY-mouseY;
prevX = mouseX;
prevY = mouseY;
yaw += dx * rotSpeed;
pitch += dy * rotSpeed;
if(pitch > 89.0)
pitch = 89.0;
if (pitch < -89.0)
pitch = -89.0;

dir = MMath.normalize(new vec3(sin(MMath.radians(yaw)) * cos(MMath.radians(pitch)),
sin(MMath.radians(pitch)),
cos(MMath.radians(yaw)) * cos(MMath.radians(pitch))));

prevX = screenSize[0]/2.0; // recenter mouse
prevY = screenSize[1]/2.0;
win.warpPointer((int) prevX, (int) prevY);
}

public boolean setKbState(final int _keyCode, final char _key, final boolean state) {
if(!isActive) return state;
switch(_key) {
case 'w':
case 'W':
return wKey = state; // forward
case 's':
case 'S':
return sKey = state; // backward
case 'a':
case 'A':
return aKey = state; // left
case 'd':
case 'D':
return dKey = state; // right
case ' ':
return spaceKey = state; // up
default:
switch(_keyCode) {
case SHIFT:
return shiftKey = state;
default:
return state;
}
}
}

public void updatePos(float dt) {
if(!isActive) return;
if (wKey)
pos.add(MMath.mul(dir, moveSpeed * dt));
if (sKey)
pos.add(MMath.mul(dir, moveSpeed * dt * -1));
if (aKey)
pos.add(MMath.mul(MMath.normalize(MMath.cross(up, dir)), moveSpeed * dt));
if (dKey)
pos.add(MMath.mul(MMath.normalize(MMath.cross(up, dir)), moveSpeed * dt * -1));
if (spaceKey)
pos.add(MMath.mul(MMath.normalize(up), moveSpeed * dt));
if (shiftKey)
pos.add(MMath.mul(MMath.normalize(up), moveSpeed * dt * -1));
}

void updateUniforms() {
mat4 currView = MMath.lookAt(pos, MMath.add(pos, dir), up);
if(!view.equals(currView)) { // camera view or position changed
screenSize[2] = 0;
view.m = currView.m;
invView = MMath.inverse(view);
gl.glUniformMatrix4fv(shaderViewLoc, 1, false, view.m, 0);
gl.glUniformMatrix4fv(invViewLoc, 1, false, invView.m, 0);
}
}
}
180 changes: 180 additions & 0 deletions Runner/Math.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
public static class vec3 {
public float x;
public float y;
public float z;

public vec3(float _x, float _y, float _z) {
x = _x;
y = _y;
z = _z;
}

public void add(vec3 other) {
x += other.x;
y += other.y;
z += other.z;
}

public String toString() {
return "vec3(" + x + ", " + y + ", " + z + ")";
}
}

public static class mat4 {
// row-major
public float m[];

public mat4() {
//defaults to identity matrix
m = new float[16];
set(0,0,1);
set(1,1,1);
set(2,2,1);
set(3,3,1);
}

public void zero() {
for(int i = 0; i < 16; ++i) {
m[i] = 0;
}
}

public void set(int row, int col, float val) {
m[row*4+col] = val;
}

public float get(int row, int col) {
return m[row*4+col];
}

public boolean equals(mat4 other) {
for(int i = 0; i < 16; ++i) {
if(m[i] != other.m[i]) return false;
}
return true;
}
}

public static class MMath {
public static final float PI = 3.141592;
private MMath() {}

public static vec3 normalize(vec3 vec) {
float len = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
return new vec3(vec.x/len, vec.y/len, vec.z/len);
}

public static vec3 add(vec3 a, vec3 b) { // no operator overloading in java :(
return new vec3(a.x+b.x, a.y+b.y, a.z+b.z);
}

public static vec3 sub(vec3 a, vec3 b) {
return new vec3(a.x-b.x, a.y-b.y, a.z-b.z);
}

public static vec3 mul(vec3 a, float scalar) {
return new vec3(a.x*scalar, a.y*scalar, a.z*scalar);
}

public static float dot(vec3 a, vec3 b) {
return a.x*b.x + a.y*b.y + a.z*b.z;
}

public static vec3 cross(vec3 a, vec3 b) {
float x = a.y*b.z - a.z*b.y;
float y = a.z*b.x - a.x*b.z;
float z = a.x*b.y - a.y*b.x;
return new vec3(x, y, z);
}

public static float radians(float deg) {
return deg * MMath.PI / 180.0f;
}

public static mat4 perspective(float fov, float ratio, float near, float far) {
// impl identical to glm's perspective
// credit: https://github.com/g-truc/glm/blob/0.9.5/glm/gtc/matrix_transform.inl#L208
float halfTanFov = tan(fov/2);
mat4 result = new mat4();
result.zero();
result.set(0,0,1 / (ratio * halfTanFov));
result.set(1,1,1 / halfTanFov);
result.set(2,2,-(far + near) / (far - near));
result.set(2,3,-1);
result.set(3,2,-(2 * far * near) / (far - near));
return result;
}

public static mat4 lookAt(vec3 pos, vec3 center, vec3 up) {
// impl identical to glm's lookAt
// credit: https://github.com/g-truc/glm/blob/0.9.5/glm/gtc/matrix_transform.inl#L385
vec3 f = normalize(sub(center, pos));
vec3 s = normalize(cross(f, up));
vec3 u = cross(s, f);
mat4 result = new mat4();

result.set(0,0,s.x);
result.set(1,0,s.y);
result.set(2,0,s.z);
result.set(0,1,u.x);
result.set(1,1,u.y);
result.set(2,1,u.z);
result.set(0,2, -f.x);
result.set(1,2, -f.y);
result.set(2,2, -f.z);
result.set(3,0, -dot(s, pos));
result.set(3,1, -dot(u, pos));
result.set(3,2,dot(f, pos));
return result;
}

public static mat4 inverse(mat4 mat) {
// credit to David Moore, https://cgit.freedesktop.org/mesa/glu/tree/src/libutil/project.c
float det;
mat4 result = new mat4();

result.m[0] = mat.m[5]*mat.m[10]*mat.m[15] - mat.m[5]*mat.m[11]*mat.m[14] - mat.m[9]*mat.m[6]*mat.m[15]
+ mat.m[9]*mat.m[7]*mat.m[14] + mat.m[13]*mat.m[6]*mat.m[11] - mat.m[13]*mat.m[7]*mat.m[10];
result.m[4] = -mat.m[4]*mat.m[10]*mat.m[15] + mat.m[4]*mat.m[11]*mat.m[14] + mat.m[8]*mat.m[6]*mat.m[15]
- mat.m[8]*mat.m[7]*mat.m[14] - mat.m[12]*mat.m[6]*mat.m[11] + mat.m[12]*mat.m[7]*mat.m[10];
result.m[8] = mat.m[4]*mat.m[9]*mat.m[15] - mat.m[4]*mat.m[11]*mat.m[13] - mat.m[8]*mat.m[5]*mat.m[15]
+ mat.m[8]*mat.m[7]*mat.m[13] + mat.m[12]*mat.m[5]*mat.m[11] - mat.m[12]*mat.m[7]*mat.m[9];
result.m[12] = -mat.m[4]*mat.m[9]*mat.m[14] + mat.m[4]*mat.m[10]*mat.m[13] + mat.m[8]*mat.m[5]*mat.m[14]
- mat.m[8]*mat.m[6]*mat.m[13] - mat.m[12]*mat.m[5]*mat.m[10] + mat.m[12]*mat.m[6]*mat.m[9];
result.m[1] = -mat.m[1]*mat.m[10]*mat.m[15] + mat.m[1]*mat.m[11]*mat.m[14] + mat.m[9]*mat.m[2]*mat.m[15]
- mat.m[9]*mat.m[3]*mat.m[14] - mat.m[13]*mat.m[2]*mat.m[11] + mat.m[13]*mat.m[3]*mat.m[10];
result.m[5] = mat.m[0]*mat.m[10]*mat.m[15] - mat.m[0]*mat.m[11]*mat.m[14] - mat.m[8]*mat.m[2]*mat.m[15]
+ mat.m[8]*mat.m[3]*mat.m[14] + mat.m[12]*mat.m[2]*mat.m[11] - mat.m[12]*mat.m[3]*mat.m[10];
result.m[9] = -mat.m[0]*mat.m[9]*mat.m[15] + mat.m[0]*mat.m[11]*mat.m[13] + mat.m[8]*mat.m[1]*mat.m[15]
- mat.m[8]*mat.m[3]*mat.m[13] - mat.m[12]*mat.m[1]*mat.m[11] + mat.m[12]*mat.m[3]*mat.m[9];
result.m[13] = mat.m[0]*mat.m[9]*mat.m[14] - mat.m[0]*mat.m[10]*mat.m[13] - mat.m[8]*mat.m[1]*mat.m[14]
+ mat.m[8]*mat.m[2]*mat.m[13] + mat.m[12]*mat.m[1]*mat.m[10] - mat.m[12]*mat.m[2]*mat.m[9];
result.m[2] = mat.m[1]*mat.m[6]*mat.m[15] - mat.m[1]*mat.m[7]*mat.m[14] - mat.m[5]*mat.m[2]*mat.m[15]
+ mat.m[5]*mat.m[3]*mat.m[14] + mat.m[13]*mat.m[2]*mat.m[7] - mat.m[13]*mat.m[3]*mat.m[6];
result.m[6] = -mat.m[0]*mat.m[6]*mat.m[15] + mat.m[0]*mat.m[7]*mat.m[14] + mat.m[4]*mat.m[2]*mat.m[15]
- mat.m[4]*mat.m[3]*mat.m[14] - mat.m[12]*mat.m[2]*mat.m[7] + mat.m[12]*mat.m[3]*mat.m[6];
result.m[10] = mat.m[0]*mat.m[5]*mat.m[15] - mat.m[0]*mat.m[7]*mat.m[13] - mat.m[4]*mat.m[1]*mat.m[15]
+ mat.m[4]*mat.m[3]*mat.m[13] + mat.m[12]*mat.m[1]*mat.m[7] - mat.m[12]*mat.m[3]*mat.m[5];
result.m[14] = -mat.m[0]*mat.m[5]*mat.m[14] + mat.m[0]*mat.m[6]*mat.m[13] + mat.m[4]*mat.m[1]*mat.m[14]
- mat.m[4]*mat.m[2]*mat.m[13] - mat.m[12]*mat.m[1]*mat.m[6] + mat.m[12]*mat.m[2]*mat.m[5];
result.m[3] = -mat.m[1]*mat.m[6]*mat.m[11] + mat.m[1]*mat.m[7]*mat.m[10] + mat.m[5]*mat.m[2]*mat.m[11]
- mat.m[5]*mat.m[3]*mat.m[10] - mat.m[9]*mat.m[2]*mat.m[7] + mat.m[9]*mat.m[3]*mat.m[6];
result.m[7] = mat.m[0]*mat.m[6]*mat.m[11] - mat.m[0]*mat.m[7]*mat.m[10] - mat.m[4]*mat.m[2]*mat.m[11]
+ mat.m[4]*mat.m[3]*mat.m[10] + mat.m[8]*mat.m[2]*mat.m[7] - mat.m[8]*mat.m[3]*mat.m[6];
result.m[11] = -mat.m[0]*mat.m[5]*mat.m[11] + mat.m[0]*mat.m[7]*mat.m[9] + mat.m[4]*mat.m[1]*mat.m[11]
- mat.m[4]*mat.m[3]*mat.m[9] - mat.m[8]*mat.m[1]*mat.m[7] + mat.m[8]*mat.m[3]*mat.m[5];
result.m[15] = mat.m[0]*mat.m[5]*mat.m[10] - mat.m[0]*mat.m[6]*mat.m[9] - mat.m[4]*mat.m[1]*mat.m[10]
+ mat.m[4]*mat.m[2]*mat.m[9] + mat.m[8]*mat.m[1]*mat.m[6] - mat.m[8]*mat.m[2]*mat.m[5];

det = mat.m[0]*result.m[0] + mat.m[1]*result.m[4] + mat.m[2]*result.m[8] + mat.m[3]*result.m[12];
det = 1.0 / det;

if(det == 0) // no inv exists, just crash
throw new ArithmeticException("Inverse doesn't exist");

for (int i = 0; i < 16; i++)
result.m[i] *= det;

return result;
}
}
35 changes: 0 additions & 35 deletions Runner/Object.pde

This file was deleted.

1 change: 0 additions & 1 deletion Runner/Object2.pde

This file was deleted.

Loading