Search in sources :

Example 1 with AwdProperties

use of org.rajawali3d.loader.LoaderAWD.AwdProperties 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 2 with AwdProperties

use of org.rajawali3d.loader.LoaderAWD.AwdProperties in project Rajawali by Rajawali.

the class BlockAnimator method parseBlock.

public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
    // Lookup name
    mLookupName = dis.readVarString();
    // Animation type (skeletal or vertex)
    // u16 type
    int type = dis.readUnsignedShort();
    AwdProperties properties = null;
    switch(type) {
        case // NumAttrList props
        TYPE_SKELETAL_ANIM:
            properties = dis.readProperties(EXPECTED_PROPS);
            break;
        case // NumAttrList props
        TYPE_VERTEX_ANIM:
            dis.readProperties(null);
    }
    // the animation set to be assigned
    long animSetID = dis.readUnsignedInt();
    mAnimSet = lookupAnimationSet(blockHeader, animSetID);
    int numTargets = dis.readUnsignedShort();
    mTargets = new Object3D[numTargets];
    // the mesh animation targets
    for (int i = 0; i < numTargets; i++) mTargets[i] = lookupMesh(blockHeader, dis.readUnsignedInt());
    // the active animation & autoplay setting
    mActive = dis.readUnsignedShort();
    mAutoPlay = dis.readBoolean();
    //skip block & user properties
    dis.readProperties(null);
    dis.readProperties(null);
    if (type == TYPE_SKELETAL_ANIM) {
        buildSkeleton(blockHeader, (Long) properties.get(PROP_SKELETON, 0L));
    }
// TYPE_VERTEX_ANIM NYI
}
Also used : AwdProperties(org.rajawali3d.loader.LoaderAWD.AwdProperties) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)

Example 3 with AwdProperties

use of org.rajawali3d.loader.LoaderAWD.AwdProperties in project Rajawali by Rajawali.

the class BlockMetaData method parseBlock.

public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
    final AwdProperties properties = dis.readProperties(EXPECTED_PROPS);
    mTimeStamp = (Long) properties.get(PROP_TIMESTAMP);
    mEncoderName = properties.get(PROP_ENCODER_NAME).toString();
    mEncoderVersion = properties.get(PROP_ENCODER_VERSION).toString();
    mGeneratorName = properties.get(PROP_GENERATOR_NAME).toString();
    mGeneratorVersion = properties.get(PROP_GENERATOR_VERSION).toString();
    if (RajLog.isDebugEnabled()) {
        RajLog.d("  Timestamp: " + mTimeStamp);
        RajLog.d("  Encoder Name: " + mEncoderName);
        RajLog.d("  Encoder Version: " + mEncoderVersion);
        RajLog.d("  Generator Name: " + mGeneratorName);
        RajLog.d("  Generator Version: " + mGeneratorVersion);
    }
}
Also used : AwdProperties(org.rajawali3d.loader.LoaderAWD.AwdProperties)

Example 4 with AwdProperties

use of org.rajawali3d.loader.LoaderAWD.AwdProperties in project Rajawali by Rajawali.

the class BlockSimpleMaterial method parseBlock.

