Search in sources :

Example 1 with Object3D

use of org.rajawali3d.Object3D in project Rajawali by Rajawali.

the class RajawaliVRExampleRenderer method initScene.

@Override
public void initScene() {
    DirectionalLight light = new DirectionalLight(0.2f, -1f, 0f);
    light.setPower(.7f);
    getCurrentScene().addLight(light);
    light = new DirectionalLight(0.2f, 1f, 0f);
    light.setPower(1f);
    getCurrentScene().addLight(light);
    getCurrentCamera().setFarPlane(1000);
    getCurrentScene().setBackgroundColor(0xdddddd);
    createTerrain();
    try {
        getCurrentScene().setSkybox(R.drawable.posx, R.drawable.negx, R.drawable.posy, R.drawable.negy, R.drawable.posz, R.drawable.negz);
        LoaderAWD loader = new LoaderAWD(getContext().getResources(), getTextureManager(), R.raw.space_cruiser);
        loader.parse();
        Material cruiserMaterial = new Material();
        cruiserMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
        cruiserMaterial.setColorInfluence(0);
        cruiserMaterial.enableLighting(true);
        cruiserMaterial.addTexture(new Texture("spaceCruiserTex", R.drawable.space_cruiser_4_color_1));
        Object3D spaceCruiser = loader.getParsedObject();
        spaceCruiser.setMaterial(cruiserMaterial);
        spaceCruiser.setZ(-6);
        spaceCruiser.setY(1);
        getCurrentScene().addChild(spaceCruiser);
        spaceCruiser = spaceCruiser.clone(true);
        spaceCruiser.setZ(-12);
        spaceCruiser.setY(-3);
        spaceCruiser.setRotY(180);
        getCurrentScene().addChild(spaceCruiser);
        loader = new LoaderAWD(getContext().getResources(), getTextureManager(), R.raw.dark_fighter);
        loader.parse();
        Material darkFighterMaterial = new Material();
        darkFighterMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
        darkFighterMaterial.setColorInfluence(0);
        darkFighterMaterial.enableLighting(true);
        darkFighterMaterial.addTexture(new Texture("darkFighterTex", R.drawable.dark_fighter_6_color));
        Object3D darkFighter = loader.getParsedObject();
        darkFighter.setMaterial(darkFighterMaterial);
        darkFighter.setZ(-6);
        getCurrentScene().addChild(darkFighter);
        CatmullRomCurve3D path = new CatmullRomCurve3D();
        path.addPoint(new Vector3(0, -5, -10));
        path.addPoint(new Vector3(10, -5, 0));
        path.addPoint(new Vector3(0, -4, 8));
        path.addPoint(new Vector3(-16, -6, 0));
        path.isClosedCurve(true);
        SplineTranslateAnimation3D anim = new SplineTranslateAnimation3D(path);
        anim.setDurationMilliseconds(44000);
        anim.setRepeatMode(RepeatMode.INFINITE);
        // -- orient to path
        anim.setOrientToPath(true);
        anim.setTransformable3D(darkFighter);
        getCurrentScene().registerAnimation(anim);
        anim.play();
        loader = new LoaderAWD(getContext().getResources(), getTextureManager(), R.raw.capital);
        loader.parse();
        Material capitalMaterial = new Material();
        capitalMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
        capitalMaterial.setColorInfluence(0);
        capitalMaterial.enableLighting(true);
        capitalMaterial.addTexture(new Texture("capitalTex", R.drawable.hullw));
        capitalMaterial.addTexture(new NormalMapTexture("capitalNormTex", R.drawable.hulln));
        capital = loader.getParsedObject();
        capital.setMaterial(capitalMaterial);
        capital.setScale(18);
        getCurrentScene().addChild(capital);
        path = new CatmullRomCurve3D();
        path.addPoint(new Vector3(0, 13, 34));
        path.addPoint(new Vector3(34, 13, 0));
        path.addPoint(new Vector3(0, 13, -34));
        path.addPoint(new Vector3(-34, 13, 0));
        path.isClosedCurve(true);
        anim = new SplineTranslateAnimation3D(path);
        anim.setDurationMilliseconds(60000);
        anim.setRepeatMode(RepeatMode.INFINITE);
        anim.setOrientToPath(true);
        anim.setTransformable3D(capital);
        getCurrentScene().registerAnimation(anim);
        anim.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
    lookatSphere = new Sphere(1, 12, 12);
    Material sphereMaterial = new Material();
    sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
    sphereMaterial.enableLighting(true);
    lookatSphere.setMaterial(sphereMaterial);
    lookatSphere.setColor(Color.YELLOW);
    lookatSphere.setPosition(0, 0, -6);
    getCurrentScene().addChild(lookatSphere);
    initAudio();
}
Also used : LoaderAWD(org.rajawali3d.loader.LoaderAWD) Material(org.rajawali3d.materials.Material) Vector3(org.rajawali3d.math.vector.Vector3) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) Texture(org.rajawali3d.materials.textures.Texture) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) SplineTranslateAnimation3D(org.rajawali3d.animation.SplineTranslateAnimation3D) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) Object3D(org.rajawali3d.Object3D) Sphere(org.rajawali3d.primitives.Sphere) CatmullRomCurve3D(org.rajawali3d.curves.CatmullRomCurve3D) DirectionalLight(org.rajawali3d.lights.DirectionalLight) DiffuseMethod(org.rajawali3d.materials.methods.DiffuseMethod)

