From 8ba3c34262847cb76dda0beaf989e2a6d18b6a78 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 21 Feb 2022 18:20:32 +0100 Subject: [PATCH] dcdcdc --- src/org/atriasoft/loader3d/Face.java | 186 +- .../loader3d/resources/ResourceMesh.java | 1807 ++++++++--------- .../loader3d/resources/ResourceMeshEmf.java | 717 +++---- .../resources/ResourceMeshHeightMap.java | 64 + ...java => ResourceStaticMeshObjDynamic.java} | 12 +- .../test/atriasoft/loader3d/TestBasicLog.java | 6 +- 6 files changed, 1430 insertions(+), 1362 deletions(-) create mode 100644 src/org/atriasoft/loader3d/resources/ResourceMeshHeightMap.java rename src/org/atriasoft/loader3d/resources/{ResourceStaticMeshObjBynamic.java => ResourceStaticMeshObjDynamic.java} (80%) diff --git a/src/org/atriasoft/loader3d/Face.java b/src/org/atriasoft/loader3d/Face.java index 4a78eaa..75fd918 100644 --- a/src/org/atriasoft/loader3d/Face.java +++ b/src/org/atriasoft/loader3d/Face.java @@ -1,100 +1,16 @@ package org.atriasoft.loader3d; public class Face { - public byte nbElement; - public final int[] vertex = new int[3]; - public final int[] uv = new int[3]; - public final int[] normal = new int[3]; - public final int[] color = new int[3]; - public Face() { - this.nbElement = 1; - this.vertex[0] = -1; - this.vertex[1] = -1; - this.vertex[2] = -1; - this.uv[0] = -1; - this.uv[1] = -1; - this.uv[2] = -1; - this.normal[0] = -1; - this.normal[1] = -1; - this.normal[2] = -1; - this.color[0] = -1; - this.color[1] = -1; - this.color[2] = -1; - } - public Face(int _v1, int _t1, - int _v2, int _t2, - int _v3, int _t3) { - this.nbElement = 3; - this.vertex[0] = _v1; - this.vertex[1] = _v2; - this.vertex[2] = _v3; - this.uv[0] = _t1; - this.uv[1] = _t2; - this.uv[2] = _t3; - this.normal[0] = -1; - this.normal[1] = -1; - this.normal[2] = -1; - this.color[0] = -1; - this.color[1] = -1; - this.color[2] = -1; - } - public Face(int _v1, int _t1, int _n1, - int _v2, int _t2, int _n2, - int _v3, int _t3, int _n3) { - this.nbElement = 3; - this.vertex[0] = _v1; - this.vertex[1] = _v2; - this.vertex[2] = _v3; - this.uv[0] = _t1; - this.uv[1] = _t2; - this.uv[2] = _t3; - this.normal[0] = _n1; - this.normal[1] = _n2; - this.normal[2] = _n3; - this.color[0] = -1; - this.color[1] = -1; - this.color[2] = -1; - } - public void setVertex(int _v1) { - this.nbElement = 1; - this.vertex[0] = _v1; - } - public void setVertex(int _v1, int _v2) { - this.nbElement = 2; - this.vertex[0] = _v1; - this.vertex[1] = _v2; - } - public void setVertex(int _v1, int _v2, int _v3) { - this.nbElement = 3; - this.vertex[0] = _v1; - this.vertex[1] = _v2; - this.vertex[2] = _v3; - } - public void setTexture(int _t1, int _t2, int _t3) { - this.uv[0] = _t1; - this.uv[1] = _t2; - this.uv[2] = _t3; - } - public void setNormal(int _n1, int _n2, int _n3) { - this.normal[0] = _n1; - this.normal[1] = _n2; - this.normal[2] = _n3; - } - public void setColor(int _c1, int _c2, int _c3) { - this.color[0] = _c1; - this.color[1] = _c2; - this.color[2] = _c3; - } public static Face valueOf(String data, int offsetVertex, int offsetUV, int offsetNormal) { - while(data.length() >= 1 && data.charAt(0) == ' ') { + while (data.length() >= 1 && data.charAt(0) == ' ') { data = data.substring(1); } - String[] elem = data.split(" |,|/"); + String[] elem = data.split(" |,|/"); if (elem.length != 6 && elem.length != 9) { return null; } Face out = new Face(); - if (elem.length == 9) { + if (elem.length == 9) { out.vertex[0] = Integer.valueOf(elem[0]) + offsetVertex; out.uv[0] = Integer.valueOf(elem[1]) + offsetUV; out.normal[0] = Integer.valueOf(elem[2]) + offsetNormal; @@ -114,4 +30,100 @@ public class Face { } return out; } + + public byte nbElement; + public final int[] vertex = new int[3]; + public final int[] uv = new int[3]; + public final int[] normal = new int[3]; + public final int[] color = new int[3]; + + public Face() { + this.nbElement = 1; + this.vertex[0] = -1; + this.vertex[1] = -1; + this.vertex[2] = -1; + this.uv[0] = -1; + this.uv[1] = -1; + this.uv[2] = -1; + this.normal[0] = -1; + this.normal[1] = -1; + this.normal[2] = -1; + this.color[0] = -1; + this.color[1] = -1; + this.color[2] = -1; + } + + public Face(int _v1, int _t1, int _v2, int _t2, int _v3, int _t3) { + this.nbElement = 3; + this.vertex[0] = _v1; + this.vertex[1] = _v2; + this.vertex[2] = _v3; + this.uv[0] = _t1; + this.uv[1] = _t2; + this.uv[2] = _t3; + this.normal[0] = -1; + this.normal[1] = -1; + this.normal[2] = -1; + this.color[0] = -1; + this.color[1] = -1; + this.color[2] = -1; + } + + public Face(int _v1, int _t1, int _n1, int _v2, int _t2, int _n2, int _v3, int _t3, int _n3) { + this.nbElement = 3; + this.vertex[0] = _v1; + this.vertex[1] = _v2; + this.vertex[2] = _v3; + this.uv[0] = _t1; + this.uv[1] = _t2; + this.uv[2] = _t3; + this.normal[0] = _n1; + this.normal[1] = _n2; + this.normal[2] = _n3; + this.color[0] = -1; + this.color[1] = -1; + this.color[2] = -1; + } + + public void setColor(int _c1, int _c2, int _c3) { + this.color[0] = _c1; + this.color[1] = _c2; + this.color[2] = _c3; + } + + public void setNormal(int _n) { + this.normal[0] = _n; + this.normal[1] = _n; + this.normal[2] = _n; + } + + public void setNormal(int _n1, int _n2, int _n3) { + this.normal[0] = _n1; + this.normal[1] = _n2; + this.normal[2] = _n3; + } + + public void setTexture(int _t1, int _t2, int _t3) { + this.uv[0] = _t1; + this.uv[1] = _t2; + this.uv[2] = _t3; + } + + public void setVertex(int _v1) { + this.nbElement = 1; + this.vertex[0] = _v1; + } + + public void setVertex(int _v1, int _v2) { + this.nbElement = 2; + this.vertex[0] = _v1; + this.vertex[1] = _v2; + } + + public void setVertex(int _v1, int _v2, int _v3) { + this.nbElement = 3; + this.vertex[0] = _v1; + this.vertex[1] = _v2; + this.vertex[2] = _v3; + } } diff --git a/src/org/atriasoft/loader3d/resources/ResourceMesh.java b/src/org/atriasoft/loader3d/resources/ResourceMesh.java index b144b0f..27f0247 100644 --- a/src/org/atriasoft/loader3d/resources/ResourceMesh.java +++ b/src/org/atriasoft/loader3d/resources/ResourceMesh.java @@ -21,72 +21,56 @@ import org.atriasoft.loader3d.NormalMode; import org.atriasoft.loader3d.internal.Log; import org.atriasoft.loader3d.model.Material; - public class ResourceMesh extends ResourceStaticMesh { + public static ResourceMesh create(final Uri uri) { + Log.verbose("KEEP: Resource Mesh: " + uri); + ResourceMesh object = null; + final Resource object2 = Resource.getManager().localKeep(uri); + if (object2 != null) { + if (object2 instanceof ResourceMesh tmpp) { + return tmpp; + } + Log.critical("Request resource file : '" + uri + "' With the wrong type (dynamic cast error)"); + return null; + } + Log.debug("CREATE: new Mesh: " + uri); + if (uri.getExtention().equals("emf")) { + try { + return new ResourceMeshEmf(uri); + } catch (Exception e) { + Log.warning("Can not load file: " + e.toString()); + return null; + } + } + Log.error("Not managed extention type: '" + uri.getExtention() + "'"); + return null; + } + protected List listVertex = new ArrayList<>(); //!< List of all vertex in the element protected List listUV = new ArrayList<>(); //!< List of all UV point in the mesh (for the specify texture) protected List listColor = new ArrayList<>(); //!< List of all Color point in the mesh 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 + 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 protected NormalMode normalMode = NormalMode.NONE; + protected boolean checkNormal = false; //List physics = new ArrayList<>(); //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(); - this.materials.clear(); - this.listFaces.clear(); - this.listPaletteFaces.clear(); - this.listColor.clear(); - this.listVertexNormal.clear(); - this.listFacesNormal.clear(); - this.listUV.clear(); - this.listVertex.clear(); - this.vao = ResourceVirtualArrayObject.createDynamic(); - } - + //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 modification: + List outPosition = null; + public ResourceMesh() { this(null); } - public ResourceMesh( final Uri _fileName) { + + public ResourceMesh(final Uri _fileName) { super(_fileName); Log.verbose("Load a new mesh : '" + _fileName + "'"); // ------------------------------------------------------------------------------------------------ @@ -96,840 +80,291 @@ public class ResourceMesh extends ResourceStaticMesh { this.vao.setName("[VBO] of " + _fileName); // ------------------------------------------------------------------------------------------------ } - -// public void draw(Matrix4f _positionMatrix) { -// draw(_positionMatrix, true, true); -// } -// public void draw(Matrix4f _positionMatrix, boolean _enableDepthTest, boolean _enableDepthUpdate) { -// // TODO : Remove this, it is just for test the 23-04-2016 -// //this.checkNormal = false; -// Log.verbose("draw Mesh : " + this.name + " (start)"); -// if (this.GLprogram == null) { -// Log.error("No shader ..."); -// return; -// } -// //Log.debug(this.name + " " + this.light); -// if (_enableDepthTest == true) { -// gale::openGL::enable(gale::openGL::flag_depthTest); -// if (_enableDepthUpdate == false) { -// glDepthMask(GL_FALSE); -// } -// } else { -// gale::openGL::disable(gale::openGL::flag_depthTest); -// } -// //Log.debug(" display " + this.coord.size() + " elements" ); -// this.GLprogram.use(); -// // set Matrix : translation/positionMatrix -// Matrix4f projMatrix = gale::openGL::getMatrix(); -// Matrix4f camMatrix = gale::openGL::getCameraMatrix(); -// Matrix4f tmpMatrix = projMatrix * camMatrix; -// this.GLprogram.uniformMatrix(this.GLMatrix, tmpMatrix); -// this.GLprogram.uniformMatrix(this.GLMatrixPosition, _positionMatrix); -// // position : -// this.GLprogram.sendAttributePointer(this.GLPosition, this.verticesVBO, MESH_VBO_VERTICES); -// // Texture (if needed): -// if (this.listUV.size() != 0) { -// this.GLprogram.sendAttributePointer(this.GLtexture, this.verticesVBO, MESH_VBO_TEXTURE); -// } -// // position : -// if (this.normalMode != NormalMode.none) { -// this.GLprogram.sendAttributePointer(this.GLNormal, this.verticesVBO, MESH_VBO_VERTICES_NORMAL); -// } -// // colors : -// this.GLprogram.sendAttributePointer(this.GLColor, this.verticesVBO, MESH_VBO_COLOR); -// // draw lights : -// //Log.info("light : " + this.light); -// this.light.draw(this.GLprogram); -//// #ifdef DISPLAY_NB_VERTEX_DISPLAYED -//// int nbElementDrawTheoric = 0; -//// int nbElementDraw = 0; -//// #endif -// for (int kkk=0; kkk tmpIndexResult; -// List tmppFaces = elem.getValue().faces; -// //List tmppIndex = elem.getValue().index; -// switch(this.normalMode) { -// case NormalMode.face: -// for(int iii=0; iii= 0.0f) { -// tmpIndexResult.pushBack(iii*3); -// tmpIndexResult.pushBack(iii*3+1); -// tmpIndexResult.pushBack(iii*3+2); -// } -// } -// break; -// case NormalMode.vertex: -// for(int iii=0; iii= -0.2f) -// || ((mattttt * this.listVertexNormal[tmppFaces[iii].normal[1]]).dot(cameraNormal) >= -0.2f) -// || ((mattttt * this.listVertexNormal[tmppFaces[iii].normal[2]]).dot(cameraNormal) >= -0.2f) ) { -// tmpIndexResult.pushBack(iii*3); -// tmpIndexResult.pushBack(iii*3+1); -// tmpIndexResult.pushBack(iii*3+2); -// } -// } -// break; -// default: -// for(int iii=0; iii _draw) { -// Color tmpColor(0.0, 1.0, 0.0, 1.0); -// List vertices; -// // generate element in 2 pass : -// // - create new index dependeng a vertex is a unique componenet of position, texture, normal -// // - the index list generation (can be dynamic ... (TODO later) -// for (int kkk=0; kkk= this.listFacesNormal.size()) { -// Log.error("not enougth normal in the buffer ... " + index + " >= " + this.listFacesNormal.size()); -// return; -// } -// Vector3f normal = this.listFacesNormal[index]; -// vertices.pushBack(center); -// vertices.pushBack(center+normal*0.5f); -// } break; -// case NormalMode.none: -// break; -// } -// } -// } -// _draw.drawLine(vertices, tmpColor, _positionMatrix); -// } - public void generateVBO() { - this.listFacesDrawIndex.clear(); - this.outPosition = new ArrayList<>(); - List outNormal = new ArrayList<>(); - List outTexturePosition = new ArrayList<>(); - List outColor = new ArrayList<>(); - // calculate the normal of all faces if needed - if ( this.normalMode != NormalMode.NONE - && this.listFacesNormal.size() == 0) { - // when no normal detected == > auto generate Face normal .... - Log.error("No normal detected ???"); - // calculateNormaleFace(this.listFaces.getKeys()[0]); - - } - 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 - // - the index list generation (can be dynamic ... (TODO later)) - //for (int kkk=0; kkk 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() ); - continue; - } - switch (this.materials.get(elem.getKey()).getRenderMode()) { - case TRIANGLE: - case TRIANGLE_STRIP: - case TRIANGLE_FAN: - nbIndicInFace = 3; - break; - case LINE: - case LINE_STRIP: - case LINE_LOOP: - nbIndicInFace = 2; - break; - case POINT: - nbIndicInFace = 1; - break; - case QUAD: - case QUAD_STRIP: - nbIndicInFace = 4; - break; - case POLYGON: - nbIndicInFace = 3; - break; - } -// #ifdef TRY_MINIMAL_VBO -// int tmpppppp=0; -// #endif - FaceIndexing tmpFaceList = elem.getValue(); - for (Face face : tmpFaceList.face()) { - int[] vertexVBOId = new int[3]; - for(int indice=0 ; indice= 0) { - texturepos = new Vector2f(this.listUV.get(face.uv[indice]).x(),1.0f-this.listUV.get(face.uv[indice]).y()); - } - // Create the vectex Buffer list: - boolean elementFind = false; -// #ifdef TRY_MINIMAL_VBO -// for (int jjj=0; jjj 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); - // manage Palette interfaces: - for (Entry elem : this.listPaletteFaces.entrySet()) { - int nbIndicInFace = 3; - if (this.materials.containsKey(elem.getKey()) == false) { - Log.warning("missing materials : '" + elem.getKey() + "' not in " + this.materials.keySet() ); - continue; - } - switch (this.materials.get(elem.getKey()).getRenderMode()) { - case TRIANGLE: - case TRIANGLE_STRIP: - case TRIANGLE_FAN: - nbIndicInFace = 3; - break; - case LINE: - case LINE_STRIP: - case LINE_LOOP: - case POINT: - case QUAD: - case QUAD_STRIP: - case POLYGON: - Log.critical("Unsupported palet mode: " + this.materials.get(elem.getKey()).getRenderMode()); - break; - } - float maxValues = ResourcePaletteFile.getColorMax(); - float height = ResourcePaletteFile.getHeight(); - int indexColor = ResourcePaletteFile.getColorId(elem.getKey().substring(8)); - Vector2f texturepos = new Vector2f((indexColor+0.5f)/maxValues, 0.5f/height); - Log.error("find color : " + elem.getKey().substring(8) + " ==> " + indexColor + " pos = " + texturepos); - int faceId = -1; - FaceIndexing tmpFaceList = elem.getValue(); - for (Face face : tmpFaceList.face()) { - faceId++; - //Log.warning("Parse faceID = " + faceId); - int[] vertexVBOId = new int[3]; - for(int indice=0 ; indice outNormal = new ArrayList<>(); - Log.info("calculateNormaleEdge(" + _materialName + ")"); - RenderMode tmpRenderMode = this.materials.get(_materialName).getRenderMode(); - if ( tmpRenderMode == RenderMode.POINT - || tmpRenderMode == RenderMode.LINE - || tmpRenderMode == RenderMode.LINE_STRIP - || tmpRenderMode == RenderMode.LINE_LOOP) { - Log.error("calculateNormaleEdge(" + _materialName + ") : can not calculate normal on lines ..."); - this.normalMode = NormalMode.NONE; - return; - } - for(int iii=0 ; iii tmpFaceList = this.listFaces.get(_materialName).face(); - Vector3f normal = Vector3f.ZERO; - // add the vertex from all the element in the list for face when the element in the face ... - for(int jjj=0 ; jjj _shape) { + // if (_shape == null) { + // return; + // } + // this.physics.pushBack(_shape); + // } + /** + * @brief set the shape pointer (no type == > user might know it ...) + * @param _shape The new shape (this remove the previous one) + */ + // public void setShape(void* _shape){ + // if (this.functionFreeShape!=null) { + // this.functionFreeShape(this.pointerShape); + // this.pointerShape = null; + // } + // this.pointerShape=_shape; + // } + /** + * @brief get the pointer on the shame (no type) + * @return Pointer on shape. + */ - public void addMaterial( final String _name, final Material _data){ - if (_data == null) { - Log.error(" can not add material with null pointer"); - return; - } - if (_name == "") { - Log.error(" can not add material with no name"); - return; - } - // really add the material: - Log.warning("Add material: " + _name); - this.materials.put(_name, _data); - } - - /** - * @brief set the check of normal position befor sending it to the openGl card - * @param _status New state. - */ - public void setCheckNormal(final boolean _status) { - this.checkNormal=_status; - } - /** - * @brief get the check value of normal position befor sending it to the openGl card - * @return get the chcking stus of normal or not - */ - public boolean getCheckNormal() { - return this.checkNormal; - } - -// public List getPhysicalProperties() { -// for (auto it: this.physics) { -// if (it == null) { -// Log.warning("Get null ... "); -// continue; -// } -// if (it.getType() == ege::physics::Shape::type::concave) { -// // need to generate the internal list of point and triangle needed: -// ege::physics::shape::Concave* tmpElement = it.toConcave(); -// if (tmpElement == null) { -// Log.error(" Concave ==> can not cast in Concave"); -// return this.physics; -// } -// tmpElement.clear(); -// //Log.info(" add vertices : " + this.listVertex); -// tmpElement.setListOfVertex(this.listVertex); -// for (int kkk=0; kkk index; -// for (auto it : elem.getValue().faces) { -// index.pushBack(it.vertex[0]); -// index.pushBack(it.vertex[1]); -// index.pushBack(it.vertex[2]); -// } -// //Log.info(" add triangle : " + elem.getValue().index); -// -// //tmpElement.addTriangle(elem.getValue().index); -// tmpElement.addTriangle(index); -// } -// //Log.critical("kjlkj"); -// // Can have only one concave element in a mesh ... -// //return this.physics; -// } -// } -// return this.physics; -// } - - -// public void addPhysicElement( ememory::SharedPtr _shape) { -// if (_shape == null) { -// return; -// } -// this.physics.pushBack(_shape); -// } - /** - * @brief set the shape pointer (no type == > user might know it ...) - * @param _shape The new shape (this remove the previous one) - */ -// public void setShape(void* _shape){ -// if (this.functionFreeShape!=null) { -// this.functionFreeShape(this.pointerShape); -// this.pointerShape = null; -// } -// this.pointerShape=_shape; -// } - /** - * @brief get the pointer on the shame (no type) - * @return Pointer on shape. - */ -// public void* getShape() { -// return this.pointerShape; -// }; -// public void setFreeShapeFunction(void (*_functionFreeShape)(void* _pointer)) { -// this.functionFreeShape = _functionFreeShape; -// }; - /** - * @brief Add in the faces list the layer requested - * @param _layerName face index to add - */ - public void addFaceIndexing( final String _layerName){ - if (this.listFaces.containsKey(_layerName) == false) { - FaceIndexing empty = new FaceIndexing(); - this.listFaces.put(_layerName, empty); - } - } - 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) + " ..."); - return; - } - RenderMode tmpRenderMode = this.materials.get(_layerName).getRenderMode(); - if (tmpRenderMode != RenderMode.POINT) { - Log.error("try to add Point in a mesh material section that not support Point"); - return; - } - // try to find position: - int pos = findPositionInList(_pos); - // try to find UV mapping: - int color = findColorInList(_color); - Face tmpFace = new Face(); - tmpFace.setVertex(pos); - tmpFace.setColor(color, color, color); - this.listFaces.get(_layerName).face().add(tmpFace); - } - - public void addLine( final String _layerName, final Vector3f _pos1, final Vector3f _pos2, final Color _color) { - addLine( _layerName, _pos1, _pos2, _color, _color); - } - 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) + " ..."); - return; - } - RenderMode tmpRenderMode = this.materials.get(_layerName).getRenderMode(); - if ( tmpRenderMode != RenderMode.LINE - && tmpRenderMode != RenderMode.LINE_STRIP - && tmpRenderMode != RenderMode.LINE_LOOP) { - Log.error("try to add Line in a mesh material section that not support Line"); - return; - } - // try to find position: - int pos1 = findPositionInList(_pos1); - int pos2 = findPositionInList(_pos2); - // try to find UV mapping: - int color1 = findColorInList(_color1); - int color2 = findColorInList(_color2); - Face tmpFace = new Face(); - tmpFace.setVertex(pos1, pos2); - tmpFace.setColor(color1, color2, color2); - this.listFaces.get(_layerName).face().add(tmpFace); - } - 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* getShape() { + // return this.pointerShape; + // }; + // public void setFreeShapeFunction(void (*_functionFreeShape)(void* _pointer)) { + // this.functionFreeShape = _functionFreeShape; + // }; + /** + * @brief Add in the faces list the layer requested + * @param _layerName face index to add + */ + public void addFaceIndexing(final String _layerName) { + if (this.listFaces.containsKey(_layerName) == false) { + FaceIndexing empty = new FaceIndexing(); + this.listFaces.put(_layerName, empty); } } - - 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; - } - for (int iii=1; iii<_list.size(); ++iii) { - addLine(_layerName, _list.get(iii-1), _list.get(iii), _color.get(iii-1), _color.get(iii)); - } + + public void addLine(final String _layerName, final Vector3f _pos1, final Vector3f _pos2, final Color _color) { + addLine(_layerName, _pos1, _pos2, _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( 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) { + + 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) + " ..."); return; } RenderMode tmpRenderMode = this.materials.get(_layerName).getRenderMode(); - if ( tmpRenderMode == RenderMode.QUAD - || tmpRenderMode == RenderMode.QUAD_STRIP) { - Log.todo("Create quad interface ..."); - } else if ( tmpRenderMode == RenderMode.TRIANGLE - || tmpRenderMode == RenderMode.LINE_STRIP - || tmpRenderMode == RenderMode.TRIANGLE_FAN) { - - // try to find position: - int pos1 = findPositionInList(_pos1); - int pos2 = findPositionInList(_pos2); - int pos3 = findPositionInList(_pos3); - // try to find Color: - int color1 = findColorInList(_color1); - int color2 = findColorInList(_color2); - int color3 = findColorInList(_color3); - Face tmpFace = new Face(pos1, -1, - pos2, -1, - pos3, -1); - tmpFace.setColor(color1, color2, color3); - this.listFaces.get(_layerName).face().add(tmpFace); - } else { - Log.error("try to add Quad in a mesh material section that not support Quad"); - } - } - /** - * @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); - } - /** - * @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 _color1 color of the _pos1 element - * @param _color2 color of the _pos2 element - * @param _color3 color of the _pos3 element - * @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 _color1, final Color _color2, final Color _color3){ - - Log.info("add Triangle: " + _layerName + " ..."); - 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) + " ..."); - return; - } - RenderMode tmpRenderMode = this.materials.get(_layerName).getRenderMode(); - if ( tmpRenderMode != RenderMode.TRIANGLE - && tmpRenderMode != RenderMode.TRIANGLE_STRIP - && tmpRenderMode != RenderMode.TRIANGLE_FAN) { + if (tmpRenderMode != RenderMode.LINE && tmpRenderMode != RenderMode.LINE_STRIP && tmpRenderMode != RenderMode.LINE_LOOP) { Log.error("try to add Line in a mesh material section that not support Line"); return; } // try to find position: int pos1 = findPositionInList(_pos1); int pos2 = findPositionInList(_pos2); - int pos3 = findPositionInList(_pos3); // try to find UV mapping: - int uv1 = findTextureInList(_uv1); - int uv2 = findTextureInList(_uv2); - int uv3 = findTextureInList(_uv3); - // try to find Color: int color1 = findColorInList(_color1); int color2 = findColorInList(_color2); - int color3 = findColorInList(_color3); - Face tmpFace = new Face(pos1, uv1, - pos2, uv2, - pos3, uv3); - tmpFace.setColor(color1, color2, color3); + Face tmpFace = new Face(); + tmpFace.setVertex(pos1, pos2); + tmpFace.setColor(color1, color2, color2); this.listFaces.get(_layerName).face().add(tmpFace); - Log.info(" nbFace: " + this.listFaces.get(_layerName).face().size()); } - + + 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(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; + } + for (int iii = 1; iii < _list.size(); ++iii) { + addLine(_layerName, _list.get(iii - 1), _list.get(iii), _color.get(iii - 1), _color.get(iii)); + } + } + + public void addMaterial(final String _name, final Material _data) { + if (_data == null) { + Log.error(" can not add material with null pointer"); + return; + } + if (_name == "") { + Log.error(" can not add material with no name"); + return; + } + // really add the material: + Log.warning("Add material: " + _name); + this.materials.put(_name, _data); + } + + 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) + " ..."); + return; + } + RenderMode tmpRenderMode = this.materials.get(_layerName).getRenderMode(); + if (tmpRenderMode != RenderMode.POINT) { + Log.error("try to add Point in a mesh material section that not support Point"); + return; + } + // try to find position: + int pos = findPositionInList(_pos); + // try to find UV mapping: + int color = findColorInList(_color); + Face tmpFace = new Face(); + tmpFace.setVertex(pos); + tmpFace.setColor(color, color, color); + this.listFaces.get(_layerName).face().add(tmpFace); + } + + // public void draw(Matrix4f _positionMatrix) { + // draw(_positionMatrix, true, true); + // } + // public void draw(Matrix4f _positionMatrix, boolean _enableDepthTest, boolean _enableDepthUpdate) { + // // TODO : Remove this, it is just for test the 23-04-2016 + // //this.checkNormal = false; + // Log.verbose("draw Mesh : " + this.name + " (start)"); + // if (this.GLprogram == null) { + // Log.error("No shader ..."); + // return; + // } + // //Log.debug(this.name + " " + this.light); + // if (_enableDepthTest == true) { + // gale::openGL::enable(gale::openGL::flag_depthTest); + // if (_enableDepthUpdate == false) { + // glDepthMask(GL_FALSE); + // } + // } else { + // gale::openGL::disable(gale::openGL::flag_depthTest); + // } + // //Log.debug(" display " + this.coord.size() + " elements" ); + // this.GLprogram.use(); + // // set Matrix : translation/positionMatrix + // Matrix4f projMatrix = gale::openGL::getMatrix(); + // Matrix4f camMatrix = gale::openGL::getCameraMatrix(); + // Matrix4f tmpMatrix = projMatrix * camMatrix; + // this.GLprogram.uniformMatrix(this.GLMatrix, tmpMatrix); + // this.GLprogram.uniformMatrix(this.GLMatrixPosition, _positionMatrix); + // // position : + // this.GLprogram.sendAttributePointer(this.GLPosition, this.verticesVBO, MESH_VBO_VERTICES); + // // Texture (if needed): + // if (this.listUV.size() != 0) { + // this.GLprogram.sendAttributePointer(this.GLtexture, this.verticesVBO, MESH_VBO_TEXTURE); + // } + // // position : + // if (this.normalMode != NormalMode.none) { + // this.GLprogram.sendAttributePointer(this.GLNormal, this.verticesVBO, MESH_VBO_VERTICES_NORMAL); + // } + // // colors : + // this.GLprogram.sendAttributePointer(this.GLColor, this.verticesVBO, MESH_VBO_COLOR); + // // draw lights : + // //Log.info("light : " + this.light); + // this.light.draw(this.GLprogram); + //// #ifdef DISPLAY_NB_VERTEX_DISPLAYED + //// int nbElementDrawTheoric = 0; + //// int nbElementDraw = 0; + //// #endif + // for (int kkk=0; kkk tmpIndexResult; + // List tmppFaces = elem.getValue().faces; + // //List tmppIndex = elem.getValue().index; + // switch(this.normalMode) { + // case NormalMode.face: + // for(int iii=0; iii= 0.0f) { + // tmpIndexResult.pushBack(iii*3); + // tmpIndexResult.pushBack(iii*3+1); + // tmpIndexResult.pushBack(iii*3+2); + // } + // } + // break; + // case NormalMode.vertex: + // for(int iii=0; iii= -0.2f) + // || ((mattttt * this.listVertexNormal[tmppFaces[iii].normal[1]]).dot(cameraNormal) >= -0.2f) + // || ((mattttt * this.listVertexNormal[tmppFaces[iii].normal[2]]).dot(cameraNormal) >= -0.2f) ) { + // tmpIndexResult.pushBack(iii*3); + // tmpIndexResult.pushBack(iii*3+1); + // tmpIndexResult.pushBack(iii*3+2); + // } + // } + // break; + // default: + // for(int iii=0; iii getPhysicalProperties() { + // for (auto it: this.physics) { + // if (it == null) { + // Log.warning("Get null ... "); + // continue; + // } + // if (it.getType() == ege::physics::Shape::type::concave) { + // // need to generate the internal list of point and triangle needed: + // ege::physics::shape::Concave* tmpElement = it.toConcave(); + // if (tmpElement == null) { + // Log.error(" Concave ==> can not cast in Concave"); + // return this.physics; + // } + // tmpElement.clear(); + // //Log.info(" add vertices : " + this.listVertex); + // tmpElement.setListOfVertex(this.listVertex); + // for (int kkk=0; kkk index; + // for (auto it : elem.getValue().faces) { + // index.pushBack(it.vertex[0]); + // index.pushBack(it.vertex[1]); + // index.pushBack(it.vertex[2]); + // } + // //Log.info(" add triangle : " + elem.getValue().index); + // + // //tmpElement.addTriangle(elem.getValue().index); + // tmpElement.addTriangle(index); + // } + // //Log.critical("kjlkj"); + // // Can have only one concave element in a mesh ... + // //return this.physics; + // } + // } + // return this.physics; + // } + + 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); + } + + /** + * @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); + } + + /** + * @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 _color1 color of the _pos1 element + * @param _color2 color of the _pos2 element + * @param _color3 color of the _pos3 element + * @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 _color1, final Color _color2, final Color _color3) { + + Log.info("add Triangle: " + _layerName + " ..."); + 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) + " ..."); + return; + } + RenderMode tmpRenderMode = this.materials.get(_layerName).getRenderMode(); + if (tmpRenderMode != RenderMode.TRIANGLE && tmpRenderMode != RenderMode.TRIANGLE_STRIP && tmpRenderMode != RenderMode.TRIANGLE_FAN) { + Log.error("try to add Line in a mesh material section that not support Line"); + return; + } + // try to find position: + int pos1 = findPositionInList(_pos1); + int pos2 = findPositionInList(_pos2); + int pos3 = findPositionInList(_pos3); + // try to find UV mapping: + int uv1 = findTextureInList(_uv1); + int uv2 = findTextureInList(_uv2); + int uv3 = findTextureInList(_uv3); + // try to find Color: + int color1 = findColorInList(_color1); + int color2 = findColorInList(_color2); + int color3 = findColorInList(_color3); + Face tmpFace = new Face(pos1, uv1, pos2, uv2, pos3, uv3); + tmpFace.setColor(color1, color2, color3); + this.listFaces.get(_layerName).face().add(tmpFace); + Log.info(" nbFace: " + this.listFaces.get(_layerName).face().size()); + } + + private void calculateNormaleEdge(final String _materialName) { + this.listVertexNormal.clear(); + if (this.normalMode == NormalMode.VERTEX) { + List outNormal = new ArrayList<>(); + Log.info("calculateNormaleEdge(" + _materialName + ")"); + RenderMode tmpRenderMode = this.materials.get(_materialName).getRenderMode(); + if (tmpRenderMode == RenderMode.POINT || tmpRenderMode == RenderMode.LINE || tmpRenderMode == RenderMode.LINE_STRIP || tmpRenderMode == RenderMode.LINE_LOOP) { + Log.error("calculateNormaleEdge(" + _materialName + ") : can not calculate normal on lines ..."); + this.normalMode = NormalMode.NONE; + return; + } + for (int iii = 0; iii < this.listVertex.size(); iii++) { + List tmpFaceList = this.listFaces.get(_materialName).face(); + Vector3f normal = Vector3f.ZERO; + // add the vertex from all the element in the list for face when the element in the face ... + for (int jjj = 0; jjj < tmpFaceList.size(); jjj++) { + outNormal.add(normal); + if (tmpFaceList.get(jjj).vertex[0] == iii || tmpFaceList.get(jjj).vertex[1] == iii || tmpFaceList.get(jjj).vertex[2] == iii) { + normal = normal.add(this.listFacesNormal.get(jjj)); + } + } + if (normal.isZero()) { + this.listVertexNormal.add(Vector3f.ONE); + } else { + this.listVertexNormal.add(normal.normalize()); + } + } + this.vao.setNormals(outNormal); + } + } + + // public void createViewBox( String _materialName,float _size=1.0){ + // this.normalMode = NormalMode.NONE; + // ege::viewBox::create(this.materials, this.listFaces, this.listVertex, this.listUV, + // _materialName, _size); + // calculateNormaleFace(_materialName); + // } + // + // public void createIcoSphere( String _materialName,float _size=1.0, int _subdivision=3) { + // this.normalMode = NormalMode.NONE; + // ege::icoSphere::create(this.materials, this.listFaces, this.listVertex, this.listUV, + // _materialName, _size, _subdivision); + // calculateNormaleFace(_materialName); + // } + //private boolean loadOBJ( Uri _fileName); + //private boolean loadEMF( Uri _fileName); + private void calculateNormaleFace(final String _materialName) { + this.listFacesNormal.clear(); + if (this.normalMode == NormalMode.FACE) { + Log.verbose("calculateNormaleFace(" + _materialName + ")"); + RenderMode tmpRenderMode = this.materials.get(_materialName).getRenderMode(); + if (tmpRenderMode == RenderMode.POINT || tmpRenderMode == RenderMode.LINE || tmpRenderMode == RenderMode.LINE_STRIP || tmpRenderMode == RenderMode.LINE_LOOP) { + Log.error("calculateNormaleFace(" + _materialName + ") : can not calculate normal on lines ..."); + this.normalMode = NormalMode.NONE; + return; + } + for (Face it : this.listFaces.get(_materialName).face()) { + // for all case, We use only the 3 vertex for quad element, in theory 3D modeler export element in triangle if it is not a real plane. + Vector3f normal = (this.listVertex.get(it.vertex[0]).less(this.listVertex.get(it.vertex[1]))).cross(this.listVertex.get(it.vertex[1]).less(this.listVertex.get(it.vertex[2]))); + //Log.info("normal: " + normal.normalized()); + if (normal.isZero()) { + Log.error("Null vertor for a face ... " + this.listVertex.get(it.vertex[0]) + " " + this.listVertex.get(it.vertex[1]) + " " + this.listVertex.get(it.vertex[2])); + this.listFacesNormal.add(new Vector3f(1, 0, 0)); + } else { + this.listFacesNormal.add(normal.normalize()); + } + int normalID = this.listFacesNormal.size() - 1; + it.normal[0] = normalID; + it.normal[1] = normalID; + it.normal[2] = normalID; } } - this.listVertex.add(_pos); - return this.listVertex.size()-1; } - protected int findTextureInList( final Vector2f _uv){ - for (int iii=0; iii _draw) { + // Color tmpColor(0.0, 1.0, 0.0, 1.0); + // List vertices; + // // generate element in 2 pass : + // // - create new index dependeng a vertex is a unique componenet of position, texture, normal + // // - the index list generation (can be dynamic ... (TODO later) + // for (int kkk=0; kkk= this.listFacesNormal.size()) { + // Log.error("not enougth normal in the buffer ... " + index + " >= " + this.listFacesNormal.size()); + // return; + // } + // Vector3f normal = this.listFacesNormal[index]; + // vertices.pushBack(center); + // vertices.pushBack(center+normal*0.5f); + // } break; + // case NormalMode.none: + // break; + // } + // } + // } + // _draw.drawLine(vertices, tmpColor, _positionMatrix); + // } + public void generateVBO() { + this.listFacesDrawIndex.clear(); + this.outPosition = new ArrayList<>(); + List outNormal = new ArrayList<>(); + List outTexturePosition = new ArrayList<>(); + List outColor = new ArrayList<>(); + // calculate the normal of all faces if needed + if (this.normalMode != NormalMode.NONE && this.listFacesNormal.size() == 0) { + // when no normal detected == > auto generate Face normal .... + Log.error("No normal detected ???"); + // calculateNormaleFace(this.listFaces.getKeys()[0]); + + } + 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 + // - the index list generation (can be dynamic ... (TODO later)) + //for (int kkk=0; kkk 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()); + continue; + } + nbIndicInFace = switch (this.materials.get(elem.getKey()).getRenderMode()) { + case TRIANGLE, TRIANGLE_STRIP, TRIANGLE_FAN -> 3; + case LINE, LINE_STRIP, LINE_LOOP -> 2; + case POINT -> 1; + case QUAD, QUAD_STRIP -> 4; + case POLYGON -> 3; + }; + FaceIndexing tmpFaceList = elem.getValue(); + for (Face face : tmpFaceList.face()) { + int[] vertexVBOId = new int[3]; + for (int indice = 0; indice < nbIndicInFace; indice++) { + // get position + Vector3f position = this.listVertex.get(face.vertex[indice]); + // get Color + Color color; + if (face.color[indice] != -1) { + color = this.listColor.get(face.color[indice]); + } else { + color = Color.WHITE; + } + // get Normal + Vector3f normal = new Vector3f(1, 0, 0); + switch (this.normalMode) { + case VERTEX: + normal = this.listVertexNormal.get(face.normal[indice]); + break; + case FACE: + normal = this.listFacesNormal.get(face.normal[indice]); + break; + default: + case NONE: + break; + } + // get Texture Position + Vector2f texturepos = Vector2f.ZERO; + if (face.uv[indice] >= 0) { + texturepos = new Vector2f(this.listUV.get(face.uv[indice]).x(), 1.0f - this.listUV.get(face.uv[indice]).y()); + } + // Create the vectex Buffer list: + boolean elementFind = false; + // #ifdef TRY_MINIMAL_VBO + // for (int jjj=0; jjj 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); + // manage Palette interfaces: + for (Entry elem : this.listPaletteFaces.entrySet()) { + int nbIndicInFace = 3; + if (this.materials.containsKey(elem.getKey()) == false) { + Log.warning("missing materials : '" + elem.getKey() + "' not in " + this.materials.keySet()); + continue; + } + switch (this.materials.get(elem.getKey()).getRenderMode()) { + case TRIANGLE: + case TRIANGLE_STRIP: + case TRIANGLE_FAN: + nbIndicInFace = 3; + break; + case LINE: + case LINE_STRIP: + case LINE_LOOP: + case POINT: + case QUAD: + case QUAD_STRIP: + case POLYGON: + Log.critical("Unsupported palet mode: " + this.materials.get(elem.getKey()).getRenderMode()); + break; + } + float maxValues = ResourcePaletteFile.getColorMax(); + float height = ResourcePaletteFile.getHeight(); + int indexColor = ResourcePaletteFile.getColorId(elem.getKey().substring(8)); + Vector2f texturepos = new Vector2f((indexColor + 0.5f) / maxValues, 0.5f / height); + Log.error("find color : " + elem.getKey().substring(8) + " ==> " + indexColor + " pos = " + texturepos); + int faceId = -1; + FaceIndexing tmpFaceList = elem.getValue(); + for (Face face : tmpFaceList.face()) { + faceId++; + //Log.warning("Parse faceID = " + faceId); + int[] vertexVBOId = new int[3]; + for (int indice = 0; indice < nbIndicInFace; indice++) { + //Log.warning("find position indice = " + indice + " / " + face.vertex.length); + // get position + Vector3f position = this.listVertex.get(face.vertex[indice]); + // get Color + Color color = Color.WHITE; + // get Normal + Vector3f normal = new Vector3f(1, 0, 0); + switch (this.normalMode) { + case VERTEX: + normal = this.listVertexNormal.get(face.normal[indice]); + break; + case FACE: + normal = this.listFacesNormal.get(face.normal[indice]); + break; + default: + case NONE: + break; + } + + this.outPosition.add(position); + if (this.normalMode != NormalMode.NONE) { + outNormal.add(normal); + } + outTexturePosition.add(texturepos); + outColor.add(color); + vertexVBOId[indice] = this.outPosition.size() - 1; + } + for (int indice = 0; indice < nbIndicInFace; indice++) { + outputIndex.index().add(vertexVBOId[indice]); + outIndex.add(vertexVBOId[indice]); + } + } + } + this.listFacesDrawIndex.put("palette", new Vector2i(outPositionIndewStart, this.outPosition.size())); + } + // update all the VBO elements ... + this.vao.setNormals(outNormal); + this.vao.setPosition(this.outPosition); + this.vao.setTextureCoordinate(outTexturePosition); + this.vao.setColors(outColor); + this.vao.setIndices(outIndex); + this.vao.flush(); + } + + /** + * @brief get the check value of normal position befor sending it to the openGl card + * @return get the chcking stus of normal or not + */ + public boolean getCheckNormal() { + return this.checkNormal; + } + + public List getGeneratedPosition() { + return this.outPosition; + } + + public Set getLayers() { + return this.listFacesDrawIndex.keySet(); + } + + @Override + public void render() { + super.render(); + } + + public void render(final String layerName) { + /* + Log.info("--------------------------------------"); + for (String elem : this.listFacesDrawIndex.keySet()) { + Log.info(" - " + elem); + } + */ + Vector2i elements = this.listFacesDrawIndex.get(layerName); + super.render(elements.x(), elements.y()); + } + + /** + * @brief set the check of normal position befor sending it to the openGl card + * @param _status New state. + */ + public void setCheckNormal(final boolean _status) { + this.checkNormal = _status; + } + + public void setModifiedPosition(final float[] position) { + this.vao.setPosition(position); + this.vao.flush(); + } + + 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(); + } + + void setNormalMode(final NormalMode _mode) { + this.normalMode = _mode; + } + } diff --git a/src/org/atriasoft/loader3d/resources/ResourceMeshEmf.java b/src/org/atriasoft/loader3d/resources/ResourceMeshEmf.java index db317b0..c71325e 100644 --- a/src/org/atriasoft/loader3d/resources/ResourceMeshEmf.java +++ b/src/org/atriasoft/loader3d/resources/ResourceMeshEmf.java @@ -4,6 +4,8 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.atriasoft.etk.Uri; import org.atriasoft.etk.math.Vector2f; @@ -19,164 +21,185 @@ import org.atriasoft.loader3d.model.Material; import org.atriasoft.loader3d.model.PhysicShape; public class ResourceMeshEmf extends ResourceMesh { - - - -enum emfModuleMode { - EMFModuleNone(0), - EMFModuleMesh(1), - EMFModuleMeshVertex(2), - EMFModuleMeshUVMapping(3), - EMFModuleMeshNormalVertex(4), - EMFModuleMeshNormalFace(5), - EMFModuleMeshFace(6), - EMFModuleMeshFaceMaterial(7), - EMFModuleMeshPhysics(8), - EMFModuleMeshPhysicsNamed(9), - EMFModuleMesh_END(100), - EMFModuleMaterial(101), - EMFModuleMaterial_END(200), - EMFModulePalette(201), - EMFModulePalette_END(300), - EMFModulePhysics(301), - EMFModulePhysicsNamed(302), - EMFModulePhysics_END(400); - private final int value; - emfModuleMode(int value) { - this.value = value; - } - int getValue() { - return this.value; - } -}; - - - -// TODO : rework with string line extractor -public ResourceMeshEmf(Uri _fileName) { - super(_fileName); - mode = RenderMode.TRIANGLE; - this.checkNormal = true; - this.normalMode = NormalMode.NONE; - InputStream fileIOStream = Uri.getStream(_fileName); - if (fileIOStream == null ) { - Log.error("CAn not get the file named='" + _fileName + "'"); - return; // TODO false; - } - BufferedReader fileIO = new BufferedReader(new InputStreamReader(fileIOStream)); - // get the fileSize ... - //int size = fileIO.size(); - - String inputDataLine; - Dynamic levelIO = new Dynamic(0); - // load the first line : - inputDataLine = readLine(fileIO, levelIO); - if (inputDataLine.startsWith("EMF(STRING)") == true) { - // parse in string mode ... - } else if (inputDataLine.startsWith("EMF(BINARY)") == true) { - Log.error(" file binary mode is not supported now : 'EMF(BINARY)'"); - return; // TODO false; - } else { - Log.error(" file mode is not supported now : 'EMF(? ? ?)' = '" + inputDataLine + "'"); - return; // TODO false; - } - emfModuleMode currentMode = emfModuleMode.EMFModuleNone; - Log.verbose("Start parsing Mesh file : " + _fileName); - // mesh global param : - String currentMeshName = ""; - FaceIndexing currentFaceIndexing = null; - // material global param : - String materialName = ""; - Material material = null; - // physical shape: - PhysicShape physics = null; - boolean haveUVMapping = false; - int offsetVertexId = 0; - int offsetUV = 0; - int offsetFaceNormal = 0; - int offsetVertexNormal = 0; - while ((inputDataLine = readLine(fileIO, levelIO)) != null) { - int level = levelIO.value; - if (level == -1) { - // detect comment; - Log.warning("Detect comment"); - continue; + enum emfModuleMode { + EMFModuleSkip(-1), //!< skip the current module ==> filtered... + EMFModuleNone(0), //!< Nothing in parsing + EMFModuleMesh(1), //!< + EMFModuleMeshVertex(2), //!< + EMFModuleMeshUVMapping(3), //!< + EMFModuleMeshNormalVertex(4), //!< + EMFModuleMeshNormalFace(5), //!< + EMFModuleMeshFace(6), //!< + EMFModuleMeshFaceMaterial(7), //!< + EMFModuleMeshPhysics(8), //!< + EMFModuleMeshPhysicsNamed(9), //!< + EMFModuleMesh_END(100), //!< + EMFModuleMaterial(101), //!< + EMFModuleMaterial_END(200), //!< + EMFModulePalette(201), //!< + EMFModulePalette_END(300), //!< + EMFModulePhysics(301), //!< + EMFModulePhysicsNamed(302), //!< + EMFModulePhysics_END(400); //!< + + private final int value; + + emfModuleMode(int value) { + this.value = value; } - if (level == 0) { - // new section ... - if(inputDataLine.startsWith("Mesh:") == true) { - currentMode = emfModuleMode.EMFModuleMesh; - currentMeshName = inputDataLine.substring(5); - Log.verbose("Parse Mesh: " + currentMeshName); - offsetVertexId = this.listVertex.size(); - offsetUV = this.listUV.size(); - offsetFaceNormal = this.listFacesNormal.size(); - offsetVertexNormal = this.listVertexNormal.size(); - //Log.error("new offset: " + offsetVertexId + " " + offsetUV + " " + offsetFaceNormal + " " + offsetVertexNormal); - } else if(inputDataLine.startsWith("Materials:")) { - // add previous material: - if( materialName != "" - && material != null) { - if (currentMode == emfModuleMode.EMFModulePalette) { - this.palettes.put(materialName, material); - // TODO Remove this later ... - this.materials.put("palette:" + materialName, material); - } else { - this.materials.put(materialName, material); - } - materialName = ""; - material = null; - } - currentMode = emfModuleMode.EMFModuleMaterial; - material = new Material(); - materialName = inputDataLine.substring(10); - Log.verbose("Parse Material: " + materialName); - } else if(inputDataLine.startsWith("Palettes:")) { - // add previous material: - if( materialName != "" - && material != null) { - if (currentMode == emfModuleMode.EMFModulePalette) { - this.palettes.put(materialName, material); - // TODO Remove this later ... - this.materials.put("palette:" + materialName, material); - } else { - this.materials.put(materialName, material); - } - materialName = ""; - material = null; - } - currentMode = emfModuleMode.EMFModulePalette; - material = new Material(); - materialName = inputDataLine.substring(9); - Log.verbose("Parse Palette: " + materialName); - } else if(inputDataLine.startsWith("Physics:") == true) { - currentMode = emfModuleMode.EMFModulePhysics; - Log.verbose("Parse global Physics: "); - } else { - currentMode = emfModuleMode.EMFModuleNone; - } + + int getValue() { + return this.value; + } + } + + // TODO : rework with string line extractor + /** + * Create a mesh from Ewol Mesh File. + * @param fileName generic URI if parameter filter is set the mesh is partially loaded applying the filter + * @throws Exception pb in parsing the file maybe no vertices inside... + */ + public ResourceMeshEmf(Uri fileName) throws Exception { + super(fileName); + Pattern filterPattern = null; + if (fileName.hasProperty("filter")) { + filterPattern = Pattern.compile(fileName.getProperty("filter")); + } + this.mode = RenderMode.TRIANGLE; + this.checkNormal = true; + this.normalMode = NormalMode.NONE; + InputStream fileIOStream = Uri.getStream(fileName); + if (fileIOStream == null) { + Log.error("CAn not get the file named='" + fileName + "'"); + return; // TODO false; + } + BufferedReader fileIO = new BufferedReader(new InputStreamReader(fileIOStream)); + // get the fileSize ... + + //int size = fileIO.size(); + + String inputDataLine; + Dynamic levelIO = new Dynamic<>(0); + // load the first line : + inputDataLine = readLine(fileIO, levelIO); + if (inputDataLine.startsWith("EMF(STRING)") == true) { + // parse in string mode ... + } else if (inputDataLine.startsWith("EMF(BINARY)") == true) { + Log.error(" file binary mode is not supported now : 'EMF(BINARY)'"); + return; // TODO false; } else { - if ( currentMode.getValue() >= emfModuleMode.EMFModuleMesh.getValue() - && currentMode.getValue() <= emfModuleMode.EMFModuleMesh_END.getValue()) { + Log.error(" file mode is not supported now : 'EMF(? ? ?)' = '" + inputDataLine + "'"); + return; // TODO false; + } + emfModuleMode currentMode = emfModuleMode.EMFModuleNone; + Log.verbose("Start parsing Mesh file : " + fileName); + // mesh global param : + String currentMeshName = ""; + FaceIndexing currentFaceIndexing = null; + // material global param : + String materialName = ""; + Material material = null; + // physical shape: + PhysicShape physics = null; + boolean haveUVMapping = false; + int offsetVertexId = 0; + int offsetUV = 0; + int offsetFaceNormal = 0; + int offsetVertexNormal = 0; + while ((inputDataLine = readLine(fileIO, levelIO)) != null) { + int level = levelIO.value; + if (level == -1) { + // detect comment; + Log.warning("Detect comment"); + continue; + } + if (level == 0) { + // new section ... + if (inputDataLine.startsWith("Mesh:") == true) { + currentMode = emfModuleMode.EMFModuleMesh; + currentMeshName = inputDataLine.substring(5); + if (filterPattern != null) { + Matcher m = filterPattern.matcher(currentMeshName); + if (!m.matches()) { + currentMode = emfModuleMode.EMFModuleSkip; + Log.verbose("Parse Mesh(SKIP): " + currentMeshName); + } else { + Log.verbose("Parse Mesh: " + currentMeshName); + } + } else { + Log.verbose("Parse Mesh: " + currentMeshName); + } + offsetVertexId = this.listVertex.size(); + offsetUV = this.listUV.size(); + offsetFaceNormal = this.listFacesNormal.size(); + offsetVertexNormal = this.listVertexNormal.size(); + //Log.error("new offset: " + offsetVertexId + " " + offsetUV + " " + offsetFaceNormal + " " + offsetVertexNormal); + } else if (inputDataLine.startsWith("Materials:")) { + // add previous material: + if (materialName != "" && material != null) { + if (currentMode == emfModuleMode.EMFModulePalette) { + this.palettes.put(materialName, material); + // TODO Remove this later ... + this.materials.put("palette:" + materialName, material); + } else { + this.materials.put(materialName, material); + } + materialName = ""; + material = null; + } + currentMode = emfModuleMode.EMFModuleMaterial; + material = new Material(); + materialName = inputDataLine.substring(10); + Log.verbose("Parse Material: " + materialName); + } else if (inputDataLine.startsWith("Palettes:")) { + // add previous material: + if (materialName != "" && material != null) { + if (currentMode == emfModuleMode.EMFModulePalette) { + this.palettes.put(materialName, material); + // TODO Remove this later ... + this.materials.put("palette:" + materialName, material); + } else { + this.materials.put(materialName, material); + } + materialName = ""; + material = null; + } + currentMode = emfModuleMode.EMFModulePalette; + material = new Material(); + materialName = inputDataLine.substring(9); + Log.verbose("Parse Palette: " + materialName); + } else if (inputDataLine.startsWith("Physics:") == true) { + currentMode = emfModuleMode.EMFModulePhysics; + Log.verbose("Parse global Physics: "); + } else { + currentMode = emfModuleMode.EMFModuleNone; + } + continue; + } + if (currentMode == emfModuleMode.EMFModuleSkip) { + // skip the content... + continue; + } + if (currentMode.getValue() >= emfModuleMode.EMFModuleMesh.getValue() && currentMode.getValue() <= emfModuleMode.EMFModuleMesh_END.getValue()) { if (level == 1) { - if(inputDataLine.startsWith("Vertex:") == true) { + if (inputDataLine.startsWith("Vertex:") == true) { currentMode = emfModuleMode.EMFModuleMeshVertex; Log.verbose(" Vertex ..."); - } else if(inputDataLine.startsWith("UV-mapping:") == true) { + } else if (inputDataLine.startsWith("UV-mapping:") == true) { currentMode = emfModuleMode.EMFModuleMeshUVMapping; haveUVMapping = true; Log.verbose(" UV-mapping ..."); - } else if(inputDataLine.startsWith("Normal(vertex):") == true) { + } else if (inputDataLine.startsWith("Normal(vertex):") == true) { currentMode = emfModuleMode.EMFModuleMeshNormalVertex; Log.verbose(" Normal(vertex) ..."); - } else if(inputDataLine.startsWith("Normal(face):") == true) { + } else if (inputDataLine.startsWith("Normal(face):") == true) { currentMode = emfModuleMode.EMFModuleMeshNormalFace; Log.verbose(" Normal(face) ..."); - } else if(inputDataLine.startsWith("Face:") == true) { + } else if (inputDataLine.startsWith("Face:") == true) { currentMode = emfModuleMode.EMFModuleMeshFace; Log.verbose(" Face ..."); - } else if(inputDataLine.startsWith("Physics:") == true) { + } else if (inputDataLine.startsWith("Physics:") == true) { currentMode = emfModuleMode.EMFModuleMeshPhysics; Log.verbose(" Physics ..."); } else { @@ -292,212 +315,228 @@ public ResourceMeshEmf(Uri _fileName) { break; } continue; - } else if ( ( currentMode.getValue() >= emfModuleMode.EMFModuleMaterial.getValue() - && currentMode.getValue() <= emfModuleMode.EMFModuleMaterial_END.getValue()) - || ( currentMode.getValue() >= emfModuleMode.EMFModulePalette.getValue() - && currentMode.getValue() <= emfModuleMode.EMFModulePalette_END.getValue())) { + } + if ((currentMode.getValue() >= emfModuleMode.EMFModuleMaterial.getValue() && currentMode.getValue() <= emfModuleMode.EMFModuleMaterial_END.getValue()) + || (currentMode.getValue() >= emfModuleMode.EMFModulePalette.getValue() && currentMode.getValue() <= emfModuleMode.EMFModulePalette_END.getValue())) { if (material == null) { Log.error("material allocation error"); continue; } - if(inputDataLine.startsWith("Ns ") == true) { - float tmpVal=Float.valueOf(inputDataLine.substring(3)); + if (inputDataLine.startsWith("Ns ") == true) { + float tmpVal = Float.valueOf(inputDataLine.substring(3)); material.setShininess(tmpVal); Log.verbose(" Shininess " + tmpVal); - } else if(inputDataLine.startsWith("Ka ") == true) { + } else if (inputDataLine.startsWith("Ka ") == true) { Vector4f tmp = Vector4f.valueOf(inputDataLine.substring(3)); tmp = tmp.withW(1); material.setAmbientFactor(tmp); Log.verbose(" AmbientFactor " + tmp); - } else if(inputDataLine.startsWith("Kd ") == true) { + } else if (inputDataLine.startsWith("Kd ") == true) { Vector4f tmp = Vector4f.valueOf(inputDataLine.substring(3)); tmp = tmp.withW(1); material.setDiffuseFactor(tmp); Log.error(" DiffuseFactor " + tmp); - } else if(inputDataLine.startsWith("Ks ") == true) { + } else if (inputDataLine.startsWith("Ks ") == true) { Vector4f tmp = Vector4f.valueOf(inputDataLine.substring(3)); tmp = tmp.withW(1); - material.setSpecularFactor(tmp); + material.setSpecularFactor(tmp); Log.verbose(" SpecularFactor " + tmp); - } else if(inputDataLine.startsWith("Ni ") == true) { - float tmpVal=Float.valueOf(inputDataLine.substring(3)); + } else if (inputDataLine.startsWith("Ni ") == true) { + float tmpVal = Float.valueOf(inputDataLine.substring(3)); // TODO : ... Log.verbose(" Ni " + tmpVal); - } else if(inputDataLine.startsWith("d ") == true) { - float tmpVal=Float.valueOf(inputDataLine.substring(2)); + } else if (inputDataLine.startsWith("d ") == true) { + float tmpVal = Float.valueOf(inputDataLine.substring(2)); // TODO : ... Log.verbose(" d " + tmpVal); - } else if(inputDataLine.startsWith("illum ") == true) { - float tmpVal=Float.valueOf(inputDataLine.substring(6)); + } else if (inputDataLine.startsWith("illum ") == true) { + float tmpVal = Float.valueOf(inputDataLine.substring(6)); // TODO : ... Log.verbose(" illum " + tmpVal); - } else if(inputDataLine.startsWith("map_Kd ") == true) { - Uri tmpTexture = _fileName.getParent(); + } else if (inputDataLine.startsWith("map_Kd ") == true) { + Uri tmpTexture = fileName.getParent(); tmpTexture = tmpTexture.pathAdd(inputDataLine.substring(7)); material.setTexture0(tmpTexture); Log.verbose(" Texture " + tmpTexture); - } else if(inputDataLine.startsWith("renderMode ") == true) { + } else if (inputDataLine.startsWith("renderMode ") == true) { //RenderMode mode; //frothis.string(mode, &inputDataLine[11]); //material.setRenderMode(mode); - Log.verbose(" Texture " + mode); + Log.verbose(" Texture " + this.mode); } else { Log.error("unknow material property ... : '" + inputDataLine + "'"); } - } else if ( currentMode.getValue() >= emfModuleMode.EMFModulePhysics.getValue() - && currentMode.getValue() <= emfModuleMode.EMFModulePhysics_END.getValue()) { + continue; + } + if (currentMode.getValue() >= emfModuleMode.EMFModulePhysics.getValue() && currentMode.getValue() <= emfModuleMode.EMFModulePhysics_END.getValue()) { if (level == 1) { Log.error("Load shape : " + inputDataLine); -// physics = ege::physics::Shape::create(inputDataLine); -// if (physics == null) { -// Log.error("Allocation error when creating physical shape ..."); -// continue; -// } -// addPhysicElement(physics); -// Log.verbose(" " + this.physics.size() + " " + inputDataLine); -// currentMode = emfModuleMode.EMFModulePhysicsNamed; + // physics = ege::physics::Shape::create(inputDataLine); + // if (physics == null) { + // Log.error("Allocation error when creating physical shape ..."); + // continue; + // } + // addPhysicElement(physics); + // Log.verbose(" " + this.physics.size() + " " + inputDataLine); + // currentMode = emfModuleMode.EMFModulePhysicsNamed; } else if (currentMode == emfModuleMode.EMFModulePhysicsNamed) { -// if (physics == null) { -// Log.error("Can not parse :'" + inputDataLine + "' in physical shape ..."); -// continue; -// } -// if (physics.parse(&inputDataLine[0]) == false) { -// Log.error("ERROR when parsing :'" + inputDataLine + "' in physical shape ..."); -// } + // if (physics == null) { + // Log.error("Can not parse :'" + inputDataLine + "' in physical shape ..."); + // continue; + // } + // if (physics.parse(&inputDataLine[0]) == false) { + // Log.error("ERROR when parsing :'" + inputDataLine + "' in physical shape ..."); + // } + } + continue; + } + // unknown ... + Log.warning("Unknown type of line == > jump end of line ... " + inputDataLine); + } + // add last material ... + if (materialName != "" && material != null) { + if (currentMode == emfModuleMode.EMFModulePalette) { + this.palettes.put(materialName, material); + // TODO Remove this later ... + this.materials.put("palette:" + materialName, material); + } else { + this.materials.put(materialName, material); + } + materialName = ""; + material = null; + } + Log.verbose("Stop parsing Mesh file"); + + try { + fileIO.close(); + fileIOStream.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Log.verbose("New mesh : "); + Log.verbose(" nb vertex: " + this.listVertex.size()); + Log.verbose(" nb UV: " + this.listUV.size()); + Log.verbose(" nb Colors: " + this.listColor.size()); + Log.verbose(" nb Normal face: " + this.listFacesNormal.size()); + Log.verbose(" nb Normal vertex: " + this.listVertexNormal.size()); + Log.verbose(" nb Faces: " + this.listFaces.size()); + Log.verbose(" nb Faces (palette): " + this.listPaletteFaces.size()); + Log.verbose(" nb material: " + this.materials.size()); + Log.verbose(" nb palettes: " + this.palettes.size()); + //Log.verbose(" nb physic: " + this.physics.size()); + if (this.listVertex.size() == 0) { + throw new Exception("unable to load the mesh file: " + fileName + " ==> no vertex available."); + } + generateVBO(); + } + + //private String loadNextData(int _maxData, + // BufferedReader _file) { + // return loadNextData(_maxData, _file, false, false, true); + //} + // + //private String loadNextData(int _maxData, + // BufferedReader _file, + // boolean _removeTabs, + // boolean _stopColomn, + // boolean _stopPipe) { + // StringBuffer buf = new StringBuffer(); + // int outSize = 0; + // /* + // if (this.zipReadingOffset >= this.zipContent.size()) { + // element[0] = '\0'; + // return null; + // } + // */ + // int current = '\0'; + // try { + // current = _file.read(); + // } catch (IOException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // while (current != '\0') { + // if( _removeTabs == false + // || buf.length() == 0) { + // buf.append((char)current); + // } + // if( current == '\n' + // || current == '\r' + // || ( current == '|' + // && _stopPipe == true) + // || ( current == ':' + // && _stopColomn == true) ) + // { + // //Log.debug(" plop : '" + _elementLine + "'" ); + // return buf.toString(); + // } else if( buf.length() == 0 + // && current != '\t') { + // buf.append((char)current); + // } + // // check maxData size ... + // if (outSize >= _maxData-1) { + // return buf.toString(); + // } + // try { + // current = _file.read(); + // } catch (IOException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // } + // if (outSize != 0) { + // // send last line + // return buf.toString(); + // } + // return null; + //} + + private String readLine(BufferedReader _file, Dynamic level) { + level.value = 0; + StringBuffer buf = new StringBuffer(); + boolean startLine = true; + boolean differentThanSpace = false; + boolean hasSpace = false; + int current = '\0'; + try { + current = _file.read(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + while (current != '\0' && current != -1) { + if (current == '\r') { + // nothing to do ... + } else if (current == '\t' && startLine) { + level.value++; + } else if (current == '\n') { + if (!startLine) { + break; } } else { - // unknow ... - Log.warning("Unknow type of line == > jump end of line ... " + inputDataLine); - } - } - } - // add last material ... - if( materialName != "" - && material != null) { - if (currentMode == emfModuleMode.EMFModulePalette) { - this.palettes.put(materialName, material); - // TODO Remove this later ... - this.materials.put("palette:" + materialName, material); - } else { - this.materials.put(materialName, material); - } - materialName = ""; - material = null; - } - Log.verbose("Stop parsing Mesh file"); - - try { - fileIO.close(); - fileIOStream.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - Log.verbose("New mesh : "); - Log.verbose(" nb vertex: " + this.listVertex.size()); - Log.verbose(" nb UV: " + this.listUV.size()); - Log.verbose(" nb Colors: " + this.listColor.size()); - Log.verbose(" nb Normal face: " + this.listFacesNormal.size()); - Log.verbose(" nb Normal vertex: " + this.listVertexNormal.size()); - Log.verbose(" nb Faces: " + this.listFaces.size()); - Log.verbose(" nb Faces (palette): " + this.listPaletteFaces.size()); - Log.verbose(" nb material: " + this.materials.size()); - Log.verbose(" nb palettes: " + this.palettes.size()); - //Log.verbose(" nb physic: " + this.physics.size()); - generateVBO(); -} - - -//private String loadNextData(int _maxData, -// BufferedReader _file) { -// return loadNextData(_maxData, _file, false, false, true); -//} -// -//private String loadNextData(int _maxData, -// BufferedReader _file, -// boolean _removeTabs, -// boolean _stopColomn, -// boolean _stopPipe) { -// StringBuffer buf = new StringBuffer(); -// int outSize = 0; -// /* -// if (this.zipReadingOffset >= this.zipContent.size()) { -// element[0] = '\0'; -// return null; -// } -// */ -// int current = '\0'; -// try { -// current = _file.read(); -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// while (current != '\0') { -// if( _removeTabs == false -// || buf.length() == 0) { -// buf.append((char)current); -// } -// if( current == '\n' -// || current == '\r' -// || ( current == '|' -// && _stopPipe == true) -// || ( current == ':' -// && _stopColomn == true) ) -// { -// //Log.debug(" plop : '" + _elementLine + "'" ); -// return buf.toString(); -// } else if( buf.length() == 0 -// && current != '\t') { -// buf.append((char)current); -// } -// // check maxData size ... -// if (outSize >= _maxData-1) { -// return buf.toString(); -// } -// try { -// current = _file.read(); -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// } -// if (outSize != 0) { -// // send last line -// return buf.toString(); -// } -// return null; -//} - -private String readLine(BufferedReader _file, Dynamic level) { - level.value = 0; - StringBuffer buf = new StringBuffer(); - boolean startLine = true; - boolean differentThanSpace = false; - boolean hasSpace = false; - int current = '\0'; - try { - current = _file.read(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - while(current != '\0' && current != -1) { - if (current == '\r') { - // nothing to do ... - } else if (current == '\t' && startLine) { - level.value++; - } else if (current == '\n') { - if (!startLine) { - break; - } - } else { - startLine = false; - if (current == '#') { - // detect comment ==> remove to the end of file ==> no data availlable - if (differentThanSpace == false) { - while(current != '\0' && current != '\n') { + startLine = false; + if (current == '#') { + // detect comment ==> remove to the end of file ==> no data availlable + if (differentThanSpace == false) { + while (current != '\0' && current != '\n') { + try { + current = _file.read(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + buf = new StringBuffer(); + startLine = true; + differentThanSpace = false; + hasSpace = false; + level.value = 0; + continue; + } + // end of data removed + while (current != '\0' && current != '\n') { try { current = _file.read(); } catch (IOException e) { @@ -505,47 +544,31 @@ private String readLine(BufferedReader _file, Dynamic level) { e.printStackTrace(); } } - buf = new StringBuffer(); - startLine = true; - differentThanSpace = false; - hasSpace = false; - level.value = 0; - continue; + break; } - // end of data removed - while(current != '\0' && current != '\n') { - try { - current = _file.read(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + differentThanSpace = true; + if (current == '\t' || current == ' ') { + hasSpace = true; + } else { + if (hasSpace == true) { + buf.append(' '); + hasSpace = false; } + buf.append((char) current); } - break; } - differentThanSpace = true; - if (current == '\t' || current == ' ') { - hasSpace = true; - } else { - if (hasSpace == true) { - buf.append(' '); - hasSpace = false; - } - buf.append((char)current); + try { + current = _file.read(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } } - try { - current=_file.read(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + Log.error("Read line: offset=" + level.value + "\n" + buf.toString()); + if (buf.length() == 0) { + return null; } + return buf.toString(); } - Log.error("Read line: offset=" + level.value + "\n" + buf.toString()); - if (buf.length() == 0) { - return null; - } - return buf.toString(); -} - + } diff --git a/src/org/atriasoft/loader3d/resources/ResourceMeshHeightMap.java b/src/org/atriasoft/loader3d/resources/ResourceMeshHeightMap.java new file mode 100644 index 0000000..a04131a --- /dev/null +++ b/src/org/atriasoft/loader3d/resources/ResourceMeshHeightMap.java @@ -0,0 +1,64 @@ +package org.atriasoft.loader3d.resources; + +import java.util.Map; + +import org.atriasoft.etk.math.Vector3f; +import org.atriasoft.loader3d.Face; +import org.atriasoft.loader3d.FaceIndexing; +import org.atriasoft.loader3d.NormalMode; +import org.atriasoft.loader3d.model.Material; + +public class ResourceMeshHeightMap extends ResourceMesh { + /* + * 0 1 2 3 + * 0 *-------*-------*-------* + * \ OO / \ 02 / \ 04 / \ + * \ / \ / \ / \ + * \ / 01 \ / 03 \ / 05 \ + * *-------*-------*-------* + * / \ / \ / \ / + * / \ / \ / \ / + * / \ / \ / \ / + * *-------*-------*-------* + */ + public ResourceMeshHeightMap() { + this.normalMode = NormalMode.FACE; + } + + void addPalette(String key, Material mat) { + this.palettes.put(key, mat); + } + + Map getPalette() { + return this.palettes; + } + + public void udateData(float[][] heightMap, String[][] colorMap, int width, int length) throws Exception { + float ratio = 0.2f; + for (int yyy = 0; yyy < length - 1; yyy++) { + for (int xxx = 0; xxx < width - 1; xxx++) { + Vector3f vertex1 = new Vector3f(ratio * xxx, ratio * yyy, ratio * heightMap[yyy][xxx]); + this.listVertex.add(vertex1); + Vector3f vertex2 = new Vector3f(ratio * (xxx + 1), ratio * yyy, ratio * heightMap[yyy][xxx + 1]); + this.listVertex.add(vertex2); + Vector3f vertex3 = new Vector3f(ratio * xxx, ratio * (yyy + 1), ratio * heightMap[yyy + 1][xxx]); + this.listVertex.add(vertex3); + FaceIndexing currentFaceIndexing = null; + String meshFaceMaterialID = colorMap[yyy][xxx * 2]; + if (this.listPaletteFaces.containsKey(meshFaceMaterialID) == false) { + currentFaceIndexing = new FaceIndexing(); + this.listPaletteFaces.put(meshFaceMaterialID, currentFaceIndexing); + } else { + currentFaceIndexing = this.listPaletteFaces.get(meshFaceMaterialID); + } + Face face = new Face(); + int pos = this.listVertex.size() - 3; + face.setVertex(pos, pos + 1, pos + 2); + + currentFaceIndexing.face().add(face); + } + } + generateVBO(); + } + +} diff --git a/src/org/atriasoft/loader3d/resources/ResourceStaticMeshObjBynamic.java b/src/org/atriasoft/loader3d/resources/ResourceStaticMeshObjDynamic.java similarity index 80% rename from src/org/atriasoft/loader3d/resources/ResourceStaticMeshObjBynamic.java rename to src/org/atriasoft/loader3d/resources/ResourceStaticMeshObjDynamic.java index 5a8dd47..72fc8a9 100644 --- a/src/org/atriasoft/loader3d/resources/ResourceStaticMeshObjBynamic.java +++ b/src/org/atriasoft/loader3d/resources/ResourceStaticMeshObjDynamic.java @@ -5,10 +5,10 @@ import org.atriasoft.gale.resource.Resource; import org.atriasoft.gale.resource.ResourceVirtualArrayObject; import org.atriasoft.loader3d.internal.Log; -public class ResourceStaticMeshObjBynamic extends ResourceStaticMeshObj { +public class ResourceStaticMeshObjDynamic extends ResourceStaticMeshObj { - public static ResourceStaticMeshObjBynamic create(final Uri uriObj) { - ResourceStaticMeshObjBynamic resource; + public static ResourceStaticMeshObjDynamic create(final Uri uriObj) { + ResourceStaticMeshObjDynamic resource; Resource resource2; final String name = uriObj.getValue(); if (name.isEmpty() || name.equals("---")) { @@ -17,19 +17,19 @@ public class ResourceStaticMeshObjBynamic extends ResourceStaticMeshObj { } resource2 = Resource.getManager().localKeep(name); if (resource2 != null) { - if (resource2 instanceof ResourceStaticMeshObjBynamic tmpp) { + if (resource2 instanceof ResourceStaticMeshObjDynamic tmpp) { resource2.keep(); return tmpp; } Log.critical("Request resource file : '" + name + "' With the wrong type (dynamic cast error)"); return null; } - resource = new ResourceStaticMeshObjBynamic(uriObj); + resource = new ResourceStaticMeshObjDynamic(uriObj); Resource.getManager().localAdd(resource); return resource; } - protected ResourceStaticMeshObjBynamic(final Uri uriFile) { + protected ResourceStaticMeshObjDynamic(final Uri uriFile) { super(uriFile); } diff --git a/test/src/test/atriasoft/loader3d/TestBasicLog.java b/test/src/test/atriasoft/loader3d/TestBasicLog.java index d262fbf..0cd19d5 100644 --- a/test/src/test/atriasoft/loader3d/TestBasicLog.java +++ b/test/src/test/atriasoft/loader3d/TestBasicLog.java @@ -13,7 +13,7 @@ import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.atriasoft.etk.Uri; import org.atriasoft.gale.Gale; import org.atriasoft.gale.context.GaleContextTest; -import org.atriasoft.loader3d.resources.ResourceMeshEmf; +import org.atriasoft.loader3d.resources.ResourceMeshHeightMap; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.TestMethodOrder; @@ -27,10 +27,10 @@ public class TestBasicLog { GaleContextTest tmpContext = new GaleContextTest(); Uri.setGroup("EMF", "emf"); Uri.setApplication(TestBasicLog.class, ""); - Uri.addLibrary("test", ResourceMeshEmf.class, "/resources"); + Uri.addLibrary("test", ResourceMeshHeightMap.class, "/resources"); //ResourceMeshEmf tmp = new ResourceMeshEmf(new Uri("EMF", "tree1.emf", "test")); - ResourceMeshEmf tmp = new ResourceMeshEmf(new Uri("FILE", "/home/heero/dev/workspace-game/atriasoft/loader3d/out/eclipse/classes/resources/emf/tree1.emf")); + ResourceMeshHeightMap tmp = new ResourceMeshHeightMap(new Uri("FILE", "/home/heero/dev/workspace-game/atriasoft/loader3d/out/eclipse/classes/resources/emf/tree1.emf")); }