public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
    // Lookup name
    mLookupName = dis.readVarString();
    // Material type
    mMaterialType = dis.readByte();
    // Shading method count
    mShadingMethodCount = dis.readByte();
    // Read properties
    final AwdProperties properties = dis.readProperties(EXPECTED_PROPS);
    mSpezialType = (Integer) properties.get((short) 4, 0);
    // Spezial type 2 or higher is not supported in the specification
    if (mSpezialType >= 2)
        throw new NotParsableException("Spezial type " + mSpezialType + " is not currently supported.");
    // Debug
    if (RajLog.isDebugEnabled()) {
        RajLog.d("  Lookup Name: " + mLookupName);
        RajLog.d("  Material Type: " + mMaterialType);
        RajLog.d("  Shading Methods: " + mShadingMethodCount);
        RajLog.d("  Spezial Type: " + mSpezialType);
    }
    // Parse the methods
    for (int i = 0; i < mShadingMethodCount; ++i) {
        // TODO Looking at the AWD source, this appears to be completely unused?
        dis.readUnsignedShort();
        dis.readProperties();
        dis.readUserAttributes(null);
    }
    final HashMap<String, Object> attributes = new HashMap<String, Object>();
    dis.readUserAttributes(attributes);
    mMaterial = new Material();
    long diffuseTexture = 0, ambientTexture = 0, diffuseColor = 0;
    // remove any chars that will break shader compile
    String cleanName = cleanName(mLookupName);
    switch(mMaterialType) {
        case TYPE_COLOR:
            // default to 0xcccccc per AWD implementation
            diffuseColor = (Long) properties.get((short) 1, 0xccccccL);
            final float[] colorFloat = new float[4];
            colorFloat[0] = ((diffuseColor >> 16) & 0xff) / 255.0f;
            colorFloat[1] = ((diffuseColor >> 8) & 0xff) / 255.0f;
            colorFloat[2] = (diffuseColor & 0xff) / 255.0f;
            colorFloat[3] = (((int) ((Double) properties.get(PROP_ALPHA, 1.0d) * 0xff)) & 0xff) / 255.0f;
            mMaterial.setColor(colorFloat);
            break;
        case TYPE_TEXTURE:
            diffuseTexture = (Long) properties.get(PROP_TEXTURE, 0L);
            ambientTexture = (Long) properties.get(PROP_AMBIENT_TEXTURE, 0L);
            if (diffuseTexture == 0 && ambientTexture == 0)
                throw new ParsingException("Texture ID can not be 0, document corrupt or unsupported version.");
            if (diffuseTexture > 0)
                mMaterial.addTexture(new Texture(cleanName + diffuseTexture, lookup(blockHeader, diffuseTexture)));
            if (ambientTexture > 0)
                mMaterial.addTexture(new Texture(cleanName + ambientTexture, lookup(blockHeader, ambientTexture)));
            mMaterial.setColorInfluence(0);
            break;
    }
    // either material type can have specular and/or normal maps
    long specularTexture = (Long) properties.get(PROP_SPECULAR_TEXTURE, 0L);
    long normalTexture = (Long) properties.get(PROP_NORMAL_TEXTURE, 0L);
    // either material type can have settings for diffuse, ambient, specular lighting
    double diffuseLevel = (Double) properties.get(PROP_DIFFUSE_LEVEL, 1.0d);
    long ambientColor = (Long) properties.get(PROP_AMBIENT_COLOR, (long) Color.WHITE);
    double ambientLevel = (Double) properties.get(PROP_AMBIENT_LEVEL, 1.0d);
    long specularColor = (Long) properties.get(PROP_SPECULAR_COLOR, (long) Color.WHITE);
    double specularGloss = (Double) properties.get(PROP_SPECULAR_GLOSS, 50.0D);
    double specularLevel = (Double) properties.get(PROP_SPECULAR_LEVEL, 1.0d);
    if (specularTexture > 0)
        mMaterial.addTexture(new SpecularMapTexture(cleanName + specularTexture, lookup(blockHeader, specularTexture)));
    if (normalTexture > 0)
        mMaterial.addTexture(new NormalMapTexture(cleanName + normalTexture, lookup(blockHeader, normalTexture)));
    // ambient 1.0 is default, washes-out object; assume < 1 is intended
    ambientLevel = (ambientLevel < 1.0 ? ambientLevel : 0.0);
    mMaterial.setAmbientIntensity(ambientLevel, ambientLevel, ambientLevel);
    mMaterial.setAmbientColor((int) ambientColor);
    if (// always 1.0 in current AWD implementation
    diffuseLevel > 0)
        mMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
    if (specularLevel > 0) {
        SpecularMethod.Phong phong = new SpecularMethod.Phong();
        phong.setSpecularColor((int) specularColor);
        phong.setShininess((float) specularGloss);
        phong.setIntensity((float) specularLevel);
        mMaterial.setSpecularMethod(phong);
    }
    // don't enable lighting if specular and diffuse are absent, otherwise enable
    if (diffuseLevel > 0 || specularLevel > 0)
        mMaterial.enableLighting(true);
}
Also used : HashMap(java.util.HashMap) NotParsableException(org.rajawali3d.loader.awd.exceptions.NotParsableException) Material(org.rajawali3d.materials.Material) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) AwdProperties(org.rajawali3d.loader.LoaderAWD.AwdProperties) SpecularMapTexture(org.rajawali3d.materials.textures.SpecularMapTexture) SpecularMapTexture(org.rajawali3d.materials.textures.SpecularMapTexture) Texture(org.rajawali3d.materials.textures.Texture) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) SpecularMethod(org.rajawali3d.materials.methods.SpecularMethod) ParsingException(org.rajawali3d.loader.ParsingException)

Aggregations

AwdProperties (org.rajawali3d.loader.LoaderAWD.AwdProperties)4 ParsingException (org.rajawali3d.loader.ParsingException)2 Material (org.rajawali3d.materials.Material)2 HashMap (java.util.HashMap)1 Object3D (org.rajawali3d.Object3D)1 SkeletonJoint (org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)1 BlockHeader (org.rajawali3d.loader.LoaderAWD.BlockHeader)1 NotParsableException (org.rajawali3d.loader.awd.exceptions.NotParsableException)1 SpecularMethod (org.rajawali3d.materials.methods.SpecularMethod)1 NormalMapTexture (org.rajawali3d.materials.textures.NormalMapTexture)1 SpecularMapTexture (org.rajawali3d.materials.textures.SpecularMapTexture)1 Texture (org.rajawali3d.materials.textures.Texture)1 Matrix4 (org.rajawali3d.math.Matrix4)1 Quaternion (org.rajawali3d.math.Quaternion)1 Vector3 (org.rajawali3d.math.vector.Vector3)1