style: r/badcode jpp

This commit is contained in:
Laureηt 2022-04-14 22:42:57 +02:00
parent bb1aea52b2
commit 163c93666b
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
13 changed files with 240 additions and 264 deletions

View file

@ -44,11 +44,12 @@ public class GraphicsWrapper {
private ImageComponent drawComp = null; private ImageComponent drawComp = null;
private BufferedImage backBuffer = null; // private BufferedImage backBuffer = null;
private BufferedImage frontBuffer = null; private BufferedImage frontBuffer = null;
private void init() { private void init() {
backBuffer = new BufferedImage(width * pixelSize, height * pixelSize, BufferedImage.TYPE_INT_ARGB); // backBuffer = new BufferedImage(width * pixelSize, height * pixelSize,
// BufferedImage.TYPE_INT_ARGB);
frontBuffer = new BufferedImage(width * pixelSize, height * pixelSize, BufferedImage.TYPE_3BYTE_BGR); frontBuffer = new BufferedImage(width * pixelSize, height * pixelSize, BufferedImage.TYPE_3BYTE_BGR);
@ -204,6 +205,10 @@ public class GraphicsWrapper {
myFrame.repaint(); myFrame.repaint();
} }
public void repaint() {
myFrame.repaint();
}
/** /**
* Destroy window. * Destroy window.
*/ */

View file

@ -1,4 +1,3 @@
import java.lang.*;
import java.util.*; import java.util.*;
import algebra.*; import algebra.*;

View file

@ -1,30 +1,27 @@
import algebra.*;
import java.awt.*;
/** /**
* Simple shader that just copy the interpolated color to the screen, * Simple shader that just copy the interpolated color to the screen,
* taking the depth of the fragment into acount. * taking the depth of the fragment into acount.
*
* @author: cdehais * @author: cdehais
*/ */
public class PainterShader extends Shader { public class PainterShader extends Shader {
DepthBuffer depth; DepthBuffer depth;
public PainterShader (GraphicsWrapper screen) { public PainterShader(GraphicsWrapper screen) {
super (screen); super(screen);
depth = new DepthBuffer (screen.getWidth (), screen.getHeight ()); depth = new DepthBuffer(screen.getWidth(), screen.getHeight());
} }
public void shade (Fragment fragment) { public void shade(Fragment fragment) {
if (depth.testFragment (fragment)) { if (depth.testFragment(fragment)) {
screen.setPixel (fragment.getX (), fragment.getY (), fragment.getColor ()); screen.setPixel(fragment.getX(), fragment.getY(), fragment.getColor());
depth.writeFragment (fragment); depth.writeFragment(fragment);
} }
} }
public void reset () { public void reset() {
depth.clear (); depth.clear();
} }
} }

View file

@ -1,62 +1,62 @@
import algebra.*; import algebra.*;
import java.lang.Math.*;
/** /**
* The PerspectiveCorrectRasterizer class extends Rasterizer to perform Persepctive Correct interpolation * The PerspectiveCorrectRasterizer class extends Rasterizer to perform
* Persepctive Correct interpolation
* of attributes. * of attributes.
* *
* @author cdehais * @author cdehais
*/ */
public class PerspectiveCorrectRasterizer extends Rasterizer { public class PerspectiveCorrectRasterizer extends Rasterizer {
public PerspectiveCorrectRasterizer (Shader shader) { public PerspectiveCorrectRasterizer(Shader shader) {
super(shader); super(shader);
} }
/** /**
* Rasterizes the triangular face made of the Fragment v1, v2 and v3 * Rasterizes the triangular face made of the Fragment v1, v2 and v3
*/ */
public void rasterizeFace (Fragment v1, Fragment v2, Fragment v3) { public void rasterizeFace(Fragment v1, Fragment v2, Fragment v3) {
Matrix C = makeBarycentricCoordsMatrix (v1, v2, v3); Matrix C = makeBarycentricCoordsMatrix(v1, v2, v3);
/* iterate over the triangle's bounding box */ /* iterate over the triangle's bounding box */
int xmin = Math.min (v1.getX (), Math.min (v2.getX (), v3.getX ())); int xmin = Math.min(v1.getX(), Math.min(v2.getX(), v3.getX()));
int ymin = Math.min (v1.getY (), Math.min (v2.getY (), v3.getY ())); int ymin = Math.min(v1.getY(), Math.min(v2.getY(), v3.getY()));
int xmax = Math.max (v1.getX (), Math.max (v2.getX (), v3.getX ())); int xmax = Math.max(v1.getX(), Math.max(v2.getX(), v3.getX()));
int ymax = Math.max (v1.getY (), Math.max (v2.getY (), v3.getY ())); int ymax = Math.max(v1.getY(), Math.max(v2.getY(), v3.getY()));
Fragment fragment = new Fragment (0, 0); Fragment fragment = new Fragment(0, 0);
int numAttributes = fragment.getNumAttributes (); int numAttributes = fragment.getNumAttributes();
try { try {
for (int x = xmin; x <= xmax; x++) { for (int x = xmin; x <= xmax; x++) {
for (int y = ymin; y <= ymax; y++) { for (int y = ymin; y <= ymax; y++) {
/* setup position now to allow early clipping */
fragment.setPosition (x, y);
if (!shader.isClipped (fragment)) {
Vector3 v = new Vector3 (1.0, (double)x, (double)y); /* setup position now to allow early clipping */
Vector bar = C.multiply (v); fragment.setPosition(x, y);
if ((bar.get (0) >= 0.0) && (bar.get (1) >= 0.0) && (bar.get (2) >= 0.0)) { if (!shader.isClipped(fragment)) {
double oneOverZ = bar.get (0) / v1.getDepth () +
bar.get (1) / v2.getDepth () +
bar.get (2) / v3.getDepth ();
for (int i = 0; i < numAttributes; i++) {
double aOverZ = bar.get (0) * v1.getAttribute (i) / v1.getDepth () +
bar.get (1) * v2.getAttribute (i) / v2.getDepth () +
bar.get (2) * v3.getAttribute (i) / v3.getDepth ();
fragment.setAttribute (i, aOverZ / oneOverZ); Vector3 v = new Vector3(1.0, (double) x, (double) y);
Vector bar = C.multiply(v);
if ((bar.get(0) >= 0.0) && (bar.get(1) >= 0.0) && (bar.get(2) >= 0.0)) {
double oneOverZ = bar.get(0) / v1.getDepth() +
bar.get(1) / v2.getDepth() +
bar.get(2) / v3.getDepth();
for (int i = 0; i < numAttributes; i++) {
double aOverZ = bar.get(0) * v1.getAttribute(i) / v1.getDepth() +
bar.get(1) * v2.getAttribute(i) / v2.getDepth() +
bar.get(2) * v3.getAttribute(i) / v3.getDepth();
fragment.setAttribute(i, aOverZ / oneOverZ);
}
shader.shade(fragment);
} }
shader.shade (fragment);
} }
} }
} }
}
} catch (SizeMismatchException e) { } catch (SizeMismatchException e) {
/* should not reach */ /* should not reach */
e.printStackTrace (); e.printStackTrace();
} }
} }
} }

View file

@ -1,6 +1,5 @@
import algebra.*; import algebra.*;
import java.lang.Math.*;
/** /**
* The Rasterizer class is responsible for the discretization of geometric * The Rasterizer class is responsible for the discretization of geometric
@ -85,7 +84,6 @@ public class Rasterizer {
// } // }
// tracé d'un segment avec l'algo de Bresenham // tracé d'un segment avec l'algo de Bresenham
int numAttributes = v1.getNumAttributes();
Fragment fragment = new Fragment(0, 0); // , numAttributes); Fragment fragment = new Fragment(0, 0); // , numAttributes);
boolean sym = (Math.abs(y2 - y1) > Math.abs(x2 - x1)); boolean sym = (Math.abs(y2 - y1) > Math.abs(x2 - x1));
@ -129,9 +127,8 @@ public class Rasterizer {
for (int i = 1; i <= delta_x; i++) { for (int i = 1; i <= delta_x; i++) {
x = x + 1; x = x + 1;
y_courant = y_courant + m; y_courant = y_courant + m;
if ((ystep == 1) && (y_courant < y + 0.5) || ((ystep == -1) && (y_courant > y if ((ystep == 1) && (y_courant < y + 0.5) || ((ystep == -1) && (y_courant > y - 0.5))) {
- 0.5))) { y = y; // dafuk ?
y = y;
} else { } else {
y = y + ystep; y = y + ystep;
} }

View file

@ -98,7 +98,7 @@ public class Renderer {
Fragment v1 = fragment[faces[i + j]]; Fragment v1 = fragment[faces[i + j]];
Fragment v2 = fragment[faces[i + ((j + 1) % 3)]]; Fragment v2 = fragment[faces[i + ((j + 1) % 3)]];
rasterizer.rasterizeEdge(v1, v2); rasterizer.rasterizeEdge(v1, v2);
screen.swapBuffers(); // on repaint juste screen.repaint(); // on refresh l'écran
} }
} }
} }
@ -111,9 +111,8 @@ public class Renderer {
Fragment v1 = fragments[faces[i]]; Fragment v1 = fragments[faces[i]];
Fragment v2 = fragments[faces[i + 1]]; Fragment v2 = fragments[faces[i + 1]];
Fragment v3 = fragments[faces[i + 2]]; Fragment v3 = fragments[faces[i + 2]];
rasterizer.rasterizeFace(v1, v2, v3); rasterizer.rasterizeFace(v1, v2, v3);
screen.swapBuffers(); // on repaint juste screen.repaint(); // on refresh l'écran
} }
} }

View file

@ -1,21 +1,18 @@
import algebra.*;
import java.awt.*;
/** /**
* Simple shader that just copy the interpolated color to the screen. * Simple shader that just copy the interpolated color to the screen.
*
* @author: cdehais * @author: cdehais
*/ */
public class SimpleShader extends Shader { public class SimpleShader extends Shader {
public SimpleShader (GraphicsWrapper screen) { public SimpleShader(GraphicsWrapper screen) {
super (screen); super(screen);
} }
public void shade (Fragment fragment) { public void shade(Fragment fragment) {
//System.out.println (fragment.getX () + "," + fragment.getY ()); // System.out.println (fragment.getX () + "," + fragment.getY ());
//System.out.println ("color " + fragment.getColor ()); // System.out.println ("color " + fragment.getColor ());
screen.setPixel (fragment.getX (), fragment.getY (), fragment.getColor ()); screen.setPixel(fragment.getX(), fragment.getY(), fragment.getColor());
} }
} }

View file

@ -5,126 +5,124 @@ public class TestAlgebra {
public static void test() throws Exception { public static void test() throws Exception {
System.out.println ("Algebra\n# Test Start"); System.out.println("Algebra\n# Test Start");
Vector v = new Vector (3); Vector v = new Vector(3);
Vector v1 = new Vector (1); Vector v1 = new Vector(1);
v1.setName ("up"); v1.setName("up");
try { try {
Vector v0 = new Vector (0); new Vector(0);
} catch (Exception e) { } catch (Exception e) {
System.out.println ("Wrong size exception caught OK"); System.out.println("Wrong size exception caught OK");
} }
try { try {
Vector v0 = new Vector ("named", 0); new Vector("named", 0);
} catch (Exception e) { } catch (Exception e) {
System.out.println ("Wrong size exception caught OK"); System.out.println("Wrong size exception caught OK");
} }
System.out.println (v); System.out.println(v);
System.out.println (v1); System.out.println(v1);
Vector u1 = new Vector ("u1", 3); Vector u1 = new Vector("u1", 3);
u1.set(0, 1.0); u1.set(0, 1.0);
u1.set(1, 2.0); u1.set(1, 2.0);
u1.set(2, 3.0); u1.set(2, 3.0);
Vector u2 = new Vector (3); Vector u2 = new Vector(3);
u2.set (new double[3]); u2.set(new double[3]);
u2.set(1, 2.0); u2.set(1, 2.0);
System.out.println (u1); System.out.println(u1);
System.out.println ("norm(u1) = " + u1.norm()); System.out.println("norm(u1) = " + u1.norm());
System.out.println (u2); System.out.println(u2);
System.out.println ("u1.u2 = " + u1.dot (u2)); System.out.println("u1.u2 = " + u1.dot(u2));
try { try {
u1 = new Vector ("u1", 3); u1 = new Vector("u1", 3);
u2 = new Vector ("u2", 4); u2 = new Vector("u2", 4);
u1.dot (u2); u1.dot(u2);
} catch (SizeMismatchException e) { } catch (SizeMismatchException e) {
System.out.println ("Caught exception: " + e); System.out.println("Caught exception: " + e);
} }
Vector3 r1 = new Vector3 ("u1", 1.0, 2.0, 3.0); Vector3 r1 = new Vector3("u1", 1.0, 2.0, 3.0);
Vector3 r2 = new Vector3 ("u2", 1.0, 3.0, 0.0); Vector3 r2 = new Vector3("u2", 1.0, 3.0, 0.0);
Vector3 r = r1.cross (r2); Vector3 r = r1.cross(r2);
System.out.println (r1);
System.out.println (r2);
System.out.println ("r1.r2 = " + r1.dot (r2));
System.out.println ("r1.r2 = " + r1.dot ((Vector) r2) + " (as Vector)");
System.out.println ("r1 x r2 = " + r);
System.out.println ("norm(" + r.getName() + ") = " + r.norm());
r.normalize ();
System.out.println ("norm(" + r.getName() + ") (after ::normalize()) = " + r.norm());
System.out.println(r1);
System.out.println(r2);
System.out.println("r1.r2 = " + r1.dot(r2));
System.out.println("r1.r2 = " + r1.dot((Vector) r2) + " (as Vector)");
System.out.println("r1 x r2 = " + r);
System.out.println("norm(" + r.getName() + ") = " + r.norm());
r.normalize();
System.out.println("norm(" + r.getName() + ") (after ::normalize()) = " + r.norm());
//------------------------------------- // -------------------------------------
System.out.println ("--\n-- Matrix tests\n--"); System.out.println("--\n-- Matrix tests\n--");
Matrix M = new Matrix (3,5); Matrix M = new Matrix(3, 5);
System.out.println (M); System.out.println(M);
Matrix M1 = new Matrix ("M1", 2, 5); Matrix M1 = new Matrix("M1", 2, 5);
M1.set (0, 0, 2.0); M1.set(0, 0, 2.0);
M1.set (1, 4, -8.0); M1.set(1, 4, -8.0);
M1.set (1, 2, 5.0); M1.set(1, 2, 5.0);
try { try {
Matrix M2 = new Matrix ("M2", 0, 2); new Matrix("M2", 0, 2);
} catch (Exception e) { } catch (Exception e) {
System.out.println ("Wrong size exception caught OK"); System.out.println("Wrong size exception caught OK");
} }
try { try {
M.multiply (M1); M.multiply(M1);
} catch (SizeMismatchException e) { } catch (SizeMismatchException e) {
System.out.println ("Caught exception: " + e); System.out.println("Caught exception: " + e);
} }
Matrix M2 = Matrix.createIdentity(5);
System.out.println(M2);
Matrix M2 = Matrix.createIdentity (5); M = M1.multiply(M2);
System.out.println (M2); System.out.println(M1);
System.out.println(M2);
M = M1.multiply (M2); System.out.println(M);
System.out.println (M1);
System.out.println (M2);
System.out.println (M);
M1 = M1.transpose(); M1 = M1.transpose();
M1.name = "M1'"; M1.name = "M1'";
System.out.println (M1); System.out.println(M1);
Vector u = new Vector ("u", 5); Vector u = new Vector("u", 5);
u.ones(); u.ones();
v = M.multiply (u); v = M.multiply(u);
System.out.println (M); System.out.println(M);
System.out.println (u); System.out.println(u);
System.out.println ("M.u = " + v); System.out.println("M.u = " + v);
try { try {
u = new Vector ("u", 3); u = new Vector("u", 3);
v = M.multiply (u); v = M.multiply(u);
} catch (SizeMismatchException e) { } catch (SizeMismatchException e) {
System.out.println ("Caught exception: " + e); System.out.println("Caught exception: " + e);
} }
Matrix I = Matrix.createIdentity (5); Matrix I = Matrix.createIdentity(5);
Matrix S = I.getSubMatrix (0, 2, 3, 3); Matrix S = I.getSubMatrix(0, 2, 3, 3);
System.out.println (I); System.out.println(I);
System.out.println ("submatrix:\n" + S); System.out.println("submatrix:\n" + S);
}
public static void main(String[] args) {
try {
test() ;
System.out.println ("SUCCESS.") ;
} catch (Exception e) {
System.out.println ("FAIL: " + e) ;
e.printStackTrace () ;
} }
} public static void main(String[] args) {
try {
test();
System.out.println("SUCCESS.");
} catch (Exception e) {
System.out.println("FAIL: " + e);
e.printStackTrace();
}
}
} }

View file

@ -1,7 +1,5 @@
import java.util.*;
import java.awt.*; import java.awt.*;
import java.lang.Math.*;
public class TestGraphicWrapper { public class TestGraphicWrapper {
@ -9,24 +7,24 @@ public class TestGraphicWrapper {
static int height = 128; static int height = 128;
static GraphicsWrapper screen; static GraphicsWrapper screen;
private static void checker (int polarity, double r, double g, double b) { private static void checker(int polarity, double r, double g, double b) {
screen.clearBuffer(); screen.clearBuffer();
for (int i = 0; i < height; i++) { for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) { for (int j = 0; j < width; j++) {
if (((i / 8) % 2 == 1) == ((j / 8) % 2 == polarity)) { if (((i / 8) % 2 == 1) == ((j / 8) % 2 == polarity)) {
screen.setPixel (j, i, r, g, b); screen.setPixel(j, i, r, g, b);
} }
} }
} }
} }
static void init () { static void init() {
screen = new GraphicsWrapper (width, height, 1); screen = new GraphicsWrapper(width, height, 1);
} }
static int countNeighbours (int x0, int y0) { static int countNeighbours(int x0, int y0) {
int count = 0; int count = 0;
for (int y = y0 - 1; y <= y0 + 1; y++) { for (int y = y0 - 1; y <= y0 + 1; y++) {
@ -34,108 +32,106 @@ public class TestGraphicWrapper {
if ((x != x0) || (y != y0)) { if ((x != x0) || (y != y0)) {
int _x = x % width; int _x = x % width;
int _y = y % height; int _y = y % height;
Color pix = screen.getFrontPixel (_x, _y); Color pix = screen.getFrontPixel(_x, _y);
if (pix.getRed () != 0) { if (pix.getRed() != 0) {
count++; count++;
} }
} }
} }
} }
return count; return count;
} }
static void evolve () { static void evolve() {
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
int c = countNeighbours (x, y); int c = countNeighbours(x, y);
Color pix = screen.getFrontPixel (x, y); Color pix = screen.getFrontPixel(x, y);
Color bpix = screen.getPixel (x, y); if (pix.getRed() == 0) {
if (pix.getRed () == 0) { // System.out.println (x + " " + y + " : dead (" + c + " nbrs) "
//System.out.println (x + " " + y + " : dead (" + c + " nbrs) " // + ((bpix.getRed () == 0) ? "dead" : "alive")
// + ((bpix.getRed () == 0) ? "dead" : "alive") // );
// );
if (c == 3) { if (c == 3) {
/* born */ /* born */
screen.setPixel (x, y, 255, 255, 255); screen.setPixel(x, y, 255, 255, 255);
} else { } else {
screen.setPixel (x, y, 0, 0, 0); screen.setPixel(x, y, 0, 0, 0);
} }
} else { } else {
//System.out.println (x + " " + y + " : alive (" + c + " nbrs) " // System.out.println (x + " " + y + " : alive (" + c + " nbrs) "
// + ((bpix.getRed() == 0) ? "dead" : "alive")); // + ((bpix.getRed() == 0) ? "dead" : "alive"));
if ((c >= 2) && (c <= 3)) { if ((c >= 2) && (c <= 3)) {
/* survive */ /* survive */
screen.setPixel (x, y, 255, 255, 255); screen.setPixel(x, y, 255, 255, 255);
} else { } else {
/* die */ /* die */
screen.setPixel (x, y, 0, 0, 0); screen.setPixel(x, y, 0, 0, 0);
} }
} }
} }
} }
} }
public static void testChecker () throws Exception { public static void testChecker() throws Exception {
for (int k = 0; k < 10; k++) { for (int k = 0; k < 10; k++) {
checker (1, 1.0, 1.0, 1.0); checker(1, 1.0, 1.0, 1.0);
screen.swapBuffers (); screen.swapBuffers();
Thread.sleep (100); Thread.sleep(100);
checker (0, 1.0, 1.0, 1.0); checker(0, 1.0, 1.0, 1.0);
screen.swapBuffers (); screen.swapBuffers();
Thread.sleep (100); Thread.sleep(100);
} }
} }
public static void testConway () throws Exception { public static void testConway() throws Exception {
screen.clearBuffer ();
//screen.swapBuffers ();
//screen.clearBuffer ();
//screen.swapBuffers ();
/*
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (Math.random() < 0.3) {
screen.setPixel (x, y, 255, 255, 255);
}
}
}
*/
screen.setPixel (20, 10, 255, 255, 255);
screen.setPixel (21, 11, 255, 255, 255);
screen.setPixel (19, 12, 255, 255, 255);
screen.setPixel (20, 12, 255, 255, 255);
screen.setPixel (21, 12, 255, 255, 255);
screen.swapBuffers (); screen.clearBuffer();
Thread.sleep (1000); // screen.swapBuffers ();
// screen.clearBuffer ();
// screen.swapBuffers ();
/*
* for (int y = 0; y < height; y++) {
* for (int x = 0; x < width; x++) {
* if (Math.random() < 0.3) {
* screen.setPixel (x, y, 255, 255, 255);
* }
* }
* }
*/
screen.setPixel(20, 10, 255, 255, 255);
screen.setPixel(21, 11, 255, 255, 255);
screen.setPixel(19, 12, 255, 255, 255);
screen.setPixel(20, 12, 255, 255, 255);
screen.setPixel(21, 12, 255, 255, 255);
screen.swapBuffers();
Thread.sleep(1000);
for (int k = 0; k < 100; k++) { for (int k = 0; k < 100; k++) {
evolve (); evolve();
screen.swapBuffers (); screen.swapBuffers();
Thread.sleep (30); Thread.sleep(30);
} }
screen.destroy() ; screen.destroy();
System.exit(0); System.exit(0);
} }
public static void main(String[] args) { public static void main(String[] args) {
try { try {
init (); init();
//testChecker () ; // testChecker () ;
testConway () ; testConway();
} catch (Exception e) { } catch (Exception e) {
System.out.println("EXCEPTION: " + e) ; System.out.println("EXCEPTION: " + e);
e.printStackTrace() ; e.printStackTrace();
} }
} }
} }

