From 7f1890d5376699bd05bffef5a89721fef81e3f7a Mon Sep 17 00:00:00 2001 From: Manuel Daniel Dahmen Date: Fri, 24 Nov 2023 19:17:15 +0100 Subject: [PATCH] Fixed DRAW_LINES in ZBufferImpl issue. Instable #2 ... Updated: ZBufferImpl clean code. Signed-off-by: Manuel Daniel Dahmen --- .../java/one/empty3/library/ZBufferImpl.java | 335 +++++++++--------- .../empty3/library/ZBufferImplRecursive.java | 45 +++ 2 files changed, 203 insertions(+), 177 deletions(-) diff --git a/src/main/java/one/empty3/library/ZBufferImpl.java b/src/main/java/one/empty3/library/ZBufferImpl.java index 4652cf94..63e5eb5f 100644 --- a/src/main/java/one/empty3/library/ZBufferImpl.java +++ b/src/main/java/one/empty3/library/ZBufferImpl.java @@ -79,8 +79,9 @@ public class ZBufferImpl extends Representable implements ZBuffer { protected Scene currentScene; protected int displayType = SURFACE_DISPLAY_TEXT_TRI; protected boolean FORCE_POSITIVE_NORMALS = true; + protected StructureMatrix scale = new StructureMatrix<>(0, Double.class); ZBufferImpl that; - protected StructureMatrix scale = new StructureMatrix(0, Double.class); + public ZBufferImpl() { that = this; scene = new Scene(); @@ -94,7 +95,7 @@ public ZBufferImpl(int l, int h) { dimx = la; dimy = ha; Logger.getAnonymousLogger().log(Level.INFO, "width,height(" + la + ", " + ha + ")"); - this.ime = new ImageMapElement(); + this.ime = new ImageMap(la, ha).getIme(); } @@ -144,7 +145,7 @@ public void accept(Object o) { } public synchronized void draw(Representable r) { - if(r instanceof Precomputable) { + if (r instanceof Precomputable) { ((Precomputable) r).precompute(); } @@ -158,7 +159,7 @@ public synchronized void draw(Representable r) { r.texture().timeNext(); if (r instanceof Scene) { - Scene scene = (Scene)r; + Scene scene = (Scene) r; scene.getObjets().getData1d().forEach(representable -> draw(representable)); return; } else if (r instanceof RepresentableConteneur) { @@ -166,7 +167,7 @@ public synchronized void draw(Representable r) { return; } - if(r.texture() instanceof TextureMov) { + if (r.texture() instanceof TextureMov) { ((TextureMov) r.texture()).nextFrame(); } @@ -214,7 +215,7 @@ public synchronized void draw(Representable r) { ); break; case SURFACE_DISPLAY_LINES: - tracerLines(p1, p2, p3, p4, n.texture(), u, v , u + n.getIncrU(), v + n.getIncrV(), n); + tracerLines(p1, p2, p3, p4, n.texture(), u, v, u + n.getIncrU(), v + n.getIncrV(), n); break; case SURFACE_DISPLAY_POINTS: testDeep(p1); @@ -268,24 +269,24 @@ public synchronized void draw(Representable r) { } p1 = n.calculerPoint3D(u, v); - if((n.getQuad_not_computed()&ParametricSurface.QUAD_NOT_COMPUTE_U2)==0) { + if ((n.getQuad_not_computed() & ParametricSurface.QUAD_NOT_COMPUTE_U2) == 0) { p2 = n.calculerPoint3D(u2, v); } else { Point3D next = n.getNextV(u2, v); - p2 = next!=null?next:n.calculerPoint3D(u2, v); + p2 = next != null ? next : n.calculerPoint3D(u2, v); } - if((n.getQuad_not_computed()&ParametricSurface.QUAD_NOT_COMPUTE_U2)==0 - || (n.getQuad_not_computed()&ParametricSurface.QUAD_NOT_COMPUTE_V2)==0) + if ((n.getQuad_not_computed() & ParametricSurface.QUAD_NOT_COMPUTE_U2) == 0 + || (n.getQuad_not_computed() & ParametricSurface.QUAD_NOT_COMPUTE_V2) == 0) p3 = n.calculerPoint3D(u2, v2); else { Point3D next = n.getNextUV(u2, v2); - p3 = next!=null?next:n.calculerPoint3D(u2, v2); + p3 = next != null ? next : n.calculerPoint3D(u2, v2); } - if((n.getQuad_not_computed()&ParametricSurface.QUAD_NOT_COMPUTE_V2)==0) { + if ((n.getQuad_not_computed() & ParametricSurface.QUAD_NOT_COMPUTE_V2) == 0) { p4 = n.calculerPoint3D(u, v2); } else { Point3D next = n.getNextV(u, v2); - p4 = next!=null?next:n.calculerPoint3D(u, v2); + p4 = next != null ? next : n.calculerPoint3D(u, v2); } if (n instanceof HeightMapSurface) { HeightMapSurface h = (HeightMapSurface) n; @@ -369,7 +370,7 @@ public synchronized void draw(Representable r) { Point3D p = ((Point3DS) r).calculerPoint3D(0); testDeep(rotate(p, r), r.texture()); } else if (r instanceof LineSegment) { - LineSegment s = (LineSegment)r; + LineSegment s = (LineSegment) r; Point3D pO = s.getOrigine(); Point3D pE = s.getExtremite(); line(pO, pE, s.texture()); @@ -461,10 +462,10 @@ private void testDeep(Point3D rotate, ITexture texture) { protected void tracerLines(Point3D p1, Point3D p2, Point3D p3, Point3D p4, ITexture texture, double u1, double v1, double u2, double v2, ParametricSurface n) { - line(p1, p2, n!=null?n.texture():texture, u1, v1, u2, v1, n); - line(p2, p3, n!=null?n.texture():texture, u2, v1, u2, v2, n); - line(p3, p4, n!=null?n.texture():texture, u2, v2, u1, v2, n); - line(p4, p1, n!=null?n.texture():texture, u1, v2, u1, v1, n); + line(p1, p2, n != null ? n.texture() : texture, u1, v1, u2, v1, n); + line(p2, p3, n != null ? n.texture() : texture, u2, v1, u2, v2, n); + line(p3, p4, n != null ? n.texture() : texture, u2, v2, u1, v2, n); + line(p4, p1, n != null ? n.texture() : texture, u1, v2, u1, v1, n); } @@ -481,7 +482,7 @@ public int getColorAt(Point p) { } public int[] getData() { - return ime.Scolor; + return ime.color; } public ZBuffer getInstance(int x, int y) { @@ -572,7 +573,7 @@ public void line(Point3D p1, Point3D p2, ITexture t) { return; } Point3D n = p1.moins(p2).norme1(); - double iterate = Math.max(maxDistance(x1, x2) * 4 + 1, 1/MIN_INCR); + double iterate = Math.max(maxDistance(x1, x2) * 4 + 1, 1 / MIN_INCR); for (int i = 0; i < iterate; i++) { Point3D p = p1.plus(p2.moins(p1).mult(i / iterate)); testDeep(p, t.getColorAt(0.5, 0.5)); @@ -610,10 +611,10 @@ public void line(Point3D p1, Point3D p2, ITexture texture, double u1, double v1, double iterate = Math.sqrt((x1.getX() - x2.getX()) * (x1.getX() - x2.getX()) + (x1.getY() - x2.getY()) * (x1.getY() - x2.getY())) + 1; for (int i = 0; i < iterate; i++) { - Point3D p = p1.plus(n.mult(i / iterate*d)); + Point3D p = p1.plus(n.mult(i / iterate * d)); double u = u1 + i / iterate * (u2 - u1); double v = v1 + i / iterate * (v2 - v1); - if (surface != null && surface.texture()!=null) { + if (surface != null && surface.texture() != null) { //p = surface.calculerPoint3D(u2, v2); testDeep(p, surface.texture(), u, v, surface); } else { @@ -729,7 +730,7 @@ public void plotPoint(Color color, Point3D p) { public void plotPoint(Point3D p) { if (p != null && p.texture() != null) { - ime.dessine(p, p.texture().getColorAt(0,0)); + ime.dessine(p, p.texture().getColorAt(0, 0)); } } @@ -779,19 +780,18 @@ public void next() { } @Override - public void testDeep(Point3D p, Color c) { - testDeep(p, c.getRGB()); + public void testDeep(Point3D point3D) { + ime.testDeep(point3D); } @Override - public void testDeep(Point3D p, int c) { + public void testDeep(Point3D p, Color c) { ime.testDeep(p, c); } - public void testDeep(Point3D p) { - if (p != null && p.texture() != null) { - testDeep(p, p.texture().getColorAt(0., 0.)); - } + @Override + public void testDeep(Point3D p, int c) { + ime.testDeep(p, c); } public void testPoint(Point3D p, Color c) { @@ -856,7 +856,7 @@ public void tracerTriangle(Point3D pp1, Point3D pp2, Point3D pp3, if (!checkScreen(p3) || isOccupied(pp3)) checkedFalse++; - if (checkedFalse>=CHECKED_POINT_SIZE_TRI) { + if (checkedFalse >= CHECKED_POINT_SIZE_TRI) { return; } @@ -893,6 +893,7 @@ public void tracerTriangle(Point3D pp1, Point3D pp2, Point3D pp3, } } } + @Override public boolean checkScreen(Point p1) { return p1 != null && p1.getX() >= 0d && p1.getX() < la @@ -921,7 +922,6 @@ public void tracerQuad(Point3D pp1, Point3D pp2, Point3D pp3, Point3D pp4, IText checkedFalse++; - if (p1 == null || p2 == null || p3 == null || p4 == null || checkedFalse >= CHECKED_POINT_SIZE_QUADS) return; @@ -932,38 +932,38 @@ public void tracerQuad(Point3D pp1, Point3D pp2, Point3D pp3, Point3D pp4, IText Point3D normale = triBas.normale(); double inter = Math.max(1 / (maxDistance(p1, p2, p3, p4) + 1) / 3, MIN_INCR); //if (inter > MIN_INCR) { - for (double a = 0; a < 1.0; a += inter) { - Point3D pElevation1 = pp1.plus(pp1.mult(-1d).plus(pp2).mult(a)); - Point3D pElevation2 = pp4.plus(pp4.mult(-1d).plus(pp3).mult(a)); - - - double inter2 = Math.max(1. / (maxDistance(camera().coordonneesPoint2D(pElevation1, this), - camera().coordonneesPoint2D(pElevation2, this)) + 1.) / 3., MIN_INCR); - //if (inter2 > MIN_INCR) - for (double b = 0; b < 1.0; b += inter2) { - Point3D pFinal = (pElevation1.plus(pElevation1.mult(-1d).plus(pElevation2).mult(b))); - double uPoint = u0 + (u1 - u0) * a; - double vPoint = v0 + (v1 - v0) * b; - pFinal.setNormale(normale); - pFinal.texture(texture); - pFinal.setNormale(n.calculerNormale3D(uPoint, vPoint)); - if (n != null) { - if (displayType == DISPLAY_ALL) { - pFinal = n.calculerPoint3D(uPoint, vPoint); - pFinal.texture(texture); - } else { - pFinal.setNormale(normale); - pFinal.texture(texture); - - } - } - if (displayType <= SURFACE_DISPLAY_TEXT_QUADS) { - testDeep(pFinal, n.texture().getColorAt(uPoint, vPoint)); + for (double a = 0; a < 1.0; a += inter) { + Point3D pElevation1 = pp1.plus(pp1.mult(-1d).plus(pp2).mult(a)); + Point3D pElevation2 = pp4.plus(pp4.mult(-1d).plus(pp3).mult(a)); + + + double inter2 = Math.max(1. / (maxDistance(camera().coordonneesPoint2D(pElevation1, this), + camera().coordonneesPoint2D(pElevation2, this)) + 1.) / 3., MIN_INCR); + //if (inter2 > MIN_INCR) + for (double b = 0; b < 1.0; b += inter2) { + Point3D pFinal = (pElevation1.plus(pElevation1.mult(-1d).plus(pElevation2).mult(b))); + double uPoint = u0 + (u1 - u0) * a; + double vPoint = v0 + (v1 - v0) * b; + pFinal.setNormale(normale); + pFinal.texture(texture); + pFinal.setNormale(n.calculerNormale3D(uPoint, vPoint)); + if (n != null) { + if (displayType == DISPLAY_ALL) { + pFinal = n.calculerPoint3D(uPoint, vPoint); + pFinal.texture(texture); } else { - testDeep(pFinal, col); + pFinal.setNormale(normale); + pFinal.texture(texture); + } } + if (displayType <= SURFACE_DISPLAY_TEXT_QUADS) { + testDeep(pFinal, n.texture().getColorAt(uPoint, vPoint)); + } else { + testDeep(pFinal, col); + } } + } //} } @@ -1189,18 +1189,7 @@ public void accept(Representable representable) { public void idzpp() { idImg++; - ime = new ImageMapElement(); -/* for(int i=0;i= 0 & x < la & y >= 0 & y < ha - && (deep >= ime.getElementProf(x, y))) { - Point3D n = x3d.getNormale(); - // Vérifier : n.eye>0 sinon n = -n Avoir toutes les normales - // dans la même direction par rapport à la caméra. - if (n == null || n.norme() == 0) - n = x3d.moins(camera().getEye()); - else if (FORCE_POSITIVE_NORMALS && n.norme1().dot(scene().cameraActive().getEye().norme1()) < 0) - n = n.mult(-1); - cc = scene().lumiereTotaleCouleur(c, x3d, n); - ime.setElementID(x, y, idImg); - ime.setElementCouleur(x, y, cc); - ime.setDeep(x, y, deep); - ime.setElementPoint(x, y, x3d); - ime.setElementProf(x, y, deep); - return true; - } - return false; - } public void testDeep(Point3D p, Point3D n, Color c) { // Color cc = c.getCouleur(); p.setNormale(n); - testDeep(p, c.getRGB()); + ime.testDeep(p, c.getRGB()); } public void testDeep(Point3D p, Point3D n, int c) { @@ -1554,7 +1519,7 @@ public void testDeep(Point3D p, Point3D n, int c) { public boolean testDeep(Point3D p, ITexture texture, double u, double v, Representable representable) { if (texture.getColorAt(u, v) != texture.getTransparent() - && testDeep(p, texture.getColorAt(u, v))) { + && ime.testDeep(p, texture.getColorAt(u, v))) { Point point = camera().coordonneesPoint2D(p, that); int x = point.x, y = point.y; ime.getuMap()[x][y] = u; @@ -1565,23 +1530,6 @@ && testDeep(p, texture.getColorAt(u, v))) { return false; } - public boolean testDeep(Point3D p, ITexture texture) { - return testDeep(p, texture.getColorAt(0.5, 0.5)); - } - - public void dessine(Point3D p, ITexture texture) { - ime.dessine(p, texture.getColorAt(0.5, 0.5)); - - } - - public void testDeep(Point3D p) { - testDeep(p, (p != null && p.texture() != null) ? p.texture() : CFAST);//WTF - } - - public void testDeep(Point3D p, Color c) { - testDeep(p, p.getNormale(), c); - } - public void testDeep(Point3D pFinal, Point3D point3D, int colorAt, Representable n) { testDeep(pFinal, point3D, colorAt); Point point = camera().coordonneesPoint2D(pFinal, that); @@ -1593,54 +1541,57 @@ public void testDeep(Point3D pFinal, Point3D point3D, int colorAt, Representable public void testDeep(Point3D pFinal, int colorAt, Representable n) { testDeep(pFinal, pFinal.getNormale(), colorAt, n); } + + public ImageMapElement getIme() { + return new ImageMapElement(); + } } public class ImageMapElement { - int couleur_fond_int = -1; - ImageMapElement instance; - Representable[][] Simerepresentable; - double[][] uMap; - double[][] vMap; - Representable[][] rMap; - Point3D[][] Scordinate; - int[] Scolor; - long[][] Simeid; - double[][] Simeprof; + int couleur_fond_int = -1; + ImageMapElement instance; + Representable[][] imeRepresentable; + double[][] uMap; + double[][] vMap; + Representable[][] rMap; + Point3D[][] coordinate; + int[] color; + long[][] imeId; + double[][] imeProf; public ImageMapElement() { - ime = this; - ime.Scordinate = new Point3D[la][ha]; - ime.Scolor = new int[la * ha]; - ime.Simeid = new long[la][ha]; - ime. Simeprof = new double[la][ha]; - ime.Simerepresentable = new Representable[la][ha]; - uMap = new double[la][ha]; - vMap = new double[la][ha]; - rMap = new Representable[la][ha]; + this.coordinate = new Point3D[la][ha]; + this.color = new int[la * ha]; + this.imeId = new long[la][ha]; + this.imeProf = new double[la][ha]; + this.imeRepresentable = new Representable[la][ha]; + this.uMap = new double[la][ha]; + this.vMap = new double[la][ha]; + this.rMap = new Representable[la][ha]; for (int i = 0; i < la; i++) { for (int j = 0; j < ha; j++) { - ime.Simeprof[i][j] = INFINITY.getZ(); - ime.Simeid[i][j] = idImg; - ime.Scolor[j * la + i] = COULEUR_FOND_INT(i, j); - Simerepresentable[i][j] = null; + this.imeProf[i][j] = INFINITY.getZ(); + this.imeId[i][j] = idImg; + this.color[j * la + i] = COULEUR_FOND_INT(i, j); + this.imeRepresentable[i][j] = null; } } } public Representable getElementRepresentable(int x, int y) { - if (checkCordinates(x, y)) { - return Simerepresentable[x][y]; + if (checkCoordinates(x, y)) { + return imeRepresentable[x][y]; } return null; } public void setElementRepresentable(int x, int y, Representable representable) { - Simerepresentable[x][y] = representable; + imeRepresentable[x][y] = representable; } - public boolean checkCordinates(int x, int y) { + public boolean checkCoordinates(int x, int y) { return x >= 0 && x < la && y >= 0 && y < ha; } @@ -1650,51 +1601,51 @@ public int COULEUR_FOND_INT(int x, int y) { } public int getElementCouleur(int x, int y) { - if (checkCordinates(x, y) - && ime.Simeid[x][y] == idImg - && ime.Simeprof[x][y] > INFINITY.getZ()) { - return getRGBInt(ime.Scolor, x, y); + if (checkCoordinates(x, y) + && ime.imeId[x][y] == idImg + && ime.imeProf[x][y] > INFINITY.getZ()) { + return getRGBInt(ime.color, x, y); } else { return COULEUR_FOND_INT(x, y); } } public long getElementID(int x, int y) { - return ime.Simeid[x][y]; + return ime.imeId[x][y]; } public Point3D getElementPoint(int x, int y) { - return ime.Scordinate[x][y]; + return ime.coordinate[x][y]; } protected double getElementProf(int x, int y) { - return ime.Simeprof[x][y]; + return ime.imeProf[x][y]; } public ImageMapElement getInstance(int x, int y) { - if (instance == null) { - instance = new ImageMapElement(); + if (ime.instance == null) { + ime.instance = this; } return instance; } protected int getRGBInt(int[] sc, int x, int y) { - return sc[x + y * la]; + return color[x + y * la]; } public void setElementCouleur(int x, int y, int pc) { setElementID(x, y, idImg); - setRGBInts(pc, ime.Scolor, x, y); + setRGBInts(pc, ime.color, x, y); } public void setElementID(int x, int y, long id) { - ime.Simeid[x][y] = idImg; + ime.imeId[x][y] = idImg; } public void setElementPoint(int x, int y, Point3D px) { setElementID(x, y, idImg); - ime.Scordinate[x][y] = px; + ime.coordinate[x][y] = px; } public double[][] getuMap() { @@ -1722,18 +1673,17 @@ public void setrMap(Representable[][] rMap) { } protected void setDeep(int x, int y, double d) { - if (checkCordinates(x, y)) { - ime.Simeprof[x][y] = (float) d; + if (checkCoordinates(x, y)) { + ime.imeProf[x][y] = (float) d; } - } protected void setRGBInts(int rgb, int[] sc, int x, int y) { - sc[x + y * la] = rgb; + color[x + y * la] = rgb; } public void setElementProf(int i, int j, double pr) { - ime.Simeprof[i][j] = pr; + ime.imeProf[i][j] = pr; } public void dessine(Point3D p, Color colorAt) { @@ -1741,7 +1691,7 @@ public void dessine(Point3D p, Color colorAt) { } public void dessine(Point3D x3d, int c1) { - double [] c = Lumiere.getDoubles(c1); + double[] c = Lumiere.getDoubles(c1); Point ce = camera().coordonneesPoint2D(x3d, that); if (ce == null) { return; @@ -1771,15 +1721,46 @@ public void dessine(Point3D x3d, int c1) { } } - public void testDeep(Point3D p, int c) { + public void testDeep(Point3D p, Color c) { + testDeep(p, c.getRGB()); } - } - - public double getScale() { - return scale.getElem(); - } - public void setScale(double scale) { - this.scale.setElem(scale); + public void testDeep(Point3D p) { + if (p != null && p.texture() != null) { + testDeep(p, p.texture().getColorAt(0., 0.)); + } + } + public boolean testDeep(Point3D x3d, int c) { + if (x3d == null) + return false; + //x3d = camera().calculerPointDansRepere(x3d); + if (x3d == null) + return false; + int cc = c; + Point ce = camera().coordonneesPoint2D(x3d, that); + if (ce == null) + return false; + int x = (int) ce.getX(); + int y = (int) ce.getY(); + double deep = camera().distanceCamera(x3d); + if (x >= 0 & x < la & y >= 0 & y < ha + && (deep >= ime.getElementProf(x, y))) { + Point3D n = x3d.getNormale(); + // Vérifier : n.eye>0 sinon n = -n Avoir toutes les normales + // dans la même direction par rapport à la caméra. + if (n == null || n.norme() == 0) + n = x3d.moins(camera().getEye()); + else if (FORCE_POSITIVE_NORMALS && n.norme1().dot(scene().cameraActive().getEye().norme1()) < 0) + n = n.mult(-1); + cc = scene().lumiereTotaleCouleur(c, x3d, n); + ime.setElementID(x, y, idImg); + ime.setElementCouleur(x, y, cc); + ime.setDeep(x, y, deep); + ime.setElementPoint(x, y, x3d); + ime.setElementProf(x, y, deep); + return true; + } + return false; + } } } diff --git a/src/main/java/one/empty3/library/ZBufferImplRecursive.java b/src/main/java/one/empty3/library/ZBufferImplRecursive.java index e69de29b..ecd78b89 100644 --- a/src/main/java/one/empty3/library/ZBufferImplRecursive.java +++ b/src/main/java/one/empty3/library/ZBufferImplRecursive.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023. Manuel Daniel Dahmen + * + * + * Copyright 2012-2023 Manuel Daniel Dahmen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/*__ + * * + * Global license GNU GPL v2 + * author Manuel Dahmen _manuel.dahmen@gmx.com_ + */ +package one.empty3.library; + +import one.empty3.library.core.nurbs.*; +import one.empty3.library.core.tribase.Precomputable; +import one.empty3.pointset.PCont; + +import java.awt.*; +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.function.Consumer; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class ZBufferImplRecursive extends ZBufferImpl { +} +/*__ + * * Classe de rendu graphique + */