Example 2 with Object3D

use of org.rajawali3d.Object3D in project Rajawali by Rajawali.

the class LoaderOBJ method mergeGroupsAsObjects.

/**
	 * Collapse single-object groups. (Some obj exporters use g token for objects)
	 * @param object
	 */
private void mergeGroupsAsObjects(Object3D object) {
    if (object.isContainer() && object.getNumChildren() == 1 && object.getChildAt(0).getName().startsWith("Object")) {
        Object3D child = object.getChildAt(0);
        object.removeChild(child);
        child.setName(object.getName());
        addChildSetParent(object.getParent(), child);
        object.getParent().removeChild(object);
        object = child;
    }
    for (int i = 0; i < object.getNumChildren(); i++) {
        mergeGroupsAsObjects(object.getChildAt(i));
    }
}
Also used : Object3D(org.rajawali3d.Object3D)

Example 3 with Object3D

use of org.rajawali3d.Object3D in project Rajawali by Rajawali.

the class BlockMeshInstance method parseBlock.

public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
    // Parse scene block
    RajLog.d("Parsing SceneGraph Block at position: " + dis.getPosition());
    mSceneGraphBlock = new SceneGraphBlock();
    mSceneGraphBlock.readGraphData(blockHeader, dis);
    // Block id for geometry
    mGeometryID = dis.readUnsignedInt();
    // Lookup the geometry or create it if it does not exist.
    final BlockHeader geomHeader = blockHeader.blockHeaders.get((short) mGeometryID);
    if (geomHeader == null) {
        mGeometry = new Object3D(mSceneGraphBlock.lookupName);
    } else {
        if (geomHeader.parser == null || !(geomHeader.parser instanceof ABaseObjectBlockParser))
            throw new ParsingException("Invalid block reference.");
        mGeometry = ((ABaseObjectBlockParser) geomHeader.parser).getBaseObject3D().clone(false, true);
        mGeometry.setName(mSceneGraphBlock.lookupName);
    }
    // Apply the materials
    final int materialCount = dis.readUnsignedShort();
    final Material[] materials = new Material[materialCount];
    for (int i = 0; i < materialCount; ++i) {
        final long materialID = dis.readUnsignedInt();
        if (materialID == 0) {
            materials[i] = getDefaultMaterial();
            materials[i].addTexture(getDefaultTexture());
        } else {
            final BlockHeader materialHeader = blockHeader.blockHeaders.get((short) materialID);
            if (materialHeader == null || materialHeader.parser == null || !(materialHeader.parser instanceof ATextureBlockParser))
                throw new ParsingException("Invalid block reference " + materialID);
            materials[i] = ((ATextureBlockParser) materialHeader.parser).getMaterial();
        }
    }
    // mesh instance properties; does it cast a shadow?
    AwdProperties properties = dis.readProperties(EXPECTED_PROPS);
    mCastsShadow = (boolean) properties.get(PROP_CASTS_SHADOW, true);
    final Matrix4 matrix = new Matrix4(mSceneGraphBlock.transformMatrix);
    // Set translation
    mGeometry.setPosition(matrix.getTranslation());
    // Set scale
    final Vector3 scale = matrix.getScaling();
    mGeometry.setScale(scale.y, scale.x, scale.z);
    // Set rotation
    mGeometry.setOrientation(new Quaternion().fromMatrix(matrix));
    int m = 0;
    if (!mGeometry.isContainer())
        mGeometry.setMaterial(materials[m++]);
    for (int i = 0; i < mGeometry.getNumChildren(); i++) mGeometry.getChildAt(i).setMaterial(materials[Math.min(materials.length - 1, m++)]);
    // ignore user properties, skip to end of block
    dis.skip(blockHeader.blockEnd - dis.getPosition());
}
Also used : Quaternion(org.rajawali3d.math.Quaternion) Material(org.rajawali3d.materials.Material) Vector3(org.rajawali3d.math.vector.Vector3) AwdProperties(org.rajawali3d.loader.LoaderAWD.AwdProperties) Matrix4(org.rajawali3d.math.Matrix4) Object3D(org.rajawali3d.Object3D) ParsingException(org.rajawali3d.loader.ParsingException) BlockHeader(org.rajawali3d.loader.LoaderAWD.BlockHeader)

