From b530773a86c57a4711b392a5fe3111a35a85d30e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Tue, 19 Apr 2022 22:42:50 +0200 Subject: [PATCH] feat: better inputs --- GraphicsWrapper.java | 15 ++++---- Renderer.java | 82 ++++++++++++++++++++++++++++++-------------- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/GraphicsWrapper.java b/GraphicsWrapper.java index 7c47e44..bbb31ab 100644 --- a/GraphicsWrapper.java +++ b/GraphicsWrapper.java @@ -44,12 +44,11 @@ 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); @@ -126,7 +125,7 @@ public class GraphicsWrapper { for (int i = 0; i < pixelSize; i++) { for (int j = 0; j < pixelSize; j++) { - frontBuffer.setRGB(i + (x * pixelSize), j + (y * pixelSize), argb); + backBuffer.setRGB(i + (x * pixelSize), j + (y * pixelSize), argb); } } } @@ -142,7 +141,7 @@ public class GraphicsWrapper { int rgb = color.getRGB(); for (int i = 0; i < pixelSize; i++) { for (int j = 0; j < pixelSize; j++) { - frontBuffer.setRGB(i + (x * pixelSize), j + (y * pixelSize), rgb); + backBuffer.setRGB(i + (x * pixelSize), j + (y * pixelSize), rgb); } } } @@ -155,7 +154,7 @@ public class GraphicsWrapper { Color color; if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { - color = new Color(frontBuffer.getRGB(x, y), false); + color = new Color(backBuffer.getRGB(x, y), false); } else { color = Color.BLACK; } @@ -191,7 +190,7 @@ public class GraphicsWrapper { * */ public void clearBuffer() { - Graphics2D gd = frontBuffer.createGraphics(); + Graphics2D gd = backBuffer.createGraphics(); gd.setColor(Color.BLACK); gd.fillRect(0, 0, width * pixelSize, height * pixelSize); } @@ -201,7 +200,7 @@ public class GraphicsWrapper { * */ public void swapBuffers() { - frontBuffer = drawComp.swapImage(frontBuffer); + frontBuffer = drawComp.swapImage(backBuffer); myFrame.repaint(); } diff --git a/Renderer.java b/Renderer.java index 85d296d..6742851 100644 --- a/Renderer.java +++ b/Renderer.java @@ -1,4 +1,6 @@ +import java.util.Scanner; + import algebra.*; /** @@ -18,6 +20,7 @@ public class Renderer { static Lighting lighting; static boolean lightingEnabled; static boolean Thong; + static boolean repaint; static void init(String sceneFilename) throws Exception { scene = new Scene(sceneFilename); @@ -103,7 +106,8 @@ public class Renderer { Fragment v1 = fragment[faces[i + j]]; Fragment v2 = fragment[faces[i + ((j + 1) % 3)]]; rasterizer.rasterizeVertice(v1, v2); - screen.repaint(); // on refresh l'écran + if (repaint) // on refresh l'écran + screen.repaint(); } } } @@ -117,7 +121,8 @@ public class Renderer { Fragment v1 = fragment[faces[i + j]]; Fragment v2 = fragment[faces[i + ((j + 1) % 3)]]; rasterizer.rasterizeEdge(v1, v2); - screen.repaint(); // on refresh l'écran + if (repaint) // on refresh l'écran + screen.repaint(); } } } @@ -131,7 +136,8 @@ public class Renderer { Fragment v2 = fragments[faces[i + 1]]; Fragment v3 = fragments[faces[i + 2]]; rasterizer.rasterizeFace(v1, v2, v3); - screen.repaint(); // on refresh l'écran + if (repaint) // on refresh l'écran + screen.repaint(); } } @@ -151,6 +157,11 @@ public class Renderer { } } + public static void waitKey() { + Scanner s = new Scanner(System.in); + s.nextLine(); + } + public static void main(String[] args) { if (args.length == 0) { @@ -166,46 +177,61 @@ public class Renderer { } /* vertice rendering */ - // renderVertices(); - // wait(1); + System.out.println("vertice rendering"); + renderVertices(); + screen.swapBuffers(); + // wait(3); + waitKey(); /* wireframe rendering */ - // renderWireframe(); - // wait(1); + System.out.println("wireframe rendering"); + renderWireframe(); + screen.swapBuffers(); + // wait(3); + waitKey(); - /* solid rendering, no lighting */ - // shader = new SimpleShader(screen); - // rasterizer = new Rasterizer(shader); - // screen.clearBuffer(); - // shader.reset(); - // renderSolid(); - // wait(1); + /* solid rendering, no lighting (SimpleShader) */ + System.out.println("solid rendering, no lighting (SimpleShader)"); + shader = new SimpleShader(screen); + rasterizer = new Rasterizer(shader); + screen.clearBuffer(); + shader.reset(); + renderSolid(); + screen.swapBuffers(); + // wait(3); + waitKey(); - /* solid rendering, no lighting */ + /* solid rendering, no lighting (PainterShader) */ + System.out.println("solid rendering, no lighting (PainterShader)"); shader = new PainterShader(screen); rasterizer = new Rasterizer(shader); screen.clearBuffer(); shader.reset(); renderSolid(); - wait(3); + screen.swapBuffers(); + // wait(3); + waitKey(); /* solid rendering, with lighting */ + System.out.println("solid rendering, with lighting"); screen.clearBuffer(); shader.reset(); setLightingEnabled(true); renderSolid(); - wait(1); + screen.swapBuffers(); + // wait(3); + waitKey(); /* solid rendering, with lighting Thong */ + System.out.println("solid rendering, with lighting Thong"); screen.clearBuffer(); shader.reset(); setThongEnabled(true); setLightingEnabled(true); renderSolid(); - wait(3); - - screen.destroy(); - System.exit(0); + screen.swapBuffers(); + // wait(3); + waitKey(); /* création du texture shader */ TextureShader texShader = new TextureShader(screen); @@ -213,25 +239,31 @@ public class Renderer { shader = texShader; /* solid rendering, with texture */ + System.out.println("solid rendering, with texture"); rasterizer = new Rasterizer(shader); screen.clearBuffer(); shader.reset(); rasterizer.setShader(texShader); setLightingEnabled(true); renderSolid(); - wait(1); + screen.swapBuffers(); + // wait(3); + waitKey(); - /* solid rendering, with texture */ + /* solid rendering, with PerspectiveCorrect texture */ + System.out.println("solid rendering, with PerspectiveCorrect texture"); rasterizer = new PerspectiveCorrectRasterizer(shader); screen.clearBuffer(); shader.reset(); rasterizer.setShader(texShader); setLightingEnabled(true); renderSolid(); - wait(1); + screen.swapBuffers(); + // wait(3); + waitKey(); // on attend un peu avant de fermer la fenêtre - wait(10); + // wait(3); screen.destroy(); System.exit(0); }