feat: maintenant ya de la couleur

Co-authored-by: pejour <pejour@users.noreply.github.com>
This commit is contained in:
Laureηt 2022-04-14 11:21:44 +02:00
parent 60f9b0e464
commit e33cca55a3
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
3 changed files with 156 additions and 97 deletions

View file

@ -1,5 +1,4 @@
import java.lang.Double; import java.lang.Double;
/** /**
@ -38,9 +37,12 @@ public class DepthBuffer {
* closest at its position. * closest at its position.
*/ */
public boolean testFragment(Fragment fragment) { public boolean testFragment(Fragment fragment) {
if ((fragment.getX () >= 0) && (fragment.getX () < width) && (fragment.getY () >= 0) && (fragment.getY () < height)) { if ((fragment.getX() >= 0) && (fragment.getX() < width) && (fragment.getY() >= 0)
&& (fragment.getY() < height)) {
/* COMPLETER */ if (fragment.getDepth() < this.buffer[fragment.getY() * height + fragment.getX()]) {
return true;
}
return false; return false;
} else { } else {
@ -52,14 +54,9 @@ public class DepthBuffer {
* Writes the fragment depth to the buffer * Writes the fragment depth to the buffer
*/ */
public void writeFragment(Fragment fragment) { public void writeFragment(Fragment fragment) {
if ((fragment.getX () >= 0) && (fragment.getX () < width) && (fragment.getY () >= 0) && (fragment.getY () < height)) { if (testFragment(fragment)) {
this.buffer[fragment.getY() * height + fragment.getX()] = fragment.getDepth();
/* COMPLETER */
} }
} }
} }

View file

@ -71,77 +71,84 @@ public class Rasterizer {
int x2 = v2.getX(); int x2 = v2.getX();
int y2 = v2.getY(); int y2 = v2.getY();
/* For now : just display the vertices */ // /* For now : just display the vertices */
Fragment f = new Fragment(0, 0); // Fragment f = new Fragment(0, 0);
int size = 2; // int size = 2;
for (int i = 0; i < v1.getNumAttributes(); i++) { // for (int i = 0; i < v1.getNumAttributes(); i++) {
f.setAttribute(i, v1.getAttribute(i)); // f.setAttribute(i, v1.getAttribute(i));
} // }
for (int i = -size; i <= size; i++) { // for (int i = -size; i <= size; i++) {
for (int j = -size; j <= size; j++) { // for (int j = -size; j <= size; j++) {
f.setPosition(x1 + i, y1 + j); // f.setPosition(x1 + i, y1 + j);
shader.shade(f); // shader.shade(f);
// }
// }
// 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));
if (sym) {
int temp;
temp = x1;
x1 = y1;
y1 = temp;
temp = x2;
x2 = y2;
y2 = temp;
} }
if (x1 > x2) {
Fragment ftemp;
int temp;
temp = x1;
x1 = x2;
x2 = temp;
temp = y1;
y1 = y2;
y2 = temp;
ftemp = v1;
v1 = v2;
v2 = ftemp;
} }
/* int ystep;
* tracé d'un segment avec l'algo de Bresenham if (y1 < y2) {
* int numAttributes = v1.getNumAttributes (); ystep = 1;
* Fragment fragment = new Fragment (0, 0); //, numAttributes); } else {
* ystep = -1;
* boolean sym = (Math.abs (y2 - y1) > Math.abs (x2 - x1)); }
* if (sym) {
* int temp; int x = x1;
* temp = x1; x1 = y1 ; y1 = temp; float y_courant = y1;
* temp = x2; x2 = y2 ; y2 = temp; int y = y1;
* } float delta_y = y2 - y1;
* if (x1 > x2) { float delta_x = x2 - x1;
* Fragment ftemp; float m = delta_y / delta_x;
* int temp;
* temp = x1; x1 = x2; x2 = temp; for (int i = 1; i <= delta_x; i++) {
* temp = y1; y1 = y2; y2 = temp; x = x + 1;
* ftemp = v1; v1 = v2; v2 = ftemp; y_courant = y_courant + m;
* } if ((ystep == 1) && (y_courant < y + 0.5) || ((ystep == -1) && (y_courant > y
* - 0.5))) {
* int ystep; y = y;
* if (y1 < y2) { } else {
* ystep = 1; y = y + ystep;
* } else { }
* ystep = -1;
* } // envoi du fragment au shader
* fragment.setPosition(x, y);
* int x = x1;
* float y_courant = y1; if (!shader.isClipped(fragment)) {
* int y=y1;
* float delta_y = y2-y1; // interpolation des attributs
* float delta_x = x2-x1; interpolate2(v1, v2, fragment);
* float m = delta_y/delta_x; if (sym) {
* swapXAndY(fragment);
* }
* for (int i=1;i<=delta_x;i++) { shader.shade(fragment);
* 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;
* } else {
* y = y + ystep;
* }
*
* //envoi du fragment au shader
* fragment.setPosition (x, y);
*
* if (!shader.isClipped (fragment)) {
*
* //interpolation des attributs
* interpolate2 (v1, v2, fragment);
* if (sym) {
* swapXAndY (fragment);
* }
* shader.shade (fragment);
* }
* }
*/
} }
@ -179,6 +186,14 @@ public class Rasterizer {
return C; return C;
} }
private int min3(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
private int max3(int a, int b, int c) {
return Math.max(a, Math.max(b, c));
}
/** /**
* Rasterizes the triangular face made of the Fragment v1, v2 and v3 * Rasterizes the triangular face made of the Fragment v1, v2 and v3
*/ */
@ -188,7 +203,53 @@ public class Rasterizer {
/* iterate over the triangle's bounding box */ /* iterate over the triangle's bounding box */
/* A COMPLETER */ int x_hg = min3(v1.getX(), v2.getX(), v3.getX());
int y_hg = min3(v1.getY(), v2.getY(), v3.getY());
int x_bd = max3(v1.getX(), v2.getX(), v3.getX());
int y_bd = max3(v1.getY(), v2.getY(), v3.getY());
for (int px = x_hg; px < x_bd; px++) {
boucle: for (int py = y_hg; py < y_bd; py++) {
// System.out.println(px);
// System.out.println(py);
// System.out.println();
Vector3 point = new Vector3(1, px, py);
Vector barycentre = null;
try {
barycentre = C.multiply(point);
} catch (SizeMismatchException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
// si une des coordonnées barycentrique est négative,
// le pixel n'est pas dans le triangle
if (barycentre.get(i) < 0) {
// on passe au pixel suivant
continue boucle;
}
}
// System.out.println(barycentre);
// Le pixel est dans le triangle
// On créé un fragment
Fragment couleur = new Fragment(px, py);
// on interpole la couleur
for (int i = 0; i < couleur.getNumAttributes(); i++) {
double pondere = v1.getAttribute(i) * barycentre.get(0) + v2.getAttribute(i) * barycentre.get(1)
+ v3.getAttribute(i) * barycentre.get(2);
couleur.setAttribute(i, pondere);
}
// on affiche le pixel
shader.shade(couleur);
}
}
} }
} }