Example 4 with Object3D

use of org.rajawali3d.Object3D in project Rajawali by Rajawali.

the class SkeletalAnimationObject3D method clone.

public SkeletalAnimationObject3D clone(boolean copyMaterial, boolean cloneChildren) {
    SkeletalAnimationObject3D clone = new SkeletalAnimationObject3D();
    clone.setRotation(getOrientation());
    clone.setPosition(getPosition());
    clone.setScale(getScale());
    clone.getGeometry().copyFromGeometry3D(mGeometry);
    clone.isContainer(mIsContainerOnly);
    clone.setMaterial(mMaterial);
    clone.mElementsBufferType = GLES20.GL_UNSIGNED_INT;
    clone.mTransparent = this.mTransparent;
    clone.mEnableBlending = this.mEnableBlending;
    clone.mBlendFuncSFactor = this.mBlendFuncSFactor;
    clone.mBlendFuncDFactor = this.mBlendFuncDFactor;
    clone.mEnableDepthTest = this.mEnableDepthTest;
    clone.mEnableDepthMask = this.mEnableDepthMask;
    clone.setFrames(mFrames);
    clone.setFps(mFps);
    clone.uBoneMatrix = uBoneMatrix;
    clone.mInverseBindPoseMatrix = mInverseBindPoseMatrix;
    clone.setJoints(mJoints);
    if (!cloneChildren)
        return clone;
    for (Object3D child : mChildren) {
        if (child.getClass() == SkeletalAnimationChildObject3D.class) {
            SkeletalAnimationChildObject3D scoclone = (SkeletalAnimationChildObject3D) child.clone(copyMaterial, cloneChildren);
            // TODO: setSkeleton in addChild?
            scoclone.setSkeleton(clone);
            clone.addChild(scoclone);
        }
    }
    return clone;
}
Also used : Object3D(org.rajawali3d.Object3D)

Example 5 with Object3D

use of org.rajawali3d.Object3D in project Rajawali by Rajawali.

the class LoaderFBX method buildMesh.

