feat: jsuis con + fuck le backbuffer

This commit is contained in:
Laureηt 2022-04-14 22:35:42 +02:00
parent e56ba8d1c7
commit bb1aea52b2
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
6 changed files with 116 additions and 103 deletions

18
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch Renderer",
"request": "launch",
"mainClass": "Renderer",
"projectName": "TP_d9f3103c",
"args": [
"data/example2.scene"
]
}
]
}

View file

@ -40,7 +40,7 @@ public class DepthBuffer {
if ((fragment.getX() >= 0) && (fragment.getX() < width) && (fragment.getY() >= 0) if ((fragment.getX() >= 0) && (fragment.getX() < width) && (fragment.getY() >= 0)
&& (fragment.getY() < height)) { && (fragment.getY() < height)) {
if (fragment.getDepth() < this.buffer[fragment.getY() * height + fragment.getX()]) { if (fragment.getDepth() < this.buffer[fragment.getY() * width + fragment.getX()]) {
return true; return true;
} }
@ -55,8 +55,7 @@ public class DepthBuffer {
*/ */
public void writeFragment(Fragment fragment) { public void writeFragment(Fragment fragment) {
if (testFragment(fragment)) { if (testFragment(fragment)) {
this.buffer[fragment.getY() * height + fragment.getX()] = fragment.getDepth(); this.buffer[fragment.getY() * width + fragment.getX()] = fragment.getDepth();
} }
} }
} }

View file

@ -1,17 +1,16 @@
/** /**
* A "virtual" screen, where only "setPixel" is available * A "virtual" screen, where only "setPixel" is available
* (It is a JFrame, and JFrame.EXIT_ON_CLOSE is set) * (It is a JFrame, and JFrame.EXIT_ON_CLOSE is set)
* @author smondet * @author smondet
*/ */
import java.awt.*; import java.awt.*;
import java.awt.geom.*; import java.awt.geom.*;
import java.awt.image.*; import java.awt.image.*;
import javax.swing.*; import javax.swing.*;
import java.lang.Math; import java.lang.Math;
class ImageComponent extends Component { class ImageComponent extends Component {
BufferedImage renderedImage = null; BufferedImage renderedImage = null;
@ -35,9 +34,6 @@ class ImageComponent extends Component {
} }
public class GraphicsWrapper { public class GraphicsWrapper {
private int height = 0; private int height = 0;
@ -57,12 +53,12 @@ public class GraphicsWrapper {
frontBuffer = new BufferedImage(width * pixelSize, height * pixelSize, BufferedImage.TYPE_3BYTE_BGR); frontBuffer = new BufferedImage(width * pixelSize, height * pixelSize, BufferedImage.TYPE_3BYTE_BGR);
/* /*
Graphics2D gd = initial.createGraphics (); * Graphics2D gd = initial.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) ;
gd = drawingImage.createGraphics (); * gd = drawingImage.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) ;
*/ */
drawComp = new ImageComponent(frontBuffer); drawComp = new ImageComponent(frontBuffer);
@ -88,7 +84,8 @@ public class GraphicsWrapper {
} }
/** /**
* Build a virtual screen of size width x height, where one virtual pixel is represented by * Build a virtual screen of size width x height, where one virtual pixel is
* represented by
* a pixelSize x pixelSize square. * a pixelSize x pixelSize square.
* And set its window visible. * And set its window visible.
*/ */
@ -128,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++) {
backBuffer.setRGB(i + (x * pixelSize) , j + (y * pixelSize), argb) ; frontBuffer.setRGB(i + (x * pixelSize), j + (y * pixelSize), argb);
} }
} }
} }
@ -144,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++) {
backBuffer.setRGB(i + (x * pixelSize) , j + (y * pixelSize), rgb); frontBuffer.setRGB(i + (x * pixelSize), j + (y * pixelSize), rgb);
} }
} }
} }
@ -157,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 (backBuffer.getRGB (x, y), false); color = new Color(frontBuffer.getRGB(x, y), false);
} else { } else {
color = Color.BLACK; color = Color.BLACK;
} }
@ -176,6 +173,7 @@ public class GraphicsWrapper {
return color; return color;
} }
/** /**
* *
*/ */
@ -192,7 +190,7 @@ public class GraphicsWrapper {
* *
*/ */
public void clearBuffer() { public void clearBuffer() {
Graphics2D gd = backBuffer.createGraphics (); Graphics2D gd = frontBuffer.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);
} }
@ -202,7 +200,7 @@ public class GraphicsWrapper {
* *
*/ */
public void swapBuffers() { public void swapBuffers() {
frontBuffer = drawComp.swapImage (backBuffer) ; frontBuffer = drawComp.swapImage(frontBuffer);
myFrame.repaint(); myFrame.repaint();
} }
@ -213,5 +211,4 @@ public class GraphicsWrapper {
myFrame.dispose(); myFrame.dispose();
} }
} }

