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)
&& (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;
}
@ -55,8 +55,7 @@ public class DepthBuffer {
*/
public void writeFragment(Fragment 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
* (It is a JFrame, and JFrame.EXIT_ON_CLOSE is set)
* @author smondet
*/
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import java.lang.Math;
class ImageComponent extends Component {
BufferedImage renderedImage = null;
public ImageComponent (BufferedImage init) {
public ImageComponent(BufferedImage init) {
renderedImage = init;
}
public BufferedImage swapImage (BufferedImage bi) {
BufferedImage ret = renderedImage ;
renderedImage = bi ;
return ret ;
public BufferedImage swapImage(BufferedImage bi) {
BufferedImage ret = renderedImage;
renderedImage = bi;
return ret;
}
public void paint(Graphics g) {
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 {
private int height = 0;
private int width = 0;
private int pixelSize = 0;
private JFrame myFrame ;
private JFrame myFrame;
private ImageComponent drawComp = null;
private BufferedImage backBuffer = null ;
private BufferedImage frontBuffer = null ;
private BufferedImage backBuffer = null;
private BufferedImage frontBuffer = null;
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 ();
gd.setColor (Color.BLACK) ;
gd.fillRect (0,0, width * pixelSize, height * pixelSize) ;
gd = drawingImage.createGraphics ();
gd.setColor (Color.BLACK) ;
gd.fillRect (0,0, width * pixelSize, height * pixelSize) ;
*/
* Graphics2D gd = initial.createGraphics ();
* gd.setColor (Color.BLACK) ;
* gd.fillRect (0,0, width * pixelSize, height * pixelSize) ;
* gd = drawingImage.createGraphics ();
* gd.setColor (Color.BLACK) ;
* gd.fillRect (0,0, width * pixelSize, height * pixelSize) ;
*/
drawComp = new ImageComponent (frontBuffer);
drawComp.setPreferredSize (new Dimension (width * pixelSize, height * pixelSize)) ;
drawComp.setVisible (true);
drawComp = new ImageComponent(frontBuffer);
drawComp.setPreferredSize(new Dimension(width * pixelSize, height * pixelSize));
drawComp.setVisible(true);
myFrame = new JFrame("Simple Inverse Rasterization Renderer (TSI)");
myFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;
myFrame.add("Center", drawComp );
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.add("Center", drawComp);
myFrame.pack();
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.
* And set its window visible.
*/
public GraphicsWrapper(int width, int height, int pixelSize) {
this.height = height;
this.width = width;
this.pixelSize = pixelSize ;
this.pixelSize = pixelSize;
init();
}
@ -104,97 +101,98 @@ public class GraphicsWrapper {
* on the current draw buffer.
* Does nothing for pixels out of the screen.
*/
public void setPixel (int x, int y, double r, double g, double b) {
r = Math.min (1.0, Math.max (0.0, r));
g = Math.min (1.0, Math.max (0.0, g));
b = Math.min (1.0, Math.max (0.0, b));
public void setPixel(int x, int y, double r, double g, double b) {
r = Math.min(1.0, Math.max(0.0, r));
g = Math.min(1.0, Math.max(0.0, g));
b = Math.min(1.0, Math.max(0.0, b));
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])
* on the current draw buffer.
* on the current draw buffer.
* 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)) {
int argb = 0xFF000000 ;
argb += ((int)r) << (8 * 2) ;
argb += ((int)g) << (8 * 1) ;
argb += ((int)b);
for (int i = 0 ; i < pixelSize ; i++ ) {
for (int j = 0 ; j < pixelSize ; j++ ) {
backBuffer.setRGB(i + (x * pixelSize) , j + (y * pixelSize), argb) ;
}
}
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) {
int argb = 0xFF000000;
argb += ((int) r) << (8 * 2);
argb += ((int) g) << (8 * 1);
argb += ((int) b);
for (int i = 0; i < pixelSize; i++) {
for (int j = 0; j < pixelSize; j++) {
frontBuffer.setRGB(i + (x * pixelSize), j + (y * pixelSize), argb);
}
}
}
}
/**
* Lights the pixel (x,y) with the given color.
* 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)) {
int rgb = color.getRGB ();
for (int i = 0 ; i < pixelSize ; i++ ) {
for (int j = 0 ; j < pixelSize ; j++ ) {
backBuffer.setRGB(i + (x * pixelSize) , j + (y * pixelSize), rgb);
}
}
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) {
int rgb = color.getRGB();
for (int i = 0; i < pixelSize; i++) {
for (int j = 0; j < pixelSize; j++) {
frontBuffer.setRGB(i + (x * pixelSize), j + (y * pixelSize), rgb);
}
}
}
}
/**
* Gets the pixel in the back buffer
*/
public Color getPixel (int x, int y) {
Color color;
*/
public Color getPixel(int x, int y) {
Color color;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) {
color = new Color (backBuffer.getRGB (x, y), false);
} 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;
}
public Color getFrontPixel (int x, int y) {
Color color;
return color;
}
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) {
color = new Color (frontBuffer.getRGB (x, y), false);
} else {
color = Color.BLACK;
}
public Color getFrontPixel(int x, int y) {
Color color;
return color;
}
/**
*
*/
int getWidth () {
return width;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) {
color = new Color(frontBuffer.getRGB(x, y), false);
} else {
color = Color.BLACK;
}
int getHeight () {
return height;
}
return color;
}
/**
*
*/
int getWidth() {
return width;
}
int getHeight() {
return height;
}
/**
* Clear current draw-buffer (ie Paint it black)
*
*/
public void clearBuffer() {
Graphics2D gd = backBuffer.createGraphics ();
gd.setColor (Color.BLACK) ;
gd.fillRect (0,0, width * pixelSize, height * pixelSize) ;
Graphics2D gd = frontBuffer.createGraphics();
gd.setColor(Color.BLACK);
gd.fillRect(0, 0, width * pixelSize, height * pixelSize);
}
/**
@ -202,8 +200,8 @@ public class GraphicsWrapper {
*
*/
public void swapBuffers() {
frontBuffer = drawComp.swapImage (backBuffer) ;
myFrame.repaint ();
frontBuffer = drawComp.swapImage(frontBuffer);
myFrame.repaint();
}
/**
@ -213,5 +211,4 @@ public class GraphicsWrapper {
myFrame.dispose();
}
}

View file

@ -80,12 +80,13 @@ public class Lighting {
h.add(l);
h.normalize();
/* diffuse contribution */
double Id = light.params[3];
/* diffuse contribution */
double I_diff = Id * kd * Math.cos(normal.dot(l));
/* 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;
} catch (InstantiationException ex) {

View file

@ -202,7 +202,6 @@ public class Rasterizer {
Matrix C = makeBarycentricCoordsMatrix(v1, v2, v3);
/* iterate over the triangle's bounding box */
int x_hg = min3(v1.getX(), v2.getX(), v3.getX());
int y_hg = min3(v1.getY(), v2.getY(), v3.getY());
@ -227,7 +226,7 @@ public class Rasterizer {
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) {
if (barycentre.get(i) < -1e-3) {
// on passe au pixel suivant
continue boucle;
}

View file

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