use of net.drewke.tdme.engine.model.Face in project tdme by andreasdr.
the class DAEReader method readGeometry.
/**
* Reads a geometry
* @param authoring tools
* @param path name
* @param model
* @param group
* @param xml root
* @param xml node id
* @param material symbols
* @throws Exception
*/
public static void readGeometry(AuthoringTool authoringTool, String pathName, Model model, Group group, Element xmlRoot, String xmlNodeId, HashMap<String, String> materialSymbols) throws Exception {
StringTokenizer t;
//
FacesEntity facesEntity = null;
ArrayList<FacesEntity> facesEntities = new ArrayList<FacesEntity>(Arrays.asList(group.getFacesEntities()));
int verticesOffset = group.getVertices().length;
ArrayList<Vector3> vertices = new ArrayList<Vector3>(Arrays.asList(group.getVertices()));
int normalsOffset = group.getNormals().length;
ArrayList<Vector3> normals = new ArrayList<Vector3>(Arrays.asList(group.getNormals()));
int textureCoordinatesOffset = group.getTextureCoordinates() != null ? group.getTextureCoordinates().length : 0;
ArrayList<TextureCoordinate> textureCoordinates = group.getTextureCoordinates() != null ? new ArrayList<TextureCoordinate>(Arrays.asList(group.getTextureCoordinates())) : new ArrayList<TextureCoordinate>();
Element xmlLibraryGeometries = getChildrenByTagName(xmlRoot, "library_geometries").get(0);
for (Element xmlGeometry : getChildrenByTagName(xmlLibraryGeometries, "geometry")) {
if (xmlGeometry.getAttribute("id").equals(xmlNodeId)) {
Element xmlMesh = getChildrenByTagName(xmlGeometry, "mesh").get(0);
ArrayList<Element> xmlPolygonsList = new ArrayList<Element>();
// try to read from triangles
for (Element xmlTriangesElement : getChildrenByTagName(xmlMesh, "triangles")) {
xmlPolygonsList.add(xmlTriangesElement);
}
// try to read from polylist
for (Element xmlPolyListElement : getChildrenByTagName(xmlMesh, "polylist")) {
xmlPolygonsList.add(xmlPolyListElement);
}
// try to read from polygons
for (Element xmlPolygonsElement : getChildrenByTagName(xmlMesh, "polygons")) {
xmlPolygonsList.add(xmlPolygonsElement);
}
// parse from xml polygons elements
for (Element xmlPolygons : xmlPolygonsList) {
ArrayList<Face> faces = new ArrayList<Face>();
facesEntity = new FacesEntity(group, xmlNodeId);
if (xmlPolygons.getNodeName().toLowerCase().equals("polylist")) {
t = new StringTokenizer(getChildrenByTagName(xmlPolygons, "vcount").get(0).getTextContent());
while (t.hasMoreTokens()) {
int vertexCount = Integer.parseInt(t.nextToken());
if (vertexCount != 3) {
throw new ModelFileIOException("we only support triangles in " + xmlNodeId);
}
}
}
// parse triangles
int xmlInputs = -1;
int xmlVerticesOffset = -1;
String xmlVerticesSource = null;
int xmlNormalsOffset = -1;
String xmlNormalsSource = null;
int xmlTexCoordOffset = -1;
String xmlTexCoordSource = null;
int xmlColorOffset = -1;
String xmlColorSource = null;
// material
String xmlMaterialId = xmlPolygons.getAttribute("material");
String materialSymbol = materialSymbols.get(xmlMaterialId);
if (materialSymbol != null)
xmlMaterialId = materialSymbol.substring(1);
if (xmlMaterialId != null && xmlMaterialId.length() > 0) {
Material material = model.getMaterials().get(xmlMaterialId);
if (material == null) {
// parse material as we do not have it yet
material = readMaterial(authoringTool, pathName, model, xmlRoot, xmlMaterialId);
}
// set it up
facesEntity.setMaterial(material);
}
// parse input sources
HashSet<Integer> xmlInputSet = new HashSet<Integer>();
for (Element xmlTrianglesInput : getChildrenByTagName(xmlPolygons, "input")) {
// check for vertices sources
if (xmlTrianglesInput.getAttribute("semantic").equals("VERTEX")) {
xmlVerticesOffset = Integer.parseInt(xmlTrianglesInput.getAttribute("offset"));
xmlVerticesSource = xmlTrianglesInput.getAttribute("source").substring(1);
xmlInputSet.add(xmlVerticesOffset);
} else // check for normals sources
if (xmlTrianglesInput.getAttribute("semantic").equals("NORMAL")) {
xmlNormalsOffset = Integer.parseInt(xmlTrianglesInput.getAttribute("offset"));
xmlNormalsSource = xmlTrianglesInput.getAttribute("source").substring(1);
xmlInputSet.add(xmlNormalsOffset);
}
// check for texture coordinate sources
if (xmlTrianglesInput.getAttribute("semantic").equals("TEXCOORD")) {
xmlTexCoordOffset = Integer.parseInt(xmlTrianglesInput.getAttribute("offset"));
xmlTexCoordSource = xmlTrianglesInput.getAttribute("source").substring(1);
xmlInputSet.add(xmlTexCoordOffset);
}
// check for color coordinate sources
if (xmlTrianglesInput.getAttribute("semantic").equals("COLOR")) {
xmlColorOffset = Integer.parseInt(xmlTrianglesInput.getAttribute("offset"));
xmlColorSource = xmlTrianglesInput.getAttribute("source").substring(1);
xmlInputSet.add(xmlColorOffset);
}
}
xmlInputs = xmlInputSet.size();
// get vertices source
for (Element xmlVertices : getChildrenByTagName(xmlMesh, "vertices")) {
if (xmlVertices.getAttribute("id").equals(xmlVerticesSource)) {
for (Element xmlVerticesInput : getChildrenByTagName(xmlVertices, "input")) {
if (xmlVerticesInput.getAttribute("semantic").equalsIgnoreCase("position")) {
xmlVerticesSource = xmlVerticesInput.getAttribute("source").substring(1);
} else if (xmlVerticesInput.getAttribute("semantic").equalsIgnoreCase("normal")) {
xmlNormalsSource = xmlVerticesInput.getAttribute("source").substring(1);
}
}
}
}
// check for triangles vertices sources
if (xmlVerticesSource == null) {
throw new ModelFileIOException("Could not determine triangles vertices source for '" + xmlNodeId + "'");
}
// check for triangles normals sources
if (xmlNormalsSource == null) {
throw new ModelFileIOException("Could not determine triangles normal source for '" + xmlNodeId + "'");
}
// load vertices, normals, texture coordinates
for (Element xmlMeshSource : getChildrenByTagName(xmlMesh, "source")) {
// vertices
if (xmlMeshSource.getAttribute("id").equals(xmlVerticesSource)) {
Element xmlFloatArray = getChildrenByTagName(xmlMeshSource, "float_array").get(0);
String valueString = xmlFloatArray.getTextContent();
t = new StringTokenizer(valueString, " \n\r");
while (t.hasMoreTokens()) {
Vector3 v = new Vector3(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
vertices.add(v);
}
} else // normals
if (xmlMeshSource.getAttribute("id").equals(xmlNormalsSource)) {
Element xmlFloatArray = getChildrenByTagName(xmlMeshSource, "float_array").get(0);
String valueString = xmlFloatArray.getTextContent();
t = new StringTokenizer(valueString, " \n\r");
while (t.hasMoreTokens()) {
Vector3 v = new Vector3(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
normals.add(v);
}
}
// texture coordinates
if (xmlTexCoordSource != null) {
if (xmlMeshSource.getAttribute("id").equals(xmlTexCoordSource)) {
Element xmlFloatArray = getChildrenByTagName(xmlMeshSource, "float_array").get(0);
String valueString = xmlFloatArray.getTextContent();
t = new StringTokenizer(valueString, " \n\r");
while (t.hasMoreTokens()) {
TextureCoordinate tc = new TextureCoordinate(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
textureCoordinates.add(tc);
}
}
}
}
// load faces
for (Element xmlPolygon : getChildrenByTagName(xmlPolygons, "p")) {
String valueString = xmlPolygon.getTextContent();
t = new StringTokenizer(valueString, " \n\r");
int[] vi = new int[3];
int viIdx = 0;
int[] ni = new int[3];
int niIdx = 0;
int[] ti = xmlTexCoordSource == null ? null : new int[3];
int tiIdx = 0;
int valueIdx = 0;
boolean valid = true;
while (t.hasMoreTokens()) {
int value = Integer.parseInt(t.nextToken());
if (valueIdx % xmlInputs == xmlVerticesOffset) {
vi[viIdx++] = value;
// validate
if (value < 0 || value >= vertices.size() - verticesOffset) {
valid = false;
}
// fix for some strange models
if (xmlNormalsSource != null && xmlNormalsOffset == -1) {
ni[niIdx++] = value;
// validate
if (value < 0 || value >= normals.size() - normalsOffset) {
valid = false;
}
}
}
if (xmlNormalsOffset != -1 && valueIdx % xmlInputs == xmlNormalsOffset) {
ni[niIdx++] = value;
// validate
if (value < 0 || value >= normals.size() - normalsOffset) {
valid = false;
}
}
if (xmlTexCoordOffset != -1 && valueIdx % xmlInputs == xmlTexCoordOffset) {
ti[tiIdx++] = value;
// validate
if (value < 0 || value >= textureCoordinates.size() - textureCoordinatesOffset) {
valid = false;
}
}
if (viIdx == 3 && niIdx == 3 && (ti == null || tiIdx == 3)) {
// only add valid faces
if (valid) {
// add face
Face f = new Face(group, vi[0] + verticesOffset, vi[1] + verticesOffset, vi[2] + verticesOffset, ni[0] + normalsOffset, ni[1] + normalsOffset, ni[2] + normalsOffset);
if (ti != null) {
f.setTextureCoordinateIndices(ti[0] + textureCoordinatesOffset, ti[1] + textureCoordinatesOffset, ti[2] + textureCoordinatesOffset);
}
faces.add(f);
}
viIdx = 0;
niIdx = 0;
tiIdx = 0;
valid = true;
}
valueIdx++;
}
}
// add faces entities if we have any
if (faces.isEmpty() == false) {
facesEntity.setFaces(faces);
facesEntities.add(facesEntity);
}
}
}
}
// set up group
group.setVertices(vertices);
group.setNormals(normals);
if (textureCoordinates.size() > 0)
group.setTextureCoordinates(textureCoordinates);
group.setFacesEntities(facesEntities);
// create normal tangents and bitangents
ModelHelper.createNormalTangentsAndBitangents(group);
// determine features
group.determineFeatures();
}
use of net.drewke.tdme.engine.model.Face in project tdme by andreasdr.
the class TMWriter method writeFacesEntities.
/**
* Write faces entities to output stream
* @param output stream
* @param faces entities
* @throws IOException
*/
private static void writeFacesEntities(OutputStream os, FacesEntity[] facesEntities) throws IOException {
writeInt(os, facesEntities.length);
for (int i = 0; i < facesEntities.length; i++) {
FacesEntity fe = facesEntities[i];
writeString(os, fe.getId());
if (fe.getMaterial() == null) {
writeBoolean(os, false);
} else {
writeBoolean(os, true);
writeString(os, fe.getMaterial().getId());
}
writeInt(os, fe.getFaces().length);
for (int j = 0; j < fe.getFaces().length; j++) {
Face f = fe.getFaces()[j];
writeIndices(os, f.getVertexIndices());
writeIndices(os, f.getNormalIndices());
writeIndices(os, f.getTextureCoordinateIndices());
writeIndices(os, f.getTangentIndices());
writeIndices(os, f.getBitangentIndices());
}
}
}
use of net.drewke.tdme.engine.model.Face in project tdme by andreasdr.
the class LevelEditorView method createLevelEditorGroundPlateModel.
/**
* Creates a level editor ground plate
* @return ground
*/
private Model createLevelEditorGroundPlateModel() {
// ground selectedEntity
Model groundPlate = new Model("leveleditor.ground", "leveleditor.ground", UpVector.Y_UP, RotationOrder.XYZ, (BoundingBox) BoundingBox.createBoundingVolume(new Vector3(0f, -0.01f, 0f), new Vector3(1f, +0.01f, 1f)));
// material
Material groundPlateMaterial = new Material("ground");
groundPlateMaterial.getDiffuseColor().setAlpha(0.75f);
groundPlateMaterial.setDiffuseTexture("resources/tools/leveleditor/textures", "groundplate.png");
groundPlateMaterial.getSpecularColor().set(0f, 0f, 0f, 1f);
groundPlate.getMaterials().put("ground", groundPlateMaterial);
// group
Group groundGroup = new Group(groundPlate, null, "ground", "ground");
// faces entity
// ground
FacesEntity groupFacesEntityGround = new FacesEntity(groundGroup, "leveleditor.ground.facesentity");
groupFacesEntityGround.setMaterial(groundPlateMaterial);
// faces entity
ArrayList<FacesEntity> groupFacesEntities = new ArrayList<FacesEntity>();
groupFacesEntities.add(groupFacesEntityGround);
// vertices
ArrayList<Vector3> groundVertices = new ArrayList<Vector3>();
// left, near, ground
groundVertices.add(new Vector3(0.0f, 0.0f, 0.0f));
// left, far, ground
groundVertices.add(new Vector3(0.0f, 0.0f, +groundPlateDepth));
// right far, ground
groundVertices.add(new Vector3(+groundPlateWidth, 0.0f, +groundPlateDepth));
// right, near, ground
groundVertices.add(new Vector3(+groundPlateWidth, 0.0f, 0.0f));
// normals
ArrayList<Vector3> groundNormals = new ArrayList<Vector3>();
// ground
groundNormals.add(new Vector3(0f, 1f, 0f));
// texture coordinates
ArrayList<TextureCoordinate> groundTextureCoordinates = new ArrayList<TextureCoordinate>();
groundTextureCoordinates.add(new TextureCoordinate(0f, 1f));
groundTextureCoordinates.add(new TextureCoordinate(0f, 0f));
groundTextureCoordinates.add(new TextureCoordinate(1f, 0f));
groundTextureCoordinates.add(new TextureCoordinate(1f, 1f));
// faces ground
ArrayList<Face> groundFacesGround = new ArrayList<Face>();
groundFacesGround.add(new Face(groundGroup, 0, 1, 2, 0, 0, 0, 0, 1, 2));
groundFacesGround.add(new Face(groundGroup, 2, 3, 0, 0, 0, 0, 2, 3, 0));
// set up faces entity
groupFacesEntityGround.setFaces(groundFacesGround);
// setup ground group
groundGroup.setVertices(groundVertices);
groundGroup.setNormals(groundNormals);
groundGroup.setTextureCoordinates(groundTextureCoordinates);
groundGroup.setFacesEntities(groupFacesEntities);
// register group
groundPlate.getGroups().put(groundGroup.getId(), groundGroup);
groundPlate.getSubGroups().put(groundGroup.getId(), groundGroup);
// prepare for indexed rendering
ModelHelper.prepareForIndexedRendering(groundPlate);
//
return groundPlate;
}
use of net.drewke.tdme.engine.model.Face in project tdme by andreasdr.
the class Object3DBase method getFaceTriangles.
/**
* Retrieves complete list of face triangles for all render groups
* @return faces
*/
public Triangle[] getFaceTriangles() {
ArrayList<Triangle> triangles = new ArrayList<Triangle>();
for (Object3DGroup object3DGroup : object3dGroups) {
Vector3[] groupVerticesTransformed = object3DGroup.mesh.transformedVertices;
for (FacesEntity facesEntity : object3DGroup.group.getFacesEntities()) for (Face face : facesEntity.getFaces()) {
int[] faceVertexIndices = face.getVertexIndices();
triangles.add(new Triangle(groupVerticesTransformed[faceVertexIndices[0]].clone(), groupVerticesTransformed[faceVertexIndices[1]].clone(), groupVerticesTransformed[faceVertexIndices[2]].clone()));
}
}
Triangle[] triangleArray = new Triangle[triangles.size()];
triangles.toArray(triangleArray);
return triangleArray;
}
use of net.drewke.tdme.engine.model.Face in project tdme by andreasdr.
the class PrimitiveModel method createOrientedBoundingBoxModel.
/**
* Creates a model from oriented bounding box
* @param bounding box
* @param id
* @return model
*/
public static Model createOrientedBoundingBoxModel(OrientedBoundingBox orientedBoundingBox, String id) {
// ground model
Model model = new Model(id, id, UpVector.Y_UP, RotationOrder.XYZ, null);
// material
Material material = new Material("tdme.primitive.material");
material.getAmbientColor().set(0.5f, 0.5f, 0.5f, 1.0f);
material.getDiffuseColor().set(1.0f, 0.5f, 0.5f, 0.5f);
material.getSpecularColor().set(0f, 0f, 0f, 1f);
model.getMaterials().put(material.getId(), material);
// group
Group group = new Group(model, null, "group", "group");
// faces entity
FacesEntity groupFacesEntity = new FacesEntity(group, "faces entity");
groupFacesEntity.setMaterial(material);
// faces entity
ArrayList<FacesEntity> groupFacesEntities = new ArrayList<FacesEntity>();
groupFacesEntities.add(groupFacesEntity);
// triangle vertices indexes
int[][] fvi = OrientedBoundingBox.facesVerticesIndexes;
// vertices
ArrayList<Vector3> vertices = new ArrayList<Vector3>();
for (Vector3 vertex : orientedBoundingBox.vertices) {
vertices.add(vertex.clone());
}
Vector3[] axes = orientedBoundingBox.axes;
// normals
ArrayList<Vector3> normals = new ArrayList<Vector3>();
normals.add(axes[0].clone().scale(-1f));
normals.add(axes[0].clone());
normals.add(axes[1].clone().scale(-1f));
normals.add(axes[1].clone());
normals.add(axes[2].clone().scale(-1f));
normals.add(axes[2].clone());
// faces
ArrayList<Face> faces = new ArrayList<Face>();
// left
faces.add(new Face(group, fvi[0][0], fvi[0][1], fvi[0][2], 0, 0, 0));
faces.add(new Face(group, fvi[1][0], fvi[1][1], fvi[1][2], 0, 0, 0));
// right
faces.add(new Face(group, fvi[2][0], fvi[2][1], fvi[2][2], 1, 1, 1));
faces.add(new Face(group, fvi[3][0], fvi[3][1], fvi[3][2], 1, 1, 1));
// top
faces.add(new Face(group, fvi[4][0], fvi[4][1], fvi[4][2], 2, 2, 2));
faces.add(new Face(group, fvi[5][0], fvi[5][1], fvi[5][2], 2, 2, 2));
// bottom
faces.add(new Face(group, fvi[6][0], fvi[6][1], fvi[6][2], 3, 3, 3));
faces.add(new Face(group, fvi[7][0], fvi[7][1], fvi[7][2], 3, 3, 3));
// near
faces.add(new Face(group, fvi[8][0], fvi[8][1], fvi[8][2], 4, 4, 4));
faces.add(new Face(group, fvi[9][0], fvi[9][1], fvi[9][2], 4, 4, 4));
// far
faces.add(new Face(group, fvi[10][0], fvi[10][1], fvi[10][2], 5, 5, 5));
faces.add(new Face(group, fvi[11][0], fvi[11][1], fvi[11][2], 5, 5, 5));
// set up faces entity
groupFacesEntity.setFaces(faces);
// setup group vertex data
group.setVertices(vertices);
group.setNormals(normals);
group.setFacesEntities(groupFacesEntities);
// determine features
group.determineFeatures();
// register group
model.getGroups().put("group", group);
model.getSubGroups().put("group", group);
// prepare for indexed rendering
ModelHelper.prepareForIndexedRendering(model);
//
return model;
}
Aggregations