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,77 +1,73 @@
/** /**
* 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;
public ImageComponent (BufferedImage init) { public ImageComponent(BufferedImage init) {
renderedImage = init; renderedImage = init;
} }
public BufferedImage swapImage (BufferedImage bi) { public BufferedImage swapImage(BufferedImage bi) {
BufferedImage ret = renderedImage ; BufferedImage ret = renderedImage;
renderedImage = bi ; renderedImage = bi;
return ret ; return ret;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
if (renderedImage != null) { if (renderedImage != null) {
((Graphics2D)g).drawImage(renderedImage, new AffineTransform(1f,0f,0f,1f,0,0), null); ((Graphics2D) g).drawImage(renderedImage, new AffineTransform(1f, 0f, 0f, 1f, 0, 0), null);
} }
} }
} }
public class GraphicsWrapper { public class GraphicsWrapper {
private int height = 0; private int height = 0;
private int width = 0; private int width = 0;
private int pixelSize = 0; private int pixelSize = 0;
private JFrame myFrame ; private JFrame myFrame;
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, 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) ; 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);
drawComp.setPreferredSize (new Dimension (width * pixelSize, height * pixelSize)) ; drawComp.setPreferredSize(new Dimension(width * pixelSize, height * pixelSize));
drawComp.setVisible (true); drawComp.setVisible(true);
myFrame = new JFrame("Simple Inverse Rasterization Renderer (TSI)"); myFrame = new JFrame("Simple Inverse Rasterization Renderer (TSI)");
myFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ; myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.add("Center", drawComp ); myFrame.add("Center", drawComp);
myFrame.pack(); myFrame.pack();
myFrame.setVisible(true); myFrame.setVisible(true);
} }
@ -88,14 +84,15 @@ 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.
*/ */
public GraphicsWrapper(int width, int height, int pixelSize) { public GraphicsWrapper(int width, int height, int pixelSize) {
this.height = height; this.height = height;
this.width = width; this.width = width;
this.pixelSize = pixelSize ; this.pixelSize = pixelSize;
init(); init();
} }
@ -104,97 +101,98 @@ public class GraphicsWrapper {
* on the current draw buffer. * on the current draw buffer.
* Does nothing for pixels out of the screen. * Does nothing for pixels out of the screen.
*/ */
public void setPixel (int x, int y, double r, double g, double b) { public void setPixel(int x, int y, double r, double g, double b) {
r = Math.min (1.0, Math.max (0.0, r)); r = Math.min(1.0, Math.max(0.0, r));
g = Math.min (1.0, Math.max (0.0, g)); g = Math.min(1.0, Math.max(0.0, g));
b = Math.min (1.0, Math.max (0.0, b)); b = Math.min(1.0, Math.max(0.0, b));
setPixel(x, y, (char) (r * 255), (char) (g * 255), (char) (b * 255)); setPixel(x, y, (char) (r * 255), (char) (g * 255), (char) (b * 255));
} }
/** /**
* Lights the pixel (x,y) with color (r, g, b) (values clamped to [0, 255]) * Lights the pixel (x,y) with color (r, g, b) (values clamped to [0, 255])
* on the current draw buffer. * on the current draw buffer.
* Does nothing for pixels out of the screen. * Does nothing for pixels out of the screen.
*/ */
public void setPixel(int x, int y, char r, char g, char b) { public void setPixel(int x, int y, char r, char g, char b) {
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) {
int argb = 0xFF000000 ; int argb = 0xFF000000;
argb += ((int)r) << (8 * 2) ; argb += ((int) r) << (8 * 2);
argb += ((int)g) << (8 * 1) ; argb += ((int) g) << (8 * 1);
argb += ((int)b); argb += ((int) b);
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);
}
}
} }
}
} }
}
/** /**
* Lights the pixel (x,y) with the given color. * Lights the pixel (x,y) with the given color.
* Does nothing for pixels out of the screen. * Does nothing for pixels out of the screen.
*/ */
public void setPixel(int x, int y, Color color) { public void setPixel(int x, int y, Color color) {
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) {
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);
}
}
} }
}
} }
}
/** /**
* Gets the pixel in the back buffer * Gets the pixel in the back buffer
*/ */
public Color getPixel (int x, int y) { public Color getPixel(int x, int y) {
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;
}
return color;
} }
public Color getFrontPixel (int x, int y) { return color;
Color color; }
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { public Color getFrontPixel(int x, int y) {
color = new Color (frontBuffer.getRGB (x, y), false); Color color;
} else {
color = Color.BLACK;
}
return color; if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) {
} color = new Color(frontBuffer.getRGB(x, y), false);
/** } else {
* color = Color.BLACK;
*/
int getWidth () {
return width;
} }
int getHeight () { return color;
return height; }
}
/**
*
*/
int getWidth() {
return width;
}
int getHeight() {
return height;
}
/** /**
* Clear current draw-buffer (ie Paint it black) * Clear current draw-buffer (ie Paint it black)
* *
*/ */
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,8 +200,8 @@ 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();