feat: better inputs

This commit is contained in:
Laureηt 2022-04-19 22:42:50 +02:00
parent 1af99b92d6
commit b530773a86
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
2 changed files with 64 additions and 33 deletions

View file

@ -44,12 +44,11 @@ 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, backBuffer = new BufferedImage(width * pixelSize, height * pixelSize, BufferedImage.TYPE_INT_ARGB);
// 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);
@ -126,7 +125,7 @@ public class GraphicsWrapper {
for (int i = 0; i < pixelSize; i++) { for (int i = 0; i < pixelSize; i++) {
for (int j = 0; j < pixelSize; j++) { 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(); int rgb = color.getRGB();
for (int i = 0; i < pixelSize; i++) { for (int i = 0; i < pixelSize; i++) {
for (int j = 0; j < pixelSize; j++) { 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; Color color;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { 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 { } else {
color = Color.BLACK; color = Color.BLACK;
} }
@ -191,7 +190,7 @@ public class GraphicsWrapper {
* *
*/ */
public void clearBuffer() { public void clearBuffer() {
Graphics2D gd = frontBuffer.createGraphics(); Graphics2D gd = backBuffer.createGraphics();
gd.setColor(Color.BLACK); gd.setColor(Color.BLACK);
gd.fillRect(0, 0, width * pixelSize, height * pixelSize); gd.fillRect(0, 0, width * pixelSize, height * pixelSize);
} }
@ -201,7 +200,7 @@ public class GraphicsWrapper {
* *
*/ */
public void swapBuffers() { public void swapBuffers() {
frontBuffer = drawComp.swapImage(frontBuffer); frontBuffer = drawComp.swapImage(backBuffer);
myFrame.repaint(); myFrame.repaint();
} }

View file

@ -1,4 +1,6 @@
import java.util.Scanner;
import algebra.*; import algebra.*;
/** /**
@ -18,6 +20,7 @@ public class Renderer {
static Lighting lighting; static Lighting lighting;
static boolean lightingEnabled; static boolean lightingEnabled;
static boolean Thong; static boolean Thong;
static boolean repaint;
static void init(String sceneFilename) throws Exception { static void init(String sceneFilename) throws Exception {
scene = new Scene(sceneFilename); scene = new Scene(sceneFilename);
@ -103,7 +106,8 @@ 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.rasterizeVertice(v1, v2); 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 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.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 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.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) { public static void main(String[] args) {
if (args.length == 0) { if (args.length == 0) {
@ -166,46 +177,61 @@ public class Renderer {
} }
/* vertice rendering */ /* vertice rendering */
// renderVertices(); System.out.println("vertice rendering");
// wait(1); renderVertices();
screen.swapBuffers();
// wait(3);
waitKey();
/* wireframe rendering */ /* wireframe rendering */
// renderWireframe(); System.out.println("wireframe rendering");
// wait(1); renderWireframe();
screen.swapBuffers();
// wait(3);
waitKey();
/* solid rendering, no lighting */ /* solid rendering, no lighting (SimpleShader) */
// shader = new SimpleShader(screen); System.out.println("solid rendering, no lighting (SimpleShader)");
// rasterizer = new Rasterizer(shader); shader = new SimpleShader(screen);
// screen.clearBuffer(); rasterizer = new Rasterizer(shader);
// shader.reset(); screen.clearBuffer();
// renderSolid(); shader.reset();
// wait(1); 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); shader = new PainterShader(screen);
rasterizer = new Rasterizer(shader); rasterizer = new Rasterizer(shader);
screen.clearBuffer(); screen.clearBuffer();
shader.reset(); shader.reset();
renderSolid(); renderSolid();
wait(3); screen.swapBuffers();
// wait(3);
waitKey();
/* solid rendering, with lighting */ /* solid rendering, with lighting */
System.out.println("solid rendering, with lighting");
screen.clearBuffer(); screen.clearBuffer();
shader.reset(); shader.reset();
setLightingEnabled(true); setLightingEnabled(true);
renderSolid(); renderSolid();
wait(1); screen.swapBuffers();
// wait(3);
waitKey();
/* solid rendering, with lighting Thong */ /* solid rendering, with lighting Thong */
System.out.println("solid rendering, with lighting Thong");
screen.clearBuffer(); screen.clearBuffer();
shader.reset(); shader.reset();
setThongEnabled(true); setThongEnabled(true);
setLightingEnabled(true); setLightingEnabled(true);
renderSolid(); renderSolid();
wait(3); screen.swapBuffers();
// wait(3);
screen.destroy(); waitKey();
System.exit(0);
/* création du texture shader */ /* création du texture shader */
TextureShader texShader = new TextureShader(screen); TextureShader texShader = new TextureShader(screen);
@ -213,25 +239,31 @@ public class Renderer {
shader = texShader; shader = texShader;
/* solid rendering, with texture */ /* solid rendering, with texture */
System.out.println("solid rendering, with texture");
rasterizer = new Rasterizer(shader); rasterizer = new Rasterizer(shader);
screen.clearBuffer(); screen.clearBuffer();
shader.reset(); shader.reset();
rasterizer.setShader(texShader); rasterizer.setShader(texShader);
setLightingEnabled(true); setLightingEnabled(true);
renderSolid(); 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); rasterizer = new PerspectiveCorrectRasterizer(shader);
screen.clearBuffer(); screen.clearBuffer();
shader.reset(); shader.reset();
rasterizer.setShader(texShader); rasterizer.setShader(texShader);
setLightingEnabled(true); setLightingEnabled(true);
renderSolid(); renderSolid();
wait(1); screen.swapBuffers();
// wait(3);
waitKey();
// on attend un peu avant de fermer la fenêtre // on attend un peu avant de fermer la fenêtre
wait(10); // wait(3);
screen.destroy(); screen.destroy();
System.exit(0); System.exit(0);
} }