This commit is contained in:
Edouard DUPIN 2022-02-21 18:20:32 +01:00
parent 1679d62300
commit 8ba3c34262
6 changed files with 1430 additions and 1362 deletions

View File

@ -1,90 +1,6 @@
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) == ' ') {
data = data.substring(1);
@ -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;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -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;
@ -20,47 +22,56 @@ 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);
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;
}
int getValue() {
return this.value;
}
};
}
// TODO : rework with string line extractor
public ResourceMeshEmf(Uri _fileName) {
super(_fileName);
mode = RenderMode.TRIANGLE;
/**
* 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);
InputStream fileIOStream = Uri.getStream(fileName);
if (fileIOStream == null) {
Log.error("CAn not get the file named='" + _fileName + "'");
Log.error("CAn not get the file named='" + fileName + "'");
return; // TODO false;
}
BufferedReader fileIO = new BufferedReader(new InputStreamReader(fileIOStream));
@ -69,7 +80,7 @@ public ResourceMeshEmf(Uri _fileName) {
//int size = fileIO.size();
String inputDataLine;
Dynamic<Integer> levelIO = new Dynamic<Integer>(0);
Dynamic<Integer> levelIO = new Dynamic<>(0);
// load the first line :
inputDataLine = readLine(fileIO, levelIO);
if (inputDataLine.startsWith("EMF(STRING)") == true) {
@ -82,7 +93,7 @@ public ResourceMeshEmf(Uri _fileName) {
return; // TODO false;
}
emfModuleMode currentMode = emfModuleMode.EMFModuleNone;
Log.verbose("Start parsing Mesh file : " + _fileName);
Log.verbose("Start parsing Mesh file : " + fileName);
// mesh global param :
String currentMeshName = "";
FaceIndexing currentFaceIndexing = null;
@ -108,7 +119,17 @@ public ResourceMeshEmf(Uri _fileName) {
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();
@ -116,8 +137,7 @@ public ResourceMeshEmf(Uri _fileName) {
//Log.error("new offset: " + offsetVertexId + " " + offsetUV + " " + offsetFaceNormal + " " + offsetVertexNormal);
} else if (inputDataLine.startsWith("Materials:")) {
// add previous material:
if( materialName != ""
&& material != null) {
if (materialName != "" && material != null) {
if (currentMode == emfModuleMode.EMFModulePalette) {
this.palettes.put(materialName, material);
// TODO Remove this later ...
@ -134,8 +154,7 @@ public ResourceMeshEmf(Uri _fileName) {
Log.verbose("Parse Material: " + materialName);
} else if (inputDataLine.startsWith("Palettes:")) {
// add previous material:
if( materialName != ""
&& material != null) {
if (materialName != "" && material != null) {
if (currentMode == emfModuleMode.EMFModulePalette) {
this.palettes.put(materialName, material);
// TODO Remove this later ...
@ -156,9 +175,13 @@ public ResourceMeshEmf(Uri _fileName) {
} else {
currentMode = emfModuleMode.EMFModuleNone;
}
} else {
if ( currentMode.getValue() >= emfModuleMode.EMFModuleMesh.getValue()
&& currentMode.getValue() <= emfModuleMode.EMFModuleMesh_END.getValue()) {
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) {
currentMode = emfModuleMode.EMFModuleMeshVertex;
@ -292,10 +315,9 @@ 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;
@ -332,7 +354,7 @@ public ResourceMeshEmf(Uri _fileName) {
// TODO : ...
Log.verbose(" illum " + tmpVal);
} else if (inputDataLine.startsWith("map_Kd ") == true) {
Uri tmpTexture = _fileName.getParent();
Uri tmpTexture = fileName.getParent();
tmpTexture = tmpTexture.pathAdd(inputDataLine.substring(7));
material.setTexture0(tmpTexture);
Log.verbose(" Texture " + tmpTexture);
@ -340,12 +362,13 @@ public ResourceMeshEmf(Uri _fileName) {
//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);
@ -365,15 +388,13 @@ public ResourceMeshEmf(Uri _fileName) {
// Log.error("ERROR when parsing :'" + inputDataLine + "' in physical shape ...");
// }
}
} else {
// unknow ...
Log.warning("Unknow type of line == > jump end of line ... " + inputDataLine);
}
continue;
}
// unknown ...
Log.warning("Unknown type of line == > jump end of line ... " + inputDataLine);
}
// add last material ...
if( materialName != ""
&& material != null) {
if (materialName != "" && material != null) {
if (currentMode == emfModuleMode.EMFModulePalette) {
this.palettes.put(materialName, material);
// TODO Remove this later ...
@ -405,10 +426,12 @@ public ResourceMeshEmf(Uri _fileName) {
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);

View File

@ -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<String, Material> 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();
}
}

View File

@ -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);
}

View File

@ -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"));
}