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 BufferedImage backBuffer = null;
// private BufferedImage backBuffer = null;
private BufferedImage frontBuffer = null;
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);
@ -204,6 +205,10 @@ public class GraphicsWrapper {
myFrame.repaint();
}
public void repaint() {
myFrame.repaint();
}
/**
* Destroy window.
*/

View file

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

View file

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

View file

@ -1,62 +1,62 @@
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.
*
* @author cdehais
*/
public class PerspectiveCorrectRasterizer extends Rasterizer {
public PerspectiveCorrectRasterizer (Shader shader) {
public PerspectiveCorrectRasterizer(Shader shader) {
super(shader);
}
/**
* Rasterizes the triangular face made of the Fragment v1, v2 and v3
*/
public void rasterizeFace (Fragment v1, Fragment v2, Fragment v3) {
Matrix C = makeBarycentricCoordsMatrix (v1, v2, v3);
public void rasterizeFace(Fragment v1, Fragment v2, Fragment v3) {
Matrix C = makeBarycentricCoordsMatrix(v1, v2, v3);
/* iterate over the triangle's bounding box */
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 xmax = Math.max (v1.getX (), Math.max (v2.getX (), v3.getX ()));
int ymax = Math.max (v1.getY (), Math.max (v2.getY (), v3.getY ()));
Fragment fragment = new Fragment (0, 0);
int numAttributes = fragment.getNumAttributes ();
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 xmax = Math.max(v1.getX(), Math.max(v2.getX(), v3.getX()));
int ymax = Math.max(v1.getY(), Math.max(v2.getY(), v3.getY()));
Fragment fragment = new Fragment(0, 0);
int numAttributes = fragment.getNumAttributes();
try {
for (int x = xmin; x <= xmax; x++) {
for (int y = ymin; y <= ymax; y++) {
/* setup position now to allow early clipping */
fragment.setPosition (x, y);
if (!shader.isClipped (fragment)) {
for (int x = xmin; x <= xmax; x++) {
for (int y = ymin; y <= ymax; y++) {
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 ();
/* setup position now to allow early clipping */
fragment.setPosition(x, y);
if (!shader.isClipped(fragment)) {
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) {
/* should not reach */
e.printStackTrace ();
e.printStackTrace();
}
}
}

View file

@ -1,6 +1,5 @@
import algebra.*;
import java.lang.Math.*;
/**
* 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
int numAttributes = v1.getNumAttributes();
Fragment fragment = new Fragment(0, 0); // , numAttributes);
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++) {
x = x + 1;
y_courant = y_courant + m;
if ((ystep == 1) && (y_courant < y + 0.5) || ((ystep == -1) && (y_courant > y
- 0.5))) {
y = y;
if ((ystep == 1) && (y_courant < y + 0.5) || ((ystep == -1) && (y_courant > y - 0.5))) {
y = y; // dafuk ?
} else {
y = y + ystep;
}

View file

@ -98,7 +98,7 @@ public class Renderer {
Fragment v1 = fragment[faces[i + j]];
Fragment v2 = fragment[faces[i + ((j + 1) % 3)]];
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 v2 = fragments[faces[i + 1]];
Fragment v3 = fragments[faces[i + 2]];
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.
*
* @author: cdehais
*/
public class SimpleShader extends Shader {
public SimpleShader (GraphicsWrapper screen) {
super (screen);
public SimpleShader(GraphicsWrapper screen) {
super(screen);
}
public void shade (Fragment fragment) {
//System.out.println (fragment.getX () + "," + fragment.getY ());
//System.out.println ("color " + fragment.getColor ());
screen.setPixel (fragment.getX (), fragment.getY (), fragment.getColor ());
public void shade(Fragment fragment) {
// System.out.println (fragment.getX () + "," + fragment.getY ());
// System.out.println ("color " + 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 {
System.out.println ("Algebra\n# Test Start");
System.out.println("Algebra\n# Test Start");
Vector v = new Vector (3);
Vector v1 = new Vector (1);
v1.setName ("up");
Vector v = new Vector(3);
Vector v1 = new Vector(1);
v1.setName("up");
try {
Vector v0 = new Vector (0);
new Vector(0);
} catch (Exception e) {
System.out.println ("Wrong size exception caught OK");
System.out.println("Wrong size exception caught OK");
}
try {
Vector v0 = new Vector ("named", 0);
new Vector("named", 0);
} 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 (v1);
System.out.println(v);
System.out.println(v1);
Vector u1 = new Vector ("u1", 3);
Vector u1 = new Vector("u1", 3);
u1.set(0, 1.0);
u1.set(1, 2.0);
u1.set(2, 3.0);
Vector u2 = new Vector (3);
u2.set (new double[3]);
Vector u2 = new Vector(3);
u2.set(new double[3]);
u2.set(1, 2.0);
System.out.println (u1);
System.out.println ("norm(u1) = " + u1.norm());
System.out.println (u2);
System.out.println ("u1.u2 = " + u1.dot (u2));
System.out.println(u1);
System.out.println("norm(u1) = " + u1.norm());
System.out.println(u2);
System.out.println("u1.u2 = " + u1.dot(u2));
try {
u1 = new Vector ("u1", 3);
u2 = new Vector ("u2", 4);
u1.dot (u2);
u1 = new Vector("u1", 3);
u2 = new Vector("u2", 4);
u1.dot(u2);
} 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 r2 = new Vector3 ("u2", 1.0, 3.0, 0.0);
Vector3 r1 = new Vector3("u1", 1.0, 2.0, 3.0);
Vector3 r2 = new Vector3("u2", 1.0, 3.0, 0.0);
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());
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 ("--\n-- Matrix tests\n--");
// -------------------------------------
System.out.println("--\n-- Matrix tests\n--");
Matrix M = new Matrix (3,5);
System.out.println (M);
Matrix M1 = new Matrix ("M1", 2, 5);
M1.set (0, 0, 2.0);
M1.set (1, 4, -8.0);
M1.set (1, 2, 5.0);
Matrix M = new Matrix(3, 5);
System.out.println(M);
Matrix M1 = new Matrix("M1", 2, 5);
M1.set(0, 0, 2.0);
M1.set(1, 4, -8.0);
M1.set(1, 2, 5.0);
try {
Matrix M2 = new Matrix ("M2", 0, 2);
new Matrix("M2", 0, 2);
} catch (Exception e) {
System.out.println ("Wrong size exception caught OK");
System.out.println("Wrong size exception caught OK");
}
try {
M.multiply (M1);
M.multiply(M1);
} 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);
System.out.println (M2);
M = M1.multiply (M2);
System.out.println (M1);
System.out.println (M2);
System.out.println (M);
M = M1.multiply(M2);
System.out.println(M1);
System.out.println(M2);
System.out.println(M);
M1 = M1.transpose();
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();
v = M.multiply (u);
System.out.println (M);
System.out.println (u);
System.out.println ("M.u = " + v);
v = M.multiply(u);
System.out.println(M);
System.out.println(u);
System.out.println("M.u = " + v);
try {
u = new Vector ("u", 3);
v = M.multiply (u);
u = new Vector("u", 3);
v = M.multiply(u);
} catch (SizeMismatchException e) {
System.out.println ("Caught exception: " + e);
System.out.println("Caught exception: " + e);
}
Matrix I = Matrix.createIdentity (5);
Matrix S = I.getSubMatrix (0, 2, 3, 3);
System.out.println (I);
System.out.println ("submatrix:\n" + S);
Matrix I = Matrix.createIdentity(5);
Matrix S = I.getSubMatrix(0, 2, 3, 3);
System.out.println(I);
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.lang.Math.*;
public class TestGraphicWrapper {
@ -9,24 +7,24 @@ public class TestGraphicWrapper {
static int height = 128;
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();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
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 () {
screen = new GraphicsWrapper (width, height, 1);
static void init() {
screen = new GraphicsWrapper(width, height, 1);
}
static int countNeighbours (int x0, int y0) {
static int countNeighbours(int x0, int y0) {
int count = 0;
for (int y = y0 - 1; y <= y0 + 1; y++) {
@ -34,108 +32,106 @@ public class TestGraphicWrapper {
if ((x != x0) || (y != y0)) {
int _x = x % width;
int _y = y % height;
Color pix = screen.getFrontPixel (_x, _y);
if (pix.getRed () != 0) {
Color pix = screen.getFrontPixel(_x, _y);
if (pix.getRed() != 0) {
count++;
}
}
}
}
}
return count;
}
static void evolve () {
static void evolve() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int c = countNeighbours (x, y);
Color pix = screen.getFrontPixel (x, y);
Color bpix = screen.getPixel (x, y);
if (pix.getRed () == 0) {
//System.out.println (x + " " + y + " : dead (" + c + " nbrs) "
// + ((bpix.getRed () == 0) ? "dead" : "alive")
// );
int c = countNeighbours(x, y);
Color pix = screen.getFrontPixel(x, y);
if (pix.getRed() == 0) {
// System.out.println (x + " " + y + " : dead (" + c + " nbrs) "
// + ((bpix.getRed () == 0) ? "dead" : "alive")
// );
if (c == 3) {
/* born */
screen.setPixel (x, y, 255, 255, 255);
screen.setPixel(x, y, 255, 255, 255);
} else {
screen.setPixel (x, y, 0, 0, 0);
screen.setPixel(x, y, 0, 0, 0);
}
} else {
//System.out.println (x + " " + y + " : alive (" + c + " nbrs) "
// + ((bpix.getRed() == 0) ? "dead" : "alive"));
// System.out.println (x + " " + y + " : alive (" + c + " nbrs) "
// + ((bpix.getRed() == 0) ? "dead" : "alive"));
if ((c >= 2) && (c <= 3)) {
/* survive */
screen.setPixel (x, y, 255, 255, 255);
screen.setPixel(x, y, 255, 255, 255);
} else {
/* 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++) {
checker (1, 1.0, 1.0, 1.0);
screen.swapBuffers ();
Thread.sleep (100);
checker (0, 1.0, 1.0, 1.0);
screen.swapBuffers ();
Thread.sleep (100);
checker(1, 1.0, 1.0, 1.0);
screen.swapBuffers();
Thread.sleep(100);
checker(0, 1.0, 1.0, 1.0);
screen.swapBuffers();
Thread.sleep(100);
}
}
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);
public static void testConway() throws Exception {
screen.swapBuffers ();
Thread.sleep (1000);
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();
Thread.sleep(1000);
for (int k = 0; k < 100; k++) {
evolve ();
screen.swapBuffers ();
Thread.sleep (30);
evolve();
screen.swapBuffers();
Thread.sleep(30);
}
screen.destroy() ;
screen.destroy();
System.exit(0);
}
public static void main(String[] args) {
try {
init ();
//testChecker () ;
testConway () ;
init();
// testChecker () ;
testConway();
} catch (Exception e) {
System.out.println("EXCEPTION: " + e) ;
e.printStackTrace() ;
System.out.println("EXCEPTION: " + e);
e.printStackTrace();
}
}
}

View file

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

View file

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

View file

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

View file

@ -93,7 +93,6 @@ public class Vector3 extends Vector {
}
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]);
}