Search in sources :

Example 1 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity in project tdme by andreasdr.

the class Tools method createGroundModel.

/**
	 * Creates a ground plate
	 * @param width
	 * @param depth
	 * @param float y
	 * @return ground model
	 */
public static Model createGroundModel(float width, float depth, float y) {
    // ground model
    Model ground = new Model("ground", "ground", UpVector.Y_UP, RotationOrder.XYZ, null);
    //	material
    Material groundMaterial = new Material("ground");
    groundMaterial.getSpecularColor().set(0f, 0f, 0f, 1f);
    ground.getMaterials().put("ground", groundMaterial);
    //	group
    Group groundGroup = new Group(ground, null, "ground", "ground");
    //	faces entity
    //		ground
    FacesEntity groupFacesEntityGround = new FacesEntity(groundGroup, "ground group faces entity ground");
    groupFacesEntityGround.setMaterial(groundMaterial);
    //	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(-width, y, -depth));
    // left, far, ground
    groundVertices.add(new Vector3(-width, y, +depth));
    // right far, ground
    groundVertices.add(new Vector3(+width, y, +depth));
    // right, near, ground
    groundVertices.add(new Vector3(+width, y, -depth));
    //	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, 0f));
    groundTextureCoordinates.add(new TextureCoordinate(0f, 1f));
    groundTextureCoordinates.add(new TextureCoordinate(1f, 1f));
    groundTextureCoordinates.add(new TextureCoordinate(1f, 0f));
    //	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
    ground.getGroups().put("ground", groundGroup);
    ground.getSubGroups().put("ground", groundGroup);
    // prepare for indexed rendering
    ModelHelper.prepareForIndexedRendering(ground);
    //
    return ground;
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Group(net.drewke.tdme.engine.model.Group) Model(net.drewke.tdme.engine.model.Model) ArrayList(java.util.ArrayList) Material(net.drewke.tdme.engine.model.Material) Vector3(net.drewke.tdme.math.Vector3) TextureCoordinate(net.drewke.tdme.engine.model.TextureCoordinate) Face(net.drewke.tdme.engine.model.Face)

Example 2 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity 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();
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Vector3(net.drewke.tdme.math.Vector3) Material(net.drewke.tdme.engine.model.Material) Joint(net.drewke.tdme.engine.model.Joint) StringTokenizer(java.util.StringTokenizer) TextureCoordinate(net.drewke.tdme.engine.model.TextureCoordinate) Face(net.drewke.tdme.engine.model.Face) HashSet(java.util.HashSet)

Example 3 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity 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());
        }
    }
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Face(net.drewke.tdme.engine.model.Face) Joint(net.drewke.tdme.engine.model.Joint)

Example 4 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity in project tdme by andreasdr.

the class Object3DGroupMesh method createMesh.

/**
	 * Creates a object3d group mesh from group
	 * @param animation processing target
	 * @param group
	 * @param transformationm matrices
	 * @return object 3d group mesh
	 */
