Skip to content

Commit 848bc7d

Browse files
committed
commit of the code for the year 2014-2015
0 parents  commit 848bc7d

18 files changed

Lines changed: 2659 additions & 0 deletions

Makefile

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
################################################################################
2+
#
3+
# Makefile for the basic Computer Graphics source code
4+
#
5+
# The following lines are the only files that you as a student should edit.
6+
#
7+
################################################################################
8+
9+
# specify the directory where the source code can be found.
10+
SOURCEDIR = src
11+
12+
# specify the name of the executable jar file.
13+
EXECUTABLENAME = cgproject.jar
14+
15+
# specify the name of the class containing main method.
16+
ENTRYPOINT = main.Renderer
17+
18+
# specify the packages where the code can be found
19+
PACKAGES = camera gui main math sampling shape
20+
21+
################################################################################
22+
# Only the code above this line has to be edited if more classes are added #
23+
################################################################################
24+
25+
JAVAC = javac
26+
JFLAGS = -g -d $(SOURCEDIR) -classpath $(SOURCEDIR)
27+
JAR = jar
28+
29+
########################
30+
# default build target #
31+
########################
32+
33+
default: all
34+
35+
######################
36+
# target to make all #
37+
######################
38+
39+
all: clean classes jar
40+
41+
###########################################################################
42+
# target which compiles all the .java files specified in the SOURCES list #
43+
###########################################################################
44+
45+
classes: SOURCES := $(foreach dir,$(PACKAGES),$(wildcard $(SOURCEDIR)/$(dir)/*.java))
46+
classes:
47+
@echo -e "\nsearching .java files from the following packages:"
48+
@echo -e "$(foreach package,$(PACKAGES),\n\t$(package))"
49+
@echo -e "\ncompiling the following .java classes:"
50+
@echo -e "$(foreach java,$(SOURCES),\n\t$(java))"
51+
@$(JAVAC) $(JFLAGS) $(SOURCES)
52+
@echo -e "\nfinished compilation"
53+
54+
#############################################################################
55+
# Creates an executable JAR file from the classes in the SOURCEDIR with the #
56+
# ENTRYPOINT class as the class containing the main method #
57+
#############################################################################
58+
59+
jar:
60+
@echo -e "\nbuilding $(EXECUTABLENAME)"
61+
@$(JAR) -cfe $(EXECUTABLENAME) $(ENTRYPOINT) -C $(SOURCEDIR)/ .
62+
@echo -e "finished building $(EXECUTABLENAME)"
63+
64+
##########################################
65+
# removes all the generated .class files #
66+
##########################################
67+
68+
cleanclasses: CLASSFILES := $(foreach filename,$(foreach dir,$(PACKAGES),$(wildcard $(SOURCEDIR)/$(dir)/*.class)),$(filename))
69+
cleanclasses:
70+
@echo -e "\nremoving the following .class files:"
71+
@echo -e $(foreach filename,${CLASSFILES},'\n\t$(filename)')
72+
@$(foreach filename,${CLASSFILES},rm -f '$(filename)')
73+
74+
################################################
75+
# Removes the BINARYDIR and the executable JAR #
76+
################################################
77+
clean: cleanclasses
78+
@rm -f $(EXECUTABLENAME)
79+

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# cgpracticum
2+
Basis source code of a ray tracer for students to use for the Computer Graphics course.

src/camera/Camera.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package camera;
2+
3+
import math.Ray;
4+
import sampling.Sample;
5+
6+
/**
7+
* Interface which all {@link Camera} subclasses should implement.
8+
*
9+
* @author Niels Billen
10+
* @version 1.0
11+
*/
12+
public interface Camera {
13+
/**
14+
* Generates a new {@link Ray} from the given {@link Sample}.
15+
*
16+
* @param sample
17+
* sample to construct the {@link Ray} from.
18+
* @throws NullPointerException
19+
* when the given {@link Sample} is null.
20+
* @return a new {@link Ray} from the given {@link Sample}.
21+
*/
22+
public Ray generateRay(Sample sample) throws NullPointerException;
23+
}

src/camera/PerspectiveCamera.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package camera;
2+
3+
import math.OrthonormalBasis;
4+
import math.Point;
5+
import math.Ray;
6+
import math.Vector;
7+
import sampling.Sample;
8+
9+
/**
10+
* Implementation of a perspective {@link Camera}.
11+
*
12+
* @author Niels Billen
13+
* @version 1.0
14+
*/
15+
public class PerspectiveCamera implements Camera {
16+
private final int xResolution;
17+
private final int yResolution;
18+
private final Point origin;
19+
private final OrthonormalBasis basis;
20+
21+
private final double width;
22+
private final double height;
23+
24+
/**
25+
* Creates a new {@link PerspectiveCamera} for an image with the given
26+
* resolution, at the given position, looking into the given direction with
27+
* the given up vector as the up direction. The field of view parameter
28+
* specifies the horizontal field of view in degrees.
29+
*
30+
* @param xResolution
31+
* x resolution of the image this camera is for.
32+
* @param yResolution
33+
* y resolution of the image this camera is for.
34+
* @param origin
35+
* origin of the camera.
36+
* @param lookat
37+
* direction of the camera.
38+
* @param up
39+
* up vector.
40+
* @param fov
41+
* horizontal field of view (in degrees).
42+
* @throws NullPointerException
43+
* when the origin, look at or up vector is null.
44+
* @throws IllegalArgumentException
45+
* when the given horizontal or vertical resolution is smaller
46+
* than one.
47+
* @throws IllegalArgumentException
48+
* when the field of view is smaller than or equal to zero.
49+
* @throws IllegalArgumentException
50+
* when the field of view is larger than or equal to pi (180
51+
* degrees).
52+
*/
53+
public PerspectiveCamera(int xResolution, int yResolution, Point origin,
54+
Vector lookat, Vector up, double fov) throws NullPointerException,
55+
IllegalArgumentException {
56+
if (xResolution < 1)
57+
throw new IllegalArgumentException("the horizontal resolution "
58+
+ "cannot be smaller than one!");
59+
if (yResolution < 1)
60+
throw new IllegalArgumentException("the vertical resolution "
61+
+ "cannot be smaller than one!");
62+
if (fov <= 0)
63+
throw new IllegalArgumentException("the field of view cannot be "
64+
+ "smaller than or equal to zero degrees!");
65+
if (fov >= 180)
66+
throw new IllegalArgumentException("the field of view cannot be "
67+
+ "larger than or equal to 180 degrees!");
68+
69+
this.xResolution = xResolution;
70+
this.yResolution = yResolution;
71+
this.origin = origin;
72+
this.basis = new OrthonormalBasis(lookat, up);
73+
74+
width = 2.0 * Math.tan(0.5 * Math.toRadians(fov));
75+
height = ((double) yResolution * width) / (double) xResolution;
76+
}
77+
78+
/*
79+
* (non-Javadoc)
80+
*
81+
* @see camera.Camera#generateRay(sampling.Sample)
82+
*/
83+
public Ray generateRay(Sample sample) throws NullPointerException {
84+
double u = width * (sample.x / (double) xResolution - 0.5);
85+
double v = height * (sample.y / (double) yResolution - 0.5);
86+
87+
Vector direction = basis.w.add(basis.u.scale(u).add(basis.v.scale(v)));
88+
89+
return new Ray(origin, direction);
90+
}
91+
}

0 commit comments

Comments
 (0)