View file

@ -1,21 +1,20 @@
import algebra.*;
/** /**
* Test class for the Rasterizer class * Test class for the Rasterizer class
*
* @author: cdehais * @author: cdehais
*/ */
public class TestRasterizer { public class TestRasterizer {
static class TestShader extends Shader { static class TestShader extends Shader {
public TestShader (GraphicsWrapper screen) { public TestShader(GraphicsWrapper screen) {
super (screen); super(screen);
} }
public void shade (Fragment fragment) { public void shade(Fragment fragment) {
System.out.println (" fragment: (" + fragment.getX () + ", " + fragment.getY () + ")" System.out.println(" fragment: (" + fragment.getX() + ", " + fragment.getY() + ")"
+ " - color = (" + fragment.getColor() + ")"); + " - color = (" + fragment.getColor() + ")");
} }
} }
@ -23,25 +22,25 @@ public class TestRasterizer {
System.out.println("OFF\n# Test Start"); System.out.println("OFF\n# Test Start");
TestShader shader = new TestShader (new GraphicsWrapper (256, 256)); TestShader shader = new TestShader(new GraphicsWrapper(256, 256));
Rasterizer rasterizer = new Rasterizer (shader); Rasterizer rasterizer = new Rasterizer(shader);
System.out.println ("Rasterizing edge"); System.out.println("Rasterizing edge");
Fragment v1 = new Fragment (0, 20); Fragment v1 = new Fragment(0, 20);
v1.setColor (0, 0, 0); v1.setColor(0, 0, 0);
Fragment v2 = new Fragment (5, -35); Fragment v2 = new Fragment(5, -35);
v2.setColor (50, 100, 0); v2.setColor(50, 100, 0);
rasterizer.rasterizeEdge (v1, v2); rasterizer.rasterizeEdge(v1, v2);
} }
public static void main (String[] args) { public static void main(String[] args) {
try { try {
test() ; test();
} catch (Exception e) { } catch (Exception e) {
System.out.println("EXCEPTION: " + e) ; System.out.println("EXCEPTION: " + e);
e.printStackTrace() ; e.printStackTrace();
} }
} }
} }