View file

@ -80,12 +80,13 @@ public class Lighting {
h.add(l); h.add(l);
h.normalize(); h.normalize();
/* diffuse contribution */
double Id = light.params[3]; double Id = light.params[3];
/* diffuse contribution */
double I_diff = Id * kd * Math.cos(normal.dot(l)); double I_diff = Id * kd * Math.cos(normal.dot(l));
/* specular contribution */ /* specular contribution */
double I_spec = Id * ks * Math.pow(Math.cos(h.dot(normal)), 2); double I_spec = Id * ks * Math.pow(Math.cos(h.dot(normal)), s);
I += I_diff + I_spec; I += I_diff + I_spec;
} catch (InstantiationException ex) { } catch (InstantiationException ex) {

View file

@ -202,7 +202,6 @@ public class Rasterizer {
Matrix C = makeBarycentricCoordsMatrix(v1, v2, v3); Matrix C = makeBarycentricCoordsMatrix(v1, v2, v3);
/* iterate over the triangle's bounding box */ /* iterate over the triangle's bounding box */
int x_hg = min3(v1.getX(), v2.getX(), v3.getX()); int x_hg = min3(v1.getX(), v2.getX(), v3.getX());
int y_hg = min3(v1.getY(), v2.getY(), v3.getY()); int y_hg = min3(v1.getY(), v2.getY(), v3.getY());
@ -227,7 +226,7 @@ public class Rasterizer {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
// si une des coordonnées barycentrique est négative, // si une des coordonnées barycentrique est négative,
// le pixel n'est pas dans le triangle // le pixel n'est pas dans le triangle
if (barycentre.get(i) < 0) { if (barycentre.get(i) < -1e-3) {
// on passe au pixel suivant // on passe au pixel suivant
continue boucle; continue boucle;
} }

View file

@ -29,9 +29,7 @@ public class Renderer {
rasterizer = new Rasterizer(shader); rasterizer = new Rasterizer(shader);
xform = new Transformation(); xform = new Transformation();
xform.setLookAt(scene.getCameraPosition(), xform.setLookAt(scene.getCameraPosition(), scene.getCameraLookAt(), scene.getCameraUp());
scene.getCameraLookAt(),
scene.getCameraUp());
xform.setProjection(); xform.setProjection();
xform.setCalibration(scene.getCameraFocal(), scene.getScreenW(), scene.getScreenH()); xform.setCalibration(scene.getCameraFocal(), scene.getScreenW(), scene.getScreenH());
@ -100,6 +98,7 @@ 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.swapBuffers(); // on repaint juste
} }
} }
} }
@ -114,6 +113,7 @@ public class Renderer {
Fragment v3 = fragments[faces[i + 2]]; Fragment v3 = fragments[faces[i + 2]];
rasterizer.rasterizeFace(v1, v2, v3); rasterizer.rasterizeFace(v1, v2, v3);
screen.swapBuffers(); // on repaint juste
} }
} }
@ -145,16 +145,15 @@ public class Renderer {
/* wireframe rendering */ /* wireframe rendering */
renderWireframe(); renderWireframe();
screen.swapBuffers(); // screen.swapBuffers();
wait(1); wait(3);
/* solid rendering, no lighting */ /* solid rendering, no lighting */
screen.clearBuffer(); screen.clearBuffer();
shader.reset(); shader.reset();
renderSolid(); renderSolid();
screen.swapBuffers(); screen.swapBuffers();
wait(1); wait(3);
/* solid rendering, with lighting */ /* solid rendering, with lighting */
screen.clearBuffer(); screen.clearBuffer();