private void buildMesh(Model model, Stack<ALight> lights) throws TextureException, ParsingException {
    Object3D o = new Object3D(model.name);
    boolean hasUVs = model.layerElementUV.uVIndex != null;
    int[] vidx = model.polygonVertexIndex.data;
    int[] uvidx = null;
    float[] modelVerts = model.vertices.data;
    float[] modelNorm = model.layerElementNormal.normals.data;
    float[] modelUv = null;
    ArrayList<Integer> indices = new ArrayList<Integer>();
    ArrayList<Float> vertices = new ArrayList<Float>();
    ArrayList<Float> normals = new ArrayList<Float>();
    ArrayList<Float> uvs = null;
    if (hasUVs) {
        uvs = new ArrayList<Float>();
        uvidx = model.layerElementUV.uVIndex.data;
        modelUv = model.layerElementUV.uV.data;
    }
    int count = 0;
    int indexCount = 0;
    int[] triIds = new int[3];
    int[] quadIds = new int[6];
    int i = 0, j = 0, k = 0;
    int vidxLen = vidx.length;
    for (i = 0; i < vidxLen; ++i) {
        count++;
        if (vidx[i] < 0) {
            if (count == 3) {
                int index1 = vidx[i - 2], index2 = vidx[i - 1], index3 = (vidx[i] * -1) - 1;
                indices.add(indexCount++);
                indices.add(indexCount++);
                indices.add(indexCount++);
                triIds[0] = index1 * 3;
                triIds[1] = index2 * 3;
                triIds[2] = index3 * 3;
                for (j = 0; j < 3; ++j) {
                    int cid = triIds[j];
                    for (k = 0; k < 3; ++k) {
                        vertices.add(modelVerts[cid + k]);
                        int dir = i == 0 ? -1 : 1;
                        normals.add(modelNorm[cid + k] * dir);
                    }
                }
                if (hasUVs) {
                    int uvIndex3 = uvidx[i] * 2;
                    int uvIndex2 = uvidx[i - 1] * 2;
                    int uvIndex1 = uvidx[i - 2] * 2;
                    uvs.add(modelUv[uvIndex1 + 0]);
                    uvs.add(1f - modelUv[uvIndex1 + 1]);
                    uvs.add(modelUv[uvIndex2 + 0]);
                    uvs.add(1f - modelUv[uvIndex2 + 1]);
                    uvs.add(modelUv[uvIndex3 + 0]);
                    uvs.add(1f - modelUv[uvIndex3 + 1]);
                }
            } else {
                int index1 = vidx[i - 3];
                int index2 = vidx[i - 2];
                int index3 = vidx[i - 1];
                int index4 = (vidx[i] * -1) - 1;
                indices.add(indexCount++);
                indices.add(indexCount++);
                indices.add(indexCount++);
                indices.add(indexCount++);
                indices.add(indexCount++);
                indices.add(indexCount++);
                quadIds[0] = index1 * 3;
                quadIds[1] = index2 * 3;
                quadIds[2] = index3 * 3;
                quadIds[3] = index4 * 3;
                quadIds[4] = index1 * 3;
                quadIds[5] = index3 * 3;
                for (j = 0; j < 6; ++j) {
                    int cid = quadIds[j];
                    for (k = 0; k < 3; ++k) {
                        vertices.add(modelVerts[cid + k]);
                        normals.add(modelNorm[cid + k]);
                    }
                }
                if (hasUVs) {
                    int uvIndex1 = uvidx[i - 3] * 2;
                    int uvIndex2 = uvidx[i - 2] * 2;
                    int uvIndex3 = uvidx[i - 1] * 2;
                    int uvIndex4 = uvidx[i] * 2;
                    quadIds[0] = uvIndex1;
                    quadIds[1] = uvIndex2;
                    quadIds[2] = uvIndex3;
                    quadIds[3] = uvIndex4;
                    quadIds[4] = uvIndex1;
                    quadIds[5] = uvIndex3;
                    for (j = 0; j < 6; ++j) {
                        int cid = quadIds[j];
                        for (k = 0; k < 2; ++k) {
                            if (k == 0)
                                uvs.add(modelUv[cid + k]);
                            else
                                uvs.add(1f - modelUv[cid + k]);
                        }
                    }
                }
            }
            count = 0;
        }
    }
    o.setData(convertFloats(vertices), convertFloats(normals), hasUVs ? convertFloats(uvs) : null, null, convertIntegers(indices), false);
    vertices.clear();
    vertices = null;
    normals.clear();
    normals = null;
    if (hasUVs) {
        uvs.clear();
        uvs = null;
    }
    indices.clear();
    indices = null;
    o.setMaterial(getMaterialForMesh(o, model.name));
    setMeshTextures(o, model.name);
    o.setPosition(model.properties.lclTranslation);
    o.setX(o.getX() * -1);
    o.setScale(model.properties.lclScaling);
    o.setRotation(model.properties.lclRotation);
    o.setRotZ(-o.getRotZ());
    mRootObject.addChild(o);
}
Also used : ArrayList(java.util.ArrayList) Object3D(org.rajawali3d.Object3D)

Aggregations

Object3D (org.rajawali3d.Object3D)18 Vector3 (org.rajawali3d.math.vector.Vector3)5 Material (org.rajawali3d.materials.Material)4 ArrayList (java.util.ArrayList)3 SkeletalAnimationChildObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D)2 SkeletalAnimationObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationObject3D)2 ParsingException (org.rajawali3d.loader.ParsingException)2 DiffuseMethod (org.rajawali3d.materials.methods.DiffuseMethod)2 TextureException (org.rajawali3d.materials.textures.ATexture.TextureException)2 Matrix4 (org.rajawali3d.math.Matrix4)2 Quaternion (org.rajawali3d.math.Quaternion)2 SparseArray (android.util.SparseArray)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ByteBuffer (java.nio.ByteBuffer)1