protected static Object3DGroupMesh createMesh(Engine.AnimationProcessingTarget animationProcessingTarget, Group group, HashMap<String, Matrix4x4> transformationMatrices) {
    Object3DGroupMesh mesh = new Object3DGroupMesh();
    // group data
    Vector3[] groupVertices = group.getVertices();
    Vector3[] groupNormals = group.getNormals();
    TextureCoordinate[] groupTextureCoordinates = group.getTextureCoordinates();
    Vector3[] groupTangents = group.getTangents();
    Vector3[] groupBitangents = group.getBitangents();
    // determine face count
    int faceCount = group.getFaceCount();
    // set up face count
    mesh.faces = faceCount;
    // animation processing target
    mesh.animationProcessingTarget = animationProcessingTarget;
    // transformations for skinned meshes
    Skinning skinning = group.getSkinning();
    mesh.skinning = skinning != null;
    // transformed mesh vertices
    mesh.transformedVertices = new Vector3[groupVertices.length];
    for (int j = 0; j < mesh.transformedVertices.length; j++) {
        mesh.transformedVertices[j] = new Vector3().set(groupVertices[j]);
    }
    // transformed mesh normals
    mesh.transformedNormals = new Vector3[groupNormals.length];
    for (int j = 0; j < mesh.transformedNormals.length; j++) {
        mesh.transformedNormals[j] = new Vector3().set(groupNormals[j]);
    }
    // texture coordinates
    if (groupTextureCoordinates != null) {
        mesh.textureCoordinates = new TextureCoordinate[groupTextureCoordinates.length];
        for (int j = 0; j < mesh.textureCoordinates.length; j++) {
            mesh.textureCoordinates[j] = new TextureCoordinate(groupTextureCoordinates[j]);
        }
    }
    // transformed mesh tangents
    if (groupTangents != null) {
        mesh.transformedTangents = new Vector3[groupTangents.length];
        for (int j = 0; j < mesh.transformedTangents.length; j++) {
            mesh.transformedTangents[j] = new Vector3().set(groupTangents[j]);
        }
    }
    // transformed mesh bitangents
    if (groupBitangents != null) {
        mesh.transformedBitangents = new Vector3[groupBitangents.length];
        for (int j = 0; j < mesh.transformedBitangents.length; j++) {
            mesh.transformedBitangents[j] = new Vector3().set(groupBitangents[j]);
        }
    }
    // indices
    int indicesCount = 0;
    for (FacesEntity facesEntity : group.getFacesEntities()) {
        indicesCount += 3 * facesEntity.getFaces().length;
    }
    mesh.indices = new short[indicesCount];
    {
        int j = 0;
        // create face vertex indices
        for (FacesEntity facesEntity : group.getFacesEntities()) for (Face face : facesEntity.getFaces()) for (int vertexIndex : face.getVertexIndices()) {
            mesh.indices[j++] = (short) vertexIndex;
        }
    }
    //
    mesh.recreatedBuffers = false;
    // create mesh upload buffers
    if (mesh.animationProcessingTarget != Engine.AnimationProcessingTarget.CPU_NORENDERING) {
        mesh.sbIndices = ByteBuffer.allocateDirect(mesh.faces * 3 * Short.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asShortBuffer();
        mesh.fbVertices = ByteBuffer.allocateDirect(groupVertices.length * 3 * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
        mesh.fbNormals = ByteBuffer.allocateDirect(groupNormals.length * 3 * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
        mesh.fbTextureCoordinates = groupTextureCoordinates != null ? ByteBuffer.allocateDirect(groupTextureCoordinates.length * 2 * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer() : null;
        mesh.fbTangents = groupTangents != null ? ByteBuffer.allocateDirect(groupTangents.length * 3 * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer() : null;
        mesh.fbBitangents = groupBitangents != null ? ByteBuffer.allocateDirect(groupBitangents.length * 3 * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer() : null;
        // create face vertex indices, will never be changed in engine
        for (FacesEntity facesEntity : group.getFacesEntities()) for (Face face : facesEntity.getFaces()) for (int vertexIndex : face.getVertexIndices()) {
            mesh.sbIndices.put((short) vertexIndex);
        }
        mesh.sbIndices.flip();
        // create texture coordinates buffer, will never be changed in engine
        if (mesh.fbTextureCoordinates != null) {
            // construct texture coordinates byte buffer as this will not change usually
            for (TextureCoordinate textureCoordinate : groupTextureCoordinates) {
                mesh.fbTextureCoordinates.put(textureCoordinate.getArray());
            }
            mesh.fbTextureCoordinates.flip();
        }
    }
    // group transformations matrix
    if (mesh.animationProcessingTarget == Engine.AnimationProcessingTarget.CPU || mesh.animationProcessingTarget == Engine.AnimationProcessingTarget.CPU_NORENDERING) {
        // group transformations matrix 
        mesh.cGroupTransformationsMatrix = transformationMatrices.get(group.getId());
    }
    // skinning
    if (skinning != null) {
        // skinning computation caches if computing skinning on CPU
        if (mesh.animationProcessingTarget == Engine.AnimationProcessingTarget.CPU || mesh.animationProcessingTarget == Engine.AnimationProcessingTarget.CPU_NORENDERING) {
            mesh.cSkinningJointWeight = new float[groupVertices.length][];
            mesh.cSkinningJointBindMatrices = new Matrix4x4[groupVertices.length][];
            mesh.cSkinningJointTransformationsMatrices = new Matrix4x4[groupVertices.length][];
            mesh.cTransformationsMatrix = new Matrix4x4();
            // compute joint weight caches
            Joint[] joints = skinning.getJoints();
            float[] weights = skinning.getWeights();
            JointWeight[][] jointsWeights = skinning.getVerticesJointsWeights();
            for (int vertexIndex = 0; vertexIndex < groupVertices.length; vertexIndex++) {
                int vertexJointWeights = jointsWeights[vertexIndex].length;
                if (vertexJointWeights > mesh.cSkinningMaxVertexWeights)
                    mesh.cSkinningMaxVertexWeights = vertexJointWeights;
                mesh.cSkinningJointWeight[vertexIndex] = new float[vertexJointWeights];
                mesh.cSkinningJointBindMatrices[vertexIndex] = new Matrix4x4[vertexJointWeights];
                mesh.cSkinningJointTransformationsMatrices[vertexIndex] = new Matrix4x4[vertexJointWeights];
                int jointWeightIdx = 0;
                for (JointWeight jointWeight : jointsWeights[vertexIndex]) {
                    Joint joint = joints[jointWeight.getJointIndex()];
                    // 
                    mesh.cSkinningJointWeight[vertexIndex][jointWeightIdx] = weights[jointWeight.getWeightIndex()];
                    mesh.cSkinningJointBindMatrices[vertexIndex][jointWeightIdx] = joint.getBindMatrix();
                    mesh.cSkinningJointTransformationsMatrices[vertexIndex][jointWeightIdx] = transformationMatrices.get(joint.getGroupId());
                    // next
                    jointWeightIdx++;
                }
            }
        } else // GPU setup
        if (mesh.animationProcessingTarget == Engine.AnimationProcessingTarget.GPU) {
            // create skinning buffers
            mesh.gIbSkinningVerticesJoints = ByteBuffer.allocateDirect(groupVertices.length * 1 * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
            mesh.gFbSkinningVerticesVertexJointsIdxs = ByteBuffer.allocateDirect(groupVertices.length * 4 * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
            mesh.gFbSkinningVerticesVertexJointsWeights = ByteBuffer.allocateDirect(groupVertices.length * 4 * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
            mesh.gFbSkinningJointsTransformationsMatrices = ByteBuffer.allocateDirect(60 * 16 * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
            mesh.gFbSkinningTransformationMatrix = new Matrix4x4();
            // fill skinning buffers, joint bind matrices
            mesh.skinningJoints = skinning.getJoints().length;
            mesh.gSkinningJointBindMatrices = new ArrayList<Matrix4x4>();
            for (Joint joint : skinning.getJoints()) {
                mesh.gSkinningJointBindMatrices.add(joint.getBindMatrix());
            }
            //
            JointWeight[][] jointsWeights = skinning.getVerticesJointsWeights();
            float[] weights = skinning.getWeights();
            for (int groupVertexIndex = 0; groupVertexIndex < groupVertices.length; groupVertexIndex++) {
                int vertexJoints = jointsWeights[groupVertexIndex].length;
                // put number of joints
                mesh.gIbSkinningVerticesJoints.put((float) vertexJoints);
                // vertex joint idx 1..4
                for (int i = 0; i < 4; i++) {
                    mesh.gFbSkinningVerticesVertexJointsIdxs.put((float) (vertexJoints > i ? jointsWeights[groupVertexIndex][i].getJointIndex() : -1));
                }
                // vertex joint weight 1..4
                for (int i = 0; i < 4; i++) {
                    mesh.gFbSkinningVerticesVertexJointsWeights.put(vertexJoints > i ? weights[jointsWeights[groupVertexIndex][i].getWeightIndex()] : 0.0f);
                }
            }
            // put number of joints
            mesh.gIbSkinningVerticesJoints.flip();
            // vertex joint idx 1..4
            mesh.gFbSkinningVerticesVertexJointsIdxs.flip();
            // vertex joint weight 1..4
            mesh.gFbSkinningVerticesVertexJointsWeights.flip();
        }
    }
    // temp vector3
    mesh.tmpVector3 = new Vector3();
    // issue a recreate buffer and upload to graphics board
    mesh.recreateBuffers(group);
    //
    return mesh;
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) ArrayList(java.util.ArrayList) Vector3(net.drewke.tdme.math.Vector3) Joint(net.drewke.tdme.engine.model.Joint) JointWeight(net.drewke.tdme.engine.model.JointWeight) Joint(net.drewke.tdme.engine.model.Joint) Matrix4x4(net.drewke.tdme.math.Matrix4x4) TextureCoordinate(net.drewke.tdme.engine.model.TextureCoordinate) Skinning(net.drewke.tdme.engine.model.Skinning) Face(net.drewke.tdme.engine.model.Face)

Example 5 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity in project tdme by andreasdr.

the class Object3DInternal method setDynamicDiffuseTexture.

/**
	 * Bind a texture to a group and faces entity
	 * @param group id
	 * @param faces entity id or null if texture should be bound to all faces entities
	 * @param texture id
	 */
private void setDynamicDiffuseTexture(String groupId, String facesEntityId, int textureId) {
    for (int i = 0; i < object3dGroups.length; i++) {
        Object3DGroup object3DGroup = object3dGroups[i];
        // skip if a group is desired but not matching
        if (groupId != null && groupId.equals(object3DGroup.group.getId()) == false)
            continue;
        FacesEntity[] facesEntities = object3DGroup.group.getFacesEntities();
        for (int facesEntityIdx = 0; facesEntityIdx < facesEntities.length; facesEntityIdx++) {
            FacesEntity facesEntity = facesEntities[facesEntityIdx];
            // skip if a faces entity is desired but not matching
            if (facesEntityId != null && facesEntityId.equals(facesEntity.getId()) == false)
                continue;
            // gl texture id, skip if not set up
            object3DGroup.dynamicDiffuseTextureIdsByEntities[facesEntityIdx] = textureId;
        }
    }
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity)

Aggregations

FacesEntity (net.drewke.tdme.engine.model.FacesEntity)22 Face (net.drewke.tdme.engine.model.Face)15 Material (net.drewke.tdme.engine.model.Material)15 Vector3 (net.drewke.tdme.math.Vector3)12 ArrayList (java.util.ArrayList)11 Model (net.drewke.tdme.engine.model.Model)9 Group (net.drewke.tdme.engine.model.Group)8 TextureCoordinate (net.drewke.tdme.engine.model.TextureCoordinate)6 Joint (net.drewke.tdme.engine.model.Joint)5 StringTokenizer (java.util.StringTokenizer)2 Object3D (net.drewke.tdme.engine.Object3D)2 BufferedReader (java.io.BufferedReader)1 DataInputStream (java.io.DataInputStream)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 Engine (net.drewke.tdme.engine.Engine)1 Object3DModel (net.drewke.tdme.engine.Object3DModel)1 Color4 (net.drewke.tdme.engine.model.Color4)1 JointWeight (net.drewke.tdme.engine.model.JointWeight)1 Skinning (net.drewke.tdme.engine.model.Skinning)1