View file

@ -1,58 +1,50 @@
import algebra.*;
import java.awt.*;
/** /**
* Simple shader that just copy the interpolated color to the screen, * Simple shader that just copy the interpolated color to the screen,
* taking the depth of the fragment into acount. * taking the depth of the fragment into acount.
*
* @author: cdehais * @author: cdehais
*/ */
public class TextureShader extends Shader { public class TextureShader extends Shader {
DepthBuffer depth; DepthBuffer depth;
Texture texture; Texture texture;
boolean combineWithBaseColor; boolean combineWithBaseColor;
public TextureShader (GraphicsWrapper screen) { public TextureShader(GraphicsWrapper screen) {
super (screen); super(screen);
depth = new DepthBuffer (screen.getWidth (), screen.getHeight ()); depth = new DepthBuffer(screen.getWidth(), screen.getHeight());
texture = null; texture = null;
} }
public void setTexture (String path) { public void setTexture(String path) {
try { try {
texture = new Texture (path); texture = new Texture(path);
} catch (Exception e) { } catch (Exception e) {
System.out.println ("Could not load texture " + path); System.out.println("Could not load texture " + path);
e.printStackTrace (); e.printStackTrace();
texture = null; texture = null;
} }
} }
public void setCombineWithBaseColor (boolean combineWithBaseColor) { public void setCombineWithBaseColor(boolean combineWithBaseColor) {
this.combineWithBaseColor = combineWithBaseColor; this.combineWithBaseColor = combineWithBaseColor;
} }
public void shade (Fragment fragment) { public void shade(Fragment fragment) {
if (depth.testFragment (fragment)) { if (depth.testFragment(fragment)) {
/* The Fragment may not have texture coordinates */ /* The Fragment may not have texture coordinates */
try { try {
/* à compléter */
/* à compléter */
} catch (ArrayIndexOutOfBoundsException e) { } catch (ArrayIndexOutOfBoundsException e) {
screen.setPixel (fragment.getX (), fragment.getY (), fragment.getColor ()); screen.setPixel(fragment.getX(), fragment.getY(), fragment.getColor());
} }
depth.writeFragment (fragment); depth.writeFragment(fragment);
} }
} }
public void reset () { public void reset() {
depth.clear (); depth.clear();
} }
} }

View file

@ -4,8 +4,6 @@
package algebra; package algebra;
import java.lang.Math;
public class Matrix { public class Matrix {
protected Matrix() { protected Matrix() {

View file

@ -93,7 +93,6 @@ public class Vector3 extends Vector {
} }
public double dot(Vector3 v) { public double dot(Vector3 v) {
double r = 0.0;
return (values[0] * v.values[0] + values[1] * v.values[1] + values[2] * v.values[2]); return (values[0] * v.values[0] + values[1] * v.values[1] + values[2] * v.values[2]);
} }