From df595223660b8cd30a04f70218b463e088b923ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Tue, 19 Apr 2022 11:19:30 +0200 Subject: [PATCH] feat: meilleurs affichage --- Rasterizer.java | 34 ++++++++++++++++++----------- Renderer.java | 57 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/Rasterizer.java b/Rasterizer.java index 6345cd7..bb8c225 100644 --- a/Rasterizer.java +++ b/Rasterizer.java @@ -58,6 +58,27 @@ public class Rasterizer { f.setPosition(f.getY(), f.getX()); } + public void rasterizeVertice(Fragment v1, Fragment v2) { + /* Coordinates of V1 and V2 */ + int x1 = v1.getX(); + int y1 = v1.getY(); + int x2 = v2.getX(); + int y2 = v2.getY(); + + /* For now : just display the vertices */ + Fragment f = new Fragment(0, 0); + int size = 2; + for (int i = 0; i < v1.getNumAttributes(); i++) { + f.setAttribute(i, v1.getAttribute(i)); + } + for (int i = -size; i <= size; i++) { + for (int j = -size; j <= size; j++) { + f.setPosition(x1 + i, y1 + j); + shader.shade(f); + } + } + } + /** * Rasterizes the edge between the projected vectors v1 and v2. * Generates Fragment's and calls the Shader::shade() metho on each of them. @@ -70,19 +91,6 @@ public class Rasterizer { int x2 = v2.getX(); int y2 = v2.getY(); - // /* For now : just display the vertices */ - // Fragment f = new Fragment(0, 0); - // int size = 2; - // for (int i = 0; i < v1.getNumAttributes(); i++) { - // f.setAttribute(i, v1.getAttribute(i)); - // } - // for (int i = -size; i <= size; i++) { - // for (int j = -size; j <= size; j++) { - // f.setPosition(x1 + i, y1 + j); - // shader.shade(f); - // } - // } - // tracé d'un segment avec l'algo de Bresenham Fragment fragment = new Fragment(0, 0); // , numAttributes); diff --git a/Renderer.java b/Renderer.java index 79194b5..5febee8 100644 --- a/Renderer.java +++ b/Renderer.java @@ -23,10 +23,9 @@ public class Renderer { mesh = new Mesh(scene.getMeshFileName()); screen = new GraphicsWrapper(scene.getScreenW(), scene.getScreenH()); screen.clearBuffer(); - // shader = new SimpleShader(screen); - shader = new PainterShader(screen); - rasterizer = new PerspectiveCorrectRasterizer(shader); - // rasterizer = new Rasterizer(shader); + shader = new SimpleShader(screen); + // rasterizer = new PerspectiveCorrectRasterizer(shader); + rasterizer = new Rasterizer(shader); xform = new Transformation(); xform.setLookAt(scene.getCameraPosition(), scene.getCameraLookAt(), scene.getCameraUp()); @@ -89,6 +88,20 @@ public class Renderer { return fragments; } + static void renderVertices() { + Fragment[] fragment = projectVertices(); + int[] faces = mesh.getFaces(); + + for (int i = 0; i < 3 * mesh.getNumFaces(); i += 3) { + for (int j = 0; j < 3; j++) { + Fragment v1 = fragment[faces[i + j]]; + Fragment v2 = fragment[faces[i + ((j + 1) % 3)]]; + rasterizer.rasterizeVertice(v1, v2); + screen.repaint(); // on refresh l'écran + } + } + } + static void renderWireframe() { Fragment[] fragment = projectVertices(); int[] faces = mesh.getFaces(); @@ -142,16 +155,28 @@ public class Renderer { } } + /* vertice rendering */ + renderVertices(); + wait(1); + /* wireframe rendering */ renderWireframe(); - // screen.swapBuffers(); wait(1); /* solid rendering, no lighting */ + shader = new SimpleShader(screen); + rasterizer = new Rasterizer(shader); + screen.clearBuffer(); + shader.reset(); + renderSolid(); + wait(1); + + /* solid rendering, no lighting */ + shader = new PainterShader(screen); + rasterizer = new Rasterizer(shader); screen.clearBuffer(); shader.reset(); renderSolid(); - screen.swapBuffers(); wait(1); /* solid rendering, with lighting */ @@ -159,20 +184,32 @@ public class Renderer { shader.reset(); setLightingEnabled(true); renderSolid(); - screen.swapBuffers(); wait(1); - /* solid rendering, with texture */ - screen.clearBuffer(); + /* création du texture shader */ TextureShader texShader = new TextureShader(screen); texShader.setTexture("data/brick.jpg"); shader = texShader; + + /* solid rendering, with texture */ + rasterizer = new Rasterizer(shader); + screen.clearBuffer(); + shader.reset(); rasterizer.setShader(texShader); setLightingEnabled(true); renderSolid(); - screen.swapBuffers(); wait(1); + /* solid rendering, with texture */ + rasterizer = new PerspectiveCorrectRasterizer(shader); + screen.clearBuffer(); + shader.reset(); + rasterizer.setShader(texShader); + setLightingEnabled(true); + renderSolid(); + wait(1); + + // on attend un peu avant de fermer la fenêtre wait(60); screen.destroy(); System.exit(0);