View file

@ -23,9 +23,9 @@ public class Renderer {
mesh = new Mesh(scene.getMeshFileName()); mesh = new Mesh(scene.getMeshFileName());
screen = new GraphicsWrapper(scene.getScreenW(), scene.getScreenH()); screen = new GraphicsWrapper(scene.getScreenW(), scene.getScreenH());
screen.clearBuffer(); screen.clearBuffer();
shader = new SimpleShader(screen); // shader = new SimpleShader(screen);
// shader = new PainterShader (screen); shader = new PainterShader(screen);
// rasterizer = new PerspectiveCorrectRasterizer (shader); rasterizer = new PerspectiveCorrectRasterizer(shader);
rasterizer = new Rasterizer(shader); rasterizer = new Rasterizer(shader);
xform = new Transformation(); xform = new Transformation();
@ -146,16 +146,17 @@ public class Renderer {
/* wireframe rendering */ /* wireframe rendering */
renderWireframe(); renderWireframe();
screen.swapBuffers(); screen.swapBuffers();
wait(10); wait(1);
/* solid rendering, no lighting */ /* solid rendering, no lighting */
/*
* screen.clearBuffer (); screen.clearBuffer();
* shader.reset (); shader.reset();
* renderSolid (); renderSolid();
* screen.swapBuffers (); screen.swapBuffers();
* wait (3); wait(1);
*/
wait(10);
/* solid rendering, with lighting */ /* solid rendering, with lighting */
/* /*