From 320be27f8c3c5d51e00b3d6d0879b65d525d3774 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sat, 29 May 2021 00:46:31 +0200 Subject: [PATCH] [DEV] add capability to update mesh --- .../loader3d/resources/ResourceMesh.java | 385 +++++++++--------- .../resources/ResourceStaticMesh.java | 7 + 2 files changed, 207 insertions(+), 185 deletions(-) diff --git a/src/org/atriasoft/loader3d/resources/ResourceMesh.java b/src/org/atriasoft/loader3d/resources/ResourceMesh.java index 9b08bab..a7bf7b2 100644 --- a/src/org/atriasoft/loader3d/resources/ResourceMesh.java +++ b/src/org/atriasoft/loader3d/resources/ResourceMesh.java @@ -5,10 +5,12 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import org.atriasoft.etk.Color; import org.atriasoft.etk.Uri; import org.atriasoft.etk.math.Vector2f; +import org.atriasoft.etk.math.Vector2i; import org.atriasoft.etk.math.Vector3f; import org.atriasoft.gale.backend3d.OpenGL.RenderMode; import org.atriasoft.gale.resource.Resource; @@ -27,6 +29,7 @@ public class ResourceMesh extends ResourceStaticMesh { protected List listFacesNormal = new ArrayList<>(); //!< List of all Face normal, when calculated protected List listVertexNormal = new ArrayList<>(); //!< List of all Face normal, when calculated protected Map listFaces = new HashMap<>(); //!< List of all Face for the mesh + protected Map listFacesDrawIndex = new HashMap<>(); //!< List of all Face for the mesh protected Map listPaletteFaces = new HashMap<>(); //!< List of all Face for the mesh protected Map materials = new HashMap<>(); protected Map palettes = new HashMap<>(); // this is for information when we use external palette @@ -36,6 +39,36 @@ public class ResourceMesh extends ResourceStaticMesh { //void (*this.functionFreeShape)(void* _pointer); //private void* this.pointerShape; //!< all mesh have a basic shape (bullet or other) the void pointer mermit to not depent on the bullet lib + // TODO: Rework this: this is for the GUI dynamic modiification: + List outPosition = null; + + public List getGeneratedPosition() { + return this.outPosition; + } + public void setModifiedPosition(final List position) { + this.vao.setPosition(position); + this.vao.flush(); + } + public void setModifiedPosition(final Vector3f[] position) { + this.vao.setPosition(position); + this.vao.flush(); + } + public void setModifiedPosition(final float[] position) { + this.vao.setPosition(position); + this.vao.flush(); + } + + @Override + public void render() { + super.render(); + } + public void render(final String layerName) { + Vector2i elements = this.listFacesDrawIndex.get(layerName); + super.render(elements.x(), elements.y()); + } + public Set getLayers() { + return this.listFacesDrawIndex.keySet(); + } void clean(){ //this.physics.clear(); @@ -53,7 +86,7 @@ public class ResourceMesh extends ResourceStaticMesh { public ResourceMesh() { this(null); } - public ResourceMesh( Uri _fileName) { + public ResourceMesh( final Uri _fileName) { super(_fileName); Log.verbose("Load a new mesh : '" + _fileName + "'"); // ------------------------------------------------------------------------------------------------ @@ -62,25 +95,6 @@ public class ResourceMesh extends ResourceStaticMesh { // TO facilitate some debugs we add a name of the VBO: this.vao.setName("[VBO] of " + _fileName); // ------------------------------------------------------------------------------------------------ - // load the curent file : - String extention = _fileName.getExtention().toLowerCase(); - // select the corect loader : - if (extention == "obj") { - Log.error("Can not load OBJ file ..."); -// if (loadOBJ(_fileName) == false) { -// Log.error("Error To load OBJ file " + _fileName ); -// return; -// } - } else if (extention == "emf") { - Log.error("Can not load EMF file ..."); -// if (loadEMF(_fileName) == false) { -// Log.error("Error To load EMF file " + _fileName ); -// return; -// } - //Log.critical("Load a new mesh : '" + _fileName + "' (end)"); - } else { - // nothing to do == > reject an empty mesh ==> user manage it ... - } } // public void draw(Matrix4f _positionMatrix) { @@ -214,7 +228,7 @@ public class ResourceMesh extends ResourceStaticMesh { // Log.verbose("draw Mesh : " + this.name + " ( end )"); // } -void setNormalMode(NormalMode _mode) { +void setNormalMode(final NormalMode _mode) { this.normalMode = _mode; } // public void draw(Matrix4f _positionMatrix, @@ -292,12 +306,9 @@ void setNormalMode(NormalMode _mode) { // } // _draw.drawLine(vertices, tmpColor, _positionMatrix); // } - public void generateElementFromPalettes() { - - } public void generateVBO() { - generateElementFromPalettes(); - List outPosition = new ArrayList<>(); + this.listFacesDrawIndex.clear(); + this.outPosition = new ArrayList<>(); List outNormal = new ArrayList<>(); List outTexturePosition = new ArrayList<>(); List outColor = new ArrayList<>(); @@ -310,6 +321,7 @@ void setNormalMode(NormalMode _mode) { } Log.warning("Generate VBO for nb faces layers: " + this.listFaces.size() + " list layer=" + this.listFaces.keySet()); + // generate element in 2 pass: // - create new index depending on a vertex is a unique component of position, texture, normal @@ -318,6 +330,7 @@ void setNormalMode(NormalMode _mode) { for (Entry elem : this.listFaces.entrySet()) { // clean faces indexes : elem.getValue().index().clear(); + int outPositionIndewStart = this.outPosition.size(); int nbIndicInFace = 3; if (this.materials.containsKey(elem.getKey()) == false) { Log.warning("missing materials : '" + elem.getKey() + "' not in " + this.materials.keySet() ); @@ -396,19 +409,22 @@ void setNormalMode(NormalMode _mode) { // } // #endif if (elementFind == false) { - outPosition.add(position); + this.outPosition.add(position); if (this.normalMode != NormalMode.NONE) { outNormal.add(normal); } outTexturePosition.add(texturepos); outColor.add(color); - vertexVBOId[indice] = outPosition.size()-1; + vertexVBOId[indice] = this.outPosition.size()-1; } } for(int indice=0 ; indice outIndex = new ArrayList<>(); if (this.listPaletteFaces.size() != 0) { + int outPositionIndewStart = this.outPosition.size(); FaceIndexing outputIndex = new FaceIndexing(); //Add palette virtual material: this.listFaces.put("palette", outputIndex); @@ -473,13 +490,13 @@ void setNormalMode(NormalMode _mode) { break; } - outPosition.add(position); + this.outPosition.add(position); if (this.normalMode != NormalMode.NONE) { outNormal.add(normal); } outTexturePosition.add(texturepos); outColor.add(color); - vertexVBOId[indice] = outPosition.size()-1; + vertexVBOId[indice] = this.outPosition.size()-1; } for(int indice=0 ; indice outNormal = new ArrayList<>(); @@ -547,9 +565,9 @@ void setNormalMode(NormalMode _mode) { // add the vertex from all the element in the list for face when the element in the face ... for(int jjj=0 ; jjj getPhysicalProperties() { // for (auto it: this.physics) { @@ -675,13 +693,13 @@ void setNormalMode(NormalMode _mode) { * @brief Add in the faces list the layer requested * @param _layerName face index to add */ - public void addFaceIndexing( String _layerName){ + public void addFaceIndexing( final String _layerName){ if (this.listFaces.containsKey(_layerName) == false) { FaceIndexing empty = new FaceIndexing(); this.listFaces.put(_layerName, empty); } } - public void addPoint( String _layerName, Vector3f _pos, Color _color) { + public void addPoint( final String _layerName, final Vector3f _pos, final Color _color) { if ( this.listFaces.containsKey(_layerName) == false || this.materials.containsKey(_layerName) == false) { Log.error("Mesh layer : " + _layerName + " does not exist in list faces=" + this.listFaces.containsKey(_layerName) + " materials=" + this.listFaces.containsKey(_layerName) + " ..."); @@ -702,10 +720,10 @@ void setNormalMode(NormalMode _mode) { this.listFaces.get(_layerName).face().add(tmpFace); } - public void addLine( String _layerName, Vector3f _pos1, Vector3f _pos2, Color _color) { + public void addLine( final String _layerName, final Vector3f _pos1, final Vector3f _pos2, final Color _color) { addLine( _layerName, _pos1, _pos2, _color, _color); } - public void addLine( String _layerName, Vector3f _pos1, Vector3f _pos2, Color _color1, Color _color2) { + public void addLine( final String _layerName, final Vector3f _pos1, final Vector3f _pos2, final Color _color1, final Color _color2) { if ( this.listFaces.containsKey(_layerName) == false || this.materials.containsKey(_layerName) == false) { Log.error("Mesh layer : " + _layerName + " does not exist in list faces=" + this.listFaces.containsKey(_layerName) + " materials=" + this.listFaces.containsKey(_layerName) + " ..."); @@ -729,13 +747,13 @@ void setNormalMode(NormalMode _mode) { tmpFace.setColor(color1, color2, color2); this.listFaces.get(_layerName).face().add(tmpFace); } - public void addLines( String _layerName, List _list, Color _color) { + public void addLines( final String _layerName, final List _list, final Color _color) { for (int iii=1; iii<_list.size(); ++iii) { addLine(_layerName, _list.get(iii-1), _list.get(iii), _color); } } - public void addLines( String _layerName, List _list, List _color) { + public void addLines( final String _layerName, final List _list, final List _color) { if (_color.size() != _list.size()) { Log.error("Can not add line with changing color without same number of color"); return; @@ -747,33 +765,33 @@ void setNormalMode(NormalMode _mode) { - /** - * @not_in_doc - * @brief draw a colored triangle (usefull for debug and test) - * @param _layerName Material and face indexing layer name - * @param _pos1 First point position - * @param _pos2 Second point position - * @param _pos3 Third point position - * @param _color1 color of the _pos1 element - * @param _color2 color of the _pos2 element - * @param _color3 color of the _pos3 element - */ - public void addTriangle( String _layerName, Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, Color _color) { + /** + * @not_in_doc + * @brief draw a colored triangle (usefull for debug and test) + * @param _layerName Material and face indexing layer name + * @param _pos1 First point position + * @param _pos2 Second point position + * @param _pos3 Third point position + * @param _color1 color of the _pos1 element + * @param _color2 color of the _pos2 element + * @param _color3 color of the _pos3 element + */ + public void addTriangle( final String _layerName, final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, final Color _color) { addTriangle(_layerName, _pos1, _pos2, _pos3, _color, _color, _color); } - /** - * @not_in_doc - * @brief draw a colored triangle (usefull for debug and test) - * @param _layerName Material and face indexing layer name - * @param _pos1 First point position - * @param _pos2 Second point position - * @param _pos3 Third point position - * @param _color1 color of the _pos1 element - * @param _color2 color of the _pos2 element - * @param _color3 color of the _pos3 element - */ - public void addTriangle( String _layerName, Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, - Color _color1, Color _color2, Color _color3){ + /** + * @not_in_doc + * @brief draw a colored triangle (usefull for debug and test) + * @param _layerName Material and face indexing layer name + * @param _pos1 First point position + * @param _pos2 Second point position + * @param _pos3 Third point position + * @param _color1 color of the _pos1 element + * @param _color2 color of the _pos2 element + * @param _color3 color of the _pos3 element + */ + public void addTriangle( final String _layerName, final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, + final Color _color1, final Color _color2, final Color _color3){ if ( this.listFaces.containsKey(_layerName) == false || this.materials.containsKey(_layerName) == false) { Log.error("Mesh layer : " + _layerName + " does not exist in list faces=" + this.listFaces.containsKey(_layerName) + " materials=" + this.listFaces.containsKey(_layerName) + " ..."); @@ -802,61 +820,60 @@ void setNormalMode(NormalMode _mode) { this.listFaces.get(_layerName).face().add(tmpFace); } else { Log.error("try to add Quad in a mesh material section that not support Quad"); - return; } } - /** - * @not_in_doc - * @brief draw a colored quad (usefull for debug and test) - * @param _layerName Material and face indexing layer name - * @param _pos1 First point position - * @param _pos2 Second point position - * @param _pos3 Third point position - * @param _pos4 faurth point position - * @param _color color of all elements - */ - public void addQuad( String _layerName, Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, Vector3f _pos4, Color _color) { - addQuad(_layerName, _pos1, _pos2, _pos3, _pos4, _color, _color, _color, _color); - } - /** - * @not_in_doc - * @brief draw a colored quad (usefull for debug and test) - * @param _layerName Material and face indexing layer name - * @param _pos1 First point position - * @param _pos2 Second point position - * @param _pos3 Third point position - * @param _pos4 faurth point position - * @param _color1 color of the _pos1 element - * @param _color2 color of the _pos2 element - * @param _color3 color of the _pos3 element - * @param _color4 color of the _pos4 element - */ - public void addQuad( String _layerName, Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, Vector3f _pos4, - Color _color1, Color _color2, Color _color3, Color _color4) { - addTriangle(_layerName, _pos1, _pos2, _pos3, _color1, _color2, _color3); - addTriangle(_layerName, _pos1, _pos3, _pos4, _color1, _color3, _color4); - } - /** - * @not_in_doc - * @brief draw a textured colored triangle (usefull for debug and test) - * @param _layerName Material and face indexing layer name - * @param _pos1 First point position - * @param _pos2 Second point position - * @param _pos3 Third point position - * @param _color color of all elements - * @param _uv1 texture position of the _pos1 element - * @param _uv2 texture position of the _pos2 element - * @param _uv3 texture position of the _pos3 element - */ - public void addTriangle( String _layerName, - Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, - Vector2f _uv1, Vector2f _uv2, Vector2f _uv3, - Color _color) { - addTriangle(_layerName, _pos1, _pos2, _pos3, _uv1, _uv2, _uv3, _color, _color, _color); - } - public void addTriangle( String _layerName, - Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, - Vector2f _uv1, Vector2f _uv2, Vector2f _uv3){ + /** + * @not_in_doc + * @brief draw a colored quad (usefull for debug and test) + * @param _layerName Material and face indexing layer name + * @param _pos1 First point position + * @param _pos2 Second point position + * @param _pos3 Third point position + * @param _pos4 faurth point position + * @param _color color of all elements + */ + public void addQuad( final String _layerName, final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, final Vector3f _pos4, final Color _color) { + addQuad(_layerName, _pos1, _pos2, _pos3, _pos4, _color, _color, _color, _color); + } + /** + * @not_in_doc + * @brief draw a colored quad (usefull for debug and test) + * @param _layerName Material and face indexing layer name + * @param _pos1 First point position + * @param _pos2 Second point position + * @param _pos3 Third point position + * @param _pos4 faurth point position + * @param _color1 color of the _pos1 element + * @param _color2 color of the _pos2 element + * @param _color3 color of the _pos3 element + * @param _color4 color of the _pos4 element + */ + public void addQuad( final String _layerName, final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, final Vector3f _pos4, + final Color _color1, final Color _color2, final Color _color3, final Color _color4) { + addTriangle(_layerName, _pos1, _pos2, _pos3, _color1, _color2, _color3); + addTriangle(_layerName, _pos1, _pos3, _pos4, _color1, _color3, _color4); + } + /** + * @not_in_doc + * @brief draw a textured colored triangle (usefull for debug and test) + * @param _layerName Material and face indexing layer name + * @param _pos1 First point position + * @param _pos2 Second point position + * @param _pos3 Third point position + * @param _color color of all elements + * @param _uv1 texture position of the _pos1 element + * @param _uv2 texture position of the _pos2 element + * @param _uv3 texture position of the _pos3 element + */ + public void addTriangle( final String _layerName, + final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, + final Vector2f _uv1, final Vector2f _uv2, final Vector2f _uv3, + final Color _color) { + addTriangle(_layerName, _pos1, _pos2, _pos3, _uv1, _uv2, _uv3, _color, _color, _color); + } + public void addTriangle( final String _layerName, + final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, + final Vector2f _uv1, final Vector2f _uv2, final Vector2f _uv3){ addTriangle(_layerName,_pos1, _pos2, _pos3, _uv1, _uv2, _uv3, Color.WHITE, Color.WHITE, Color.WHITE); @@ -875,10 +892,10 @@ void setNormalMode(NormalMode _mode) { * @param _uv2 texture position of the _pos2 element * @param _uv3 texture position of the _pos3 element */ - public void addTriangle( String _layerName, - Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, - Vector2f _uv1, Vector2f _uv2, Vector2f _uv3, - Color _color1, Color _color2, Color _color3){ + public void addTriangle( final String _layerName, + final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, + final Vector2f _uv1, final Vector2f _uv2, final Vector2f _uv3, + final Color _color1, final Color _color2, final Color _color3){ Log.info("add Triangle: " + _layerName + " ..."); if ( this.listFaces.containsKey(_layerName) == false @@ -911,63 +928,63 @@ void setNormalMode(NormalMode _mode) { tmpFace.setColor(color1, color2, color3); this.listFaces.get(_layerName).face().add(tmpFace); Log.info(" nbFace: " + this.listFaces.get(_layerName).face().size()); - } + } - /** - * @not_in_doc - * @brief draw a textured colored quad (usefull for debug and test) - * @param _layerName Material and face indexing layer name - * @param _pos1 First point position - * @param _pos2 Second point position - * @param _pos3 Third point position - * @param _pos4 faurth point position - * @param _color color of all elements - * @param _uv1 texture position of the _pos1 element - * @param _uv2 texture position of the _pos2 element - * @param _uv3 texture position of the _pos3 element - * @param _uv4 texture position of the _pos4 element - */ - public void addQuad( String _layerName, - Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, Vector3f _pos4, - Vector2f _uv1, Vector2f _uv2, Vector2f _uv3, Vector2f _uv4, - Color _color) { - addQuad(_layerName, _pos1, _pos2, _pos3, _pos4, _uv1, _uv2, _uv3, _uv4, _color, _color, _color, _color); - } + /** + * @not_in_doc + * @brief draw a textured colored quad (usefull for debug and test) + * @param _layerName Material and face indexing layer name + * @param _pos1 First point position + * @param _pos2 Second point position + * @param _pos3 Third point position + * @param _pos4 faurth point position + * @param _color color of all elements + * @param _uv1 texture position of the _pos1 element + * @param _uv2 texture position of the _pos2 element + * @param _uv3 texture position of the _pos3 element + * @param _uv4 texture position of the _pos4 element + */ + public void addQuad( final String _layerName, + final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, final Vector3f _pos4, + final Vector2f _uv1, final Vector2f _uv2, final Vector2f _uv3, final Vector2f _uv4, + final Color _color) { + addQuad(_layerName, _pos1, _pos2, _pos3, _pos4, _uv1, _uv2, _uv3, _uv4, _color, _color, _color, _color); + } - public void addQuad( String _layerName, - Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, Vector3f _pos4, - Vector2f _uv1, Vector2f _uv2, Vector2f _uv3, Vector2f _uv4) { + public void addQuad( final String _layerName, + final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, final Vector3f _pos4, + final Vector2f _uv1, final Vector2f _uv2, final Vector2f _uv3, final Vector2f _uv4) { - addQuad( _layerName, + addQuad( _layerName, _pos1, _pos2, _pos3, _pos4, _uv1, _uv2, _uv3, _uv4, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE); -} - /** - * @not_in_doc - * @brief draw a textured quad (usefull for debug and test) - * @param _layerName Material and face indexing layer name - * @param _pos1 First point position - * @param _pos2 Second point position - * @param _pos3 Third point position - * @param _pos4 faurth point position - * @param _uv1 texture position of the _pos1 element - * @param _uv2 texture position of the _pos2 element - * @param _uv3 texture position of the _pos3 element - * @param _uv4 texture position of the _pos4 element - * @param _color1 color of the _pos1 element - * @param _color2 color of the _pos2 element - * @param _color3 color of the _pos3 element - * @param _color4 color of the _pos4 element - */ - public void addQuad( String _layerName, - Vector3f _pos1, Vector3f _pos2, Vector3f _pos3, Vector3f _pos4, - Vector2f _uv1, Vector2f _uv2, Vector2f _uv3, Vector2f _uv4, - Color _color1, Color _color2, Color _color3, Color _color4) { - addTriangle(_layerName, _pos1, _pos2, _pos3, _uv1, _uv2, _uv3, _color1, _color2, _color3); - addTriangle(_layerName, _pos1, _pos3, _pos4, _uv1, _uv3, _uv4, _color1, _color3, _color4); - } - protected int findPositionInList( Vector3f _pos){ + } + /** + * @not_in_doc + * @brief draw a textured quad (usefull for debug and test) + * @param _layerName Material and face indexing layer name + * @param _pos1 First point position + * @param _pos2 Second point position + * @param _pos3 Third point position + * @param _pos4 faurth point position + * @param _uv1 texture position of the _pos1 element + * @param _uv2 texture position of the _pos2 element + * @param _uv3 texture position of the _pos3 element + * @param _uv4 texture position of the _pos4 element + * @param _color1 color of the _pos1 element + * @param _color2 color of the _pos2 element + * @param _color3 color of the _pos3 element + * @param _color4 color of the _pos4 element + */ + public void addQuad( final String _layerName, + final Vector3f _pos1, final Vector3f _pos2, final Vector3f _pos3, final Vector3f _pos4, + final Vector2f _uv1, final Vector2f _uv2, final Vector2f _uv3, final Vector2f _uv4, + final Color _color1, final Color _color2, final Color _color3, final Color _color4) { + addTriangle(_layerName, _pos1, _pos2, _pos3, _uv1, _uv2, _uv3, _color1, _color2, _color3); + addTriangle(_layerName, _pos1, _pos3, _pos4, _uv1, _uv3, _uv4, _color1, _color3, _color4); + } + protected int findPositionInList( final Vector3f _pos){ for (int iii=0; iii