5 Commits

10 changed files with 221 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.class

View File

@@ -26,7 +26,7 @@ public class Main {
} }
else if (Objects.equals(args[p], "--offline")) { else if (Objects.equals(args[p], "--offline")) {
allow_networking = false; allow_networking = false;
p += 2; p += 1;
} }
else { else {
matrix_file_name = args[p++]; matrix_file_name = args[p++];
@@ -67,6 +67,7 @@ public class Main {
System.out.println("Original matrix:"); System.out.println("Original matrix:");
Matrix m = new Matrix(); Matrix m = new Matrix();
m.init(raw_m); m.init(raw_m);
m.print();
// transposing // transposing
m.transpose(); m.transpose();
@@ -89,6 +90,7 @@ public class Main {
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("[ERROR] Failed to fetch resource " + output_filename + "from " + remote_url + " due to the following exception: " + e); System.out.println("[ERROR] Failed to fetch resource " + output_filename + "from " + remote_url + " due to the following exception: " + e);
System.exit(1);
} }
} }
@@ -99,8 +101,8 @@ public class Main {
if (!help_file.exists()) { if (!help_file.exists()) {
System.err.println("[WARN] Help file is missing."); System.err.println("[WARN] Help file is missing.");
if (allow_net) { if (allow_net) {
System.err.println("[INFO] Trying to recover it from the git server"); System.err.println("[INFO] Trying to recover it from the CDN server");
fetchResource("http://lab2.kpi.dev:3000/dymik739/oop-labs-collection/raw/branch/lab2-dev/labs/2/src/help.txt", "src/help.txt"); fetchResource("http://lab2.kpi.dev:16554/help.txt", "src/help.txt");
} else { } else {
System.err.println("[INFO] Networking is disabled, not recovering"); System.err.println("[INFO] Networking is disabled, not recovering");
System.exit(1); System.exit(1);

View File

@@ -19,7 +19,6 @@ public class Matrix {
} }
public void transpose() { public void transpose() {
print();
if (this.m[0].length != this.m.length) { if (this.m[0].length != this.m.length) {
int new_h = this.m[0].length; int new_h = this.m[0].length;
int new_w = this.m.length; int new_w = this.m.length;

5
labs/3/Main.java Normal file
View File

@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
}
}

3
labs/3/build.sh Normal file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
javac -d "renderSuite-classes" renderSuite/*

View File

@@ -0,0 +1,66 @@
package renderSuite;
import renderSuite.Vec3;
import renderSuite.Ray;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public class Camera {
private Vec3 p, f;
private double fov;
private int sizeX, sizeY;
public Camera(Vec3 position, Vec3 facing, double fov, int sizeX, int sizeY) {
this.p = position;
this.f = facing;
this.fov = fov;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
public void main() {
}
public void renderImageTo(String filename, Surface[] surfs) {
BufferedImage bi = new BufferedImage(this.sizeX, this.sizeY, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < this.sizeX; x++) {
for (int y = 0; y < this.sizeY; y++) {
Vec3 result = probe(x, y);
int color = ( (int)result.x ) << 16 | ( (int)result.y ) << 8 | ( (int)result.z );
bi.setRGB(x, y, color);
}
}
File f = new File(filename + ".png");
ImageIO.write(bi, "PNG", f);
}
public Vec3 probe(int x, int y) {
Vec3 result = new Vec3(0, 0, 0);
// creating ray to use as a probing object
Ray ray = new Ray(this.p.copy(), this.f.copy().get_norm());
// adjust ray facing
double offsetX = ((x/this.sizeX) - 0.5) * 2;
ray.rotate('y', offsetX * this.fov);
double offsetY = ((y/this.sizeY) - 0.5) * 2;
ray.rotate('x', offsetY * this.fov);
// check collisions with every object on scene
for (int i = 0; i < surfs.length; i++) {
if (surfs[i].intersect(ray)) {
return surfs[i].color;
}
}
// if no collision detected, return black pixel
return new Vec3(0, 0, 0);
}
}

View File

@@ -0,0 +1,40 @@
package renderSuite;
import java.lang.Math;
import renderSuite.Vec3;
public class Ray {
// position, facing
Vec3 p, f;
public Ray(Vec3 position, Vec3 facing) {
this.p = position;
this.f = facing;
}
public void main() {}
public void rotate(char axis, double angle) {
if (axis == 'x') {
this.f = new Vec3(this.f.x,
this.f.y*Math.cos(angle) - this.f.z*Math.sin(angle),
this.f.y*Math.sin(angle) + this.f.z*Math.cos(angle));
} else if (axis == 'y') {
this.f = new Vec3(this.f.x*Math.cos(angle) + this.f.z*Math.sin(angle),
this.f.y,
this.f.z*Math.cos(angle) - this.f.x*Math.sin(angle));
}
}
public void move(Vec3 mv) {
this.p.x += mv.x;
this.p.y += mv.y;
this.p.z += mv.z;
}
public void step(double l) {
this.p.x += this.f.x * l;
this.p.y += this.f.y * l;
this.p.z += this.f.z * l;
}
}

View File

@@ -0,0 +1,44 @@
package renderSuite;
import renderSuite.Vec3;
import renderSuite.Util;
import renderSuite.Ray;
public class Sphere {
Vec3 p, c;
double r;
public Sphere(Vec3 position, Vec3 color, double radius) {
this.p = position;
this.c = color;
this.r = radius;
}
public void main() {
}
public boolean intersect(Ray r) {
// OLD
//double dist = Util.d(ray_position, position);
//return (Util.d(ray_position, position) <= radius);
// using RTX
Vec3 l = new Vec3(this.p.x - r.p.x,
this.p.y - r.p.y,
this.p.z - r.p.z);
Vec3 nl = l.get_norm();
double cosine = Util.dot(r.f, nl);
// >90 degrees = no intersection
if (cosine < 0) {
return false;
}
double tc = l.len() * cosine;
double d = Math.sqrt( l.len()*l.len() - tc*tc );
return (d < this.r);
}
}

View File

@@ -0,0 +1,17 @@
package renderSuite;
import java.lang.Math;
import renderSuite.Vec3;
public class Util {
// measure distance between two points
public static double d(Vec3 p1, Vec3 p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x)
+ (p1.y - p2.y) * (p1.y - p2.y)
+ (p1.z - p2.z) * (p1.z - p2.z));
}
public static double dot(Vec3 v1, Vec3 v2) {
return Math.sqrt(v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
}
}

View File

@@ -0,0 +1,40 @@
package renderSuite;
import java.lang.Math;
public class Vec3 {
public double x, y, z;
public Vec3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void main(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double len() {
return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
}
public void normalize() {
double l = len();
this.x /= l;
this.y /= l;
this.z /= l;
}
public Vec3 get_norm() {
double l = len();
return new Vec3(this.x / l, this.y / l, this.z / l);
}
public Vec3 copy() {
return new Vec3(this.x, this.y, this.z);
}
}